LEFT | RIGHT |
(no file at all) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import argparse |
| 4 from contextlib import closing |
| 5 import hashlib |
| 6 import os |
| 7 import sys |
| 8 import shutil |
| 9 import tarfile |
| 10 import urllib2 |
| 11 |
| 12 |
| 13 def download(url): |
| 14 file_name = url.split('/')[-1] |
| 15 print 'Downloading: ' + file_name |
| 16 with closing(urllib2.urlopen(url)) as page: |
| 17 block_sz = 8912 |
| 18 with open(file_name, 'wb') as f: |
| 19 while True: |
| 20 buffer = page.read(block_sz) |
| 21 if not buffer: |
| 22 break |
| 23 f.write(buffer) |
| 24 return os.path.join(os.getcwd(),file_name) |
| 25 |
| 26 def calculate_md5(file): |
| 27 with open(file) as f: |
| 28 data = f.read() |
| 29 md5_result = hashlib.md5(data).hexdigest() |
| 30 return md5_result.strip() |
| 31 |
| 32 def read_md5(file): |
| 33 with open(file) as f: |
| 34 md5_result = f.readline() |
| 35 return md5_result.strip() |
| 36 |
| 37 def untar(tar_file): |
| 38 if tarfile.is_tarfile(tar_file): |
| 39 with tarfile.open(tar_file, 'r:gz') as tar: |
| 40 tar.extractall() |
| 41 print 'Extracted in current directory' |
| 42 return os.path.dirname(os.path.abspath(__file__)) |
| 43 |
| 44 def move_site(src, dest): |
| 45 destination = '/var/www/' + dest |
| 46 try: |
| 47 for filename in os.listdir(src): |
| 48 shutil.move(os.path.join(src,filename), |
| 49 os.path.join(destination,filename)) |
| 50 print "Moving {0} to {1} ".format(filename, destination) |
| 51 except Exception as e: |
| 52 sys.exit(e) |
| 53 |
| 54 if __name__ == '__main__': |
| 55 parser = argparse.ArgumentParser() |
| 56 parser.add_argument('--hash', action='store', type=str, nargs='?', |
| 57 help='Hash of the commit to deploy') |
| 58 parser.add_argument('--url', action='store', type=str, |
| 59 help='URL where files will be downloaded') |
| 60 parser.add_argument('--domain', action='store', type=str, nargs='?', |
| 61 help='The domain to prepend [eg. https://$domain.eyeofil
es.com') |
| 62 parser.add_argument('--website', action='store', type=str, nargs='?', |
| 63 help='The name of the website [e.g. help.eyeo.com]') |
| 64 args = parser.parse_args() |
| 65 hash = args.hash |
| 66 domain = args.domain |
| 67 if args.url: |
| 68 url_file = '{0}{1}.tar.gz'.format(url, hash) |
| 69 url_md5 = '{0}{1}.md5'.format(url, hash) |
| 70 else: |
| 71 url_file = 'https://{0}.eyeofiles.com/{1}.tar.gz'.format(domain, hash) |
| 72 url_md5 = 'https://{0}.eyeofiles.com/{1}.md5'.format(domain, hash) |
| 73 |
| 74 down_file = download(url_file) |
| 75 down_md5 = download(url_md5) |
| 76 if calculate_md5(down_file) == read_md5(down_md5): |
| 77 tar_directory = untar(down_file) |
| 78 hash_directory = os.path.join(tar_directory, hash) |
| 79 move_site(hash_directory, args.website) |
| 80 else: |
| 81 sys.exit("Hashes don't match") |
| 82 |
| 83 |
LEFT | RIGHT |