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.

322 lines
11 KiB

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 code
  47. # Parameters: %message% %code%
  48. #-------------------------------------------------------------------------------
  49. function die() {
  50. notify "${1}" ${2}
  51. printError "${1}"
  52. echo -ne "\n"
  53. exit ${2}
  54. }
  55. #-------------------------------------------------------------------------------
  56. # Checks whether a defined variable contain specified element or not
  57. # Parameters: %element% %variable%
  58. #-------------------------------------------------------------------------------
  59. function hasElement() {
  60. local NEEDLE=${1}
  61. shift
  62. local X
  63. for X in "${@}"; do
  64. [ "${X}" = "${NEEDLE}" ] && return 0
  65. done
  66. return 1
  67. }
  68. #-------------------------------------------------------------------------------
  69. # Checks whether a supplied variable is defined or not
  70. # Parameters: %variable%
  71. #-------------------------------------------------------------------------------
  72. function isDefined() {
  73. [[ ${!1-X} == ${!1-Y} ]]
  74. }
  75. #-------------------------------------------------------------------------------
  76. # Checks whether a supplied variable is enabled or not
  77. # Parameters: %variable%
  78. #-------------------------------------------------------------------------------
  79. function isEnabled() {
  80. case "${1}" in
  81. [Ee][Nn][Aa][Bb][Ll][Ee][Dd])
  82. return 0
  83. ;;
  84. [Tt][Rr][Uu][Ee])
  85. return 0
  86. ;;
  87. [Yy][Ee][Ss])
  88. return 0
  89. ;;
  90. 1)
  91. return 0
  92. ;;
  93. esac
  94. return 1
  95. }
  96. #-------------------------------------------------------------------------------
  97. # Checks whether a variable is defined and if value's length > 0
  98. # Parameters: %variable%
  99. #-------------------------------------------------------------------------------
  100. function isSet() {
  101. if isDefined ${1}; then
  102. if [[ -n ${!1} ]]; then
  103. return 0
  104. fi
  105. fi
  106. return 1
  107. }
  108. #-------------------------------------------------------------------------------
  109. # Loads EzBuild configuration
  110. #-------------------------------------------------------------------------------
  111. function loadConfiguration() {
  112. source ${EZROOT}/config/common.conf &> /dev/null || panic
  113. source ${EZROOT}/config/${EZNAME}.conf &> /dev/null || panic
  114. source ${EZROOT}/config/layout.conf &> /dev/null || panic
  115. source /etc/ezbuild/common.conf &> /dev/null || panic
  116. source /etc/ezbuild/${EZNAME}.conf &> /dev/null || panic
  117. source /etc/ezbuild/layout.conf &> /dev/null || panic
  118. }
  119. #-------------------------------------------------------------------------------
  120. # Loads all EzBuild Libraries
  121. #-------------------------------------------------------------------------------
  122. function loadLibraries() {
  123. source ${EZROOT}/libraries/constants &> /dev/null || panic
  124. source ${EZROOT}/libraries/filesystem &> /dev/null || panic
  125. source ${EZROOT}/libraries/repositories &> /dev/null || panic
  126. }
  127. #-------------------------------------------------------------------------------
  128. # Saves specified message into a log file
  129. # Parameters: %message% [%level%]
  130. #-------------------------------------------------------------------------------
  131. function logMessage() {
  132. local DATE MESSAGE TYPE
  133. if isEnabled ${LOGGING}; then
  134. MESSAGE="${1}"
  135. TYPE="${2}"
  136. DATE=$(date +"${LOGDATEFORMAT}")
  137. if [ -z ${TYPE} ]; then
  138. TYPE=" - "
  139. fi
  140. { echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}; } 2> /dev/null
  141. fi
  142. }
  143. #-------------------------------------------------------------------------------
  144. # Saves executed command multiline output into a log file
  145. # Parameters: %command_output%
  146. #-------------------------------------------------------------------------------
  147. function logOutput() {
  148. if isEnabled ${LOGGING}; then
  149. if [ -z "${@}" ]; then
  150. echo -e " )> (NO OUTPUT)" >> ${LOGFILE}
  151. else
  152. echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
  153. fi
  154. fi
  155. }
  156. #-------------------------------------------------------------------------------
  157. # Sends an e-mail notification about occured error
  158. # Parameters: %message% %code%
  159. #-------------------------------------------------------------------------------
  160. function notify() {
  161. if isEnabled ${LOGGING} && isSet NOTIFY && isEnabled ${EZNOTIFY}; then
  162. local LOGS="Full output from logs:\n\n$(cat ${LOGFILE})"
  163. local MESG="The ${EZNAME} has failed with code: ${2} and message: ${1}"
  164. local TOPIC="The ${EZNAME} process has failed!"
  165. echo -e "${MESG}\n${LOGS}" | mailx -s "${TOPIC}" "${NOTIFY}"
  166. fi
  167. }
  168. #-------------------------------------------------------------------------------
  169. # Outputs error message and aborts program execution
  170. #-------------------------------------------------------------------------------
  171. function panic() {
  172. echo -e "FATAL ERROR: Unable to load necessary files!"
  173. echo -e "Your EzBuild installation seems to be broken..."
  174. exit 1
  175. }
  176. #-------------------------------------------------------------------------------
  177. # Outputs formatted error message to both display and log file
  178. # Parameters: %message%
  179. #-------------------------------------------------------------------------------
  180. function printError() {
  181. logMessage "${1}" "ERROR"
  182. echo -e " ${MESSAGE_ERROR} ${@}"
  183. }
  184. #-------------------------------------------------------------------------------
  185. # Outputs formatted information to both display and log file
  186. # Parameters: %message%
  187. #-------------------------------------------------------------------------------
  188. function printInfo() {
  189. logMessage "${1}" "INFO"
  190. echo -e " ${MESSAGE_INFO} ${@}"
  191. }
  192. #-------------------------------------------------------------------------------
  193. # Outputs formatted information about EzBuild version and copyrights
  194. #-------------------------------------------------------------------------------
  195. function printNotice() {
  196. echo -e "\n${COLOR_BOLD}EzBuild v${EZVERSION} (${EZNAME} launched)"
  197. echo -e "${EZCOPYRIGHT}${COLOR_NORMAL}"
  198. echo -e "${BORDER_STAR}\n"
  199. }
  200. #-------------------------------------------------------------------------------
  201. # Outputs formatted warning to both display and log file
  202. # Parameters: %message%
  203. #-------------------------------------------------------------------------------
  204. function printWarn() {
  205. logMessage "${1}" "WARN"
  206. echo -e " ${MESSAGE_WARN} ${@}"
  207. }
  208. function quit() {
  209. echo -e "${COLOR_BOLD}ALL DONE!\n${COLOR_NORMAL}"
  210. exit 0
  211. }
  212. #-------------------------------------------------------------------------------
  213. # Restores original Internal Field Separator (IFS)
  214. #-------------------------------------------------------------------------------
  215. function restoreIFS() {
  216. if [ "${ORGIFS:-unset}" != "unset" ]; then
  217. IFS="${ORGIFS}"
  218. unset ORGIFS
  219. else
  220. unset IFS
  221. fi
  222. }
  223. #-------------------------------------------------------------------------------
  224. # Silently executes given command and saves its output to log file if enabled
  225. # Parameters: %command%
  226. #-------------------------------------------------------------------------------
  227. function run() {
  228. local COMMAND OUTPUT RESULT
  229. COMMAND="${@}"
  230. logMessage "Executing: \"${COMMAND}\"" "DEBUG"
  231. OUTPUT=$(${COMMAND} 2>&1)
  232. RESULT=${?}
  233. logOutput "${OUTPUT}"
  234. return ${RESULT}
  235. }
  236. #-------------------------------------------------------------------------------
  237. # Saves original Internal Field Separator (IFS) and optionally sets new value
  238. # Parameters: %new_ifs%
  239. #-------------------------------------------------------------------------------
  240. function saveIFS() {
  241. local NEWIFS
  242. if [ "${IFS:-unset}" != "unset" ]; then
  243. ORGIFS="${IFS}"
  244. fi
  245. NEWIFS="${1}"
  246. if isSet NEWIFS; then
  247. IFS="${NEWIFS}"
  248. fi
  249. }
  250. #-------------------------------------------------------------------------------
  251. # Prints EzSync usage information
  252. #-------------------------------------------------------------------------------
  253. function showEzsyncUsage() {
  254. echo -e "\tEzSync is a part of EzBuild release building metatool used by"
  255. echo -e "\treGen2 Linux for synchronizing Portage Tree with mainline.\n"
  256. echo -e "USAGE: ezsync [parameters]"
  257. echo -e " -c\t--cleanlog\tclears log file; not working with -p and -P"
  258. echo -e " -h\t--help\t\tdisplays this screen and exits immediately"
  259. echo -e " -n\t--nosync\tspecifies the ebuilds source that will be omitted"
  260. echo -e " -p\t--purge\t\tclears temporary folder"
  261. echo -e " -P\t--purgeonly\tclears temporary folder and exits"
  262. echo -e " -s\t--syscheck\tperforms prerequisites check only"
  263. echo -e "\nEXAMPLES:"
  264. echo -e " ezsync -c -n mainline"
  265. echo -e " ezsync -n mainline -n my_repo1"
  266. echo -e "\n"
  267. exit 0
  268. }
  269. #-------------------------------------------------------------------------------
  270. # Returns a UNIX timestamp
  271. #-------------------------------------------------------------------------------
  272. function timestamp() {
  273. echo $(date +%s)
  274. }
  275. #-------------------------------------------------------------------------------
  276. # Returns lowercase string
  277. # Parameters: %string%
  278. #-------------------------------------------------------------------------------
  279. function toLower() {
  280. echo "${@}" | tr '[:upper:]' '[:lower:]'
  281. }
  282. #-------------------------------------------------------------------------------
  283. # Returns uppercase string
  284. # Parameters: %string%
  285. #-------------------------------------------------------------------------------
  286. function toUpper() {
  287. echo "${@}" | tr '[:lower:]' '[:upper:]'
  288. }
  289. #-------------------------------------------------------------------------------
  290. # Returns only the first part of a string, delimited by tabs or spaces
  291. # Parameters: %string%
  292. #-------------------------------------------------------------------------------
  293. function trim() {
  294. echo ${1}
  295. }