-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig_k8s_flocker.py
executable file
·142 lines (124 loc) · 4.88 KB
/
config_k8s_flocker.py
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
#!/usr/bin/env python
import sys, os
import csv
import subprocess
# Usage: run_tests.py cluster.yml
from utils import Configurator, verify_socket, log
from twisted.internet.task import react
from twisted.internet.defer import gatherResults, inlineCallbacks
from twisted.python.filepath import FilePath
def report_completion(result, public_ip, message="Completed"):
log(message, public_ip)
return result
class UsageError(Exception):
pass
@inlineCallbacks
def main(reactor, configFile):
c = Configurator(configFile=configFile)
user = ""
if c.config["os"] == "ubuntu":
user = "ubuntu"
elif c.config["os"] == "centos":
user = "centos"
elif c.config["os"] == "amazon":
user = "ec2-user"
# Gather IPs of all nodes
nodes = c.config["agent_nodes"]
node_public_ips = [n["public"] for n in nodes]
# This assumes that your control node is a kubernetes node
# In practice its likely your flocker control node will remain
# seperate, so lets remove it.
#node_public_ips.append(c.config["control_node"])
# Wait for all nodes to boot
yield gatherResults([verify_socket(ip, 22, timeout=600) for ip in node_public_ips])
log("Generating API certs")
# generate and upload plugin.crt and plugin.key for each node
for public_ip in node_public_ips:
# use the node IP to name the local files
# so they do not overwrite each other
c.run("flocker-ca create-api-certificate %s-api" % (public_ip,))
log("Generated api certs for", public_ip)
deferreds = []
log("Uploading api certs...")
for public_ip in node_public_ips:
# upload the .crt and .key
for ext in ("crt", "key"):
d = c.scp("%s-api.%s" % (public_ip, ext,),
public_ip, "/etc/flocker/api.%s" % (ext,), async=True)
d.addCallback(report_completion, public_ip=public_ip, message=" * Uploaded api cert for")
deferreds.append(d)
yield gatherResults(deferreds)
log("Uploaded api certs")
deferreds = []
# THIS WILL ONLY WORK WITH SYSTEMD BASES UBUNTU 15.04+
# Ubuntu 14.04 uses /etc/default/kubelet and service kubelet restart
if user == "ubuntu":
for public_ip in node_public_ips:
cmd = """echo
cat >> /etc/sysconfig/kubelet << EOF
FLOCKER_CONTROL_SERVICE_HOST=%s\n
FLOCKER_CONTROL_SERVICE_PORT=4523\n
FLOCKER_CONTROL_SERVICE_CA_FILE=/etc/flocker/cluster.crt\n
FLOCKER_CONTROL_SERVICE_CLIENT_KEY_FILE=/etc/flocker/api.key\n
FLOCKER_CONTROL_SERVICE_CLIENT_CERT_FILE=/etc/flocker/api.crt
EOF""" % c.config['control_node']
d = c.runSSHAsync(public_ip, cmd)
d.addCallback(report_completion, public_ip=public_ip, message="Enabled flocker ENVs for")
deferreds.append(d)
else:
for public_ip in node_public_ips:
cmd = """echo
cat > /etc/flocker/env << EOF
FLOCKER_CONTROL_SERVICE_HOST=%s\n
FLOCKER_CONTROL_SERVICE_PORT=4523\n
FLOCKER_CONTROL_SERVICE_CA_FILE=/etc/flocker/cluster.crt\n
FLOCKER_CONTROL_SERVICE_CLIENT_KEY_FILE=/etc/flocker/api.key\n
FLOCKER_CONTROL_SERVICE_CLIENT_CERT_FILE=/etc/flocker/api.crt
EOF""" % c.config['control_node']
d = c.runSSHAsync(public_ip, cmd)
d.addCallback(report_completion, public_ip=public_ip, message="Enabled flocker ENVs for")
deferreds.append(d)
yield gatherResults(deferreds)
log("Uploaded Flocker ENV file.")
deferreds = []
if user == "ubuntu":
# Do nothing, we dont place an ENV.
pass
else:
for public_ip in node_public_ips:
cmd = """echo
sed -i -e 's,/usr/bin/kubelet,/root/kubelet,g' /etc/systemd/system/kubelet.service;
sed -i '/\[Service\]/aEnvironmentFile=/etc/flocker/env' /etc/systemd/system/kubelet.service
"""
d = c.runSSHAsync(public_ip, cmd)
d.addCallback(report_completion, public_ip=public_ip, message="Configured flocker ENVs for")
deferreds.append(d)
yield gatherResults(deferreds)
log("Configured Flocker ENV file.")
deferreds = []
if user == "ubuntu":
for public_ip in node_public_ips:
cmd = """echo
systemctl restart kubelet;
"""
d = c.runSSHAsync(public_ip, cmd)
d.addCallback(report_completion, public_ip=public_ip, message="Restarted Kubelet for")
deferreds.append(d)
else:
for public_ip in node_public_ips:
cmd = """echo
wget https://storage.googleapis.com/kubernetes-release/release/v1.1.1/bin/linux/amd64/kubelet;
chmod +x kubelet;
systemctl daemon-reload;
systemctl restart kubelet;
"""
d = c.runSSHAsync(public_ip, cmd)
d.addCallback(report_completion, public_ip=public_ip, message="Restarted Kubelet for")
deferreds.append(d)
yield gatherResults(deferreds)
log("Restarted Kubelet")
log("Completed")
def _main():
react(main, sys.argv[1:])
if __name__ == "__main__":
_main()