-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.py
76 lines (55 loc) · 2.58 KB
/
init.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
import os
import subprocess
import sys
import boto3
import time
import json
from execution_modules import execute_cofiguration, get_instance_id, get_instance_details, get_pvt_key
def replace_all(file, dic):
with open(file) as f:
file_data = f.read()
for i, j in dic.items():
file_data = file_data.replace(i, j)
return file_data
def main():
start_time = time.time()
chefautomate_instanceId, chefserver_instanceId = get_instance_id()
boto_client = boto3.client('ec2')
boto_client.start_instances(InstanceIds=[chefautomate_instanceId,chefserver_instanceId,])
pvt_key = get_pvt_key()
clock_count = 0
while clock_count < 10:
server_status = boto_client.describe_instance_status(InstanceIds=[chefserver_instanceId,])
automate_status = boto_client.describe_instance_status(InstanceIds=[chefautomate_instanceId,])
if server_status['InstanceStatuses'] and automate_status['InstanceStatuses']:
print("Servers are up, waiting for 30 seconds before starting configuration")
time.sleep(30)
break
print(f"{clock_count} attempt in 10 seconds")
time.sleep(10)
clock_count = clock_count+1
server_pubIP, server_pubfqdn = get_instance_details(chefserver_instanceId)
automate_pubIP, automate_pubfqdn = get_instance_details(chefautomate_instanceId)
tag_replace_dict = {
"@@@@automate_fqdn@@@@": automate_pubfqdn,
"@@@@automate_ip@@@@": automate_pubIP,
"@@@@server_fqdn@@@@": server_pubfqdn,
"@@@@server_ip": automate_pubIP
}
chef_server_config = replace_all('configure_chef_server.sh', tag_replace_dict)
chef_automate_config = replace_all('configure_chef_automate.sh', tag_replace_dict)
chef_workstation_config = replace_all('configure_workstation_local.sh', tag_replace_dict)
execute_cofiguration(script=chef_server_config, host=server_pubIP, username='ec2-user', pvt_key=pvt_key)
execute_cofiguration(script=chef_automate_config, host=automate_pubIP, username='ec2-user', pvt_key=pvt_key)
print(server_pubfqdn)
print(chef_workstation_config)
# os.system(chef_workstation_config)
workstation_exec = subprocess.Popen(chef_workstation_config, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
res = workstation_exec.communicate()
print("retcode =", workstation_exec.returncode)
print("res =", res)
print("stderr =", res[1])
for line in res[0].decode(encoding='utf-8').split('\n'):
print(line)
print("--- %s seconds ---" % (time.time() - start_time))
main()