-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset_script.py
100 lines (82 loc) · 4.44 KB
/
reset_script.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
from fabric import Connection
from invoke import Responder
import time
import sys
old_passwd = "raspberry"
new_passwd = str(sys.argv[1])
worker_pswd = "raspberry"
master = Connection(host="pi@"+str(sys.argv[2]), connect_kwargs={"password": old_passwd})
sudopass_master = Responder(pattern=r'\[sudo\] password:', response=new_passwd)
worker1 = Connection(host="pi@"+str(sys.argv[3]), connect_kwargs={"password": worker_pswd})
worker2 = Connection(host="pi@"+str(sys.argv[4]), connect_kwargs={"password": worker_pswd})
worker3 = Connection(host="pi@"+str(sys.argv[5]), connect_kwargs={"password": worker_pswd})
print(master, worker1, worker2, worker3)
sudopass_worker = Responder(pattern=r'\[sudo\] password:', response=worker_pswd)
# Changing password of master
if old_passwd == "raspberry":
r = master.run("echo -e \"%s\\n%s\\n%s\" | /usr/bin/passwd" % (old_passwd, new_passwd, new_passwd))
print("==============")
print("Changed password respnse:", r)
print("==============")
# transfering setup files
for node in (master, worker1, worker2, worker3):
print(node)
r1 = node.put('hostname_and_ip.sh', remote='/home/pi/')
print("HHH")
r2 = node.put('install.sh', remote='/home/pi/')
print("transfered files:", r1, r2)
print("==============")
print("beginning setupof pis")
master.run("sudo sh hostname_and_ip.sh k8s-master 192.168.1.100 192.168.1.1", pty=True, watchers=[sudopass_master])
worker1.run("sudo sh hostname_and_ip.sh k8s-worker01 192.168.1.99 192.168.1.1", pty=True, watchers=[sudopass_worker])
worker2.run("sudo sh hostname_and_ip.sh k8s-worker02 192.168.1.98 192.168.1.1", pty=True, watchers=[sudopass_worker])
worker3.run("sudo sh hostname_and_ip.sh k8s-worker03 192.168.1.97 192.168.1.1", pty=True, watchers=[sudopass_worker])
master.run("sh install.sh", pty=True, watchers=[sudopass_master])
worker1.run("sh install.sh", pty=True, watchers=[sudopass_worker])
worker2.run("sh install.sh", pty=True, watchers=[sudopass_worker])
worker3.run("sh install.sh", pty=True, watchers=[sudopass_worker])
master.run("cat /boot/cmdline.txt", pty=True, watchers=[sudopass_master])
master.run("sudo sed -i.bck '$s/$/ cgroup_memory=1 cgroup_memory=memory/' /boot/cmdline.txt", pty=True, watchers=[sudopass_master])
master.run("sudo sh -c \"echo -n ' cgroup_memory=1 cgroup_memory=memory' >> /boot/cmdline.txt\"", pty=True, watchers=[sudopass_master])
master.run("cat /boot/cmdline.txt", pty=True, watchers=[sudopass_master])
print("==============")
print("rebooting")
master.run("sudo /sbin/reboot -f > /dev/null 2>&1 &", pty=True, watchers=[sudopass_master])
worker1.run("sudo /sbin/reboot -f > /dev/null 2>&1 &", pty=True, watchers=[sudopass_worker])
worker2.run("sudo /sbin/reboot -f > /dev/null 2>&1 &", pty=True, watchers=[sudopass_worker])
worker3.run("sudo /sbin/reboot -f > /dev/null 2>&1 &", pty=True, watchers=[sudopass_worker])
print("==============")
time.sleep(36)
print("init cluster")
res = None
while res is None:
try:
# connect
master = Connection(host="[email protected]", connect_kwargs={"password": new_passwd})
worker1 = Connection(host="[email protected]", connect_kwargs={"password": worker_pswd})
worker2 = Connection(host="[email protected]", connect_kwargs={"password": worker_pswd})
worker3 = Connection(host="[email protected]", connect_kwargs={"password": worker_pswd})
result = master.run("sudo kubeadm init", pty=True, watchers=[sudopass_master])
res = True
except:
print("restart pi's mauanlly")
time.sleep(30)
pass
join_string = input("put kubeadm join string here: ")
print(join_string)
print("==============")
print("master cluster seetup")
master.run("mkdir -p $HOME/.kube", pty=True, watchers=[sudopass_master])
master.run("sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config", pty=True, watchers=[sudopass_master])
master.run("sudo chown $(id -u):$(id -g) $HOME/.kube/config", pty=True, watchers=[sudopass_master])
print("==============")
print("Joining worker nodes")
worker1.run("sudo "+join_string, pty=True, watchers=[sudopass_worker])
worker2.run("sudo "+join_string, pty=True, watchers=[sudopass_worker])
worker3.run("sudo "+join_string, pty=True, watchers=[sudopass_worker])
print("==============")
print("set up waeve netowkr")
master.run("kubectl apply -f \"https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')\"", pty=True, watchers=[sudopass_master])
print("==============")
master.run("kubectl get nodes")
print("FINISHED")