-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfabfile.py
80 lines (67 loc) · 2.45 KB
/
fabfile.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
"""
https://github.com/renskiy/fabricio/blob/master/examples/service/kubernetes
"""
import fabricio
from fabric import api as fab
from fabricio import tasks, kubernetes
from fabricio.misc import AvailableVagrantHosts
from six.moves import filter
hosts = AvailableVagrantHosts(guest_network_interface='eth1')
service = tasks.DockerTasks(
service=kubernetes.Configuration(
name='my-service',
options={
# `kubectl apply` options
'filename': 'configuration.yml',
},
),
hosts=hosts,
# rollback_command=True, # show `rollback` command in the list
# migrate_commands=True, # show `migrate` and `migrate-back` commands in the list
# backup_commands=True, # show `backup` and `restore` commands in the list
# pull_command=True, # show `pull` command in the list
# update_command=True, # show `update` command in the list
# revert_command=True, # show `revert` command in the list
# destroy_command=True, # show `destroy` command in the list
)
@fab.task(name='k8s-init')
@fab.serial
def k8s_init():
"""
create Kubernetes cluster
"""
def init():
if not init.join_command:
initialization = list(filter(None, fabricio.run(
'kubeadm init '
'--apiserver-advertise-address {0} '
'--pod-network-cidr 10.244.0.0/16'
''.format(fab.env.host),
sudo=True,
quiet=False,
).splitlines()))
init.join_command = initialization[-1].strip()
# master setup
fabricio.run('mkdir -p $HOME/.kube')
fabricio.run('cp /etc/kubernetes/admin.conf /home/vagrant/.kube/config', sudo=True)
fabricio.run('chown vagrant /home/vagrant/.kube/config', sudo=True)
# install Kubernetes network plugin
fabricio.run(
'kubectl apply --filename /vagrant/kube-rbac.yml '
'&& kubectl apply --filename /vagrant/kube-canal.yml --validate=false',
quiet=False,
)
else:
fabricio.run(init.join_command, quiet=False, sudo=True)
init.join_command = None
with fab.settings(hosts=hosts):
fab.execute(init)
@fab.task(name='k8s-reset')
def k8s_reset():
"""
reset Kubernetes cluster
"""
def reset():
fabricio.run('kubeadm reset --force', sudo=True, quiet=False)
with fab.settings(hosts=hosts):
fab.execute(reset)