-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
161 lines (116 loc) · 3.76 KB
/
server.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from fabric.api import *
from fabric.contrib.files import upload_template, append
import time
import config
@task
def uname():
"""Execute uname"""
run("uname -a")
@task
def upgrade():
"""Upgrade a sever"""
sudo("DEBIAN_FRONTEND=noninteractive apt-get update -y")
sudo("DEBIAN_FRONTEND=noninteractive apt-get upgrade -y")
sudo("DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -y")
@task
def install_sudo():
"""Install the sudo programm. Need to be runned with root"""
run("apt-get update")
run("apt-get install -y sudo")
@task
def reboot():
"""Reboot a machine"""
x = 5
while x > 0:
print "Rebooting", env.host, "in", x, "seconds..."
time.sleep(1)
x -= 1
sudo("reboot")
@task
def shutdown():
"""Shutdown a machine"""
x = 5
while x > 0:
print "Shutdowning", env.host, "in", x, "seconds..."
time.sleep(1)
x -= 1
sudo("halt")
@task
def copy_key_manager():
"""Copy the script for keymanagement [$AG:NeedKM]"""
if not hasattr(env, 'keymanagerName') or env.keymanagerName == '':
print "No keymanager name !"
return
upload_template('files/updateKeys.sh', '/root/updateKeys.sh', {
'server': env.keymanagerName,
'users': env.keyManagerUsers,
'gestion_adresse': config.GESTION_ADDRESS,
}, use_sudo=True)
sudo("chmod +x /root/updateKeys.sh")
@task
def cron_key_manager():
"""Install the crontab for the keymanagement"""
sudo('touch /tmp/crondump')
with settings(warn_only=True):
sudo('crontab -l > /tmp/crondump')
append('/tmp/crondump', '42 * * * * /root/updateKeys.sh', use_sudo=True)
sudo('crontab /tmp/crondump')
@task
def setup_key_manager():
"""Setup the key manager [$AG:NeedKM]"""
run('mkdir -p ~/.ssh/')
sudo('apt-get install -y ca-certificates')
copy_key_manager()
cron_key_manager()
execute_key_manger()
@task
def execute_key_manger():
"""Execute the keyManager"""
sudo("/root/updateKeys.sh")
@task
def copy_config():
"""Copy config files"""
put(config.AZIMUT_CONFIG + '/.vim*', '~')
put(config.AZIMUT_CONFIG + '/.screenrc', '~')
put(config.AZIMUT_CONFIG + '/.zsh*', '~')
@task
def copy_user_config():
"""Copy the config for a user [$AG:NeedUser]"""
if not hasattr(env, 'fab_user') or env.fab_user == '':
return
put(config.AZIMUT_CONFIG + '/.vim*', '/home/' + env.fab_user + '/')
put(config.AZIMUT_CONFIG + '/.screenrc', '/home/' + env.fab_user + '/')
put(config.AZIMUT_CONFIG + '/.zsh*', '/home/' + env.fab_user + '/')
put(config.AZIMUT_CONFIG + '/.zshrc-user', '/home/' + env.fab_user + '/.zshrc')
@task
def install_base_progs():
"""Install base programms"""
sudo('apt-get install -y zsh screen vim')
@task
def switch_shell_to_zsh():
"""Change the shell to ZSH"""
run('chsh -s /bin/zsh')
@task
def install_rsync():
"""Install rsync"""
sudo("apt-get install rsync")
@task
def add_gestion_for_self_vms():
"""Add a host for gestion vm so they can access the server even if on the same server [$AG:NeedGestion]"""
if not hasattr(env, 'gestion_ip') or env.gestion_ip == '':
return
sudo('echo "' + env.gestion_ip + ' ' + env.gestion_name + '" >> /etc/hosts')
@task
def setup():
"""Setup a new server [$AG:NeedKM][$AG:NeedGestion]"""
execute(install_sudo)
execute(upgrade)
execute(install_base_progs)
execute(add_gestion_for_self_vms)
execute(copy_config)
execute(switch_shell_to_zsh)
execute(install_rsync)
if not hasattr(env, 'keymanagerName') or env.keymanagerName == '':
prompt("Key manager name ?", 'keymanagerName')
prompt("Key manager users ?", 'keyManagerUsers', 'root')
execute(setup_key_manager)