2 # -*- coding: utf-8 -*-
4 # server.py - part of the FDroid server tools
5 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 from optparse import OptionParser
32 def update_awsbucket(repo_section):
34 Upload the contents of the directory `repo_section` (including
35 subdirectories) to the AWS S3 "bucket". The contents of that subdir of the
36 bucket will first be deleted.
38 Requires AWS credentials set in config.py: awsaccesskeyid, awssecretkey
41 import libcloud.security
42 libcloud.security.VERIFY_SSL_CERT = True
43 from libcloud.storage.types import Provider, ContainerDoesNotExistError
44 from libcloud.storage.providers import get_driver
46 if not config.get('awsaccesskeyid') or not config.get('awssecretkey'):
47 logging.error('To use awsbucket, you must set awssecretkey and awsaccesskeyid in config.py!')
49 awsbucket = config['awsbucket']
51 cls = get_driver(Provider.S3)
52 driver = cls(config['awsaccesskeyid'], config['awssecretkey'])
54 container = driver.get_container(container_name=awsbucket)
55 except ContainerDoesNotExistError:
56 container = driver.create_container(container_name=awsbucket)
57 logging.info('Created new container "' + container.name + '"')
59 upload_dir = 'fdroid/' + repo_section
61 for obj in container.list_objects():
62 if obj.name.startswith(upload_dir + '/'):
65 for root, _, files in os.walk(os.path.join(os.getcwd(), repo_section)):
68 file_to_upload = os.path.join(root, name)
69 object_name = 'fdroid/' + os.path.relpath(file_to_upload, os.getcwd())
70 if object_name not in objs:
73 obj = objs.pop(object_name)
74 if obj.size != os.path.getsize(file_to_upload):
77 # if the sizes match, then compare by MD5
79 with open(file_to_upload, 'rb') as f:
85 if obj.hash != md5.hexdigest():
86 s3url = 's3://' + awsbucket + '/' + obj.name
87 logging.info(' deleting ' + s3url)
88 if not driver.delete_object(obj):
89 logging.warn('Could not delete ' + s3url)
94 logging.info(' uploading "' + file_to_upload + '"...')
95 extra = {'acl': 'public-read'}
96 if file_to_upload.endswith('.sig'):
97 extra['content_type'] = 'application/pgp-signature'
98 elif file_to_upload.endswith('.asc'):
99 extra['content_type'] = 'application/pgp-signature'
100 logging.info(' uploading ' + os.path.relpath(file_to_upload)
101 + ' to s3://' + awsbucket + '/' + object_name)
102 obj = driver.upload_object(file_path=file_to_upload,
104 object_name=object_name,
107 # delete the remnants in the bucket, they do not exist locally
109 object_name, obj = objs.popitem()
110 s3url = 's3://' + awsbucket + '/' + object_name
111 if object_name.startswith(upload_dir):
112 logging.warn(' deleting ' + s3url)
113 driver.delete_object(obj)
115 logging.info(' skipping ' + s3url)
118 def update_serverwebroot(repo_section):
119 rsyncargs = ['rsync', '--update', '--recursive', '--delete']
121 rsyncargs += ['--verbose']
123 rsyncargs += ['--quiet']
124 indexxml = os.path.join(repo_section, 'index.xml')
125 indexjar = os.path.join(repo_section, 'index.jar')
126 # serverwebroot is guaranteed to have a trailing slash in common.py
127 if subprocess.call(rsyncargs +
128 ['--exclude', indexxml, '--exclude', indexjar,
129 repo_section, config['serverwebroot']]) != 0:
131 # use stricter checking on the indexes since they provide the signature
132 rsyncargs += ['--checksum']
133 sectionpath = config['serverwebroot'] + repo_section
134 if subprocess.call(rsyncargs + [indexxml, sectionpath]) != 0:
136 if subprocess.call(rsyncargs + [indexjar, sectionpath]) != 0:
141 global config, options
143 # Parse command line...
144 parser = OptionParser()
145 parser.add_option("-v", "--verbose", action="store_true", default=False,
146 help="Spew out even more information than normal")
147 parser.add_option("-q", "--quiet", action="store_true", default=False,
148 help="Restrict output to warnings and errors")
149 (options, args) = parser.parse_args()
151 config = common.read_config(options)
154 logging.critical("Specify a single command")
157 if args[0] != 'init' and args[0] != 'update':
158 logging.critical("The only commands currently supported are 'init' and 'update'")
161 if config.get('nonstandardwebroot') is True:
162 standardwebroot = False
164 standardwebroot = True
166 if config.get('serverwebroot'):
167 serverwebroot = config['serverwebroot']
168 host, fdroiddir = serverwebroot.rstrip('/').split(':')
169 serverrepobase = os.path.basename(fdroiddir)
170 if serverrepobase != 'fdroid' and standardwebroot:
171 logging.error('serverwebroot does not end with "fdroid", '
172 + 'perhaps you meant one of these:\n\t'
173 + serverwebroot.rstrip('/') + '/fdroid\n\t'
174 + serverwebroot.rstrip('/').rstrip(serverrepobase) + 'fdroid')
176 elif not config.get('awsbucket'):
177 logging.warn('No serverwebroot or awsbucket set! Edit your config.py to set one or both.')
180 repo_sections = ['repo']
181 if config['archive_older'] != 0:
182 repo_sections.append('archive')
184 if args[0] == 'init':
185 if config.get('serverwebroot'):
189 for repo_section in repo_sections:
190 cmd = sshargs + [host, 'mkdir -p', fdroiddir + '/' + repo_section]
192 # ssh -v produces different output than rsync -v, so this
194 logging.info(' '.join(cmd))
195 if subprocess.call(cmd) != 0:
197 elif args[0] == 'update':
198 for repo_section in repo_sections:
199 if config.get('serverwebroot'):
200 update_serverwebroot(repo_section)
201 if config.get('awsbucket'):
202 update_awsbucket(repo_section)
206 if __name__ == "__main__":