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
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'])
51 container = driver.get_container(container_name=awsbucket)
53 upload_dir = 'fdroid/' + repo_section
55 logging.info('Deleting existing repo on Amazon S3 bucket: "' + awsbucket
56 + '/' + upload_dir + '"')
57 for obj in container.list_objects():
58 if obj.name.startswith(upload_dir + '/'):
61 logging.info(' deleted ' + obj.name)
64 logging.info('Uploading to Amazon S3 bucket: "' + awsbucket + '/' + upload_dir + '"')
65 for root, _, files in os.walk(os.path.join(os.getcwd(), repo_section)):
67 file_to_upload = os.path.join(root, name)
68 object_name = 'fdroid/' + os.path.relpath(file_to_upload, os.getcwd())
71 logging.info(' ' + file_to_upload + '...')
72 extra = { 'acl': 'public-read' }
73 driver.upload_object(file_path=file_to_upload,
75 object_name=object_name,
78 def update_serverwebroot(repo_section):
79 rsyncargs = ['rsync', '-u', '-r', '--delete']
81 rsyncargs += ['--verbose']
83 rsyncargs += ['--quiet']
84 index = os.path.join(repo_section, 'index.xml')
85 indexjar = os.path.join(repo_section, 'index.jar')
86 # serverwebroot is guaranteed to have a trailing slash in common.py
87 if subprocess.call(rsyncargs +
88 ['--exclude', index, '--exclude', indexjar,
89 repo_section, config['serverwebroot']]) != 0:
91 if subprocess.call(rsyncargs +
92 [index, config['serverwebroot'] + repo_section]) != 0:
94 if subprocess.call(rsyncargs +
95 [indexjar, config['serverwebroot'] + repo_section]) != 0:
99 global config, options
101 # Parse command line...
102 parser = OptionParser()
103 parser.add_option("-v", "--verbose", action="store_true", default=False,
104 help="Spew out even more information than normal")
105 parser.add_option("-q", "--quiet", action="store_true", default=False,
106 help="Restrict output to warnings and errors")
107 (options, args) = parser.parse_args()
109 config = common.read_config(options)
112 logging.critical("Specify a single command")
115 if args[0] != 'init' and args[0] != 'update':
116 logging.critical("The only commands currently supported are 'init' and 'update'")
119 if 'nonstandardwebroot' in config and config['nonstandardwebroot'] == True:
120 standardwebroot = False
122 standardwebroot = True
124 if 'serverwebroot' in config:
125 serverwebroot = config['serverwebroot']
126 host, fdroiddir = serverwebroot.rstrip('/').split(':')
127 serverrepobase = os.path.basename(fdroiddir)
128 if serverrepobase != 'fdroid' and standardwebroot:
129 logging.error('serverwebroot does not end with "fdroid", '
130 + 'perhaps you meant one of these:\n\t'
131 + serverwebroot.rstrip('/') + '/fdroid\n\t'
132 + serverwebroot.rstrip('/').rstrip(serverrepobase) + 'fdroid')
134 elif 'awsbucket' not in config:
135 logging.warn('No serverwebroot or awsbucket set! Edit your config.py to set one or both.')
138 repo_sections = ['repo']
139 if config['archive_older'] != 0:
140 repo_sections.append('archive')
142 if args[0] == 'init':
143 if serverwebroot != None:
147 for repo_section in repo_sections:
148 cmd = sshargs + [host, 'mkdir -p', fdroiddir + '/' + repo_section]
150 # ssh -v produces different output than rsync -v, so this
152 logging.info(' '.join(cmd))
153 if subprocess.call(cmd) != 0:
155 elif args[0] == 'update':
156 for repo_section in repo_sections:
157 if 'serverwebroot' in config:
158 update_serverwebroot(repo_section)
159 if 'awsbucket' in config:
160 update_awsbucket(repo_section)
164 if __name__ == "__main__":