~niedbalski/charms/trusty/swift-proxy/fix-lp-1308557

« back to all changes in this revision

Viewing changes to hooks/lib/openstack_common.py

  • Committer: James Page
  • Date: 2013-06-03 16:50:28 UTC
  • mfrom: (35.1.18 swift-proxy)
  • Revision ID: james.page@canonical.com-20130603165028-f9ya6llay65w3t9e
* Python rewrite

* Support for manually assigned storage zones

* Support for high availability via hacluster subordinate.

* Adds Grizzly compatibility.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
# Common python helper functions used for OpenStack charms.
4
4
 
 
5
import apt_pkg as apt
5
6
import subprocess
 
7
import os
6
8
 
7
9
CLOUD_ARCHIVE_URL = "http://ubuntu-cloud.archive.canonical.com/ubuntu"
8
10
CLOUD_ARCHIVE_KEY_ID = '5EDB1B62EC4926EA'
11
13
    'oneiric': 'diablo',
12
14
    'precise': 'essex',
13
15
    'quantal': 'folsom',
14
 
    'raring' : 'grizzly'
 
16
    'raring': 'grizzly',
15
17
}
16
18
 
17
19
 
19
21
    '2011.2': 'diablo',
20
22
    '2012.1': 'essex',
21
23
    '2012.2': 'folsom',
22
 
    '2013.1': 'grizzly'
 
24
    '2013.1': 'grizzly',
 
25
    '2013.2': 'havana',
23
26
}
24
27
 
25
28
# The ugly duckling
27
30
    '1.4.3': 'diablo',
28
31
    '1.4.8': 'essex',
29
32
    '1.7.4': 'folsom',
30
 
    '1.8.0': 'grizzly'
 
33
    '1.7.6': 'grizzly',
 
34
    '1.7.7': 'grizzly',
 
35
    '1.8.0': 'grizzly',
31
36
}
32
37
 
 
38
 
33
39
def juju_log(msg):
34
40
    subprocess.check_call(['juju-log', msg])
35
41
 
68
74
        ca_rel = ca_rel.split('%s-' % ubuntu_rel)[1].split('/')[0]
69
75
        return ca_rel
70
76
 
71
 
    # Best guess match based on deb or ppa provided strings
 
77
    # Best guess match based on deb string provided
72
78
    if src.startswith('deb') or src.startswith('ppa'):
73
79
        for k, v in openstack_codenames.iteritems():
74
80
            if v in src:
75
81
                return v
76
82
 
 
83
 
77
84
def get_os_codename_version(vers):
78
85
    '''Determine OpenStack codename from version number.'''
79
86
    try:
95
102
 
96
103
def get_os_codename_package(pkg):
97
104
    '''Derive OpenStack release codename from an installed package.'''
98
 
    cmd = ['dpkg', '-l', pkg]
99
 
 
 
105
    apt.init()
 
106
    cache = apt.Cache()
100
107
    try:
101
 
        output = subprocess.check_output(cmd)
102
 
    except subprocess.CalledProcessError:
103
 
        e = 'Could not derive OpenStack version from package that is not '\
104
 
            'installed; %s' % pkg
105
 
        error_out(e)
106
 
 
107
 
    def _clean(line):
108
 
        line = line.split(' ')
109
 
        clean = []
110
 
        for c in line:
111
 
            if c != '':
112
 
                clean.append(c)
113
 
        return clean
114
 
 
115
 
    vers = None
116
 
    for l in output.split('\n'):
117
 
        if l.startswith('ii'):
118
 
            l = _clean(l)
119
 
            if l[1] == pkg:
120
 
                vers = l[2]
121
 
 
122
 
    if not vers:
 
108
        pkg = cache[pkg]
 
109
    except:
123
110
        e = 'Could not determine version of installed package: %s' % pkg
124
111
        error_out(e)
125
112
 
126
 
    vers = vers[:6]
 
113
    vers = apt.UpstreamVersion(pkg.current_ver.ver_str)
 
114
 
127
115
    try:
128
 
        if 'swift' in pkg:
 
116
        if 'swift' in pkg.name:
129
117
            vers = vers[:5]
130
118
            return swift_codenames[vers]
131
119
        else:
151
139
    e = "Could not determine OpenStack version for package: %s" % pkg
152
140
    error_out(e)
153
141
 
 
142
 
154
143
def configure_installation_source(rel):
155
144
    '''Configure apt installation source.'''
156
145
 
157
 
    def _import_key(id):
 
146
    def _import_key(keyid):
158
147
        cmd = "apt-key adv --keyserver keyserver.ubuntu.com " \
159
 
              "--recv-keys %s" % id
 
148
              "--recv-keys %s" % keyid
160
149
        try:
161
150
            subprocess.check_call(cmd.split(' '))
162
 
        except:
163
 
            error_out("Error importing repo key %s" % id)
 
151
        except subprocess.CalledProcessError:
 
152
            error_out("Error importing repo key %s" % keyid)
164
153
 
165
154
    if rel == 'distro':
166
155
        return
169
158
        subprocess.check_call(["add-apt-repository", "-y", src])
170
159
    elif rel[:3] == "deb":
171
160
        l = len(rel.split('|'))
172
 
        if l ==  2:
 
161
        if l == 2:
173
162
            src, key = rel.split('|')
174
163
            juju_log("Importing PPA key from keyserver for %s" % src)
175
164
            _import_key(key)
222
211
            f.write(src)
223
212
    else:
224
213
        error_out("Invalid openstack-release specified: %s" % rel)
 
214
 
 
215
 
 
216
def save_script_rc(script_path="scripts/scriptrc", **env_vars):
 
217
    """
 
218
    Write an rc file in the charm-delivered directory containing
 
219
    exported environment variables provided by env_vars. Any charm scripts run
 
220
    outside the juju hook environment can source this scriptrc to obtain
 
221
    updated config information necessary to perform health checks or
 
222
    service changes.
 
223
    """
 
224
    charm_dir = os.getenv('CHARM_DIR')
 
225
    juju_rc_path = "%s/%s" % (charm_dir, script_path)
 
226
    with open(juju_rc_path, 'wb') as rc_script:
 
227
        rc_script.write(
 
228
            "#!/bin/bash\n")
 
229
        [rc_script.write('export %s=%s\n' % (u, p))
 
230
         for u, p in env_vars.iteritems() if u != "script_path"]