LEFT | RIGHT |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # |
| 3 # This file is part of the Adblock Plus infrastructure |
| 4 # Copyright (C) 2018-present eyeo GmbH |
| 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. |
| 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. |
| 14 # |
| 15 # You should have received a copy of the GNU General Public License |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
2 | 17 |
3 import argparse | 18 import argparse |
4 from contextlib import closing | |
5 from filecmp import dircmp | 19 from filecmp import dircmp |
6 import hashlib | 20 import hashlib |
7 import os | 21 import os |
8 import sys | 22 import sys |
9 import shutil | 23 import shutil |
10 import tarfile | 24 import tarfile |
11 import tempfile | 25 import tempfile |
12 import urllib | 26 import urllib |
13 import urllib2 | |
14 | |
15 _tmp_dir = tempfile.mkdtemp() | |
16 | 27 |
17 | 28 |
18 def download(url): | 29 __doc__ = """This script MUST be renamed in the form of $WEBSITE, e.g. |
| 30 help.eyeo.com, --name must be provided in order to fetch the |
| 31 files, expected files to be fetched are $NAME.tar.gz and $NAME.md5 in |
| 32 order to compare the hashes. --source must be an URL, e.g. |
| 33 https://helpcenter.eyeofiles.com""" |
| 34 |
| 35 |
| 36 def download(url, temporary_directory): |
19 file_name = url.split('/')[-1] | 37 file_name = url.split('/')[-1] |
20 abs_file_name = os.path.join(_tmp_dir, file_name) | 38 absolute_file_path = os.path.join(temporary_directory, file_name) |
21 print 'Downloading: ' + file_name | 39 print 'Downloading: ' + file_name |
22 try: | 40 urllib.urlretrieve(url, absolute_file_path) |
23 filename, _ = urllib.urlretrieve(url, abs_file_name) | 41 return absolute_file_path |
24 return abs_file_name | |
25 except urllib2.HTTPError as e: | |
26 if e.code == 404: | |
27 sys.exit('File not found on remote source') | |
28 except Exception as e: | |
29 sys.exit(e) | |
30 | 42 |
31 | 43 |
32 def calculate_md5(file): | 44 def calculate_md5(file): |
33 with open(file) as f: | 45 with open(file) as file_handle: |
34 data = f.read() | 46 data = file_handle.read() |
35 md5_result = hashlib.md5(data).hexdigest() | 47 md5_result = hashlib.md5(data).hexdigest() |
36 return md5_result.strip() | 48 return md5_result.strip() |
37 | 49 |
38 | 50 |
39 def read_md5(file): | 51 def read_md5(file): |
40 with open(file) as f: | 52 with open(file) as file_handle: |
41 md5_result = f.readline() | 53 md5_result = file_handle.readline() |
42 return md5_result.strip() | 54 return md5_result.strip() |
43 | 55 |
44 | 56 |
45 def untar(tar_file): | 57 def untar(tar_file, temporary_directory): |
46 if tarfile.is_tarfile(tar_file): | 58 if tarfile.is_tarfile(tar_file): |
47 with tarfile.open(tar_file, 'r:gz') as tar: | 59 with tarfile.open(tar_file, 'r:gz') as tar: |
48 tar.extractall(_tmp_dir) | 60 tar.extractall(temporary_directory) |
49 | 61 |
50 | 62 |
51 def remove_tree(to_remove): | 63 def remove_tree(to_remove): |
52 if os.path.exists(to_remove): | 64 if os.path.exists(to_remove): |
53 if os.path.isdir(to_remove): | 65 if os.path.isdir(to_remove): |
54 shutil.rmtree(to_remove) | 66 shutil.rmtree(to_remove) |
55 else: | 67 else: |
56 os.remove(to_remove) | 68 os.remove(to_remove) |
57 | 69 |
58 | 70 |
59 def deploy_files(dcmp): | 71 def deploy_files(directory_comparison): |
60 for name in dcmp.diff_files: | 72 for name in directory_comparison.diff_files: |
61 copytree(dcmp.right, dcmp.left) | 73 copytree(directory_comparison.right, directory_comparison.left) |
62 for name in dcmp.left_only: | 74 for name in directory_comparison.left_only: |
63 remove_tree(dcmp.left + "/" + name) | 75 remove_tree(os.path.join(directory_comparison.left, name)) |
64 for name in dcmp.right_only: | 76 for name in directory_comparison.right_only: |
65 copytree(dcmp.right, dcmp.left) | 77 copytree(directory_comparison.right, directory_comparison.left) |
66 for sub_dcmp in dcmp.subdirs.values(): | 78 for subdirectory_comparison in directory_comparison.subdirs.values(): |
67 deploy_files(sub_dcmp) | 79 deploy_files(subdirectory_comparison) |
68 | 80 |
69 | 81 |
70 def copytree(src, dst): | 82 # shutil.copytree copies a tree but the destination directory MUST NOT exist |
71 if not os.path.exists(dst): | 83 # this might break the site for the duration of the files being deployed |
72 os.makedirs(dst) | 84 # for more info read: https://docs.python.org/2/library/shutil.html |
73 shutil.copystat(src, dst) | 85 def copytree(source, destination): |
74 lst = os.listdir(src) | 86 if not os.path.exists(destination): |
75 for item in lst: | 87 os.makedirs(destination) |
76 s = os.path.join(src, item) | 88 shutil.copystat(source, destination) |
77 d = os.path.join(dst, item) | 89 source_items = os.listdir(source) |
78 if os.path.isdir(s): | 90 for item in source_items: |
79 copytree(s, d) | 91 source_path = os.path.join(source, item) |
| 92 destination_path = os.path.join(destination, item) |
| 93 if os.path.isdir(source_path): |
| 94 copytree(source_path, destination_path) |
80 else: | 95 else: |
81 shutil.copy2(s, d) | 96 shutil.copy2(source_path, destination_path) |
82 | 97 |
83 | 98 |
84 if __name__ == '__main__': | 99 if __name__ == '__main__': |
| 100 website = os.path.basename(__file__) |
85 parser = argparse.ArgumentParser( | 101 parser = argparse.ArgumentParser( |
86 description="""Fetch a compressed archive in the form of $HASH.tar.gz | 102 description="""Fetch a compressed archive in the form of $NAME.tar.gz |
87 and deploy it to /var/www/$WEBSITE folder""", | 103 and deploy it to /var/www/{0} folder""".format(website), |
88 epilog="""--hash must be provided in order to fetch the files, | 104 epilog=__doc__, |
89 expected files to be fetched are $HASH.tar.gz and $HASH.md5 in | |
90 order to compare the hashes. | |
91 --source must be an URL, e.g. | |
92 https://helpcenter.eyeofiles.com""", | |
93 ) | 105 ) |
94 parser.add_argument('--hash', action='store', type=str, | 106 parser.add_argument('--name', action='store', type=str, required=True, |
95 nargs='?', required=True, | 107 help='Name of the tarball to deploy') |
96 help='Hash of the commit to deploy') | 108 parser.add_argument('--source', action='store', type=str, required=True, |
97 parser.add_argument('--source', action='store', type=str, | |
98 required=True, nargs='?', | |
99 help='The source where files will be downloaded') | 109 help='The source where files will be downloaded') |
100 parser.add_argument('--website', action='store', type=str, nargs='?', | 110 arguments = parser.parse_args() |
101 help='The name of the website [e.g. help.eyeo.com]') | 111 name = arguments.name |
102 args = parser.parse_args() | 112 source = arguments.source |
103 hash = args.hash | 113 url_file = '{0}/{1}.tar.gz'.format(source, name) |
104 source = args.source | 114 url_md5 = '{0}/{1}.md5'.format(source, name) |
105 url_file = '{0}/{1}.tar.gz'.format(source, hash) | 115 temporary_directory = tempfile.mkdtemp() |
106 url_md5 = '{0}/{1}.md5'.format(source, hash) | |
107 down_file = download(url_file) | |
108 down_md5 = download(url_md5) | |
109 try: | 116 try: |
110 if calculate_md5(down_file) == read_md5(down_md5): | 117 downloaded_file = download(url_file, temporary_directory) |
111 untar(down_file) | 118 downloaded_md5 = download(url_md5, temporary_directory) |
112 hash_directory = os.path.join(_tmp_dir, hash) | 119 if calculate_md5(downloaded_file) == read_md5(downloaded_md5): |
113 destination = '/var/www/' + args.website | 120 untar(downloaded_file, temporary_directory) |
114 dcmp = dircmp(destination, hash_directory) | 121 tarball_directory = os.path.join(temporary_directory, name) |
115 print "Deploying files" | 122 destination = os.path.join('/var/www/', website) |
116 deploy_files(dcmp) | 123 directory_comparison = dircmp(destination, tarball_directory) |
| 124 print 'Deploying files' |
| 125 deploy_files(directory_comparison) |
117 else: | 126 else: |
118 sys.exit("Hashes don't match") | 127 error_message = """{0}.tar.gz md5 computation doesn't match {0}.md5 |
119 except Exception as e: | 128 contents""".format(name) |
120 sys.exit(e) | 129 sys.exit(error_message) |
| 130 except Exception as error: |
| 131 sys.exit(error) |
121 finally: | 132 finally: |
122 shutil.rmtree(_tmp_dir) | 133 shutil.rmtree(temporary_directory) |
LEFT | RIGHT |