~fcorrea/charms/trusty/landscape-client/trunk

« back to all changes in this revision

Viewing changes to hooks/install.py

Merging lp:~tribaal/charms/trusty/landscape-client/put-install-hook-in-its-own-file [r=sparkiegeek, fcorrea]

This moves the install hook to its own file to prevent import problems.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
"""This is the install hook file.
 
3
 
 
4
It is its own file because the rest of the hooks import landscape classes and
 
5
functions, and landscape is not installed until this hook runs.
 
6
"""
 
7
 
 
8
import os
 
9
import subprocess
 
10
import sys
 
11
from charmhelpers.core.hookenv import (
 
12
    Hooks, UnregisteredHookError, log)
 
13
from charmhelpers.fetch import (
 
14
    apt_install, _run_apt_command, add_source, apt_update)
 
15
from charmhelpers.core.hookenv import config
 
16
 
 
17
 
 
18
hooks = Hooks()
 
19
 
 
20
 
 
21
@hooks.hook("install")
 
22
def install():
 
23
    charm_config = config()
 
24
    apt_install(["apt-transport-https", "wget"])
 
25
 
 
26
    origin = charm_config.get("origin")
 
27
    if origin == "distro":
 
28
        origin = None
 
29
 
 
30
    if origin is not None:
 
31
        add_apt_source(origin)
 
32
 
 
33
    apt_update()
 
34
    apt_install(["landscape-client"])
 
35
    data_path = charm_config.get("data-path")
 
36
    if not data_path:
 
37
        data_path = "/var/lib/landscape/client"
 
38
 
 
39
    result = subprocess.call(["landscape-config", "--init", "-d", data_path])
 
40
    if result != 0:
 
41
        result = subprocess.check_call([
 
42
            "install", "-o", "landscape", "-g", "root", "-m", "755", "-d",
 
43
            data_path])
 
44
 
 
45
def build_from_launchpad(url):
 
46
    """The charm will install the code from the passed lp branch.
 
47
    """
 
48
    apt_install(["devscripts", "bzr", "pbuilder"], fatal=True)
 
49
    subprocess.check_call(["rm", "-rf", "landscape-client-source"])
 
50
    subprocess.check_call(["bzr", "branch", url, "landscape-client-source"])
 
51
    os.chdir("landscape-client-source")
 
52
    subprocess.check_call("/usr/lib/pbuilder/pbuilder-satisfydepends")
 
53
    env = {"DEBUILD_OPTS": "-uc -us"}
 
54
    subprocess.check_call(["make", "package"], env=env)
 
55
    #TODO: The following call should be retried (potential race condition to
 
56
    # acquire the dpkg lock).
 
57
    subprocess.call(["dpkg", "-i", "../landscape-client_*.deb",
 
58
                     "../landscape-common_*.deb"])
 
59
    # The _run_apt_command will ensure the command is retried in case we cannot
 
60
    # acquire the lock for some reason.
 
61
    _run_apt_command(["apt-get", "-f", "install"], fatal=True)
 
62
    os.chdir("..")
 
63
 
 
64
 
 
65
def add_apt_source(url):
 
66
    """Add an apt source entry, with passed in key.
 
67
 
 
68
    This is a thin wrapper over the charmhelper "add_source" method to allow
 
69
    for the URLs to contain the key explicitely, like:
 
70
        - https://blah/blah|KEY
 
71
        - deb https://blah/blah|KEY
 
72
        - ppa:landscape/blah
 
73
    """
 
74
    key = None
 
75
    if url.startswith("lp"):
 
76
        return build_from_launchpad(url)
 
77
 
 
78
    if "|" in url:
 
79
        # We have a particular key specified. Url = url without the key now.
 
80
        url, key = url.split("|", 1)  # Maximum of 1 split.
 
81
        if key == "":
 
82
            log("Archive key for '%s' is empty.", level="WARNING")
 
83
 
 
84
    return add_source(url, key)
 
85
 
 
86
if __name__ == '__main__':
 
87
    try:
 
88
        sys.exit(hooks.execute(sys.argv))
 
89
    except UnregisteredHookError as e:
 
90
        log('Unknown hook {} - skipping.'.format(e))