forked from QualiSystems/EyScriptsDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_deploy.py
More file actions
197 lines (183 loc) · 7.18 KB
/
Copy pathlinux_deploy.py
File metadata and controls
197 lines (183 loc) · 7.18 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
import sys
import time
import logging
import platform
import socket
import requests
timestr = time.strftime("%Y%m%d-%H%M%S")
newpath = r'/tmp/csd'
if not os.path.exists(newpath):
os.makedirs(newpath)
os.system('cd /tmp/csd')
logfilename = newpath + '/' + socket.gethostname() + '_linux_deploy_' + timestr + '.txt'
logging.basicConfig(filename=logfilename,level=logging.DEBUG, format='%(asctime)s %(message)s')
# Log env vars
logging.info('linux_deploy.py =====V0.1====== ')
for key in os.environ.keys():
logging.info("%30s %s " % (key,os.environ[key]))
qsscript_count = 0
qsuname = ''
qspword = ''
qsexecute_command = ''
reservation_id = ''
resource_list = ''
ftpsrvr = ''
ftpuser = ''
ftppass = ''
ftphostkey = ''
# Load variables from env vars
try:
qsscript_count = os.environ["qsscript_count"]
except:
logging.warn("no env var found for qsscript_count")
try:
qsuname = os.environ["qsuname"]
except:
logging.warn("no env var found for qsuname")
try:
qspword = os.environ["qspword"]
except:
logging.warn("no env var found for qspword")
try:
qsexecute_command = os.environ["qsexecute_command"]
except:
logging.warn("no env var found for qsexecute_command")
try:
reservation_id = os.environ["reservation_id"]
except:
logging.warn("no env var found for reservation_id")
try:
ftpsrvr = os.environ["ftpsrvr"]
except:
logging.warn("no env var found for ftpsrvr")
try:
ftpuser = os.environ["ftpuser"]
except:
logging.warn("no env var found for ftpuser")
try:
ftppass = os.environ["ftppass"]
except:
logging.warn("no env var found for ftppass")
try:
ftphostkey = os.environ["ftphostkey"]
except:
logging.warn("no env var found for ftphostkey")
try:
resource_list = os.environ["resource_list"]
except:
logging.warn("no env var found for resource_list")
# Connect to share
try:
newpath = r'/tmp/csd/RemoteLogs'
if not os.path.exists(newpath):
os.makedirs(newpath)
mountup = "chmod 775 /tmp/csd/RemoteLogs\n"
mountup += "wuser='Qu@l!R0cks#@!'\n"
mountup += "srusr='" + qspword + "'\n"
mountup += "echo $wuser | sudo -S mount -t cifs -o username=L.CMFCS.01,password=$srusr,domain=et.lab," \
"dir_mode=0777,file_mode=0777 //" + ftpsrvr + "/remotelogs/ /tmp/csd/RemoteLogs\n"
mountup += "ls /tmp/csd/RemoteLogs\n"
mountcmds = open('/tmp/csd/mnt.sh', 'w+')
mountcmds.write(mountup)
mountcmds.close()
mountperm = os.system('chmod 775 /tmp/csd/mnt.sh')
mountchk = os.popen('/tmp/csd/mnt.sh').read().replace('\n', ' ')
logging.info("Connected to share.")
except:
logging.error('Failed to connect to share. %s' % sys.exc_info()[0])
raise Exception(sys.exc_info()[0])
# If Ubuntu, use curl. Else for red hat and suse, install python pip, request_ntlm
whichLinux = platform.linux_distribution()
if not 'Ubuntu' in whichLinux[0]:
# Red Hat or SuSE assumed
try:
newpath = r'/tmp/csd/py-files'
if not os.path.exists(newpath):
os.makedirs(newpath)
logging.info('get-pip....')
gets_pip = os.system('cp /tmp/csd/RemoteLogs/ForLinux/get-pip.py /tmp/csd/py-files/')
inst_pip = os.system("echo 'Qu@l!R0cks#@!' | sudo -S python /tmp/csd/py-files/get-pip.py")
logging.info('install requests_ntlm...')
inst_ntlm = os.system("echo 'Qu@l!R0cks#@!' | sudo -S pip install requests_ntlm")
logging.info('Libs installed...')
from requests_ntlm import HttpNtlmAuth
logging.info('Imports complete')
except:
logging.error('Failed to install libs. %s' % sys.exc_info()[0])
raise Exception(sys.exc_info()[0])
# Download Scripts/Files
for scr_num in range(1, int(qsscript_count) +1):
script_name = 'qsscript{0}_url'.format(str(scr_num))
logging.info("Checking {0}".format(script_name))
script_url = os.environ[script_name]
if script_url != 'tbd' and not '.exe' in script_url:
script_name = script_url.rsplit('/', 1)[-1]
logging.info("Processing %s" % script_name)
try:
if not 'Ubuntu' in whichLinux[0]:
session = requests.Session()
session.auth = HttpNtlmAuth(qsuname,qspword)
content = session.get(script_url)
# save content to file
save_to = open(("/tmp/csd/%s" % script_name), "w+")
save_to.write(content.content)
save_to.close()
else:
curlcmd = "curl --ntlm --user '%s':'%s' '%s' -o /tmp/csd/%s" % (qsuname, qspword, script_url, script_name)
logging.info("curlcmd: %s" % curlcmd)
curlgo = os.system(curlcmd)
logging.info("curl result: %s " % curlgo)
if script_name.endswith('.sh'):
logging.info("Changing file permissions to 775")
chmod = os.system('chmod 775 /tmp/csd/%s' % script_name)
if chmod != 0:
logging.error("Could not chmod .sh script %s" % script_name)
raise Execption(sys.exc_info()[0])
except:
logging.error('Failed to process file. %s' % sys.exc_info()[0])
raise Exception(sys.exc_info()[0])
# Process Execute Command
try:
logging.info('Executing %s ' % qsexecute_command)
# exec_results = subprocess.call([qsexecute_command]) #
exec_results = os.popen(qsexecute_command).read()
logging.info(exec_results)
logging.info('execution complete... did it work?')
except:
logging.error('Failed to successfully execute command. %s' % sys.exc_info()[0])
raise Exception(sys.exc_info()[0])
# create metadatda file
try:
metafile = open("/tmp/csd/rsvn-meta-data.txt","w+")
metafile.writelines("rsvnId: %s \r\n" % reservation_id)
metafile.writelines("ftp_srvr: %s \r\n" % ftpsrvr)
metafile.writelines("ftp_user: %s \r\n" % ftpuser)
metafile.writelines("ftp_pass: %s \r\n" % ftppass)
metafile.writelines("ftp_hostkey: %s \r\n" % ftphostkey)
metafile.writelines("resource_list: %s \r\n" % resource_list)
metafile.close()
logging.info("Meta-data file created")
except:
logging.error('Failed to create meta-data file. %s' % sys.exc_info()[0])
raise Exception(sys.exc_info()[0])
# Push log remote using a mount to a windows share
if not reservation_id in mountchk:
os.system('mkdir /tmp/csd/RemoteLogs/%s' % reservation_id)
mountup = "cp " + logfilename + ' /tmp/csd/RemoteLogs/' + reservation_id + '/\n'
mountup += "ls /tmp/csd/RemoteLogs/" + reservation_id + '/\n'
mountchk = os.popen(mountup).read().replace('\n', ' ')
if 'linux_deploy' in mountchk:
logging.info("Pushed log %s" % mountchk)
else:
logging.error("Failed to push log to server.")
# unmount share
try:
unmount = "echo 'Qu@l!R0cks#@!' | sudo -S umount -f -l ./RemoteLogs"
logging.info(unmount)
unmrslt = os.popen(unmount).read()
logging.info(unmrslt)
except:
logging.error('Failed to unmont share. %s %s' % (unmrslt, sys.exc_info()[0]))
raise Exception(sys.exc_info()[0])
logging.info("End of Script")