forked from ZickZakk/HTWK_SmartDriving_2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer-to-car.py
executable file
·171 lines (131 loc) · 4.79 KB
/
transfer-to-car.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
166
167
168
169
170
171
import argparse
import os
import re
import subprocess
import tarfile
from os import walk
import time
import sys
__author__ = 'pbachmann'
WHITELIST = ['bin/filter/',
'config/',
'configuration_files/',
'description/']
ARCHIVE_FILENAME = 'deploy.tar.gz'
SCRIPT_FILENAME = 'deploy.py'
SCP_HOST = '{0}@{1}:{2}'
SSH_HOST = '{0}@{1}'
class Settings(object):
def __init__(self, args):
self.pw = args.pw
self.user = args.user
self.host = args.host
self.compress = args.compress
self.path = str(args.path)
if not self.path.endswith('/'):
self.path += '/'
def main():
print 'transfer-to-car.py'
print 'Working dir is ' + os.getcwd()
print
sys.stdout.flush()
parser = setup_argparser()
args = parser.parse_args()
settings = Settings(args)
print 'Checking host ' + settings.host
return_code = subprocess.call('/bin/ping -c 1 -W 1 ' + settings.host, shell=True, stdout=subprocess.PIPE)
if return_code > 0:
print settings.host + ' is down!'
exit(return_code)
else:
print 'Ok'
print
print 'Collecting files'
abs_filepaths = collect_files()
print 'Compressing files'
sys.stdout.flush()
mode = 'w'
if settings.compress:
mode = 'w:gz'
with tarfile.open('.' + os.sep + ARCHIVE_FILENAME, mode=mode) as archive:
for abs_filepath in abs_filepaths:
rel_filepath = os.path.relpath(abs_filepath, '.')
archive.add(abs_filepath, rel_filepath)
print 'Created .' + os.sep + ARCHIVE_FILENAME
print
sys.stdout.flush()
print 'Copying archive'
sys.stdout.flush()
host = SCP_HOST.format(settings.user, settings.host, settings.path) + ARCHIVE_FILENAME
copy_process = subprocess.Popen(['/usr/bin/scp', '-o', 'StrictHostKeyChecking=no',
'.' + os.sep + ARCHIVE_FILENAME, host],
cwd='./',
env={'PATH': os.environ['PATH']})
return_code = copy_process.wait()
if return_code > 0:
cleanup()
exit(return_code)
print
print 'Copying deploy script'
sys.stdout.flush()
host = SCP_HOST.format(settings.user, settings.host, settings.path) + SCRIPT_FILENAME
copy_process = subprocess.Popen(['/usr/bin/scp', '-o', 'StrictHostKeyChecking=no',
'.' + os.sep + SCRIPT_FILENAME, host],
cwd='./',
env={'PATH': os.environ['PATH']})
return_code = copy_process.wait()
if return_code > 0:
cleanup()
exit(return_code)
print
print 'Executing deploy script'
sys.stdout.flush()
host = SSH_HOST.format(settings.user, settings.host)
command = 'cd {0} && /usr/bin/python ./{1}'.format(settings.path, SCRIPT_FILENAME)
deploy_process = subprocess.Popen(['/usr/bin/ssh', '-o', 'StrictHostKeyChecking=no',
host, command],
cwd='./',
env={'PATH': os.environ['PATH']})
return_code = deploy_process.wait()
if return_code > 0:
cleanup()
exit(return_code)
cleanup()
def cleanup():
print('')
print('Cleanup')
print('Removing ' + ARCHIVE_FILENAME)
print('')
os.remove('.' + os.sep + ARCHIVE_FILENAME)
def collect_files():
files = []
for (dirpath, dirnames, filenames) in walk('.'):
for full_filename in filenames:
abs_filepath = dirpath + os.sep + full_filename
rel_filepath = os.path.relpath(abs_filepath, '.')
for f in WHITELIST:
if re.match(f, rel_filepath):
files.append(abs_filepath)
return files
def setup_argparser():
parser = argparse.ArgumentParser(description="My argument pareser")
parser.add_argument('--host',
required=True,
help='Specifies address of the remote host.')
parser.add_argument('--user',
required=True,
help='Specifies user for the login on the remote host.')
parser.add_argument('--pw',
required=True,
help='Specifies password for the login on the remote host.')
parser.add_argument('--path',
required=True,
help='Specifies path where the files should be deployed on the remote host.')
parser.add_argument('--compress', dest='compress', action='store_true')
return parser
if __name__ == '__main__':
start = time.time()
main()
elapsedTime = time.time() - start
print 'Deployment finished in ' + str(elapsedTime)
sys.stdout.flush()