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.

482 lines
17KB

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