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/>.
23 from optparse import OptionParser
30 def update_awsbucket(repo_section):
32 Upload the contents of the directory `repo_section` (including
33 subdirectories) to the AWS S3 "bucket". The contents of that subdir of the
34 bucket will first be deleted.
36 Requires AWS credentials set in config.py: awsaccesskeyid, awssecretkey
39 import libcloud.security
40 libcloud.security.VERIFY_SSL_CERT = True
41 from libcloud.storage.types import Provider, ContainerDoesNotExistError
42 from libcloud.storage.providers import get_driver
44 if 'awsaccesskeyid' not in config or 'awssecretkey' not in config:
45 logging.error('To use awsbucket, you must set awssecretkey and awsaccesskeyid in config.py!')
47 awsbucket = config['awsbucket']
49 cls = get_driver(Provider.S3)
50 driver = cls(config['awsaccesskeyid'], config['awssecretkey'])
52 container = driver.get_container(container_name=awsbucket)
53 except ContainerDoesNotExistError:
54 container = driver.create_container(container_name=awsbucket)
55 logging.info('Created new container "' + container.name + '"')
57 upload_dir = 'fdroid/' + repo_section
59 logging.info('Deleting existing repo on Amazon S3 bucket: "' + awsbucket
60 + '/' + upload_dir + '"')
61 for obj in container.list_objects():
62 if obj.name.startswith(upload_dir + '/'):
65 logging.info(' deleted ' + obj.name)
68 logging.info('Uploading to Amazon S3 bucket: "' + awsbucket + '/' + upload_dir + '"')
69 for root, _, files in os.walk(os.path.join(os.getcwd(), repo_section)):
71 file_to_upload = os.path.join(root, name)
72 object_name = 'fdroid/' + os.path.relpath(file_to_upload, os.getcwd())
75 logging.info(' ' + file_to_upload + '...')
76 extra = { 'acl': 'public-read' }
77 driver.upload_object(file_path=file_to_upload,
79 object_name=object_name,
82 def update_serverwebroot(repo_section):
83 rsyncargs = ['rsync', '-u', '-r', '--delete']
85 rsyncargs += ['--verbose']
87 rsyncargs += ['--quiet']
88 index = os.path.join(repo_section, 'index.xml')
89 indexjar = os.path.join(repo_section, 'index.jar')
90 # serverwebroot is guaranteed to have a trailing slash in common.py
91 if subprocess.call(rsyncargs +
92 ['--exclude', index, '--exclude', indexjar,
93 repo_section, config['serverwebroot']]) != 0:
95 if subprocess.call(rsyncargs +
96 [index, config['serverwebroot'] + repo_section]) != 0:
98 if subprocess.call(rsyncargs +
99 [indexjar, config['serverwebroot'] + repo_section]) != 0:
103 global config, options
105 # Parse command line...
106 parser = OptionParser()
107 parser.add_option("-v", "--verbose", action="store_true", default=False,
108 help="Spew out even more information than normal")
109 parser.add_option("-q", "--quiet", action="store_true", default=False,
110 help="Restrict output to warnings and errors")
111 (options, args) = parser.parse_args()
113 config = common.read_config(options)
116 logging.critical("Specify a single command")
119 if args[0] != 'init' and args[0] != 'update':
120 logging.critical("The only commands currently supported are 'init' and 'update'")
123 if 'nonstandardwebroot' in config and config['nonstandardwebroot'] == True:
124 standardwebroot = False
126 standardwebroot = True
128 if 'serverwebroot' in config:
129 serverwebroot = config['serverwebroot']
130 host, fdroiddir = serverwebroot.rstrip('/').split(':')
131 serverrepobase = os.path.basename(fdroiddir)
132 if serverrepobase != 'fdroid' and standardwebroot:
133 logging.error('serverwebroot does not end with "fdroid", '
134 + 'perhaps you meant one of these:\n\t'
135 + serverwebroot.rstrip('/') + '/fdroid\n\t'
136 + serverwebroot.rstrip('/').rstrip(serverrepobase) + 'fdroid')
138 elif 'awsbucket' not in config:
139 logging.warn('No serverwebroot or awsbucket set! Edit your config.py to set one or both.')
142 repo_sections = ['repo']
143 if config['archive_older'] != 0:
144 repo_sections.append('archive')
146 if args[0] == 'init':
147 if serverwebroot != None:
151 for repo_section in repo_sections:
152 cmd = sshargs + [host, 'mkdir -p', fdroiddir + '/' + repo_section]
154 # ssh -v produces different output than rsync -v, so this
156 logging.info(' '.join(cmd))
157 if subprocess.call(cmd) != 0:
159 elif args[0] == 'update':
160 for repo_section in repo_sections:
161 if 'serverwebroot' in config:
162 update_serverwebroot(repo_section)
163 if 'awsbucket' in config:
164 update_awsbucket(repo_section)
168 if __name__ == "__main__":