~canonical-ci-engineering/charms/trusty/core-image-publisher/trunk

« back to all changes in this revision

Viewing changes to hooks/actions.py

  • Committer: Celso Providelo
  • Date: 2015-03-25 04:13:43 UTC
  • Revision ID: celso.providelo@canonical.com-20150325041343-jw05jaz6jscs3c8f
fork of core-image-watcher

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import base64
 
2
import os
 
3
import shutil
 
4
import subprocess
 
5
 
 
6
from charmhelpers import fetch
 
7
from charmhelpers.core import hookenv
 
8
from charmhelpers.core.host import adduser
 
9
from charmhelpers.payload import (archive, execd)
 
10
 
 
11
SERVICE_NAME = 'core-image-publisher'
 
12
SERVICE_CONFIGNAME = 'core-service.conf'
 
13
REQUIRED_PACKAGES = [
 
14
    'autopkgtest', 'python-novaclient', 'python-neutronclient',
 
15
    'python-virtualenv', 'python3-dev']
 
16
 
 
17
config = hookenv.config()
 
18
 
 
19
 
 
20
def _service_dir():
 
21
    template = '/srv/{}/{}'
 
22
    return template.format(config['environment'], SERVICE_NAME)
 
23
 
 
24
 
 
25
def basenode(service_name):
 
26
    hookenv.log('Executing basenode')
 
27
    execd.execd_preinstall()
 
28
 
 
29
 
 
30
def log_start(service_name):
 
31
    hookenv.log('%s starting', SERVICE_NAME)
 
32
 
 
33
 
 
34
def install_packages(service_name):
 
35
    hookenv.log('Installing dependencies...')
 
36
    fetch.add_source('ppa:canonical-ci-engineering/ci-airline-phase-0')
 
37
    fetch.configure_sources(update=True)
 
38
    fetch.apt_install(REQUIRED_PACKAGES, fatal=True)
 
39
 
 
40
 
 
41
def install_service_tarball(service_name):
 
42
    files_dir = os.path.join(hookenv.charm_dir(), 'files')
 
43
    tarball = os.path.join(files_dir, '{}.tgz'.format(SERVICE_NAME))
 
44
    if not os.path.exists(_service_dir()):
 
45
        hookenv.log('Installing the code for the first time from tarball')
 
46
        archive.extract_tarfile(tarball, os.path.dirname(_service_dir()))
 
47
 
 
48
 
 
49
def update_config_file(service_name):
 
50
    hookenv.log(
 
51
        'Updating service configuration file: %s', SERVICE_CONFIGNAME)
 
52
    config_content = base64.b64decode(config['config-file'])
 
53
    config_path = os.path.join(_service_dir(), SERVICE_CONFIGNAME)
 
54
    with open(config_path, 'w') as f:
 
55
        f.write(config_content)
 
56
 
 
57
 
 
58
def create_user(service_name):
 
59
    username = 'core-worker'
 
60
    hookenv.log('Creating service user: %s', username)
 
61
    adduser(username)
 
62
 
 
63
 
 
64
def install_python_packages(service_name):
 
65
    env_dir = os.path.join(_service_dir(), 've')
 
66
    if os.path.exists(env_dir):
 
67
        hookenv.log('Service venv already exists, nothing to do ...')
 
68
        return
 
69
 
 
70
    hookenv.log('Installing python packages into the venv ...')
 
71
    subprocess.check_call(['virtualenv', '-p', 'python3', env_dir])
 
72
 
 
73
    pip_cache = os.path.join(hookenv.charm_dir(), 'files', 'pip-cache')
 
74
    requirements = os.path.join(_service_dir(), 'requirements.txt')
 
75
 
 
76
    hookenv.log('Installing from download cache.')
 
77
    subprocess.check_call(['%s/bin/pip' % env_dir,
 
78
                           'install',
 
79
                           '--no-index',
 
80
                           '--find-links={}'.format(pip_cache),
 
81
                           '-r', requirements])
 
82
 
 
83
    subprocess.check_call(['%s/bin/pip' % env_dir,
 
84
                           'install', '--no-deps', '-e', _service_dir()])