~cprov/tanuki-continuous-deployer/local-provider

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
#
# Copyright (C) 2015 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""CI Automation: mojo spec deployer."""


import argparse
import configparser
import os
import sys


from ci_automation import (
    branch,
    config,
    juju,
    mojo,
    utils,
)


SERIES = 'trusty'


def fix_permissions(project):
    ''' "Why do I get permission denied?" from
    https://mojo.canonical.com/readme.html'''

    utils.check_call(['sudo', 'chmod', '755', '/var/lib/lxc'])
    path = '{}.{}'.format(project, SERIES)
    path = os.path.join('/var/lib/lxc', path)
    utils.check_call(['sudo', 'chmod', '755', path])


def main():
    desc = 'Deploy a given mojo spec in isolation.'
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument('--project', required=True)
    parser.add_argument('--stage', required=True)
    parser.add_argument('--branch', required=True)
    parser.add_argument('--devel', action="store_true", default=False)
    parser.add_argument('--base')
    parser.add_argument('--network')
    parser.add_argument('--identifier', required=True)
    args = parser.parse_args()

    base = args.base or '/srv/juju-environments'

    if not os.path.exists(base):
        print('Please create {}.'.format(base))
        return 1

    if not mojo.project_exists(args.project):
        msg = 'Please run `sudo mojo project-new --series trusty {}`.'
        msg = msg.format(args.project)
        print(msg)
        return 1

    # Le sigh.
    if args.devel:
        fix_permissions(args.project)

    # If there's config on disk, copy it to the correct location.
    if config.deployment_has_config(args.stage):
        config_target = os.path.join('/srv/mojo/LOCAL/', args.project)
        config.export_latest_config_for_deployment(args.stage, config_target)

    # Relies on the canonical-mojo-specs layout, e.g.:
    # ue/mojo-ue-adt-cloud-worker/devel
    spec_name = args.stage.split('/')[1]
    env_name = spec_name

    try:
        mojo.new_workspace(args.project, args.stage, args.branch, args.identifier)
        env_dir = os.path.join(args.base, env_name)
        if not os.path.exists(env_dir):
            print("Creating juju environment for first-time deploy.")
            juju.new_environment(
                env_name,
                base,
                devel=args.devel,
                network=args.network
            )
        mojo.deploy(args.project, args.stage, args.branch, env_dir, args.identifier)
    except utils.BetterCalledProcessError as err:
        print(err)
        return 1

    print('Deployed environment: {}'.format(env_dir))

    return 0


if __name__ == '__main__':
    sys.exit(main())