install.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/bin/sh
  2. # -*- coding: utf-8 -*-
  3. basedir="$(realpath "$0" | xargs dirname)"
  4. info()
  5. {
  6. echo "--- $*"
  7. }
  8. error()
  9. {
  10. echo "=== ERROR: $*" >&2
  11. }
  12. warning()
  13. {
  14. echo "=== WARNING: $*" >&2
  15. }
  16. die()
  17. {
  18. error "$*"
  19. exit 1
  20. }
  21. do_install()
  22. {
  23. info "install $*"
  24. install "$@" || die "Failed install $*"
  25. }
  26. do_systemctl()
  27. {
  28. info "systemctl $*"
  29. systemctl "$@" || die "Failed to systemctl $*"
  30. }
  31. try_systemctl()
  32. {
  33. info "systemctl $*"
  34. systemctl "$@" 2>/dev/null
  35. }
  36. entry_checks()
  37. {
  38. [ -d "$target" ] ||\
  39. die "CMS is not built! Run ./build.sh"
  40. [ "$(id -u)" = "0" ] ||\
  41. die "Must be root to install CMS."
  42. }
  43. stop_services()
  44. {
  45. try_systemctl stop apache2
  46. try_systemctl stop cms-backd.socket
  47. try_systemctl stop cms-backd.service
  48. try_systemctl stop cms-postd.socket
  49. try_systemctl stop cms-postd.service
  50. try_systemctl stop cms-fsd.socket
  51. try_systemctl stop cms-fsd.service
  52. try_systemctl disable cms-backd.socket
  53. try_systemctl disable cms-backd.service
  54. try_systemctl disable cms-postd.socket
  55. try_systemctl disable cms-postd.service
  56. try_systemctl disable cms-fsd.socket
  57. try_systemctl disable cms-fsd.service
  58. }
  59. start_services()
  60. {
  61. do_systemctl start cms-fsd.socket
  62. do_systemctl start cms-postd.socket
  63. do_systemctl start cms-backd.socket
  64. do_systemctl start apache2
  65. }
  66. install_dirs()
  67. {
  68. rm -rf /opt/cms/bin
  69. rm -rf /opt/cms/lib
  70. rm -rf /opt/cms/libexec
  71. rm -rf /opt/cms/share
  72. do_install \
  73. -o root -g root -m 0755 \
  74. -d /opt/cms/bin
  75. do_install \
  76. -o root -g root -m 0755 \
  77. -d /opt/cms/etc/cms
  78. do_install \
  79. -o root -g root -m 0755 \
  80. -d /opt/cms/libexec/cms-cgi
  81. }
  82. install_fsd()
  83. {
  84. do_install \
  85. -o root -g root -m 0755 \
  86. "$target/cms-fsd" \
  87. /opt/cms/bin/
  88. do_install \
  89. -o root -g root -m 0644 \
  90. "$basedir/cms-fsd/cms-fsd.service" \
  91. /etc/systemd/system/
  92. do_install \
  93. -o root -g root -m 0644 \
  94. "$basedir/cms-fsd/cms-fsd.socket" \
  95. /etc/systemd/system/
  96. do_systemctl enable cms-fsd.service
  97. do_systemctl enable cms-fsd.socket
  98. }
  99. install_postd()
  100. {
  101. do_install \
  102. -o root -g root -m 0755 \
  103. "$target/cms-postd" \
  104. /opt/cms/bin/
  105. do_install \
  106. -o root -g root -m 0644 \
  107. "$basedir/cms-postd/cms-postd.service" \
  108. /etc/systemd/system/
  109. do_install \
  110. -o root -g root -m 0644 \
  111. "$basedir/cms-postd/cms-postd.socket" \
  112. /etc/systemd/system/
  113. do_systemctl enable cms-postd.service
  114. do_systemctl enable cms-postd.socket
  115. }
  116. install_cgi()
  117. {
  118. do_install \
  119. -o root -g root -m 0755 --no-target-directory \
  120. "$target/cms-cgi" \
  121. /opt/cms/libexec/cms-cgi/cms.cgi
  122. }
  123. install_backd()
  124. {
  125. do_install \
  126. -o root -g root -m 0755 \
  127. "$target/cms-backd" \
  128. /opt/cms/bin/
  129. do_install \
  130. -o root -g root -m 0644 \
  131. "$basedir/cms-backd/cms-backd.service" \
  132. /etc/systemd/system/
  133. do_install \
  134. -o root -g root -m 0644 \
  135. "$basedir/cms-backd/cms-backd.socket" \
  136. /etc/systemd/system/
  137. do_systemctl enable cms-backd.service
  138. do_systemctl enable cms-backd.socket
  139. }
  140. install_conf()
  141. {
  142. if ! [ -f /opt/cms/etc/cms/backd.conf ]; then
  143. do_install \
  144. -o root -g cms-backd -m 0640 \
  145. "$basedir/example/backd.conf" \
  146. /opt/cms/etc/cms/
  147. fi
  148. }
  149. release="release"
  150. while [ $# -ge 1 ]; do
  151. case "$1" in
  152. --debug|-d)
  153. release="debug"
  154. ;;
  155. --release|-r)
  156. release="release"
  157. ;;
  158. *)
  159. die "Invalid option: $1"
  160. ;;
  161. esac
  162. shift
  163. done
  164. target="$basedir/target/$release"
  165. entry_checks
  166. stop_services
  167. install_dirs
  168. install_fsd
  169. install_postd
  170. install_cgi
  171. install_backd
  172. install_conf
  173. start_services
  174. # vim: ts=4 sw=4 expandtab