summarylogtreecommitdiffstats
path: root/dokku.install
blob: d0173fbc7eca6ac2c40362f326c098390f214b8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash
set -eo pipefail
[[ $TRACE ]] && set -x

readonly DOKKU_ROOT="${DOKKU_ROOT:-/home/dokku}"
readonly DOKKU_LIB_ROOT="${DOKKU_LIB_PATH:-/var/lib/dokku}"
readonly DOKKU_LOGS_DIR="${DOKKU_LOGS_DIR:-/var/log/dokku}"

call-sshcommand() {
  if [[ -x /usr/bin/sshcommand ]]; then
    /usr/bin/sshcommand "$@"
  else
    echo "Unable to find sshcommand binary" 1>&2
    exit 1
  fi
}

setup-user() {
  echo "Setting up dokku user"
  call-sshcommand create dokku /usr/bin/dokku
  grep -i -E "^docker" /etc/group || groupadd docker
  usermod -aG docker dokku
  mkdir -p "${DOKKU_ROOT}/.ssh" "${DOKKU_ROOT}/.dokkurc"
  touch "${DOKKU_ROOT}/.ssh/authorized_keys"
  chown -R dokku:dokku "${DOKKU_ROOT}/.ssh" "${DOKKU_ROOT}/.dokkurc"
}

setup-storage() {
  echo "Setting up storage directories"
  mkdir -p "${DOKKU_LIB_ROOT}/data" "${DOKKU_LIB_ROOT}/data/storage"
  chown dokku:dokku "${DOKKU_LIB_ROOT}/data"
}

setup-plugins() {
  echo "Deleting invalid plugins"
  if [ -f "${DOKKU_LIB_ROOT}/core-plugins/available/" ]; then
    find "${DOKKU_LIB_ROOT}/core-plugins/available/" -type d -empty -delete
  fi
  if [ -f "${DOKKU_LIB_ROOT}/core-plugins/enabled/" ]; then
    find "${DOKKU_LIB_ROOT}/core-plugins/enabled/" -type d -empty -delete
  fi
  if [ -f "${DOKKU_LIB_ROOT}/plugins/available/" ]; then
    find "${DOKKU_LIB_ROOT}/plugins/available/" -type d -empty -delete
  fi
  if [ -f "${DOKKU_LIB_ROOT}/plugins/enabled/" ]; then
    find "${DOKKU_LIB_ROOT}/plugins/enabled/" -type d -empty -delete
  fi

  echo "Setting up plugin directories"
  # should be replaced by `plugn init`
  mkdir -p "${DOKKU_LIB_ROOT}/core-plugins/available" "${DOKKU_LIB_ROOT}/plugins/available"
  mkdir -p "${DOKKU_LIB_ROOT}/core-plugins/enabled" "${DOKKU_LIB_ROOT}/plugins/enabled"
  touch "${DOKKU_LIB_ROOT}/core-plugins/config.toml" "${DOKKU_LIB_ROOT}/plugins/config.toml"

  echo "Migrating old plugins"
  find "${DOKKU_LIB_ROOT}/plugins" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | while read -r plugin; do
    if [ "${plugin}" = "available" ] || [ "${plugin}" = "enabled" ]; then
      continue
    elif [ -f "${DOKKU_LIB_ROOT}/plugins/${plugin}/.core" ]; then
      rm -rf "${DOKKU_LIB_ROOT}/plugins/${plugin}"
    elif [ ! -d "${DOKKU_LIB_ROOT}/plugins/available/${plugin}" ]; then
      mv "${DOKKU_LIB_ROOT}/plugins/${plugin}" "${DOKKU_LIB_ROOT}/plugins/available"
    fi
  done

  echo "Enabling all core plugins"
  find "${DOKKU_LIB_ROOT}/core-plugins/available" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | while read -r plugin; do
    if [ ! -d "${DOKKU_LIB_ROOT}/plugins/available/${plugin}" ]; then
      ln -s "${DOKKU_LIB_ROOT}/core-plugins/available/${plugin}" "${DOKKU_LIB_ROOT}/plugins/available/${plugin}"
      PLUGIN_PATH="${DOKKU_LIB_ROOT}/core-plugins" plugn enable "${plugin}"
      PLUGIN_PATH="${DOKKU_LIB_ROOT}/plugins" plugn enable "${plugin}"
    fi
  done
  find -L "${DOKKU_LIB_ROOT}/core-plugins" -type l -delete
  find -L "${DOKKU_LIB_ROOT}/plugins" -type l -delete
  chown dokku:dokku -R "${DOKKU_LIB_ROOT}/plugins" "${DOKKU_LIB_ROOT}/core-plugins"

  echo "Install all core plugins"
  echo -e "#!/bin/sh\\n\\n~/.basher/bash -l \$1 \"\$2 \$3\"" > /tmp/login-basher
  chmod +x /tmp/login-basher
  BASH_BIN=/tmp/login-basher dokku plugin:install --core
  rm /tmp/login-basher

  echo "%dokku ALL=(ALL) NOPASSWD:/usr/bin/crontab -l -u dokku" > /etc/sudoers.d/dokku-crontab
  echo "%dokku ALL=(ALL) NOPASSWD:/usr/bin/crontab -r -u dokku" >> /etc/sudoers.d/dokku-crontab
  echo "%dokku ALL=(ALL) NOPASSWD:/usr/bin/crontab -u dokku *" >> /etc/sudoers.d/dokku-crontab
  chmod 440 /etc/sudoers.d/dokku-crontab

  rm -f /etc/update-motd.d/99-dokku
  sed -i 's/ syslog dokku$/ root dokku/g' /etc/logrotate.d/dokku
}

