-
Notifications
You must be signed in to change notification settings - Fork 8
/
run.py
135 lines (108 loc) · 4.18 KB
/
run.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
import sys
import os
import subprocess
import json
import string
import random
import boto3
import botocore.session
def generate_random_string(N):
res = ''.join(random.choices(string.ascii_lowercase +
string.digits, k=N))
return res
def generate():
try:
stream = os.popen(f'az storage queue create -n {queue_name} --account-name {storage_account_name}')
stream.close()
except Exception as e:
print(e)
pass
def create_aws_credentials_file():
print(f":: Creating credentials file for AWS ::")
session = botocore.session.get_session()
access_key_id = session.get_credentials().access_key
secret_access_key = session.get_credentials().secret_key
credentials = {"access_key_id": access_key_id, "secret_access_key": secret_access_key}
with open(f"serwo/{user_dir}/aws_credentials.json", "w+") as out:
out.write(json.dumps(credentials, indent=4))
def set_up():
stream = os.popen("az group exists --name xfaasQueues")
xd = bool(stream.read())
stream.close()
if xd == True:
generate()
else:
print("Storage Queue Not Found, Run python3 xfaas_env_setup.py")
exit()
stream = os.popen(f'az storage account show-connection-string --name {storage_account_name} --resource-group {resource}')
json_str = json.loads(stream.read())
stream.close()
out_path = 'serwo/python/src/utils/CollectLogDirectories'
template_path = 'serwo/python/src/utils/CollectLogDirectories/CollectLogsTemplate/func.py'
print("Queue connection string here",json_str)
output_logs_dir = out_path+'/CollectLogs'
if not os.path.exists(output_logs_dir):
os.mkdir(output_logs_dir)
connection_str = json_str['connectionString']
print(queue_name)
write_output_files(output_logs_dir, connection_str, template_path,queue_name)
return {
"queue": queue_name,
"connectionString": connection_str
}
def write_output_files(out_path, connection_str, template_path,queue):
temp_code = open(template_path, 'r').read()
find_string = 'CONNECTION_STRING_PLACEHOLDER'
out = temp_code.replace(find_string, connection_str)
find_string = 'QUEUE_NAME_PLACEHOLDER'
outt = out.replace(find_string, queue)
ou = open(out_path + '/func.py', 'w')
ou.write(outt)
ou.close()
#--partition, --fusion, --single-cloud
option = sys.argv[3]
user_dir = sys.argv[1]
user_json = sys.argv[2]
random_string = generate_random_string(N=7)
queue_name = f"xfaas-logging-queue-{random_string}"
location = "centralindia"
resource = "xfaasQueues"
storage_account_name = 'xfaasstorage'
queue_details = set_up()
# create credentials file for aws
create_aws_credentials_file()
if option == '--partition':
if len(sys.argv)>4 and sys.argv[4]=='--fusion':
print('OPERATION NOT YET SUPPORTED, STAY TUNED!')
exit()
os.chdir('serwo')
with open(f"{user_dir}/queue_details.json", "w+") as f:
f.write(json.dumps(queue_details, indent=4))
subprocess.call(['python3','serwo_create_multicloud_statemachine.py',user_dir,user_json])
# stream = os.popen(f"python3 serwo_create_multicloud_statemachine.py {user_dir} {user_json}")
# stream.close()
elif option == '--single-cloud':
csp = sys.argv[4]
os.chdir('serwo')
with open(f"{user_dir}/queue_details.json", "w+") as f:
f.write(json.dumps(queue_details, indent=4))
subprocess.call(['python3','serwo_single_cloud.py',user_dir,user_json,csp])
# save to file
# stream = os.popen(f"python3 serwo_single_cloud.py {user_dir} {user_json} {csp}")
# stream.close()
elif option == '--fusion':
csp = sys.argv[4]
os.chdir('serwo')
if csp == 'aws':
fuse_csp_call = 'AWS'
elif csp =='azure':
fuse_csp_call = 'Azure'
with open(f"{user_dir}/queue_details.json", "w+") as f:
f.write(json.dumps(queue_details, indent=4))
subprocess.call(['python3','serwo_function_fuse.py',user_dir,user_json,fuse_csp_call])
else:
print('INVALID INPUT')
credentials_filepath = f"{user_dir}/aws_credentials.json"
if os.path.exists(credentials_filepath):
print("::Deleting AWS Credentials file::")
os.system(f"rm -r {credentials_filepath}")