Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 import argparse | |
4 from filecmp import dircmp | |
5 import hashlib | |
6 import os | |
7 import sys | |
8 import shutil | |
9 import tarfile | |
10 import tempfile | |
11 import urllib | |
12 import urllib2 | |
13 | |
14 _tmp_dir = tempfile.mkdtemp() | |
15 | |
16 | |
17 def download(url): | |
18 file_name = url.split('/')[-1] | |
19 abs_file_name = os.path.join(_tmp_dir, file_name) | |
20 print 'Downloading: ' + file_name | |
21 try: | |
22 filename, _ = urllib.urlretrieve(url, abs_file_name) | |
Vasily Kuznetsov
2018/05/22 16:38:37
If you're throwing away `filename`, you can just i
f.lopez
2018/06/06 00:05:03
Acknowledged.
| |
23 return abs_file_name | |
24 except urllib2.HTTPError as e: | |
25 if e.code == 404: | |
26 sys.exit('File not found on remote source') | |
27 except Exception as e: | |
28 sys.exit(e) | |
29 | |
30 | |
31 def calculate_md5(file): | |
32 with open(file) as f: | |
33 data = f.read() | |
34 md5_result = hashlib.md5(data).hexdigest() | |
35 return md5_result.strip() | |
36 | |
37 | |
38 def read_md5(file): | |
39 with open(file) as f: | |
40 md5_result = f.readline() | |
41 return md5_result.strip() | |
42 | |
43 | |
44 def untar(tar_file): | |
45 if tarfile.is_tarfile(tar_file): | |
46 with tarfile.open(tar_file, 'r:gz') as tar: | |
47 tar.extractall(_tmp_dir) | |
48 | |
49 | |
50 def remove_tree(to_remove): | |
51 if os.path.exists(to_remove): | |
52 if os.path.isdir(to_remove): | |
53 shutil.rmtree(to_remove) | |
54 else: | |
55 os.remove(to_remove) | |
56 | |
57 | |
58 def deploy_files(dcmp): | |
59 for name in dcmp.diff_files: | |
60 copytree(dcmp.right, dcmp.left) | |
61 for name in dcmp.left_only: | |
62 remove_tree(dcmp.left + "/" + name) | |
Vasily Kuznetsov
2018/05/23 12:25:43
It's more portable (and generally a better practic
f.lopez
2018/06/06 00:05:03
Acknowledged.
| |
63 for name in dcmp.right_only: | |
64 copytree(dcmp.right, dcmp.left) | |
65 for sub_dcmp in dcmp.subdirs.values(): | |
66 deploy_files(sub_dcmp) | |
67 | |
68 | |
69 def copytree(src, dst): | |
70 if not os.path.exists(dst): | |
71 os.makedirs(dst) | |
72 shutil.copystat(src, dst) | |
73 lst = os.listdir(src) | |
74 for item in lst: | |
75 s = os.path.join(src, item) | |
76 d = os.path.join(dst, item) | |
77 if os.path.isdir(s): | |
78 copytree(s, d) | |
79 else: | |
80 shutil.copy2(s, d) | |
81 | |
82 | |
83 if __name__ == '__main__': | |
84 parser = argparse.ArgumentParser( | |
85 description="""Fetch a compressed archive in the form of $HASH.tar.gz | |
86 and deploy it to /var/www/$WEBSITE folder""", | |
87 epilog="""--hash must be provided in order to fetch the files, | |
88 expected files to be fetched are $HASH.tar.gz and $HASH.md5 in | |
89 order to compare the hashes. | |
90 --source must be an URL, e.g. | |
91 https://helpcenter.eyeofiles.com""", | |
92 ) | |
93 parser.add_argument('--hash', action='store', type=str, | |
94 nargs='?', required=True, | |
95 help='Hash of the commit to deploy') | |
96 parser.add_argument('--source', action='store', type=str, | |
97 required=True, nargs='?', | |
Vasily Kuznetsov
2018/05/22 16:38:37
Won't nargs='?' make the argument (of --source) op
f.lopez
2018/06/06 00:05:03
Acknowledged.
| |
98 help='The source where files will be downloaded') | |
99 parser.add_argument('--website', action='store', type=str, nargs='?', | |
100 help='The name of the website [e.g. help.eyeo.com]') | |
101 args = parser.parse_args() | |
102 hash = args.hash | |
103 source = args.source | |
104 url_file = '{0}/{1}.tar.gz'.format(source, hash) | |
105 url_md5 = '{0}/{1}.md5'.format(source, hash) | |
106 down_file = download(url_file) | |
107 down_md5 = download(url_md5) | |
108 try: | |
109 if calculate_md5(down_file) == read_md5(down_md5): | |
110 untar(down_file) | |
111 hash_directory = os.path.join(_tmp_dir, hash) | |
112 destination = '/var/www/' + args.website | |
Vasily Kuznetsov
2018/05/23 12:25:43
Maybe also `os.path.join`?
Also, do you think it'
f.lopez
2018/06/06 00:05:03
This is not expected to change, since it is the de
| |
113 dcmp = dircmp(destination, hash_directory) | |
114 print "Deploying files" | |
Vasily Kuznetsov
2018/05/23 12:25:44
Remember the quotes rule ;P
f.lopez
2018/06/06 00:05:03
Oh my god...
| |
115 deploy_files(dcmp) | |
116 else: | |
117 sys.exit("Hashes don't match") | |
118 except Exception as e: | |
119 sys.exit(e) | |
120 finally: | |
121 shutil.rmtree(_tmp_dir) | |
Vasily Kuznetsov
2018/05/23 12:25:44
The temporary directory won't be deleted if downlo
f.lopez
2018/06/06 00:05:03
ok I like it, thanks
| |
OLD | NEW |