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.

common 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 [ ! -e /dev/null ]; then
  13. printError "The /dev directory seems to be not mounted!"
  14. return 1
  15. fi
  16. if [ ! -e /proc/mounts ]; then
  17. printError "The /proc directory seems to be not mounted!"
  18. return 1
  19. fi
  20. ping -c 1 google.com &> /dev/null
  21. if [ ${?} -ne 0 ]; then
  22. printError "There seems to be no Internet conectivity!"
  23. return 1
  24. fi
  25. return 0
  26. }
  27. #-------------------------------------------------------------------------------
  28. # Tests for a minimum version level by comparing version numbers
  29. # Parameters: %min_version% %test_version%
  30. #-------------------------------------------------------------------------------
  31. function checkVersion() {
  32. local BEST REF TST
  33. REF=$(echo "${1}" | \
  34. sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/")
  35. TST=$(echo "${2}" | \
  36. sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/")
  37. BEST=$(echo -e "${REF}\n${TST}" | sed "/^$/d" | sort -Vr | head -1)
  38. [[ "${BEST}" = "${TST}" ]] && return 0
  39. return 1
  40. }
  41. #-------------------------------------------------------------------------------
  42. # Outputs formatted error message and aborts script execution with given status
  43. # Parameters: %message% [%status%]
  44. #-------------------------------------------------------------------------------
  45. function die() {
  46. local STATUS=${2:-1}
  47. notify ${STATUS} "${1}"
  48. printError "${1}"
  49. echo -ne "\n"
  50. exit ${STATUS}
  51. }
  52. #-------------------------------------------------------------------------------
  53. # Failure handler; outputs warning or error depending on user settings
  54. # Parameters: %message% [%status%]
  55. #-------------------------------------------------------------------------------
  56. function fail() {
  57. local STATUS=${2:-1}
  58. if isEnabled ${FATALERRORS}; then
  59. die "${1}" ${STATUS}
  60. else
  61. pushNotification ${STATUS} "${1}"
  62. printWarn "${1}"
  63. fi
  64. }
  65. #-------------------------------------------------------------------------------
  66. # Checks if the given function has been defined
  67. # Parameters: %function%
  68. #-------------------------------------------------------------------------------
  69. function functionExists() {
  70. type ${1} 2>/dev/null | grep -q "is a function"
  71. return ${?}
  72. }
  73. #-------------------------------------------------------------------------------
  74. # Checks whether a defined variable contain specified element or not
  75. # Parameters: %element% %variable%
  76. #-------------------------------------------------------------------------------
  77. function hasElement() {
  78. local NEEDLE=${1}
  79. shift
  80. local X
  81. for X in "${@}"; do
  82. [ "${X}" = "${NEEDLE}" ] && return 0
  83. done
  84. return 1
  85. }
  86. #-------------------------------------------------------------------------------
  87. # Checks whether a supplied variable is defined or not
  88. # Parameters: %variable%
  89. #-------------------------------------------------------------------------------
  90. function isDefined() {
  91. [[ ${!1-X} == ${!1-Y} ]]
  92. }
  93. #-------------------------------------------------------------------------------
  94. # Checks whether a supplied variable is enabled or not
  95. # Parameters: %variable%
  96. #-------------------------------------------------------------------------------
  97. function isEnabled() {
  98. case "${1}" in
  99. [Ee][Nn][Aa][Bb][Ll][Ee][Dd])
  100. return 0
  101. ;;
  102. [Tt][Rr][Uu][Ee])
  103. return 0
  104. ;;
  105. [Yy][Ee][Ss])
  106. return 0
  107. ;;
  108. 1)
  109. return 0
  110. ;;
  111. esac
  112. return 1
  113. }
  114. #-------------------------------------------------------------------------------
  115. # Checks whether a variable is defined and if value's length > 0
  116. # Parameters: %variable%
  117. #-------------------------------------------------------------------------------
  118. function isSet() {
  119. if isDefined ${1}; then
  120. if [[ -n ${!1} ]]; then
  121. return 0
  122. fi
  123. fi
  124. return 1
  125. }
  126. #-------------------------------------------------------------------------------
  127. # Prints a list of available built-in modules
  128. #-------------------------------------------------------------------------------
  129. function listModules() {
  130. local MODULE
  131. echo -e "Modules can be used to enhance EzBuild functionality by handling"
  132. echo -e "triggered events. They can be easily enabled and/or disabled in"
  133. echo -e "configuration files. Every event has its own list of modules."
  134. echo -e "List of built-in modules available for ${EZNAME}:\n"
  135. for MODULE in ${EZROOT}/modules/*.ezmod; do
  136. source ${MODULE} &> /dev/null
  137. if [ ${?} -ne 0 ] || ! hasElement ${EZNAME} ${EZMOD_COMPATIBILITY}; then
  138. unloadModule
  139. continue
  140. fi
  141. MODULE=$(basename "${MODULE}" .ezmod)
  142. EZMOD_AUTHOR=${EZMOD_AUTHOR:-Unknown}
  143. EZMOD_DESCRIPTION=${EZMOD_DESCRIPTION:-No description provided}
  144. EZMOD_VERSION=${EZMOD_VERSION:-N/A}
  145. echo -e "${COLOR_CYAN}${COLOR_BOLD} ${MODULE}${COLOR_NORMAL}"
  146. echo -e "\t${COLOR_RED}Description:\t${COLOR_CYAN}${EZMOD_DESCRIPTION}"
  147. echo -e "\t${COLOR_RED}Version:\t${COLOR_CYAN}${EZMOD_VERSION}"
  148. echo -e "\t${COLOR_RED}Author:\t\t${COLOR_CYAN}${EZMOD_AUTHOR}\n"
  149. unloadModule
  150. done
  151. echo -e "${COLOR_WHITE}"
  152. exit 0
  153. }
  154. #-------------------------------------------------------------------------------
  155. # Loads EzBuild configuration
  156. #-------------------------------------------------------------------------------
  157. function loadConfiguration() {
  158. source ${EZROOT}/config/common.conf &> /dev/null || panic
  159. source ${EZROOT}/config/${EZNAME}.conf &> /dev/null || panic
  160. source ${EZROOT}/config/layout.conf &> /dev/null || panic
  161. source ~/.ezbuild/common.conf &> /dev/null || panic
  162. source ~/.ezbuild/${EZNAME}.conf &> /dev/null || panic
  163. source ~/.ezbuild/layout.conf &> /dev/null || panic
  164. }
  165. #-------------------------------------------------------------------------------
  166. # Loads all EzBuild Libraries
  167. #-------------------------------------------------------------------------------
  168. function loadLibraries() {
  169. source ${EZROOT}/libraries/constants &> /dev/null || panic
  170. source ${EZROOT}/libraries/filesystem &> /dev/null || panic
  171. source ${EZROOT}/libraries/repositories &> /dev/null || panic
  172. }
  173. #-------------------------------------------------------------------------------
  174. # Loads and executes given EzBuild module
  175. # Parameters: %module% %step%
  176. #-------------------------------------------------------------------------------
  177. function loadModule() {
  178. local MODULE="${1}"
  179. local FUNCTION="${EZNAME}_${2}"
  180. if [ -e ${MODULESCUSTOMDIR}/${MODULE}.ezmod ]; then
  181. source ${MODULESCUSTOMDIR}/${MODULE}.ezmod &> /dev/null
  182. else
  183. source ${EZROOT}/modules/${MODULE}.ezmod &> /dev/null
  184. fi
  185. if [ ${?} -eq 0 ] && hasElement ${EZNAME} ${EZMOD_COMPATIBILITY}; then
  186. if [ -e ${MODULESCUSTOMDIR}/${MODULE}.conf ]; then
  187. source ${EZROOT}/config/modules/${MODULE}.conf &> /dev/null
  188. source ${MODULESCUSTOMDIR}/${MODULE}.conf &> /dev/null
  189. elif [ -e ~/.ezbuild/modules/${MODULE}.conf ]; then
  190. source ${EZROOT}/config/modules/${MODULE}.conf &> /dev/null
  191. source ~/.ezbuild/modules/${MODULE}.conf &> /dev/null
  192. fi
  193. if [ ${?} -eq 0 ]; then
  194. if functionExists ${FUNCTION}; then
  195. eval ${FUNCTION}
  196. return ${?}
  197. else
  198. return 255
  199. fi
  200. fi
  201. fi
  202. return 1
  203. }
  204. #-------------------------------------------------------------------------------
  205. # Saves specified message into a log file
  206. # Parameters: %message% [%level%]
  207. #-------------------------------------------------------------------------------
  208. function logMessage() {
  209. local DATE MESSAGE TYPE
  210. if isEnabled ${LOGGING}; then
  211. MESSAGE="${1}"
  212. TYPE="${2}"
  213. DATE=$(date +"${LOGDATEFORMAT}")
  214. if [ -z ${TYPE} ]; then
  215. TYPE=" - "
  216. fi
  217. { echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}; } 2> /dev/null
  218. fi
  219. }
  220. #-------------------------------------------------------------------------------
  221. # Saves executed command multiline output into a log file
  222. # Parameters: %command_output%
  223. #-------------------------------------------------------------------------------
  224. function logOutput() {
  225. if isEnabled ${LOGGING}; then
  226. if [ -z "${@}" ]; then
  227. echo -e " )> (NO OUTPUT)" >> ${LOGFILE}
  228. else
  229. echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
  230. fi
  231. fi
  232. }
  233. #-------------------------------------------------------------------------------
  234. # Sends an e-mail notification about occured error
  235. # Parameters: %status% %message%
  236. #-------------------------------------------------------------------------------
  237. function notify() {
  238. if isEnabled ${LOGGING} && isSet NOTIFY && isEnabled ${EZNOTIFY}; then
  239. local MESG=""
  240. local SEND=0
  241. if [ ${1} -ne 0 ]; then
  242. MESG="The ${EZNAME} has failed with code: ${1} and message: ${2}"
  243. SEND=1
  244. if [ ${#NOTIFICATIONS[@]} -ne 0 ]; then
  245. MESG="${MESG}\nThere were also some non-fatal errors that has \
  246. been treated as warnings:"
  247. fi
  248. elif [ ${#NOTIFICATIONS[@]} -ne 0 ]; then
  249. MESG="The ${EZNAME} process has ended its job without any fatal \
  250. error, but there still were some non-fatal errors that has \
  251. been treated as warnings:"
  252. SEND=1
  253. fi
  254. MESG=$(echo -e "${MESG}" | awk -v OFS=" " '$1=$1')
  255. if [ ${SEND} -eq 1 ]; then
  256. if [ ${#NOTIFICATIONS[@]} -ne 0 ]; then
  257. for NOTIFICATION in "${NOTIFICATIONS[@]}"; do
  258. MESG="${MESG}\n * ${NOTIFICATION}"
  259. done
  260. fi
  261. local LOGS="Full output from log file:\n\n$(cat ${LOGFILE})"
  262. local TOPIC="The ${EZNAME} process has produced an error!"
  263. echo -e "${MESG}\n\n\n${LOGS}" | mailx -s "${TOPIC}" "${NOTIFY}"
  264. fi
  265. fi
  266. }
  267. #-------------------------------------------------------------------------------
  268. # Outputs error message and aborts program execution
  269. #-------------------------------------------------------------------------------
  270. function panic() {
  271. echo -e "FATAL ERROR: Unable to load necessary files!"
  272. echo -e "Your EzBuild installation seems to be broken..."
  273. exit 1
  274. }
  275. #-------------------------------------------------------------------------------
  276. # Outputs formatted error message to both display and log file
  277. # Parameters: %message%
  278. #-------------------------------------------------------------------------------
  279. function printError() {
  280. logMessage "${1}" "ERROR"
  281. echo -e " ${MESSAGE_ERROR} ${@}"
  282. }
  283. #-------------------------------------------------------------------------------
  284. # Outputs formatted information to both display and log file
  285. # Parameters: %message%
  286. #-------------------------------------------------------------------------------
  287. function printInfo() {
  288. logMessage "${1}" "INFO"
  289. echo -e " ${MESSAGE_INFO} ${@}"
  290. }
  291. #-------------------------------------------------------------------------------
  292. # Outputs formatted information about EzBuild version and copyrights
  293. #-------------------------------------------------------------------------------
  294. function printNotice() {
  295. echo -e "\n${COLOR_BOLD}EzBuild v${EZVERSION} (${EZNAME} launched)"
  296. echo -e "${EZCOPYRIGHT}${COLOR_NORMAL}"
  297. echo -e "${BORDER_STAR}\n"
  298. }
  299. #-------------------------------------------------------------------------------
  300. # Outputs formatted warning to both display and log file
  301. # Parameters: %message%
  302. #-------------------------------------------------------------------------------
  303. function printWarn() {
  304. logMessage "${1}" "WARN"
  305. echo -e " ${MESSAGE_WARN} ${@}"
  306. }
  307. #-------------------------------------------------------------------------------
  308. # Adds new non-fatal error notification message to the queue
  309. # Parameters: %status% %message%
  310. #-------------------------------------------------------------------------------
  311. function pushNotification() {
  312. local NUM=${#NOTIFICATIONS[@]}
  313. NOTIFICATIONS[${NUM}]="Code: ${1} Message: ${2}"
  314. }
  315. #-------------------------------------------------------------------------------
  316. # Optionally sends all notifications from queue and aborts program execution
  317. #-------------------------------------------------------------------------------
  318. function quit() {
  319. notify 0
  320. echo -e "${COLOR_BOLD}ALL DONE!\n${COLOR_NORMAL}"
  321. exit 0
  322. }
  323. #-------------------------------------------------------------------------------
  324. # Restores original Internal Field Separator (IFS)
  325. #-------------------------------------------------------------------------------
  326. function restoreIFS() {
  327. if [ "${ORGIFS:-unset}" != "unset" ]; then
  328. IFS="${ORGIFS}"
  329. unset ORGIFS
  330. else
  331. unset IFS
  332. fi
  333. }
  334. #-------------------------------------------------------------------------------
  335. # Silently executes given command and saves its output to log file if enabled
  336. # Parameters: %command%
  337. #-------------------------------------------------------------------------------
  338. function run() {
  339. local COMMAND OUTPUT RESULT
  340. COMMAND="${@}"
  341. logMessage "Executing: \`${COMMAND}\`" "DEBUG"
  342. OUTPUT=$(eval ${COMMAND} 2>&1)
  343. RESULT=${?}
  344. logOutput "${OUTPUT}"
  345. return ${RESULT}
  346. }
  347. #-------------------------------------------------------------------------------
  348. # Saves original Internal Field Separator (IFS) and optionally sets new value
  349. # Parameters: %new_ifs%
  350. #-------------------------------------------------------------------------------
  351. function saveIFS() {
  352. local NEWIFS
  353. if [ "${IFS:-unset}" != "unset" ]; then
  354. ORGIFS="${IFS}"
  355. fi
  356. NEWIFS="${1}"
  357. if isSet NEWIFS; then
  358. IFS="${NEWIFS}"
  359. fi
  360. }
  361. #-------------------------------------------------------------------------------
  362. # Prints EzBuild usage information
  363. #-------------------------------------------------------------------------------
  364. function showEzbuildUsage() {
  365. echo -e "EzBuild is release building metatool used by reGen2 developers for"
  366. echo -e "building a custom Linux releases and templates.\n"
  367. echo -e "USAGE: ezsync [parameters]"
  368. echo -e " -c\t--cleanlog\tclears log file; not working with -p and -P"
  369. echo -e " -D\t--tarballdate\toverrides produced image version/date format"
  370. echo -e " -h\t--help\t\tdisplays this screen and exits immediately"
  371. echo -e " -l\t--listmods\tshows the list of available built-in modules"
  372. echo -e " -p\t--purge\t\tclears temporary folder"
  373. echo -e " -P\t--purgeonly\tclears temporary folder and exits"
  374. echo -e " -s\t--syscheck\tperforms prerequisites check only"
  375. echo -e " -t\t--notrigger\tdisables all build triggers and modules"
  376. echo -e " -T\t--tarballformat\toverrides default compression algorithm"
  377. echo -e "\n"
  378. exit 0
  379. }
  380. #-------------------------------------------------------------------------------
  381. # Prints EzSync usage information
  382. #-------------------------------------------------------------------------------
  383. function showEzsyncUsage() {
  384. echo -e "\tEzSync is a part of EzBuild release building metatool used by"
  385. echo -e "\treGen2 Linux for synchronizing Portage Tree with mainline.\n"
  386. echo -e "USAGE: ezsync [parameters]"
  387. echo -e " -c\t--cleanlog\tclears log file; not working with -p and -P"
  388. echo -e " -D\t--tarballdate\toverrides produced image version/date format"
  389. echo -e " -h\t--help\t\tdisplays this screen and exits immediately"
  390. echo -e " -l\t--listmods\tshows the list of available built-in modules"
  391. echo -e " -n\t--nosync\tspecifies the ebuilds source that will be omitted"
  392. echo -e " -N\t--nopush\tavoids automatic commits into repositories"
  393. echo -e " -p\t--purge\t\tclears temporary folder"
  394. echo -e " -P\t--purgeonly\tclears temporary folder and exits"
  395. echo -e " -s\t--syscheck\tperforms prerequisites check only"
  396. echo -e " -t\t--notrigger\tdisables all build triggers and modules"
  397. echo -e " -T\t--tarballformat\toverrides default compression algorithm"
  398. echo -e "\nEXAMPLES:"
  399. echo -e " ezsync -c -n mainline"
  400. echo -e " ezsync -n mainline -n my_repo1"
  401. echo -e "\n"
  402. exit 0
  403. }
  404. #-------------------------------------------------------------------------------
  405. # Returns a UNIX timestamp
  406. #-------------------------------------------------------------------------------
  407. function timestamp() {
  408. echo $(date +%s)
  409. }
  410. #-------------------------------------------------------------------------------
  411. # Returns lowercase string
  412. # Parameters: %string%
  413. #-------------------------------------------------------------------------------
  414. function toLower() {
  415. echo "${@}" | tr '[:upper:]' '[:lower:]'
  416. }
  417. #-------------------------------------------------------------------------------
  418. # Returns uppercase string
  419. # Parameters: %string%
  420. #-------------------------------------------------------------------------------
  421. function toUpper() {
  422. echo "${@}" | tr '[:lower:]' '[:upper:]'
  423. }
  424. #-------------------------------------------------------------------------------
  425. # Returns only the first part of a string, delimited by tabs or spaces
  426. # Parameters: %string%
  427. #-------------------------------------------------------------------------------
  428. function trim() {
  429. echo ${1}
  430. }
  431. #-------------------------------------------------------------------------------
  432. # Silently unloads any previously loaded module
  433. #-------------------------------------------------------------------------------
  434. function unloadModule() {
  435. unset EZMOD_AUTHOR
  436. unset EZMOD_COMPATIBILITY
  437. unset EZMOD_DESCRIPTION
  438. unset EZMOD_VERSION
  439. unset ezsync_precommit
  440. unset ezsync_presync
  441. unset ezsync_postcommit
  442. unset ezsync_postsync
  443. }