setup-sshcommand() {
  echo "Ensure proper sshcommand path"
  echo '/usr/bin/dokku' > "${DOKKU_ROOT}/.sshcommand"
  if [[ -f .ssh/authorized_keys ]]; then
    sed -i.bak 's#/usr/local/bin/dokku#/usr/bin/dokku#' "${DOKKU_ROOT}/.ssh/authorized_keys"
    rm "${DOKKU_ROOT}/.ssh/authorized_keys"
  fi
}

post_install() {
  setup-user
  setup-storage
  setup-plugins
  setup-sshcommand

  echo "
    If using virtualhost routing is desired (eg. my-app => my-app.dokku.me), set a global domain for your server:
      $ dokku domains:set-global dokku.me

    Setup a user's ssh key for deployment by passing in the public ssh key as shown:
      $ echo 'CONTENTS_OF_ID_RSA_PUB_FILE' | dokku ssh-keys:add admin

    To enable nginx autoconfiguration, add to your /etc/nginx/nginx.conf in http section:
      include /etc/nginx/conf.d/*.conf;
    And reload nginx:
      $ sudo systemctl reload nginx
  "
}

post_upgrade() {
  post_install "$1"
}

pre_remove() {
  echo "Destroying deployed applications"
  for app in $(DOKKU_QUIET_OUTPUT=1 dokku apps:list); do
    dokku --force apps:destroy "$app"
  done

  # HACK: Only disable core plugins, as we don't know what data users store in non-core plugin directories
  echo "Disabling all core plugins"
  find "${DOKKU_LIB_ROOT}/core-plugins/available" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | while read -r plugin; do
    if [[ ! -d "${DOKKU_LIB_ROOT}/plugins/available/$plugin" ]]; then
      rm "${DOKKU_LIB_ROOT}/plugins/available/$plugin"
      PLUGIN_PATH="${DOKKU_LIB_ROOT}/core-plugins" plugn disable "$plugin"
      PLUGIN_PATH="${DOKKU_LIB_ROOT}/plugins" plugn disable "$plugin"
    fi
  done

  dokku cleanup
}

post_remove() {
  rm -f /var/cache/dokku-completion
  rm -f /etc/systemd/system/dokku-redeploy.service
  rm -f /etc/systemd/system/dokku-retire.timer
  rm -f /etc/systemd/system/dokku-retire.service
  rm -f /etc/logrotate.d/dokku
  rm -f /etc/sudoers.d/dokku-crontab

  # Remove nginx configuration
  rm -f /etc/nginx/conf.d/dokku.conf

  # Remove data and logs
  rm -rf "${DOKKU_LIB_ROOT}"
  rm -rf "${DOKKU_LOGS_DIR}"

  # Remove dokku user and group
  userdel -r dokku || true
  groupdel dokku &>/dev/null

  echo "
    Please remove to your /etc/nginx/nginx.conf in http section:
      include /etc/nginx/conf.d/*.conf;
    And reload nginx:
      $ sudo systemctl reload nginx
  "
}