-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmake.py
executable file
·165 lines (138 loc) · 4.92 KB
/
make.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
162
163
164
165
"""
Copyright (c) 2022 Cisco Systems Inc or its affiliates.
All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--------------------------------------------------------------------------------
Name: make.py
Purpose: To build zip files from python files for lambda functions.
In "target" directory, zip files & template files will be copied.
"""
from __future__ import print_function
import platform
import os
import subprocess
import sys
import shutil
configure_asav_cluster_zip = 'configure_asav_cluster.zip'
lifecycle_asav_cluster_zip = 'lifecycle_asav_cluster.zip'
lambda_layer_zip = 'cluster_layer.zip'
full_dir_path = os.path.dirname(os.path.realpath(__file__)) + '/'
target_path = full_dir_path + "target/"
def print_function_name(function):
def echo_func(*func_args, **func_kwargs):
print('Running function: {}'.format(function.__name__))
return function(*func_args, **func_kwargs)
return echo_func
def main():
try:
print("Argument passed to make.py: {}".format(sys.argv[1]))
except IndexError as e:
print(e)
print("Please use 'clean' or 'build' as argument to make.py")
print("example: 'python make.py clean'")
try:
if sys.argv[1] == 'clean':
# Cleans the target directory
clean()
elif sys.argv[1] == 'build':
# Cleans the target directory
clean()
# Checks if all requirements for build
setup()
# Builds in target directory
build()
else:
print("No valid argument passed to make! "
"Please use clean/build argument.")
except Exception as e:
print(e)
return
@print_function_name
def build():
# Zips python files for lambda function
zip_()
# Copies the files to target directory
copy()
return
@print_function_name
def setup():
print("setup creates target and its sub-directories")
# folder_path = ['./target', './target/templates', './target/lambda_functions', './target/asa_config_files']
folder_path = [target_path]
for path in folder_path:
try:
isdir = os.path.isdir(path)
if isdir:
pass
else:
os.mkdir(path)
except Exception as e:
print(e)
return
@print_function_name
def clean():
print("clean deletes target directory and contents if exists, further creates empty target directory")
dir_path = target_path
try:
shutil.rmtree(dir_path)
except Exception as e:
pass
try:
isdir = os.path.isdir(dir_path)
if isdir:
pass
else:
os.mkdir(dir_path)
except Exception as e:
print(e)
return
@print_function_name
def zip_():
print("zip_ creates lambda zip files with only required python files")
list_of_files_configure_asav_cluster = ['aws.py', 'configure_asav_cluster.py', 'constant.py',
'asav.py', 'utility.py']
cmd = 'zip -jr ' + target_path + configure_asav_cluster_zip + ' '
for file in list_of_files_configure_asav_cluster:
file = full_dir_path + 'lambda-python-files/' + file
cmd = cmd + file + ' '
execute_cmd(cmd)
list_of_files_lifecycle_asav_cluster = ['aws.py', 'lifecycle_asav_cluster.py', 'constant.py', 'utility.py']
cmd = 'zip -jr ' + target_path + lifecycle_asav_cluster_zip + ' '
for file in list_of_files_lifecycle_asav_cluster:
file = full_dir_path + 'lambda-python-files/' + file
cmd = cmd + file + ' '
execute_cmd(cmd)
return
@print_function_name
def copy():
print("copies contents to target directory")
# cmd = "cp " + full_dir_path + "README.md" + " " + target_path
# execute_cmd(cmd)
cmd = "cp " + full_dir_path + 'lambda-python-files/' + lambda_layer_zip + " " + target_path
execute_cmd(cmd)
list_template_files = [
'deploy_asav_clustering.yaml',
'infrastructure.yaml'
]
for file in list_template_files:
cmd = "cp " + full_dir_path + 'templates/' + file + " " + target_path
execute_cmd(cmd)
return
@print_function_name
def execute_cmd(cmd):
# print(cmd)
subprocess.call(cmd, shell=True)
if __name__ == '__main__':
if platform.system() == 'Darwin' or platform.system() == 'Linux':
main()
else:
print("Un-supported platform: %s" % platform.system())
print("Supported platforms: Darwin, Linux")