install-server.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. do_chown()
  32. {
  33. info "chown $*"
  34. chown "$@" || die "Failed to chown $*"
  35. }
  36. do_chmod()
  37. {
  38. info "chmod $*"
  39. chmod "$@" || die "Failed to chmod $*"
  40. }
  41. try_systemctl()
  42. {
  43. info "systemctl $*"
  44. systemctl "$@" 2>/dev/null
  45. }
  46. do_chown()
  47. {
  48. info "chown $*"
  49. chown "$@" || die "Failed to chown $*"
  50. }
  51. do_chmod()
  52. {
  53. info "chmod $*"
  54. chmod "$@" || die "Failed to chmod $*"
  55. }
  56. entry_checks()
  57. {
  58. [ -d "$target" ] || die "letmein is not built! Run ./build.sh"
  59. [ "$(id -u)" = "0" ] || die "Must be root to install letmein."
  60. if ! grep -qe '^letmeind:' /etc/passwd; then
  61. die "The system user 'letmeind' does not exist in /etc/passwd. Please run ./create-user.sh"
  62. fi
  63. if ! grep -qe '^letmeind:' /etc/group; then
  64. die "The system group 'letmeind' does not exist in /etc/group. Please run ./create-user.sh"
  65. fi
  66. }
  67. stop_services()
  68. {
  69. try_systemctl stop letmeind.socket
  70. try_systemctl stop letmeind.service
  71. try_systemctl stop letmeinfwd.socket
  72. try_systemctl stop letmeinfwd.service
  73. try_systemctl disable letmeind.service
  74. try_systemctl disable letmeind.socket
  75. try_systemctl disable letmeinfwd.service
  76. try_systemctl disable letmeinfwd.socket
  77. }
  78. start_services()
  79. {
  80. do_systemctl start letmeinfwd.socket
  81. do_systemctl start letmeinfwd.service
  82. do_systemctl start letmeind.socket
  83. do_systemctl start letmeind.service
  84. }
  85. install_dirs()
  86. {
  87. do_install \
  88. -o root -g root -m 0755 \
  89. -d /opt/letmein/bin
  90. do_install \
  91. -o root -g root -m 0755 \
  92. -d /opt/letmein/etc
  93. }
  94. install_conf()
  95. {
  96. if [ -e /opt/letmein/etc/letmeind.conf ]; then
  97. do_chown root:letmeind /opt/letmein/etc/letmeind.conf
  98. do_chmod 0640 /opt/letmein/etc/letmeind.conf
  99. else
  100. do_install \
  101. -o root -g letmeind -m 0640 \
  102. "$basedir/letmeind/letmeind.conf" \
  103. /opt/letmein/etc/letmeind.conf
  104. fi
  105. }
  106. install_letmeinfwd()
  107. {
  108. do_install \
  109. -o root -g root -m 0755 \
  110. "$target/letmeinfwd" \
  111. /opt/letmein/bin/
  112. do_install \
  113. -o root -g root -m 0644 \
  114. "$basedir/letmeinfwd/letmeinfwd.service" \
  115. /etc/systemd/system/
  116. do_install \
  117. -o root -g root -m 0644 \
  118. "$basedir/letmeinfwd/letmeinfwd.socket" \
  119. /etc/systemd/system/
  120. do_systemctl enable letmeinfwd.socket
  121. do_systemctl enable letmeinfwd.service
  122. }
  123. install_letmeind()
  124. {
  125. do_install \
  126. -o root -g root -m 0755 \
  127. "$target/letmeind" \
  128. /opt/letmein/bin/
  129. do_install \
  130. -o root -g root -m 0644 \
  131. "$basedir/letmeind/letmeind.service" \
  132. /etc/systemd/system/
  133. do_install \
  134. -o root -g root -m 0644 \
  135. "$basedir/letmeind/letmeind.socket" \
  136. /etc/systemd/system/
  137. do_systemctl enable letmeind.socket
  138. do_systemctl enable letmeind.service
  139. }
  140. release="release"
  141. while [ $# -ge 1 ]; do
  142. case "$1" in
  143. --debug|-d)
  144. release="debug"
  145. ;;
  146. --release|-r)
  147. release="release"
  148. ;;
  149. *)
  150. die "Invalid option: $1"
  151. ;;
  152. esac
  153. shift
  154. done
  155. target="$basedir/target/$release"
  156. entry_checks
  157. stop_services
  158. install_dirs
  159. install_conf
  160. install_letmeinfwd
  161. install_letmeind
  162. start_services
  163. # vim: ts=4 sw=4 expandtab