forked from StreisandEffect/streisand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sted
executable file
·87 lines (82 loc) · 5.69 KB
/
sted
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
#!/usr/bin/env bash
PLAYBOOK_PORT="${PLAYBOOK_PORT:-22}"
DEBUG_MODE="${DEBUG_MODE:-'no'}"
DOIMAGE_NAME=${DOIMAGE_NAME:-"hwdm/dostreisand:latest"}
if docker images | sed '1d' | awk '{print $1":"$2}' | grep -wq "${DOIMAGE_NAME}"; then
echo "${DOIMAGE_NAME} docker image is ready. We're going to run it."
else
echo "${DOIMAGE_NAME} doesn't exist. We're going to run: docker pull ${DOIMAGE_NAME}"
docker pull ${DOIMAGE_NAME} && echo "${DOIMAGE_NAME} is ready. We're going to run it."
fi
if [ $# -lt 1 ];then
echo "We're going to run ${DOIMAGE_NAME} as streisand cli:"
echo "Please specify the SSH keypair name either in ~/.ssh or in ./ssh! like: ./sted keypairname"
echo "If a ./streisand OR ./playbooks dir exists, it will be mounted for code/config customization"
echo "DEBUG_MODE is ${DEBUG_MODE}, export DEBUG_MODE='yes' before running this script if you wanna print host vars."
echo "PLAYBOOK_PORT is ${PLAYBOOK_PORT}, export PLAYBOOK_PORT=newport before running this script if you wanna change."
exit 0
else
if [ ! -d "$(pwd)/serv" ]; then
mkdir -p "$(pwd)/serv"
fi
exec > >(tee $(pwd)/serv/runsted-${1}.log )
exec 2> >(tee -a $(pwd)/serv/runsted-${1}.log >&2)
if [ -f "${HOME}/.ssh/${1}.pem" ] && [ -f "${HOME}/.ssh/${1}.pub" ]; then
ssh_prvkey="${HOME}/.ssh/${1}.pem"
ssh_pubkey="${HOME}/.ssh/${1}.pub"
echo "Use existing keypair ${1} in ${HOME}/.ssh"
elif [ -f "$(pwd)/serv/sshkey/id_rsa" ] && [ -f "$(pwd)/serv/sshkey/id_rsa.pub" ]; then
ssh_prvkey="$(pwd)/serv/sshkey/id_rsa"
ssh_pubkey="$(pwd)/serv/sshkey/id_rsa.pub"
echo "Use existing keypair in $(pwd)/serv/sshkey which ought to be autogenerated remote sshkey ${1}."
else
echo "No exsitng keypair is found. It will be auto generated."
fi
fi
echo "Running ${DOIMAGE_NAME} as streisand cli:<Ctrl+C/Ctrl+D to terminate>"
## Create a data container:
## The ${DOIMAGE_NAME} image declares the following two volume mounts:
## /root/.ssh/ streisand expects your provider's default ssh key, here saved as id_rsa.pub
## /streisand/generated-docs/ upon completion, streisand will publish connection information in this directory as html files.
sudo docker rm -f streisand-data 2>/dev/null
sudo docker rm -f streisand-key 2>/dev/null
sudo docker rm -f streisand-cli 2>/dev/null
## This is the SSH key registered on your provider (Digitalocean) that new VMs are bootstrapped with.
if [ -f "${ssh_pubkey}" ] && [ -f "${ssh_prvkey}" ]; then
echo "docker run --name streisand-data ${DOIMAGE_NAME}"
if [ -d "$(pwd)/streisand" ]; then
echo "Mounting $(pwd)/streisand and $(dirname ${ssh_pubkey}) to streisand-data container"
sudo docker run --name streisand-data -v $(pwd)/cust:/root/.streisand -v $(pwd)/serv:/serv -v $(pwd)/streisand:/streisand -v ${ssh_prvkey}:/root/.ssh/id_rsa -v ${ssh_pubkey}:/root/.ssh/id_rsa.pub ${DOIMAGE_NAME}
elif [ -d "$(pwd)/playbooks" ]; then
echo "Mounting $(pwd)/playbooks and $(dirname ${ssh_pubkey}) to streisand-data container"
sudo docker run --name streisand-data -v $(pwd)/cust:/root/.streisand -v $(pwd)/serv:/serv -v $(pwd)/playbooks:/streisand/playbooks -v ${ssh_prvkey}:/root/.ssh/id_rsa -v ${ssh_pubkey}:/root/.ssh/id_rsa.pub ${DOIMAGE_NAME}
else
sudo docker run --name streisand-data -v $(pwd)/cust:/root/.streisand -v $(pwd)/serv:/serv -v ${ssh_prvkey}:/root/.ssh/id_rsa -v ${ssh_pubkey}:/root/.ssh/id_rsa.pub ${DOIMAGE_NAME}
fi
echo "Backing up the used ssh keypairs to $(pwd)/serv ..."
sudo docker run --rm --volumes-from=streisand-data -v $(pwd):/output ${DOIMAGE_NAME} cp -rf /root/.ssh /output/serv/sshkey 2>/dev/null
## Execute streisand:
echo "docker run -it --rm --name streisand-cli -e DEBUG_MODE=${DEBUG_MODE} -e PLAYBOOK_PORT=${PLAYBOOK_PORT} --volumes-from=streisand-data ${DOIMAGE_NAME}"
sudo docker run -it --rm --name streisand-cli -e DEBUG_MODE=${DEBUG_MODE} -e PLAYBOOK_PORT=${PLAYBOOK_PORT} --volumes-from=streisand-data ${DOIMAGE_NAME}
else
echo "docker run --name streisand-data ${DOIMAGE_NAME}"
if [ -d "$(pwd)/streisand" ]; then
sudo docker run --name streisand-data -v $(pwd)/cust:/root/.streisand -v $(pwd)/serv:/serv -v $(pwd)/streisand:/streisand ${DOIMAGE_NAME}
elif [ -d "$(pwd)/playbooks" ]; then
sudo docker run --name streisand-data -v $(pwd)/cust:/root/.streisand -v $(pwd)/serv:/serv -v $(pwd)/playbooks:/streisand/playbooks ${DOIMAGE_NAME}
else
sudo docker run --name streisand-data -v $(pwd)/cust:/root/.streisand -v $(pwd)/serv:/serv ${DOIMAGE_NAME}
fi
## Generate a new default SSH key if they doesn't exist:
echo "docker run -it --rm --volumes-from=streisand-data ${DOIMAGE_NAME} ssh-keygen -t rsa -b 4096 -f /root/.ssh/id_rsa -N ''"
sudo docker run -it --rm --name streisand-key --volumes-from=streisand-data ${DOIMAGE_NAME} ssh-keygen -t rsa -b 4096 -f /root/.ssh/id_rsa -N ''
echo "Dumping the auto generated ssh keypairs to $(pwd)/serv ..."
sudo docker run --rm --volumes-from=streisand-data -v $(pwd):/output ${DOIMAGE_NAME} cp -rf /root/.ssh /output/serv/sshkey 2>/dev/null
## Execute streisand:
echo "docker run -it --rm --name streisand-cli -e DEBUG_MODE=${DEBUG_MODE} -e PLAYBOOK_PORT=${PLAYBOOK_PORT} --volumes-from=streisand-data ${DOIMAGE_NAME}"
sudo docker run -it --rm --name streisand-cli -e DEBUG_MODE=${DEBUG_MODE} -e PLAYBOOK_PORT=${PLAYBOOK_PORT} --volumes-from=streisand-data ${DOIMAGE_NAME}
fi
## Retrieve generated docs:
sudo docker run --rm --volumes-from=streisand-data -v $(pwd):/output ${DOIMAGE_NAME} cp -rf /streisand/generated-docs /output/docs 2>/dev/null
sudo docker rm -f streisand-data 2>/dev/null
## Generated docs can now be found at ./generated-docs/streisand.html.