~joetalbott/charms/trusty/snappy-proposed-image-builder/add_shutil_import

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import base64
import os
import shutil
import subprocess

from charmhelpers import fetch
from charmhelpers.core import hookenv
from charmhelpers.core.host import adduser
from charmhelpers.payload import (archive, execd)

SERVICE_NAME = 'snappy-proposed-image-builder'
SERVICE_CONFIGNAME = 'core-service.conf'
REQUIRED_PACKAGES = [
    'python-virtualenv', 'python3-dev',  'libssl-dev', 'libffi-dev',
    'qemu-utils', 'ubuntu-device-flash', 'gcc',
]

config = hookenv.config()


def _service_dir():
    template = '/srv/{}/{}'
    return template.format(config['environment'], SERVICE_NAME)


def basenode(service_name):
    hookenv.log('Executing basenode')
    execd.execd_preinstall()


def log_start(service_name):
    hookenv.log('%s starting', SERVICE_NAME)


def install_packages(service_name):
    hookenv.log('Installing dependencies...')
    fetch.add_source('ppa:canonical-ci-engineering/u-services')
    fetch.configure_sources(update=True)
    fetch.apt_install(REQUIRED_PACKAGES, fatal=True)


def install_service_tarball(service_name):
    files_dir = os.path.join(hookenv.charm_dir(), 'files')
    tarball = os.path.join(files_dir, '{}.tgz'.format(SERVICE_NAME))
    if not os.path.exists(_service_dir()):
        hookenv.log('Installing the code for the first time from tarball')
        archive.extract_tarfile(tarball, os.path.dirname(_service_dir()))


def install_ubuntu_device_flash(service_name):
    files_dir = os.path.join(hookenv.charm_dir(), 'files')
    shutil.copy(os.path.join(files_dir, 'ubuntu-device-flash'),
                _service_dir())


def update_config_file(service_name):
    hookenv.log(
        'Updating service configuration file: %s', SERVICE_CONFIGNAME)
    config_content = base64.b64decode(config['config-file'])
    config_path = os.path.join(_service_dir(), SERVICE_CONFIGNAME)
    with open(config_path, 'w') as f:
        f.write(config_content)


def create_user(service_name):
    username = 'core-worker'
    hookenv.log('Creating service user: %s', username)
    adduser(username)


def install_python_packages(service_name):
    env_dir = os.path.join(_service_dir(), 've')
    if os.path.exists(env_dir):
        hookenv.log('Service venv already exists, nothing to do ...')
        return

    hookenv.log('Installing python packages into the venv ...')
    subprocess.check_call(['virtualenv', '-p', 'python3', env_dir])

    pip_cache = os.path.join(hookenv.charm_dir(), 'files', 'pip-cache')
    requirements = os.path.join(_service_dir(), 'requirements.txt')

    hookenv.log('Installing from download cache.')
    subprocess.check_call(['%s/bin/pip' % env_dir,
                           'install',
                           '--no-index',
                           '--find-links={}'.format(pip_cache),
                           '-r', requirements])

    subprocess.check_call(['%s/bin/pip' % env_dir,
                           'install', '--no-deps', '-e', _service_dir()])