chiark / gitweb /
if the AWS S3 bucket does not exist, create it
[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, ContainerDoesNotExistError
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     try:
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 + '"')
56
57     upload_dir = 'fdroid/' + repo_section
58     if options.verbose:
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 + '/'):
63             obj.delete()
64             if options.verbose:
65                 logging.info('  deleted ' + obj.name)
66
67     if options.verbose:
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)):
70         for name in files:
71             file_to_upload = os.path.join(root, name)
72             object_name = 'fdroid/' + os.path.relpath(file_to_upload, os.getcwd())
73
74             if options.verbose:
75                 logging.info('  ' + file_to_upload + '...')
76             extra = { 'acl': 'public-read' }
77             driver.upload_object(file_path=file_to_upload,
78                                  container=container,
79                                  object_name=object_name,
80                                  extra=extra)
81
82 def update_serverwebroot(repo_section):
83     rsyncargs = ['rsync', '-u', '-r', '--delete']
84     if options.verbose:
85         rsyncargs += ['--verbose']
86     if options.quiet:
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:
94         sys.exit(1)
95     if subprocess.call(rsyncargs +
96                        [index, config['serverwebroot'] + repo_section]) != 0:
97         sys.exit(1)
98     if subprocess.call(rsyncargs +
99                        [indexjar, config['serverwebroot'] + repo_section]) != 0:
100         sys.exit(1)
101
102 def main():
103     global config, options
104
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()
112
113     config = common.read_config(options)
114
115     if len(args) != 1:
116         logging.critical("Specify a single command")
117         sys.exit(1)
118
119     if args[0] != 'init' and args[0] != 'update':
120         logging.critical("The only commands currently supported are 'init' and 'update'")
121         sys.exit(1)
122
123     if 'nonstandardwebroot' in config and config['nonstandardwebroot'] == True:
124         standardwebroot = False
125     else:
126         standardwebroot = True
127
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')
137             sys.exit(1)
138     elif 'awsbucket' not in config:
139         logging.warn('No serverwebroot or awsbucket set! Edit your config.py to set one or both.')
140         sys.exit(1)
141
142     repo_sections = ['repo']
143     if config['archive_older'] != 0:
144         repo_sections.append('archive')
145
146     if args[0] == 'init':
147         if serverwebroot != None:
148             sshargs = ['ssh']
149             if options.quiet:
150                 sshargs += ['-q']
151             for repo_section in repo_sections:
152                 cmd = sshargs + [host, 'mkdir -p', fdroiddir + '/' + repo_section]
153                 if options.verbose:
154                     # ssh -v produces different output than rsync -v, so this
155                     # simulates rsync -v
156                     logging.info(' '.join(cmd))
157                 if subprocess.call(cmd) != 0:
158                     sys.exit(1)
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)
165
166     sys.exit(0)
167
168 if __name__ == "__main__":
169     main()