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.

307 lines
10 KiB

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; 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: %variable% %element%
  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/${EZNAME}.conf &> /dev/null || panic
  118. source ${EZROOT}/config/layout.conf &> /dev/null || panic
  119. source /etc/ezbuild/${EZNAME}.conf &> /dev/null || panic
  120. source /etc/ezbuild/layout.conf &> /dev/null || panic
  121. }
  122. #-------------------------------------------------------------------------------
  123. # Loads all EzBuild Libraries
  124. #-------------------------------------------------------------------------------
  125. function loadLibraries() {
  126. source ${EZROOT}/libraries/constants &> /dev/null || panic
  127. source ${EZROOT}/libraries/filesystem &> /dev/null || panic
  128. }
  129. #-------------------------------------------------------------------------------
  130. # Saves specified message into a log file
  131. # Parameters: %message% %level%
  132. #-------------------------------------------------------------------------------
  133. function logMessage() {
  134. local DATE MESSAGE TYPE
  135. if isEnabled ${LOGGING}; then
  136. MESSAGE="${1}"
  137. TYPE="${2}"
  138. DATE=$(date +"${LOGDATEFORMAT}")
  139. if [ -z ${TYPE} ]; then
  140. TYPE=" - "
  141. fi
  142. { echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}; } 2> /dev/null
  143. fi
  144. }
  145. #-------------------------------------------------------------------------------
  146. # Saves executed command multiline output into a log file
  147. # Parameters: %command_output%
  148. #-------------------------------------------------------------------------------
  149. function logOutput() {
  150. if isEnabled ${LOGGING}; then
  151. if [ -z "${@}" ]; then
  152. echo -e " )> (NO OUTPUT)" >> ${LOGFILE}
  153. else
  154. echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
  155. fi
  156. fi
  157. }
  158. #-------------------------------------------------------------------------------
  159. # Outputs error message and aborts program execution
  160. #-------------------------------------------------------------------------------
  161. function panic() {
  162. echo -e "FATAL ERROR: Unable to load necessary files!"
  163. echo -e "Your EzBuild installation seems to be broken..."
  164. exit 1
  165. }
  166. #-------------------------------------------------------------------------------
  167. # Outputs formatted error message to both display and log file
  168. # Parameters: %message%
  169. #-------------------------------------------------------------------------------
  170. function printError() {
  171. logMessage "${1}" "ERROR"
  172. echo -e " ${MESSAGE_ERROR} ${@}"
  173. }
  174. #-------------------------------------------------------------------------------
  175. # Outputs formatted information to both display and log file
  176. # Parameters: %message%
  177. #-------------------------------------------------------------------------------
  178. function printInfo() {
  179. logMessage "${1}" "INFO"
  180. echo -e " ${MESSAGE_INFO} ${@}"
  181. }
  182. #-------------------------------------------------------------------------------
  183. # Outputs formatted information about EzBuild version and copyrights
  184. #-------------------------------------------------------------------------------
  185. function printNotice() {
  186. echo -e "\n${COLOR_BOLD}EzBuild v${EZVERSION} (${EZNAME} launched)"
  187. echo -e "${EZCOPYRIGHT}${COLOR_NORMAL}"
  188. echo -e "${BORDER_STAR}\n"
  189. }
  190. #-------------------------------------------------------------------------------
  191. # Outputs formatted warning to both display and log file
  192. # Parameters: %message%
  193. #-------------------------------------------------------------------------------
  194. function printWarn() {
  195. logMessage "${1}" "WARN"
  196. echo -e " ${MESSAGE_WARN} ${@}"
  197. }
  198. function quit() {
  199. echo -e "${COLOR_BOLD}ALL DONE!\n${COLOR_NORMAL}"
  200. exit 0
  201. }
  202. #-------------------------------------------------------------------------------
  203. # Restores original Internal Field Separator (IFS)
  204. #-------------------------------------------------------------------------------
  205. function restoreIFS() {
  206. if [ "${ORGIFS:-unset}" != "unset" ]; then
  207. IFS="${ORGIFS}"
  208. unset ORGIFS
  209. else
  210. unset IFS
  211. fi
  212. }
  213. #-------------------------------------------------------------------------------
  214. # Silently executes given command and saves its output to log file if enabled
  215. # Parameters: %command%
  216. #-------------------------------------------------------------------------------
  217. function run() {
  218. local COMMAND OUTPUT RESULT
  219. COMMAND="${@}"
  220. logMessage "Executing: \"${COMMAND}\"" "DEBUG"
  221. OUTPUT=$(${COMMAND} 2>&1)
  222. RESULT=${?}
  223. logOutput "${OUTPUT}"
  224. return ${RESULT}
  225. }
  226. #-------------------------------------------------------------------------------
  227. # Saves original Internal Field Separator (IFS) and optionally sets new value
  228. # Parameters: %new_ifs%
  229. #-------------------------------------------------------------------------------
  230. function saveIFS() {
  231. local NEWIFS
  232. if [ "${IFS:-unset}" != "unset" ]; then
  233. ORGIFS="${IFS}"
  234. fi
  235. NEWIFS="${1}"
  236. if isSet NEWIFS; then
  237. IFS="${NEWIFS}"
  238. fi
  239. }
  240. #-------------------------------------------------------------------------------
  241. # Prints EzSync usage information
  242. #-------------------------------------------------------------------------------
  243. function showEzsyncUsage() {
  244. echo -e "\tEzSync is a part of EzBuild release building metatool used by"
  245. echo -e "\treGen2 Linux for synchronizing Portage Tree with mainline.\n"
  246. echo -e "USAGE: ezsync [parameters]"
  247. echo -e " -c\t--cleanlog\tclears log file; not working with -p and -P"
  248. echo -e " -h\t--help\t\tdisplays this screen and exits immediately"
  249. echo -e " -p\t--purge\t\tclears temporary folder"
  250. echo -e " -P\t--purgeonly\tclears temporary folder and exits"
  251. echo -e " -s\t--syscheck\tperforms prerequisites check only"
  252. echo -e "\n"
  253. exit 0
  254. }
  255. #-------------------------------------------------------------------------------
  256. # Returns a UNIX timestamp
  257. #-------------------------------------------------------------------------------
  258. function timestamp() {
  259. echo $(date +%s)
  260. }
  261. #-------------------------------------------------------------------------------
  262. # Returns lowercase string
  263. # Parameters: %string%
  264. #-------------------------------------------------------------------------------
  265. function toLower() {
  266. echo "${@}" | tr '[:upper:]' '[:lower:]'
  267. }
  268. #-------------------------------------------------------------------------------
  269. # Returns uppercase string
  270. # Parameters: %string%
  271. #-------------------------------------------------------------------------------
  272. function toUpper() {
  273. echo "${@}" | tr '[:lower:]' '[:upper:]'
  274. }
  275. #-------------------------------------------------------------------------------
  276. # Returns only the first part of a string, delimited by tabs or spaces
  277. # Parameters: %string%
  278. #-------------------------------------------------------------------------------
  279. function trim() {
  280. echo ${1}
  281. }