~brad-marshall/charms/trusty/apache2-wsgi/fix-haproxy-relations

« back to all changes in this revision

Viewing changes to hooks/lib/tasks.py

  • Committer: Robin Winslow
  • Date: 2014-05-27 14:00:44 UTC
  • Revision ID: robin.winslow@canonical.com-20140527140044-8rpmb3wx4djzwa83
Add all files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import os
 
4
from datetime import datetime
 
5
from base64 import b64decode
 
6
 
 
7
import lib.sh
 
8
from lib.charmhelpers.core.hookenv import config
 
9
 
 
10
 
 
11
charm_dir = os.path.abspath(
 
12
    os.path.join(os.path.dirname(__file__), os.pardir)
 
13
)
 
14
 
 
15
 
 
16
def install_apt_packages():
 
17
    """
 
18
    Install both core and extra apt packages
 
19
    """
 
20
 
 
21
    install(config('core_packages').split())
 
22
    install(config('extra_packages').split())
 
23
 
 
24
 
 
25
def extract_app_files():
 
26
    """
 
27
    Extract the app zip file
 
28
    into an install directory (specified in config)
 
29
    """
 
30
 
 
31
    app_tgz_path = get_app_tgz()
 
32
 
 
33
    install_dir = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
 
34
    install_path = os.path.join(config('install_path'), install_dir)
 
35
 
 
36
    # Extract files into install dir
 
37
    sh.tar(
 
38
        file=app_tgz_path,
 
39
        directory=install_path,
 
40
        strip=1, z=True, x=True
 
41
    )
 
42
 
 
43
    return install_path
 
44
 
 
45
 
 
46
def set_current(app_path):
 
47
    """
 
48
    Set an app directory to the currently live app
 
49
    by creating a symlink as specified in config
 
50
    """
 
51
 
 
52
    current_link_path = os.path.join(
 
53
        config('install_path'),
 
54
        config('live_symlink_name')
 
55
    )
 
56
 
 
57
    sh.rm(current_link_path, force=True)
 
58
    sh.ln(app_path, current_link_path, symbolic=True)
 
59
 
 
60
 
 
61
def get_app_tgz():
 
62
    """
 
63
    Get the app tar.gz file from the config (if specified)
 
64
    and save it in the cache directory
 
65
    for later extraction
 
66
    """
 
67
 
 
68
    app_tgz_path = os.path.join(cache_dir(), 'project.tgz')
 
69
    b64_tgz = config('app_package')
 
70
 
 
71
    if b64_tgz:
 
72
        tgz_contents = b64decode()
 
73
 
 
74
        with open(app_tgz_path, 'w') as tgz_file:
 
75
            tgz_file.write(tgz_contents)
 
76
 
 
77
    return app_tgz_path
 
78
 
 
79
 
 
80
def install_dependencies(app_path):
 
81
    """
 
82
    Install pip and gem dependencies
 
83
    for the app
 
84
    """
 
85
 
 
86
    pip_dependencies_path = 
 
87
 
 
88
 
 
89
def install(packages):
 
90
    """
 
91
    Install a list of packages
 
92
    if the list contains any
 
93
    """
 
94
 
 
95
    if packages:
 
96
        sh.apt_get.install(packages, y=True)
 
97
 
 
98
 
 
99
def cache_dir():
 
100
    """
 
101
    Create the cache directory, if it doesn't exist
 
102
    and return its path
 
103
    """
 
104
 
 
105
    cache_dir = os.path.join(charm_dir, 'cache')
 
106
    sh.mv(cache_dir, p=True)
 
107
 
 
108
    return cache_dir