forked from cedricziel/dokku-deployment-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-build
executable file
·45 lines (37 loc) · 1.96 KB
/
pre-build
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
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_ENABLED_PATH/common/functions"
APP="$1";
IMAGE=$(get_app_image_name $APP)
APP_SPECIFIC_KEY_FOLDER="$DOKKU_ROOT/.deployment-keys/$APP/.ssh"
SHARED_KEY_FOLDER="$DOKKU_ROOT/.deployment-keys/shared/.ssh"
bash $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/install
[[ ! -f "$APP_SPECIFIC_KEY_FOLDER/id_rsa" ]] && [[ ! -f "$SHARED_KEY_FOLDER/id_rsa" ]] && return
if [[ -f "$APP_SPECIFIC_KEY_FOLDER/id_rsa" ]]; then
FINAL_SSH_FOLDER="$APP_SPECIFIC_KEY_FOLDER"
KEYTYPE="app specific"
else
if [[ -f "$SHARED_KEY_FOLDER/id_rsa" ]]; then
FINAL_SSH_FOLDER="$SHARED_KEY_FOLDER"
KEYTYPE="shared"
fi
fi
dokku_log_info1 "Adding $KEYTYPE deployment-keys to build environment ..."
# 1. Create the .ssh folder
id=$(docker run $DOKKU_GLOBAL_RUN_ARGS -d $IMAGE /bin/bash -c "mkdir -p /app/.ssh")
test $(docker wait $id) -eq 0
docker commit $id $IMAGE > /dev/null
# 2. Transfer the app specific private key to the container
# shellcheck disable=SC2002
idWithKeys=$(cat "$FINAL_SSH_FOLDER/id_rsa" | docker run $DOKKU_GLOBAL_RUN_ARGS -i -a stdin $IMAGE /bin/bash -c "cat >> /app/.ssh/id_rsa && chmod 600 /app/.ssh/id_rsa")
test $(docker wait $idWithKeys) -eq 0
docker commit $idWithKeys $IMAGE > /dev/null
# 3. Transfer the app specific public key to the container
# shellcheck disable=SC2002
idWithPublicKeys=$(cat "$FINAL_SSH_FOLDER/id_rsa.pub" | docker run $DOKKU_GLOBAL_RUN_ARGS -i -a stdin $IMAGE /bin/bash -c "cat >> /app/.ssh/id_rsa.pub && chmod 600 /app/.ssh/id_rsa && chmod 644 /app/.ssh/id_rsa.pub && chown -R 1001:1001 /app/.ssh")
test $(docker wait $idWithPublicKeys) -eq 0
docker commit $idWithPublicKeys $IMAGE > /dev/null
# 4. Add identity file option to global SSH config
idWithConfig=$(echo "IdentityFile /app/.ssh/id_rsa" | docker run $DOKKU_GLOBAL_RUN_ARGS -i -a stdin $IMAGE /bin/bash -c "cat >> /etc/ssh/ssh_config" )
test $(docker wait $idWithConfig) -eq 0
docker commit $idWithConfig $IMAGE > /dev/null