-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm_preparer.py
executable file
·79 lines (61 loc) · 2.61 KB
/
vm_preparer.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 28 12:41:21 2014
@author: Konstantin
"""
import cuisine, configparser, json
from fabric.api import env, execute, task, run, sudo, put, parallel
from fabric.contrib.files import sed
@parallel(pool_size=5)
@task
def prepare_machine():
sudo('rm -rf /root/.ssh/authorized_keys')
sudo('mkdir -p /root/.ssh')
sudo('chmod 777 /root')
sudo('chmod 777 /root/.ssh')
sudo('touch /root/.ssh/authorized_keys')
put(local_path='/root/.ssh/id_rsa.pub',remote_path='/root/.ssh/id_rsa.pub')
sudo('chmod 777 /root/.ssh/authorized_keys')
sudo('chmod 777 /root/.ssh/id_rsa.pub')
sudo('cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys')
sudo('chmod 644 /root/.ssh/authorized_keys')
sudo('chmod 644 /root/.ssh/id_rsa.pub')
sudo('chmod 700 /root/.ssh')
sudo('chmod 700 /root')
sudo('service ssh restart')
# cuisine.package_update()
def prepare_machines(vms_to_update, nagios_server_ip, nagios_server_fixed_ip):
config = configparser.ConfigParser()
config.read('config.ini')
nagios_server = dict(config['NAGIOS_SERVER'])
nagios_server_name = str(nagios_server['nagios_server'])
ssh_credentials = dict(config['SSH_CREDENTIALS'])
ssh_user = str(ssh_credentials['ssh.username'])
ssh_password = str(ssh_credentials['ssh.password'])
ssh_key_filename = str(ssh_credentials['ssh.key_filename'])
env.hosts = [ip for (host, ip) in vms_to_update.items() if host != nagios_server_name]
env.user = ssh_user
env.password = ssh_password
env.key_filename = ssh_key_filename
env.warn_only = True
env.connection_attempts = 5
execute(prepare_machine)
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read('config.ini')
HOST_LIST = json.load(open('server_list', 'r'))
nagios_server = dict(config['NAGIOS_SERVER'])
nagios_server_name = str(nagios_server['nagios_server'])
nagios_server_ip = HOST_LIST[nagios_server_name]
ssh_credentials = dict(config['SSH_CREDENTIALS'])
ssh_user = str(ssh_credentials['ssh.username'])
ssh_password = str(ssh_credentials['ssh.password'])
ssh_key_filename = str(ssh_credentials['ssh.key_filename'])
env.hosts = [ip for (host, ip) in HOST_LIST.items() if host != nagios_server_name]
env.user = ssh_user
env.password = ssh_password
env.key_filename = ssh_key_filename
nagios_server_fixed_ip = HOST_LIST[nagios_server_name]
env.warn_only = True
env.connection_attempts = 5
execute(prepare_machine)