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

« back to all changes in this revision

Viewing changes to hooks/hooks.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:
4
4
import socket
5
5
import os
6
6
import base64
7
 
import subprocess
8
7
 
9
8
from charmhelpers.core.hookenv import (
10
9
    Hooks, UnregisteredHookError, log, local_unit)
11
 
from charmhelpers.fetch import (
12
 
    apt_install, _run_apt_command, add_source, apt_update)
13
 
from charmhelpers.core.hookenv import config
14
10
from shutil import rmtree
15
11
from subprocess import CalledProcessError
16
12
 
29
25
hooks = Hooks()
30
26
 
31
27
 
32
 
@hooks.hook("install")
33
 
def install():
34
 
    charm_config = config()
35
 
    apt_install(["apt-transport-https", "wget"])
36
 
 
37
 
    origin = charm_config.get("origin")
38
 
    if origin == "distro":
39
 
        origin = None
40
 
 
41
 
    if origin is not None:
42
 
        add_apt_source(origin)
43
 
 
44
 
    apt_update()
45
 
    apt_install(["landscape-client"])
46
 
    data_path = charm_config.get("data-path")
47
 
    if not data_path:
48
 
        data_path = "/var/lib/landscape/client"
49
 
 
50
 
    result = subprocess.call(["landscape-config", "--init", "-d", data_path])
51
 
    if result != 0:
52
 
        result = subprocess.check_call([
53
 
            "install", "-o", "landscape", "-g", "root", "-m", "755", "-d",
54
 
            data_path])
55
 
 
56
 
 
57
28
@hooks.hook("registration-relation-joined", "registration-relation-changed")
58
29
def registration_relation(juju_broker=JUJU_BROKER,
59
30
                          landscape_broker=LANDSCAPE_BROKER):
230
201
    return 0
231
202
 
232
203
 
233
 
def build_from_launchpad(url):
234
 
    """The charm will install the code from the passed lp branch.
235
 
    """
236
 
    apt_install(["devscripts", "bzr", "pbuilder"], fatal=True)
237
 
    subprocess.check_call(["rm", "-rf", "landscape-client-source"])
238
 
    subprocess.check_call(["bzr", "branch", url, "landscape-client-source"])
239
 
    os.chdir("landscape-client-source")
240
 
    subprocess.check_call("/usr/lib/pbuilder/pbuilder-satisfydepends")
241
 
    env = {"DEBUILD_OPTS": "-uc -us"}
242
 
    subprocess.check_call(["make", "package"], env=env)
243
 
    #TODO: The following call should be retried (potential race condition to
244
 
    # acquire the dpkg lock).
245
 
    subprocess.call(["dpkg", "-i", "../landscape-client_*.deb",
246
 
                     "../landscape-common_*.deb"])
247
 
    # The _run_apt_command will ensure the command is retried in case we cannot
248
 
    # acquire the lock for some reason.
249
 
    _run_apt_command(["apt-get", "-f", "install"], fatal=True)
250
 
    os.chdir("..")
251
 
 
252
 
 
253
 
def add_apt_source(url):
254
 
    """Add an apt source entry, with passed in key.
255
 
 
256
 
    This is a thin wrapper over the charmhelper "add_source" method to allow
257
 
    for the URLs to contain the key explicitely, like:
258
 
        - https://blah/blah|KEY
259
 
        - deb https://blah/blah|KEY
260
 
        - ppa:landscape/blah
261
 
    """
262
 
    key = None
263
 
    if url.startswith("lp"):
264
 
        return build_from_launchpad(url)
265
 
 
266
 
    if "|" in url:
267
 
        # We have a particular key specified. Url = url without the key now.
268
 
        url, key = url.split("|", 1)  # Maximum of 1 split.
269
 
        if key == "":
270
 
            log("Archive key for '%s' is empty.", level="WARNING")
271
 
 
272
 
    return add_source(url, key)
273
 
 
274
 
 
275
204
def _write_certificate(certificate, filename):
276
205
    """
277
206
    @param certificate Text of the certificate, base64 encoded.