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.

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