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.

382 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 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. # Loads EzBuild configuration
  132. #-------------------------------------------------------------------------------
  133. function loadConfiguration() {
  134. source ${EZROOT}/config/common.conf &> /dev/null || panic
  135. source ${EZROOT}/config/${EZNAME}.conf &> /dev/null || panic
  136. source ${EZROOT}/config/layout.conf &> /dev/null || panic
  137. source /etc/ezbuild/common.conf &> /dev/null || panic
  138. source /etc/ezbuild/${EZNAME}.conf &> /dev/null || panic
  139. source /etc/ezbuild/layout.conf &> /dev/null || panic
  140. }
  141. #-------------------------------------------------------------------------------
  142. # Loads all EzBuild Libraries
  143. #-------------------------------------------------------------------------------
  144. function loadLibraries() {
  145. source ${EZROOT}/libraries/constants &> /dev/null || panic
  146. source ${EZROOT}/libraries/filesystem &> /dev/null || panic
  147. source ${EZROOT}/libraries/repositories &> /dev/null || panic
  148. }
  149. #-------------------------------------------------------------------------------
  150. # Saves specified message into a log file
  151. # Parameters: %message% [%level%]
  152. #-------------------------------------------------------------------------------
  153. function logMessage() {
  154. local DATE MESSAGE TYPE
  155. if isEnabled ${LOGGING}; then
  156. MESSAGE="${1}"
  157. TYPE="${2}"
  158. DATE=$(date +"${LOGDATEFORMAT}")
  159. if [ -z ${TYPE} ]; then
  160. TYPE=" - "
  161. fi
  162. { echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}; } 2> /dev/null
  163. fi
  164. }
  165. #-------------------------------------------------------------------------------
  166. # Saves executed command multiline output into a log file
  167. # Parameters: %command_output%
  168. #-------------------------------------------------------------------------------
  169. function logOutput() {
  170. if isEnabled ${LOGGING}; then
  171. if [ -z "${@}" ]; then
  172. echo -e " )> (NO OUTPUT)" >> ${LOGFILE}
  173. else
  174. echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
  175. fi
  176. fi
  177. }
  178. #-------------------------------------------------------------------------------
  179. # Sends an e-mail notification about occured error
  180. # Parameters: %status% %message%
  181. #-------------------------------------------------------------------------------
  182. function notify() {
  183. if isEnabled ${LOGGING} && isSet NOTIFY && isEnabled ${EZNOTIFY}; then
  184. local MESG=""
  185. local SEND=0
  186. if [ ${1} -ne 0 ]; then
  187. MESG="The ${EZNAME} has failed with code: ${1} and message: ${2}"
  188. SEND=1
  189. if [ ${#NOTIFICATIONS[@]} -ne 0 ]; then
  190. MESG="${MESG}\nThere were also some non-fatal errors that has \
  191. been treated as warnings:"
  192. fi
  193. elif [ ${#NOTIFICATIONS[@]} -ne 0 ]; then
  194. MESG="The ${EZNAME} process has ended its job without any fatal \
  195. error, but there still were some non-fatal errors that has \
  196. been treated as warnings:"
  197. SEND=1
  198. fi
  199. MESG=$(echo -e "${MESG}" | awk -v OFS=" " '$1=$1')
  200. if [ ${SEND} -eq 1 ]; then
  201. if [ ${#NOTIFICATIONS[@]} -ne 0 ]; then
  202. for NOTIFICATION in "${NOTIFICATIONS[@]}"; do
  203. MESG="${MESG}\n * ${NOTIFICATION}"
  204. done
  205. fi
  206. local LOGS="Full output from log file:\n\n$(cat ${LOGFILE})"
  207. local TOPIC="The ${EZNAME} process has produced an error!"
  208. echo -e "${MESG}\n\n\n${LOGS}" | mailx -s "${TOPIC}" "${NOTIFY}"
  209. fi
  210. fi
  211. }
  212. #-------------------------------------------------------------------------------
  213. # Outputs error message and aborts program execution
  214. #-------------------------------------------------------------------------------
  215. function panic() {
  216. echo -e "FATAL ERROR: Unable to load necessary files!"
  217. echo -e "Your EzBuild installation seems to be broken..."
  218. exit 1
  219. }
  220. #-------------------------------------------------------------------------------
  221. # Outputs formatted error message to both display and log file
  222. # Parameters: %message%
  223. #-------------------------------------------------------------------------------
  224. function printError() {
  225. logMessage "${1}" "ERROR"
  226. echo -e " ${MESSAGE_ERROR} ${@}"
  227. }
  228. #-------------------------------------------------------------------------------
  229. # Outputs formatted information to both display and log file
  230. # Parameters: %message%
  231. #-------------------------------------------------------------------------------
  232. function printInfo() {
  233. logMessage "${1}" "INFO"
  234. echo -e " ${MESSAGE_INFO} ${@}"
  235. }
  236. #-------------------------------------------------------------------------------
  237. # Outputs formatted information about EzBuild version and copyrights
  238. #-------------------------------------------------------------------------------
  239. function printNotice() {
  240. echo -e "\n${COLOR_BOLD}EzBuild v${EZVERSION} (${EZNAME} launched)"
  241. echo -e "${EZCOPYRIGHT}${COLOR_NORMAL}"
  242. echo -e "${BORDER_STAR}\n"
  243. }
  244. #-------------------------------------------------------------------------------
  245. # Outputs formatted warning to both display and log file
  246. # Parameters: %message%
  247. #-------------------------------------------------------------------------------
  248. function printWarn() {
  249. logMessage "${1}" "WARN"
  250. echo -e " ${MESSAGE_WARN} ${@}"
  251. }
  252. #-------------------------------------------------------------------------------
  253. # Adds new non-fatal error notification message to the queue
  254. # Parameters: %status% %message%
  255. #-------------------------------------------------------------------------------
  256. function pushNotification() {
  257. local NUM=${#NOTIFICATIONS[@]}
  258. NOTIFICATIONS[${NUM}]="Code: ${1} Message: ${2}"
  259. }
  260. #-------------------------------------------------------------------------------
  261. # Optionally sends all notifications from queue and aborts program execution
  262. #-------------------------------------------------------------------------------
  263. function quit() {
  264. notify 0
  265. echo -e "${COLOR_BOLD}ALL DONE!\n${COLOR_NORMAL}"
  266. exit 0
  267. }
  268. #-------------------------------------------------------------------------------
  269. # Restores original Internal Field Separator (IFS)
  270. #-------------------------------------------------------------------------------
  271. function restoreIFS() {
  272. if [ "${ORGIFS:-unset}" != "unset" ]; then
  273. IFS="${ORGIFS}"
  274. unset ORGIFS
  275. else
  276. unset IFS
  277. fi
  278. }
  279. #-------------------------------------------------------------------------------
  280. # Silently executes given command and saves its output to log file if enabled
  281. # Parameters: %command%
  282. #-------------------------------------------------------------------------------
  283. function run() {
  284. local COMMAND OUTPUT RESULT
  285. COMMAND="${@}"
  286. logMessage "Executing: \"${COMMAND}\"" "DEBUG"
  287. OUTPUT=$(eval ${COMMAND} 2>&1)
  288. RESULT=${?}
  289. logOutput "${OUTPUT}"
  290. return ${RESULT}
  291. }
  292. #-------------------------------------------------------------------------------
  293. # Saves original Internal Field Separator (IFS) and optionally sets new value
  294. # Parameters: %new_ifs%
  295. #-------------------------------------------------------------------------------
  296. function saveIFS() {
  297. local NEWIFS
  298. if [ "${IFS:-unset}" != "unset" ]; then
  299. ORGIFS="${IFS}"
  300. fi
  301. NEWIFS="${1}"
  302. if isSet NEWIFS; then
  303. IFS="${NEWIFS}"
  304. fi
  305. }
  306. #-------------------------------------------------------------------------------
  307. # Prints EzSync usage information
  308. #-------------------------------------------------------------------------------
  309. function showEzsyncUsage() {
  310. echo -e "\tEzSync is a part of EzBuild release building metatool used by"
  311. echo -e "\treGen2 Linux for synchronizing Portage Tree with mainline.\n"
  312. echo -e "USAGE: ezsync [parameters]"
  313. echo -e " -c\t--cleanlog\tclears log file; not working with -p and -P"
  314. echo -e " -h\t--help\t\tdisplays this screen and exits immediately"
  315. echo -e " -n\t--nosync\tspecifies the ebuilds source that will be omitted"
  316. echo -e " -N\t--nopush\tavoids automatic commits into repositories"
  317. echo -e " -p\t--purge\t\tclears temporary folder"
  318. echo -e " -P\t--purgeonly\tclears temporary folder and exits"
  319. echo -e " -s\t--syscheck\tperforms prerequisites check only"
  320. echo -e "\nEXAMPLES:"
  321. echo -e " ezsync -c -n mainline"
  322. echo -e " ezsync -n mainline -n my_repo1"
  323. echo -e "\n"
  324. exit 0
  325. }
  326. #-------------------------------------------------------------------------------
  327. # Returns a UNIX timestamp
  328. #-------------------------------------------------------------------------------
  329. function timestamp() {
  330. echo $(date +%s)
  331. }
  332. #-------------------------------------------------------------------------------
  333. # Returns lowercase string
  334. # Parameters: %string%
  335. #-------------------------------------------------------------------------------
  336. function toLower() {
  337. echo "${@}" | tr '[:upper:]' '[:lower:]'
  338. }
  339. #-------------------------------------------------------------------------------
  340. # Returns uppercase string
  341. # Parameters: %string%
  342. #-------------------------------------------------------------------------------
  343. function toUpper() {
  344. echo "${@}" | tr '[:lower:]' '[:upper:]'
  345. }
  346. #-------------------------------------------------------------------------------
  347. # Returns only the first part of a string, delimited by tabs or spaces
  348. # Parameters: %string%
  349. #-------------------------------------------------------------------------------
  350. function trim() {
  351. echo ${1}
  352. }