Repository navigation
Expand file tree
/
Copy pathstreisand
More file actions
executable file
·150 lines (126 loc) · 4.83 KB
/
Copy pathstreisand
File metadata and controls
executable file
·150 lines (126 loc) · 4.83 KB
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
#!/usr/bin/env bash
# Streisand provisioning script
# Set errexit option to exit immediately on any non-zero status return
set -e
echo -e "\n\033[38;5;255m\033[48;5;234m\033[1m S T R E I S A N D \033[0m\n"
# Update the PYTHONPATH to include the monkey patch directory for Streisand
# workarounds packaged as Python monkey patches
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
export PYTHONPATH="$SCRIPT_DIR/monkey:$PYTHONPATH"
# Include the check_ansible function from ansible_check.sh
source util/ansible_check.sh
# check_python checks whether the 'python' interpreter is Python 2 or Python 3.
# If it is Python 2 then the inventory file is updated to set the
# ansible_interpretter host var explicitly
function check_python() {
local PYTHON_VERSION
PYTHON_VERSION="$(python --version 2>/dev/null)"
if [[ -n $PYTHON_VERSION && ! $PYTHON_VERSION =~ ^Python\ 2\..* ]]; then
local INVENTORY_DIR="$SCRIPT_DIR/inventories/"
for INV_FILE in $INVENTORY_DIR/*; do
sed 's/=python$/=python2/' "$INV_FILE" > "$INV_FILE.new"
mv "$INV_FILE.new" "$INV_FILE"
git -C "$INVENTORY_DIR" update-index --assume-unchanged "$INV_FILE" 2>/dev/null || true
done
fi
}
# check_ssh_key checks that there is a `~/.ssh/id_rsa.pub` file present.
# Streisand requires this key be present.
function check_ssh_key() {
local STREISAND_SSH_KEY="$HOME/.ssh/id_rsa.pub"
if [ ! -f "$STREISAND_SSH_KEY" ]
then
echo "
Streisand requires a RSA SSH key be present at '$HOME/.ssh/id_rsa.pub'.
Please create this keypair by running 'ssh-keygen'"
exit 1
fi
}
# run_genesis invokes the genesis playbook file specified by the first argument
# to the function, or `streisand.yml` if none is provided. It uses the default
# `inventory` file from the inventories directory as the Ansible inventory.
function run_genesis() {
local GENESIS_PLAYBOOK=$1
if [ -z "${GENESIS_PLAYBOOK}" ]; then
GENESIS_PLAYBOOK=streisand.yml
fi
echo; echo; ansible-playbook -i inventories/inventory "playbooks/$GENESIS_PLAYBOOK"
}
# be_careful_friend asks you to pay attention because you're about to do
# something that might be impossible to undo!
function be_careful_friend() {
read -r -p "$1" confirm
case "$confirm" in
streisand) echo; echo "Confirmed. Continuing";;
*) echo; echo "Cancelling & exiting."; exit 1;;
esac
}
# local_provision handles provisioning the same machine as is running the
# Streisand script/Ansible. It performs an additional "ARE YOU SURE" step before
# invoking ansible-playbook. It uses the `inventory-local-provision` inventory
# file from the inventories directory as the Ansible inventory.
function local_provision() {
be_careful_friend "
LOCAL PROVISIONING WILL OVERWRITE CONFIGURATION ON **THIS** MACHINE.
THE MACHINE YOU ARE CURRENTLY EXECUTING THIS SHELL SCRIPT ON.
ARE YOU 100% SURE THAT YOU WISH TO CONTINUE?
Please enter the word 'streisand' to continue: "
check_python
echo; echo; ansible-playbook -i inventories/inventory-local-provision playbooks/localhost.yml
}
function existing_server() {
read -r -p "What is the IP of the existing server: " SERVER_IP
be_careful_friend "
THIS WILL OVERWRITE CONFIGURATION ON THE EXISTING SERVER.
STREISAND ASSUMES $SERVER_IP IS A BRAND NEW UBUNTU INSTANCE AND WILL
NOT PRESERVE EXISTING CONFIGURATION OR DATA.
ARE YOU 100% SURE THAT YOU WISH TO CONTINUE?
Please enter the word 'streisand' to continue: "
# If ANSIBLE_SSH_USER is empty, default to root
if [ -z "${ANSIBLE_SSH_USER}" ]; then
SSH_USER='root'
# Otherwise, use whatever ANSIBLE_SSH_USER is set to as the SSH_USER
else
SSH_USER=${ANSIBLE_SSH_USER}
fi
# Create an inventory file string on the fly
read -r -d '' TEMPL << EOF || true
[localhost]
localhost ansible_connection=local ansible_python_interpreter=python
[streisand-host]
$SERVER_IP ansible_user=$SSH_USER
EOF
# Test a SSH connection to the server using the key Streisand assumes will be
# used for server access
ssh -i ~/.ssh/id_rsa "$SSH_USER@$SERVER_IP" -o BatchMode=yes -t true
# Create the inventory file
echo "$TEMPL" > inventories/inventory-existing
check_python
# Invoke the Streisand playbook on the existing server inventory
echo; echo; ansible-playbook -i inventories/inventory-existing playbooks/existing-server.yml
}
# Make sure the system is ready for the Streisand playbooks
check_ansible
check_ssh_key
# Figure out which genesis role to invoke
read -r -p "Which provider are you using?
1. Amazon
2. Azure
3. DigitalOcean
4. Google
5. Linode
6. Rackspace
7. Localhost (Advanced)
8. Existing Server (Advanced)
: " reply
case "$reply" in
1) run_genesis amazon.yml;;
2) run_genesis azure.yml;;
3) run_genesis digitalocean.yml;;
4) run_genesis google.yml;;
5) run_genesis linode.yml;;
6) run_genesis rackspace.yml;;
7) local_provision;;
8) existing_server;;
*) echo; echo "Invalid provider selected."; exit 1;;
esac