chiark / gitweb /
support cloud storage with libcloud, starting with Amazon AWS S3
[fdroidserver.git] / fdroidserver / server.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
4 # server.py - part of the FDroid server tools
5 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
6 #
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.
11 #
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.
16 #
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/>.
19
20 import sys
21 import os
22 import subprocess
23 from optparse import OptionParser
24 import logging
25 import common
26
27 config = None
28 options = None
29
30 def update_awsbucket(repo_section):
31     '''
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.
35
36     Requires AWS credentials set in config.py: awsaccesskeyid, awssecretkey
37     '''
38
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
43
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!')
46         sys.exit(1)
47     awsbucket = config['awsbucket']
48
49     cls = get_driver(Provider.S3)
50     driver = cls(config['awsaccesskeyid'], config['awssecretkey'])
51     container = driver.get_container(container_name=awsbucket)
52
53     upload_dir = 'fdroid/' + repo_section
54     if options.verbose:
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 + '/'):
59             obj.delete()
60             if options.verbose:
61                 logging.info('  deleted ' + obj.name)
62
63     if options.verbose:
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)):
66         for name in files:
67             file_to_upload = os.path.join(root, name)
68             object_name = 'fdroid/' + os.path.relpath(file_to_upload, os.getcwd())
69
70             if options.verbose:
71                 logging.info('  ' + file_to_upload + '...')
72             extra = { 'acl': 'public-read' }
73             driver.upload_object(file_path=file_to_upload,
74                                  container=container,
75                                  object_name=object_name,
76                                  extra=extra)
77
78 def update_serverwebroot(repo_section):
79     rsyncargs = ['rsync', '-u', '-r', '--delete']
80     if options.verbose:
81         rsyncargs += ['--verbose']
82     if options.quiet:
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:
90         sys.exit(1)
91     if subprocess.call(rsyncargs +
92                        [index, config['serverwebroot'] + repo_section]) != 0:
93         sys.exit(1)
94     if subprocess.call(rsyncargs +
95                        [indexjar, config['serverwebroot'] + repo_section]) != 0:
96         sys.exit(1)
97
98 def main():
99     global config, options
100
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()
108
109     config = common.read_config(options)
110
111     if len(args) != 1:
112         logging.critical("Specify a single command")
113         sys.exit(1)
114
115     if args[0] != 'init' and args[0] != 'update':
116         logging.critical("The only commands currently supported are 'init' and 'update'")
117         sys.exit(1)
118
119     if 'nonstandardwebroot' in config and config['nonstandardwebroot'] == True:
120         standardwebroot = False
121     else:
122         standardwebroot = True
123
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')
133             sys.exit(1)
134     elif 'awsbucket' not in config:
135         logging.warn('No serverwebroot or awsbucket set! Edit your config.py to set one or both.')
136         sys.exit(1)
137
138     repo_sections = ['repo']
139     if config['archive_older'] != 0:
140         repo_sections.append('archive')
141
142     if args[0] == 'init':
143         if serverwebroot != None:
144             sshargs = ['ssh']
145             if options.quiet:
146                 sshargs += ['-q']
147             for repo_section in repo_sections:
148                 cmd = sshargs + [host, 'mkdir -p', fdroiddir + '/' + repo_section]
149                 if options.verbose:
150                     # ssh -v produces different output than rsync -v, so this
151                     # simulates rsync -v
152                     logging.info(' '.join(cmd))
153                 if subprocess.call(cmd) != 0:
154                     sys.exit(1)
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)
161
162     sys.exit(0)
163
164 if __name__ == "__main__":
165     main()