PageRenderTime 20ms CodeModel.GetById 9ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/silverlining/mgr-scripts/update-service.py

https://bitbucket.org/ianb/silverlining/
Python | 73 lines | 71 code | 1 blank | 1 comment | 0 complexity | 1363b34b2bd0e092355badc2911af774 MD5 | raw file
 1#!/usr/bin/env python
 2import sys
 3sys.path.insert(0, '/usr/local/share/silverlining/lib')
 4import os
 5import shutil
 6from optparse import OptionParser
 7from silversupport.appconfig import AppConfig
 8from silversupport.appdata import instance_for_location, normalize_location
 9from silversupport.shell import apt_install, run
10
11parser = OptionParser(
12    usage="%prog INSTANCE_NAME",
13    description="""\
14Runs any setup necessary for the new instance's services.  Also
15apt-get installs any packages listed in app.ini.
16""")
17
18parser.add_option(
19    '--clear',
20    action='store_true',
21    help='Clear the database')
22
23parser.add_option(
24    '--location',
25    help='location to update/clear (instead of INSTANCE_NAME)')
26
27php_packages = [
28    'php5',
29    'php5-cgi',
30    'php5-cli',
31    'libapache2-mod-php5',
32    ]
33
34
35def update_service(instance_name, clear=False):
36    app_config = AppConfig.from_instance_name(instance_name)
37    app_config.install_services(clear)
38    for script in app_config.package_install_script:
39        print 'Running install script %s' % script
40        run([script])
41    packages = app_config.packages
42    if packages:
43        print 'Confirming packages installed: %s' % ', '.join(packages)
44        apt_install(packages)
45    if (app_config.platform == 'php' and
46        (not os.path.exists('/usr/share/doc/php5')
47         or not os.path.exists('/usr/bin/php5'))):
48        apt_install(php_packages)
49        run(['a2enmod', 'php5'])
50        run(['/etc/init.d/apache2', 'restart'])
51    tmp = os.path.join('/var/lib/silverlining/tmp', app_config.app_name)
52    if os.path.exists(tmp):
53        for name in os.listdir(tmp):
54            fn = os.path.join(tmp, name)
55            if os.path.isdir(fn):
56                shutil.rmtree(fn)
57            else:
58                os.unlink(fn)
59    else:
60        os.makedirs(tmp)
61    if not os.path.exists(app_config.log_dir):
62        run(['sudo', '-u', 'www-data', 'mkdir', '-p', app_config.log_dir])
63    for script in app_config.after_install_script:
64        print 'Running post script %s' % script
65        run([script])
66
67if __name__ == '__main__':
68    options, args = parser.parse_args()
69    if options.location:
70        instance_name = instance_for_location(*normalize_location(options.location))
71    else:
72        instance_name = args[0]
73    update_service(instance_name, options.clear)