Add create_networks
continuous-integration/drone/push Build is failing Details

This commit is contained in:
3wc 2020-09-27 19:57:41 +02:00
parent ed773e37f0
commit d4ea740256
1 changed files with 22 additions and 15 deletions

View File

@ -8,7 +8,14 @@ PLUGIN_PORT=${PLUGIN_PORT:-222}
PLUGIN_PURGE=${PLUGIN_PURGE:-"false"}
PLUGIN_USER=${PLUGIN_USER:-drone}
DOCKER_HOST="ssh://$PLUGIN_USER@$PLUGIN_HOST:$PLUGIN_PORT"
REMOTE_DOCKER_HOST="ssh://$PLUGIN_USER@$PLUGIN_HOST:$PLUGIN_PORT"
create_networks() {
for NETWORK in $PLUGIN_NETWORKS; do
docker -H "$REMOTE_DOCKER_HOST" \
network create --driver=overlay $NETWORK --scope swarm || true
done
}
generate_secrets() {
echo "--- start secrets ---"
@ -18,16 +25,14 @@ generate_secrets() {
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY} -O /usr/bin/yq &&\
chmod +x /usr/bin/yq
export DOCKER_HOST="ssh://$PLUGIN_USER@$PLUGIN_HOST:$PLUGIN_PORT"
for SECRET in $(yq r "$PLUGIN_COMPOSE" 'secrets.*.name'); do
SECRET=$(eval echo "$SECRET")
if docker secret ls | grep -q "$SECRET"; then
if docker -H "$REMOTE_DOCKER_HOST" secret ls | grep -q "$SECRET"; then
echo "Skipping existing secret $SECRET"
else
eval "echo \"generating $SECRET\""
PW=$(</dev/urandom tr -dc 'A-Za-z0-9' | head -c 40; echo)
eval "echo \"$PW\" | docker secret create \"$SECRET\" -";
eval "echo \"$PW\" | docker -H \"$REMOTE_DOCKER_HOST\" secret create \"$SECRET\" -";
fi
done
echo "--- end secrets ---"
@ -52,28 +57,26 @@ output_versions(){
run_stack_deploy() {
echo "--- start deploy ---"
docker -H "$DOCKER_HOST" stack deploy -c "$PLUGIN_COMPOSE" "$PLUGIN_STACK"
docker stack deploy -c "$PLUGIN_COMPOSE" "$PLUGIN_STACK"
echo "--- end deploy ---"
}
run_stack_wait() {
export DOCKER_HOST="ssh://$PLUGIN_USER@$PLUGIN_HOST:$PLUGIN_PORT"
docker run --rm vitalets/docker-stack-wait-deploy \
| sed 's/True/true/' \
| bash /dev/stdin "$PLUGIN_STACK"
docker -H "$REMOTE_DOCKER_HOST" run --rm vitalets/docker-stack-wait-deploy \
| sed 's/True/true/' \
| bash /dev/stdin "$PLUGIN_STACK"
}
run_purge() {
echo "--- start purge ---"
docker -H "$DOCKER_HOST" stack rm "$PLUGIN_STACK"
docker -H "$REMOTE_DOCKER_HOST" stack rm "$PLUGIN_STACK"
# See https://github.com/moby/moby/issues/30942#issuecomment-540699206
until [ -z "$(docker stack ps "$PLUGIN_STACK" -q)" ]; do sleep 1; done
docker -H "$DOCKER_HOST" system prune --all --volumes --force
until [ -z "$(docker -H "$REMOTE_DOCKER_HOST" stack ps "$PLUGIN_STACK" -q)" ]; do sleep 1; done
docker -H "$REMOTE_DOCKER_HOST" system prune --all --volumes --force
# try and remove all secrets; Docker will leave ones which are in use
docker -H "$DOCKER_HOST" secret ls --format '{{ .Name }}' | xargs -i sh -c "echo {}; docker -H \"$DOCKER_HOST\" secret rm {} || true"
docker -H "$REMOTE_DOCKER_HOST" secret ls --format '{{ .Name }}' | xargs -i sh -c "echo {}; docker -H \"$REMOTE_DOCKER_HOST\" secret rm {} || true"
echo "--- end purge ---"
}
@ -82,6 +85,10 @@ run_plugin() {
load_deploy_key
output_versions
if [ -n "$PLUGIN_NETWORKS" ]; then
create_networks
fi
if [ -n "$PLUGIN_GENERATE_SECRETS" ]; then
generate_secrets
fi