~abentley/charms/precise/juju-reports/industrial-test-dir

« back to all changes in this revision

Viewing changes to hooks/common.py

  • Committer: Aaron Bentley
  • Date: 2014-06-06 14:58:36 UTC
  • mfrom: (31.1.1 templates-and-ssh)
  • Revision ID: aaron.bentley@canonical.com-20140606145836-4m3g0t7tmvtqkxlf
Allow ini to be based on templates, allow ubuntu to use ssh.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
    NoSectionError)
4
4
import errno
5
5
import os
 
6
from pwd import getpwnam
6
7
import re
7
8
from StringIO import StringIO
8
9
import subprocess
17
18
HOME = '/home/ubuntu'
18
19
PROJECT_DIR = os.path.join(HOME, 'juju-reports')
19
20
INI = 'production.ini'
20
 
 
21
 
 
22
 
def in_juju_reports(fn):
23
 
    def dec_fn(*args):
24
 
        os.chdir(PROJECT_DIR)
25
 
        fn(*args)
26
 
        os.chdir(CHARM_DIR)
27
 
    return dec_fn
 
21
INI_TEMPLATE = 'resources/production.ini.template'
28
22
 
29
23
 
30
24
def get_ini_path():
37
31
    return config
38
32
 
39
33
 
40
 
def update_or_install_ini(mongo_url=None):
41
 
    config = get_ini()
42
 
    if mongo_url is not None:
43
 
        config.set('app:main', 'mongo.url', mongo_url)
 
34
def install_ini(mongo_url):
 
35
    config = ConfigParser()
 
36
    path = os.path.join(PROJECT_DIR, INI_TEMPLATE)
 
37
    if not os.path.exists(path):
 
38
        path = get_ini_path()
 
39
    config.read(path)
 
40
    config.set('app:main', 'mongo.url', mongo_url)
44
41
    config.write(open(get_ini_path(), 'w'))
45
42
    return config
46
43
 
86
83
    call_script('scripts/run')
87
84
 
88
85
 
89
 
def get_config():
90
 
    config = hookenv.config()
91
 
    if config['source'] == '':
92
 
        raise IncompleteConfig('source not specified.')
93
 
    if re.match('(lp:|.*://bazaar.launchpad.net/)', config['source']):
94
 
        if config['lp-key'] == '':
95
 
            raise IncompleteConfig('lp-key not specified.')
96
 
    return config
97
 
 
98
 
 
99
 
class IncompleteConfig(ValueError):
100
 
    """Error class indicating that ssh is not set to a valid value."""
101
 
 
102
 
 
103
 
@in_juju_reports
104
 
def update_source(source_url, revno, ssh_key):
105
 
    if ssh_key != '':
106
 
        fd = os.open('/root/.ssh/lp_rsa', os.O_WRONLY | os.O_TRUNC |
107
 
                     os.O_CREAT, 0600)
108
 
        try:
109
 
            os.write(fd, ssh_key)
110
 
        finally:
111
 
            os.close(fd)
112
 
        # Login to LP
113
 
        subprocess.check_call(['bzr', 'lp-login', 'juju-qa-bot'])
114
 
 
 
86
def open_secret(path, uid=None, gid=None):
 
87
    fd = os.open(path, os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0600)
 
88
    if uid is not None:
 
89
        os.fchown(fd, uid, gid)
 
90
    return os.fdopen(fd, 'w')
 
91
 
 
92
 
 
93
def configure_lp(ssh_key):
 
94
    if ssh_key == '':
 
95
        return
 
96
    with open_secret('/root/.ssh/lp_rsa') as key_file:
 
97
        key_file.write(ssh_key)
 
98
    pwd = getpwnam('ubuntu')
 
99
    ubuntu_path = os.path.join(HOME, '.ssh/lp_rsa')
 
100
    with open_secret(ubuntu_path, pwd.pw_uid, pwd.pw_gid) as key_file:
 
101
        key_file.write(ssh_key)
 
102
    # Login to LP
 
103
    subprocess.check_call(['bzr', 'lp-login', 'juju-qa-bot'])
 
104
 
 
105
 
 
106
def update_source(source_url, revno):
115
107
    # Grab the code
116
108
    revno = 'revno:%s' % revno
117
109
    bzr_cmd = ['bzr', 'pull', '--overwrite', source_url, '-r', revno, '-d',
118
 
               PROJECT_DIR]
 
110
               PROJECT_DIR, '--remember']
119
111
    subprocess.check_call(bzr_cmd)
120
 
 
121
 
    # Remove the unnecessary INI files--if something tries to use them we need
122
 
    # a failure.
123
 
    for ini in ['test.ini', 'development.ini']:
124
 
        try:
125
 
            os.unlink(os.path.join(PROJECT_DIR, ini))
126
 
        except OSError as e:
127
 
            if e.errno != errno.ENOENT:
128
 
                raise
129
 
 
130
 
 
131
 
@in_juju_reports
 
112
    # Restore the templates if they are missing/altered.
 
113
    subprocess.check_call(['bzr', 'revert',
 
114
                           os.path.join(PROJECT_DIR, 'resources')])
 
115
 
 
116
 
132
117
def install_production(dist_clean):
133
 
    subprocess.check_call(['make', 'deploydeps'])
 
118
    subprocess.check_call(['make', 'deploydeps'], cwd=PROJECT_DIR)
134
119
    if dist_clean:
135
 
        subprocess.check_call(['make', 'distclean'])
136
 
    subprocess.check_call(['make', 'prodinstall'])
 
120
        subprocess.check_call(['make', 'distclean'], cwd=PROJECT_DIR)
 
121
    subprocess.check_call(['make', 'prodinstall'], cwd=PROJECT_DIR)
137
122
    subprocess.check_call(['apt-get', 'autoremove', '--yes'])
138
123
 
139
124
 
192
177
 
193
178
def update_from_config():
194
179
    ini = get_ini()
195
 
    try:
196
 
        try:
197
 
            mongo_url = get_mongo_url()
198
 
            if mongo_url == '':
199
 
                raise IncompleteConfig('No mongodb set up.')
200
 
            config = get_config()
201
 
        except:
202
 
            set_port(ini, False)
203
 
            stop()
204
 
            raise
205
 
    except IncompleteConfig as e:
206
 
        subprocess.call(['juju-log', 'Incomplete config: %s' % e])
207
 
        sys.exit(0)
 
180
    mongo_url = get_mongo_url()
 
181
    config = hookenv.config()
208
182
    set_port(ini, False)
209
183
    stop()
210
 
    update_source(config['source'], config['revno'], config['lp-key'])
211
 
    # ini may be updated by update_source
212
 
    ini = get_ini()
 
184
    configure_lp(config['lp-key'])
 
185
    if config['source'] == '':
 
186
        hookenv.log('Incomplete config: source')
 
187
        return
 
188
    update_source(config['source'], config['revno'])
213
189
    install_production(config['dist-clean'])
214
 
    ini = update_or_install_ini(mongo_url)
215
 
    install_cronjob(config['cron-interval'], config['error-email'])
 
190
    if mongo_url == '':
 
191
        hookenv.log('No mongodb set up.')
 
192
        return
 
193
    ini = install_ini(mongo_url)
 
194
    if config['lp-key'] == '':
 
195
        hookenv.log('Not setting up cron because lp-key not set.')
 
196
    else:
 
197
        install_cronjob(config['cron-interval'], config['error-email'])
216
198
    start()
217
199
    set_port(ini, True)
218
200
    update_website(ini)