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.

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