reGen2 Linux Release Metatool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

373 lines
13 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/bin/bash
  2. # Copyright 2010-2012, Asio Software Technologies
  3. # Distributed under the terms of the GNU General Public License v3
  4. #-------------------------------------------------------------------------------
  5. # Checks if system running EzBuild meets its requirements
  6. #-------------------------------------------------------------------------------
  7. function checkPrerequisites() {
  8. if [ "$(uname -s)" != "Linux" ]; then
  9. printError "You are trying to launch ${EZNAME} on non-Linux system!"
  10. return 1
  11. fi
  12. if [ "${EUID}" != "0" ] && [ "${USER}" != "root" ]; then
  13. printError "The ${EZNAME} needs to be launched from root account!"
  14. return 1
  15. fi
  16. if [ ! -e /dev/null ]; then
  17. printError "The /dev directory seems to be not mounted!"
  18. return 1
  19. fi
  20. if [ ! -e /proc/mounts ]; then
  21. printError "The /proc directory seems to be not mounted!"
  22. return 1
  23. fi
  24. ping -c 1 google.com &> /dev/null
  25. if [ ${?} -ne 0 ]; then
  26. printError "There seems to be no Internet conectivity!"
  27. return 1
  28. fi
  29. return 0
  30. }
  31. #-------------------------------------------------------------------------------
  32. # Tests for a minimum version level by comparing version numbers
  33. # Parameters: %min_version% %test_version%
  34. #-------------------------------------------------------------------------------
  35. function checkVersion() {
  36. local BEST REF TST
  37. REF=$(echo "${1}" | \
  38. sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/")
  39. TST=$(echo "${2}" | \
  40. sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/")
  41. BEST=$(echo -e "${REF}\n${TST}" | sed "/^$/d" | sort -Vr | head -1)
  42. [[ "${BEST}" = "${TST}" ]] && return 0
  43. return 1
  44. }
  45. #-------------------------------------------------------------------------------
  46. # Outputs formatted error message and aborts script execution with given status
  47. # Parameters: %message% [%status%]
  48. #-------------------------------------------------------------------------------
  49. function die() {
  50. local STATUS=${2:-1}
  51. notify ${STATUS} "${1}"
  52. printError "${1}"
  53. echo -ne "\n"
  54. exit ${STATUS}
  55. }
  56. #-------------------------------------------------------------------------------
  57. # Failure handler; outputs warning or error depending on user settings
  58. # Parameters: %message% [%status%]
  59. #-------------------------------------------------------------------------------
  60. function fail() {
  61. local STATUS=${2:-1}
  62. if isEnabled ${FATALERRORS}; then
  63. die "${1}" ${STATUS}
  64. else
  65. pushNotification ${STATUS} "${1}"
  66. printWarn "${1}"
  67. fi
  68. }
  69. #-------------------------------------------------------------------------------
  70. # Checks whether a defined variable contain specified element or not
  71. # Parameters: %element% %variable%
  72. #-------------------------------------------------------------------------------
  73. function hasElement() {
  74. local NEEDLE=${1}
  75. shift
  76. local X
  77. for X in "${@}"; do
  78. [ "${X}" = "${NEEDLE}" ] && return 0
  79. done
  80. return 1
  81. }
  82. #-------------------------------------------------------------------------------
  83. # Checks whether a supplied variable is defined or not
  84. # Parameters: %variable%
  85. #-------------------------------------------------------------------------------
  86. function isDefined() {
  87. [[ ${!1-X} == ${!1-Y} ]]
  88. }
  89. #-------------------------------------------------------------------------------
  90. # Checks whether a supplied variable is enabled or not
  91. # Parameters: %variable%
  92. #-------------------------------------------------------------------------------
  93. function isEnabled() {
  94. case "${1}" in
  95. [Ee][Nn][Aa][Bb][Ll][Ee][Dd])
  96. return 0
  97. ;;
  98. [Tt][Rr][Uu][Ee])
  99. return 0
  100. ;;
  101. [Yy][Ee][Ss])
  102. return 0
  103. ;;
  104. 1)
  105. return 0
  106. ;;
  107. esac
  108. return 1
  109. }
  110. #-------------------------------------------------------------------------------
  111. # Checks whether a variable is defined and if value's length > 0
  112. # Parameters: %variable%
  113. #-------------------------------------------------------------------------------
  114. function isSet() {
  115. if isDefined ${1}; then
  116. if [[ -n ${!1} ]]; then
  117. return 0
  118. fi
  119. fi
  120. return 1
  121. }
  122. #-------------------------------------------------------------------------------
  123. # Loads EzBuild configuration
  124. #-------------------------------------------------------------------------------
  125. function loadConfiguration() {
  126. source ${EZROOT}/config/common.conf &> /dev/null || panic
  127. source ${EZROOT}/config/${EZNAME}.conf &> /dev/null || panic
  128. source ${EZROOT}/config/layout.conf &> /dev/null || panic
  129. source /etc/ezbuild/common.conf &> /dev/null || panic
  130. source /etc/ezbuild/${EZNAME}.conf &> /dev/null || panic
  131. source /etc/ezbuild/layout.conf &> /dev/null || panic
  132. }
  133. #-------------------------------------------------------------------------------
  134. # Loads all EzBuild Libraries
  135. #-------------------------------------------------------------------------------
  136. function loadLibraries() {
  137. source ${EZROOT}/libraries/constants &> /dev/null || panic
  138. source ${EZROOT}/libraries/filesystem &> /dev/null || panic
  139. source ${EZROOT}/libraries/repositories &> /dev/null || panic
  140. }
  141. #-------------------------------------------------------------------------------
  142. # Saves specified message into a log file
  143. # Parameters: %message% [%level%]
  144. #-------------------------------------------------------------------------------
  145. function logMessage() {
  146. local DATE MESSAGE TYPE
  147. if isEnabled ${LOGGING}; then
  148. MESSAGE="${1}"
  149. TYPE="${2}"
  150. DATE=$(date +"${LOGDATEFORMAT}")
  151. if [ -z ${TYPE} ]; then
  152. TYPE=" - "
  153. fi
  154. { echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}; } 2> /dev/null
  155. fi
  156. }
  157. #-------------------------------------------------------------------------------
  158. # Saves executed command multiline output into a log file
  159. # Parameters: %command_output%
  160. #-------------------------------------------------------------------------------
  161. function logOutput() {
  162. if isEnabled ${LOGGING}; then
  163. if [ -z "${@}" ]; then
  164. echo -e " )> (NO OUTPUT)" >> ${LOGFILE}
  165. else
  166. echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
  167. fi
  168. fi
  169. }
  170. #-------------------------------------------------------------------------------
  171. # Sends an e-mail notification about occured error
  172. # Parameters: %status% %message%
  173. #-------------------------------------------------------------------------------
  174. function notify() {
  175. if isEnabled ${LOGGING} && isSet NOTIFY && isEnabled ${EZNOTIFY}; then
  176. local MESG=""
  177. local SEND=0
  178. if [ ${1} -ne 0 ]; then
  179. MESG="The ${EZNAME} has failed with code: ${1} and message: ${2}"
  180. SEND=1
  181. if [ ${#NOTIFICATIONS[@]} -ne 0 ]; then
  182. MESG="${MESG}\nThere were also some non-fatal errors that has \
  183. been treated as warnings:"
  184. fi
  185. elif [ ${#NOTIFICATIONS[@]} -ne 0 ]; then
  186. MESG="The ${EZNAME} process has ended its job without any fatal \
  187. error, but there still were some non-fatal errors that has \
  188. been treated as warnings:"
  189. SEND=1
  190. fi
  191. MESG=$(echo -e "${MESG}" | awk -v OFS=" " '$1=$1')
  192. if [ ${SEND} -eq 1 ]; then
  193. if [ ${#NOTIFICATIONS[@]} -ne 0 ]; then
  194. for NOTIFICATION in "${NOTIFICATIONS[@]}"; do
  195. MESG="${MESG}\n * ${NOTIFICATION}"
  196. done
  197. fi
  198. local LOGS="Full output from log file:\n\n$(cat ${LOGFILE})"
  199. local TOPIC="The ${EZNAME} process has produced an error!"
  200. echo -e "${MESG}\n\n\n${LOGS}" | mailx -s "${TOPIC}" "${NOTIFY}"
  201. fi
  202. fi
  203. }
  204. #-------------------------------------------------------------------------------
  205. # Outputs error message and aborts program execution
  206. #-------------------------------------------------------------------------------
  207. function panic() {
  208. echo -e "FATAL ERROR: Unable to load necessary files!"
  209. echo -e "Your EzBuild installation seems to be broken..."
  210. exit 1
  211. }
  212. #-------------------------------------------------------------------------------
  213. # Outputs formatted error message to both display and log file
  214. # Parameters: %message%
  215. #-------------------------------------------------------------------------------
  216. function printError() {
  217. logMessage "${1}" "ERROR"
  218. echo -e " ${MESSAGE_ERROR} ${@}"
  219. }
  220. #-------------------------------------------------------------------------------
  221. # Outputs formatted information to both display and log file
  222. # Parameters: %message%
  223. #-------------------------------------------------------------------------------
  224. function printInfo() {
  225. logMessage "${1}" "INFO"
  226. echo -e " ${MESSAGE_INFO} ${@}"
  227. }
  228. #-------------------------------------------------------------------------------
  229. # Outputs formatted information about EzBuild version and copyrights
  230. #-------------------------------------------------------------------------------
  231. function printNotice() {
  232. echo -e "\n${COLOR_BOLD}EzBuild v${EZVERSION} (${EZNAME} launched)"
  233. echo -e "${EZCOPYRIGHT}${COLOR_NORMAL}"
  234. echo -e "${BORDER_STAR}\n"
  235. }
  236. #-------------------------------------------------------------------------------
  237. # Outputs formatted warning to both display and log file
  238. # Parameters: %message%
  239. #-------------------------------------------------------------------------------
  240. function printWarn() {
  241. logMessage "${1}" "WARN"
  242. echo -e " ${MESSAGE_WARN} ${@}"
  243. }
  244. #-------------------------------------------------------------------------------
  245. # Adds new non-fatal error notification message to the queue
  246. # Parameters: %status% %message%
  247. #-------------------------------------------------------------------------------
  248. function pushNotification() {
  249. local NUM=${#NOTIFICATIONS[@]}
  250. NOTIFICATIONS[${NUM}]="Code: ${1} Message: ${2}"
  251. }
  252. #-------------------------------------------------------------------------------
  253. # Optionally sends all notifications from queue and aborts program execution
  254. #-------------------------------------------------------------------------------
  255. function quit() {
  256. notify 0
  257. echo -e "${COLOR_BOLD}ALL DONE!\n${COLOR_NORMAL}"
  258. exit 0
  259. }
  260. #-------------------------------------------------------------------------------
  261. # Restores original Internal Field Separator (IFS)
  262. #-------------------------------------------------------------------------------
  263. function restoreIFS() {
  264. if [ "${ORGIFS:-unset}" != "unset" ]; then
  265. IFS="${ORGIFS}"
  266. unset ORGIFS
  267. else
  268. unset IFS
  269. fi
  270. }
  271. #-------------------------------------------------------------------------------
  272. # Silently executes given command and saves its output to log file if enabled
  273. # Parameters: %command%
  274. #-------------------------------------------------------------------------------
  275. function run() {
  276. local COMMAND OUTPUT RESULT
  277. COMMAND="${@}"
  278. logMessage "Executing: \"${COMMAND}\"" "DEBUG"
  279. OUTPUT=$(eval ${COMMAND} 2>&1)
  280. RESULT=${?}
  281. logOutput "${OUTPUT}"
  282. return ${RESULT}
  283. }
  284. #-------------------------------------------------------------------------------
  285. # Saves original Internal Field Separator (IFS) and optionally sets new value
  286. # Parameters: %new_ifs%
  287. #-------------------------------------------------------------------------------
  288. function saveIFS() {
  289. local NEWIFS
  290. if [ "${IFS:-unset}" != "unset" ]; then
  291. ORGIFS="${IFS}"
  292. fi
  293. NEWIFS="${1}"
  294. if isSet NEWIFS; then
  295. IFS="${NEWIFS}"
  296. fi
  297. }
  298. #-------------------------------------------------------------------------------
  299. # Prints EzSync usage information
  300. #-------------------------------------------------------------------------------
  301. function showEzsyncUsage() {
  302. echo -e "\tEzSync is a part of EzBuild release building metatool used by"
  303. echo -e "\treGen2 Linux for synchronizing Portage Tree with mainline.\n"
  304. echo -e "USAGE: ezsync [parameters]"
  305. echo -e " -c\t--cleanlog\tclears log file; not working with -p and -P"
  306. echo -e " -h\t--help\t\tdisplays this screen and exits immediately"
  307. echo -e " -n\t--nosync\tspecifies the ebuilds source that will be omitted"
  308. echo -e " -N\t--nopush\tavoids automatic commits into repositories"
  309. echo -e " -p\t--purge\t\tclears temporary folder"
  310. echo -e " -P\t--purgeonly\tclears temporary folder and exits"
  311. echo -e " -s\t--syscheck\tperforms prerequisites check only"
  312. echo -e "\nEXAMPLES:"
  313. echo -e " ezsync -c -n mainline"
  314. echo -e " ezsync -n mainline -n my_repo1"
  315. echo -e "\n"
  316. exit 0
  317. }
  318. #-------------------------------------------------------------------------------
  319. # Returns a UNIX timestamp
  320. #-------------------------------------------------------------------------------
  321. function timestamp() {
  322. echo $(date +%s)
  323. }
  324. #-------------------------------------------------------------------------------
  325. # Returns lowercase string
  326. # Parameters: %string%
  327. #-------------------------------------------------------------------------------
  328. function toLower() {
  329. echo "${@}" | tr '[:upper:]' '[:lower:]'
  330. }
  331. #-------------------------------------------------------------------------------
  332. # Returns uppercase string
  333. # Parameters: %string%
  334. #-------------------------------------------------------------------------------
  335. function toUpper() {
  336. echo "${@}" | tr '[:lower:]' '[:upper:]'
  337. }
  338. #-------------------------------------------------------------------------------
  339. # Returns only the first part of a string, delimited by tabs or spaces
  340. # Parameters: %string%
  341. #-------------------------------------------------------------------------------
  342. function trim() {
  343. echo ${1}
  344. }