install.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/bin/bash
  2. #
  3. # install: Installation script
  4. #
  5. # Copyright (c) Embedthis Software LLC, 2003-2014. All Rights Reserved.
  6. #
  7. # Usage: install [configFile]
  8. #
  9. ################################################################################
  10. #
  11. # The configFile is of the format:
  12. # FMT=[rpm|deb|tar] # Package format to use
  13. # installbin=[YN] # Install binary package
  14. # runDaemon=[YN] # Run the program as a daemon
  15. # httpPort=portNumber # Http port to listen on
  16. # sslPort=portNumber # SSL port to listen on
  17. # username=username # User account for goahead
  18. # groupname=groupname # Group account for goahead
  19. # hostname=groupname # Serving host
  20. #
  21. HOME=`pwd`
  22. FMT=
  23. SITE=localhost
  24. PAGE=/index.html
  25. HOSTNAME=`hostname`
  26. COMPANY="${settings.company}"
  27. PRODUCT="${settings.name}"
  28. NAME="${settings.title}"
  29. VERSION="${settings.version}"
  30. OS="${platform.os}"
  31. CPU="${platform.arch}"
  32. DIST="${platform.dist}"
  33. ROOT_PREFIX="${prefixes.root}"
  34. BASE_PREFIX="${prefixes.base}"
  35. STATE_PREFIX="${prefixes.state}"
  36. APP_PREFIX="${prefixes.app}"
  37. VAPP_PREFIX="${prefixes.vapp}"
  38. BIN_PREFIX="${prefixes.bin}"
  39. SBIN_PREFIX="${prefixes.sbin}"
  40. ETC_PREFIX="${prefixes.etc}"
  41. INC_PREFIX="${prefixes.inc}"
  42. LIB_PREFIX="${prefixes.lib}"
  43. MAN_PREFIX="${prefixes.man}"
  44. WEB_PREFIX="${prefixes.web}"
  45. ABIN="${VAPP_PREFIX}/bin"
  46. AINC="${VAPP_PREFIX}/in"
  47. installbin=Y
  48. runDaemon=Y
  49. headless=${HEADLESS:-0}
  50. HTTP_PORT=80
  51. SSL_PORT=443
  52. PATH="$PATH:/sbin:/usr/sbin"
  53. unset CDPATH
  54. export CYGWIN=nodosfilewarning
  55. CYGWIN=nodosfilewarning
  56. ###############################################################################
  57. setup() {
  58. umask 022
  59. if [ $OS != windows -a `id -u` != "0" ] ; then
  60. echo "You must be root to install this product."
  61. exit 255
  62. fi
  63. #
  64. # Headless install
  65. #
  66. if [ $# -ge 1 ] ; then
  67. if [ ! -f $1 ] ; then
  68. echo "Could not find installation config file \"$1\"." 1>&2
  69. exit 255
  70. else
  71. . $1
  72. installFiles $FMT
  73. if [ "$installbin" = "Y" ] ; then
  74. appman stop disable uninstall >/dev/null 2>&1
  75. patchConfiguration
  76. appman install
  77. if [ "$runDaemon" = "Y" ] ; then
  78. appman enable start
  79. fi
  80. fi
  81. fi
  82. exit 0
  83. fi
  84. sleuthPackageFormat
  85. getAccountDetails
  86. [ "$headless" != 1 ] && echo -e "\n$NAME ${VERSION} Installation\n"
  87. }
  88. getAccountDetails() {
  89. local g u
  90. #
  91. # Select default username
  92. #
  93. for u in www-data _www nobody Administrator
  94. do
  95. grep "$u" /etc/passwd >/dev/null
  96. if [ $? = 0 ] ; then
  97. username=$u
  98. break
  99. fi
  100. done
  101. if [ "$username" = "" ] ; then
  102. echo "Can't find a suitable username in /etc/passwd for $PRODUCT" 1>&2
  103. exit 255
  104. fi
  105. #
  106. # Select default group name
  107. #
  108. for g in www-data _www nogroup nobody Administrators
  109. do
  110. grep "$g" /etc/group >/dev/null
  111. if [ $? = 0 ] ; then
  112. groupname=$g
  113. break
  114. fi
  115. done
  116. if [ "$groupname" = "" ] ; then
  117. echo "Can't find a suitable group in /etc/group for $PRODUCT" 1>&2
  118. exit 255
  119. fi
  120. }
  121. sleuthPackageFormat() {
  122. local name
  123. FMT=
  124. name=`createPackageName ${PRODUCT}-bin`
  125. if [ -x contents ] ; then
  126. FMT=tar
  127. else
  128. for f in deb rpm tgz ; do
  129. if [ -f ${name}.${f} ] ; then
  130. FMT=${f%.gz}
  131. break
  132. fi
  133. done
  134. fi
  135. if [ "$FMT" = "" ] ; then
  136. echo -e "\nYou may be be missing a necessary package file. "
  137. echo "Check that you have the correct $NAME package".
  138. exit 255
  139. fi
  140. }
  141. askUser() {
  142. local finished
  143. [ "$headless" != 1 ] && echo "Enter requested information or press <ENTER> to accept the default. "
  144. #
  145. # Confirm the configuration
  146. #
  147. finished=N
  148. while [ "$finished" = "N" ]
  149. do
  150. [ "$headless" != 1 ] && echo
  151. installbin=`yesno "Install binary package" "$installbin"`
  152. if [ "$installbin" = "Y" ] ; then
  153. runDaemon=`yesno "Start $PRODUCT automatically at system boot" $runDaemon`
  154. HTTP_PORT=`ask "Enter the HTTP port number" "$HTTP_PORT"`
  155. # SSL_PORT=`ask "Enter the SSL port number" "$SSL_PORT"`
  156. username=`ask "Enter the user account for $PRODUCT" "$username"`
  157. groupname=`ask "Enter the user group for $PRODUCT" "$groupname"`
  158. else
  159. runDaemon=N
  160. fi
  161. if [ "$headless" != 1 ] ; then
  162. echo -e "\nInstalling with this configuration:"
  163. echo -e " Install binary package: $installbin"
  164. if [ "$installbin" = "Y" ] ; then
  165. echo -e " Start automatically at system boot: $runDaemon"
  166. echo -e " HTTP port number: $HTTP_PORT"
  167. # echo -e " SSL port number: $SSL_PORT"
  168. echo -e " Username: $username"
  169. echo -e " Groupname: $groupname"
  170. fi
  171. echo
  172. fi
  173. finished=`yesno "Accept this configuration" "Y"`
  174. done
  175. [ "$headless" != 1 ] && echo
  176. if [ "$installbin" = "N" ] ; then
  177. echo -e "Nothing to install, exiting. "
  178. exit 0
  179. fi
  180. }
  181. createPackageName() {
  182. echo ${1}-${VERSION}-${DIST}-${OS}-${CPU}
  183. }
  184. #
  185. # Get a yes/no answer from the user. Usage: ans=`yesno "prompt" "default"`
  186. # Echos 1 for Y or 0 for N
  187. #
  188. yesno() {
  189. local ans
  190. if [ "$headless" = 1 ] ; then
  191. echo "Y"
  192. return
  193. fi
  194. echo -n "$1 [$2] : " 1>&2
  195. while [ 1 ]
  196. do
  197. read ans
  198. if [ "$ans" = "" ] ; then
  199. echo $2 ; break
  200. elif [ "$ans" = "Y" -o "$ans" = "y" ] ; then
  201. echo "Y" ; break
  202. elif [ "$ans" = "N" -o "$ans" = "n" ] ; then
  203. echo "N" ; break
  204. fi
  205. echo -e "\nMust enter a 'y' or 'n'\n " 1>&1
  206. done
  207. }
  208. #
  209. # Get input from the user. Usage: ans=`ask "prompt" "default"`
  210. # Returns the answer or default if <ENTER> is pressed
  211. #
  212. ask() {
  213. local ans
  214. default=$2
  215. if [ "$headless" = 1 ] ; then
  216. echo "$default"
  217. return
  218. fi
  219. echo -n "$1 [$default] : " 1>&2
  220. read ans
  221. if [ "$ans" = "" ] ; then
  222. echo $default
  223. fi
  224. echo $ans
  225. }
  226. removeOld() {
  227. if [ -x /usr/lib/goahead/bin/uninstall ] ; then
  228. goahead_HEADLESS=1 /usr/lib/goahead/bin/uninstall </dev/null 2>&1 >/dev/null
  229. fi
  230. }
  231. installFiles() {
  232. local dir pkg doins NAME upper target
  233. [ "$headless" != 1 ] && echo -e "\nExtracting files ...\n"
  234. for pkg in bin ; do
  235. doins=`eval echo \\$install${pkg}`
  236. if [ "$doins" = Y ] ; then
  237. upper=`echo $pkg | tr '[:lower:]' '[:upper:]'`
  238. suffix="-${pkg}"
  239. NAME=`createPackageName ${PRODUCT}${suffix}`.$FMT
  240. if [ "$runDaemon" != "Y" ] ; then
  241. export APPWEB_DONT_START=1
  242. fi
  243. if [ "$FMT" = "rpm" ] ; then
  244. [ "$headless" != 1 ] && echo -e "rpm -Uhv $NAME"
  245. rpm -Uhv $HOME/$NAME
  246. elif [ "$FMT" = "deb" ] ; then
  247. [ "$headless" != 1 ] && echo -e "dpkg -i $NAME"
  248. dpkg -i $HOME/$NAME >/dev/null
  249. elif [ "$FMT" = "tar" ] ; then
  250. target=/
  251. [ $OS = windows ] && target=`cygpath ${HOMEDRIVE}/`
  252. (cd contents ; tar cf - . | (cd $target && tar xBf -))
  253. fi
  254. fi
  255. done
  256. if [ -f /etc/redhat-release -a -x /usr/bin/chcon ] ; then
  257. if sestatus | grep enabled >/dev/nulll ; then
  258. for f in $ABIN/*.so ; do
  259. chcon /usr/bin/chcon -t texrel_shlib_t $f 2>&1 >/dev/null
  260. done
  261. fi
  262. fi
  263. [ "$headless" != 1 ] && echo -e "\nSetting file permissions ..."
  264. if [ $OS = windows ] ; then
  265. # Cygwin bug. Chmod fails to stick if not in current directory for paths with spaces
  266. home=`pwd` >/dev/null
  267. cd "$ETC_PREFIX" ; chmod -R g+r,o+r . ; find . -type d -exec chmod 755 "{}" \;
  268. cd "$APP_PREFIX" ; chmod -R g+r,o+r . ; find . -type d -exec chmod 755 "{}" \;
  269. cd "$WEB_PREFIX" ; chmod -R g+r,o+r . ; find . -type d -exec chmod 755 "{}" \;
  270. cd "$ABIN" ; chmod 755 *.dll *.exe
  271. cd "$ETC_PREFIX" ; chmod 777 . ; chown $username .
  272. cd "${home}" >/dev/null
  273. fi
  274. [ "$headless" != 1 ] && echo
  275. }
  276. patchConfiguration() {
  277. :
  278. }
  279. #
  280. # Not currently used
  281. #
  282. startBrowser() {
  283. if [ "$headless" = 1 ] ; then
  284. return
  285. fi
  286. #
  287. # Conservative delay to allow goahead to start and initialize
  288. #
  289. sleep 5
  290. [ "$headless" != 1 ] && echo -e "Starting browser to view the $NAME Home Page."
  291. if [ $OS = windows ] ; then
  292. cygstart --shownormal http://$SITE:$HTTP_PORT$PAGE
  293. elif [ $OS = macosx ] ; then
  294. open http://$SITE:$HTTP_PORT$PAGE >/dev/null 2>&1 &
  295. else
  296. for f in /usr/bin/htmlview /usr/bin/firefox /usr/bin/mozilla /usr/bin/konqueror
  297. do
  298. if [ -x ${f} ] ; then
  299. sudo -H -b ${f} http://$SITE:$HTTP_PORT$PAGE >/dev/null 2>&1 &
  300. break
  301. fi
  302. done
  303. fi
  304. }
  305. #
  306. # Main program for install script
  307. #
  308. setup $*
  309. askUser
  310. if [ "$installbin" = "Y" ] ; then
  311. [ "$headless" != 1 ] && echo "Disable existing service"
  312. appman stop disable uninstall >/dev/null 2>&1
  313. if [ $OS = windows ] ; then
  314. "$ABIN/goaheadMonitor" --stop >/dev/null 2>&1
  315. fi
  316. fi
  317. removeOld
  318. installFiles $FMT
  319. if [ "$installbin" = "Y" ] ; then
  320. "$ABIN/appman" stop disable uninstall >/dev/null 2>&1
  321. patchConfiguration
  322. "$ABIN/appman" install
  323. if [ "$runDaemon" = "Y" ] ; then
  324. "$ABIN/appman" enable
  325. "$ABIN/appman" start
  326. fi
  327. fi
  328. if [ $OS = windows ] ; then
  329. "$ABIN/goaheadMonitor" >/dev/null 2>&1 &
  330. fi
  331. if [ "$headless" != 1 ] ; then
  332. echo -e "$NAME installation successful."
  333. fi
  334. exit 0