forked from cedricziel/dokku-hostkeys-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pre-build
executable file
·62 lines (50 loc) · 2.27 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
APP="$1"
if ([[ -f "$(dirname $0)/../dokku_common" ]]); then
source "$(dirname $0)/../dokku_common"
fi
[[ $(declare -f -F check_app_name) ]] && check_app_name "$APP" || IMAGE="dokku/$APP"
if [[ -n $DOKKU_API_VERSION ]]; then
source "$PLUGIN_ENABLED_PATH/common/functions"
IMAGE=$(get_app_image_name $APP)
fi
APP_SPECIFIC_KEY_FOLDER="$DOKKU_ROOT/.hostkeys/$APP/.ssh"
SHARED_KEY_FOLDER="$DOKKU_ROOT/.hostkeys/shared/.ssh"
echo "-----> Checking Hostkeys Plugin sanity ..."
bash $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/install
echo "-----> Installing Hostkeys in build environment ..."
KNOWN_HOSTS_COMBINED=""
if ([[ -f "$APP_SPECIFIC_KEY_FOLDER/known_hosts" ]]); then
KNOWN_HOSTS_COMBINED=$(cat "$APP_SPECIFIC_KEY_FOLDER/known_hosts")
echo " App specific keys available. Adding."
else
echo " No app keys available."
fi
if ([[ -f "$SHARED_KEY_FOLDER/known_hosts" ]]); then
if ([[ -z "$KNOWN_HOSTS_COMBINED" ]]); then
echo " Adding shared keys."
KNOWN_HOSTS_COMBINED="$KNOWN_HOSTS_COMBINED"$(cat "$SHARED_KEY_FOLDER/known_hosts")
else
echo " Appending shared keys."
KNOWN_HOSTS_COMBINED="$KNOWN_HOSTS_COMBINED"$'\n'$(cat "$SHARED_KEY_FOLDER/known_hosts")
fi
fi
if ([[ ! -z "$KNOWN_HOSTS_COMBINED" ]]); then
# 1. Create the .ssh folder
id=$(docker run $DOKKU_GLOBAL_RUN_ARGS -d $IMAGE /bin/bash -c "mkdir -p -m 700 /app/.ssh")
test $(docker wait $id) -eq 0
docker commit $id $IMAGE > /dev/null
# 2. Transfer the keyfile to the container
idWithKeys=$(echo -e "$KNOWN_HOSTS_COMBINED" | docker run $DOKKU_GLOBAL_RUN_ARGS -i -a stdin $IMAGE /bin/bash -c "cat >> /etc/ssh/ssh_known_hosts && chmod 644 /etc/ssh/ssh_known_hosts")
test $(docker wait $idWithKeys) -eq 0
docker commit $idWithKeys $IMAGE > /dev/null
idWithConfig=$(echo "UserKnownHostsFile /etc/ssh/ssh_known_hosts" | 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
echo "-----> Done Installing Hostkeys in build environment ..."
echo " Added the following keys:"
echo " $KNOWN_HOSTS_COMBINED"
else
echo " No hostkeys found. Continueing.."
fi