~jamesbeedy/charms/trusty/nova-compute/next

« back to all changes in this revision

Viewing changes to hooks/nova_compute_utils.py

Finish up: new templates, ssh key creation, Makefile.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
from base64 import b64decode
5
5
from copy import deepcopy
6
 
from subprocess import check_call
 
6
from subprocess import check_call, check_output
7
7
 
8
8
from charmhelpers.core.hookenv import (
9
9
    config,
27
27
 
28
28
CA_CERT_PATH = '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt'
29
29
 
30
 
TEMPLATES='templates/'
 
30
TEMPLATES = 'templates/'
31
31
 
32
32
BASE_PACKAGES = [
33
33
    'nova-compute',
39
39
        'services': ['libvirt-bin'],
40
40
        'contexts': [],
41
41
    },
 
42
    '/etc/libvirt/libvirtd.conf': {
 
43
        'services': ['libvirt-bin'],
 
44
        'contexts': [NovaComputeLibvirtContext()],
 
45
    },
42
46
    '/etc/default/libvirt-bin': {
43
47
        'services': ['libvirt-bin'],
44
48
        'contexts': [NovaComputeLibvirtContext()],
129
133
 
130
134
    return resource_map
131
135
 
 
136
 
132
137
def restart_map():
133
138
    '''
134
139
    Constructs a restart map based on charm config settings and relation
136
141
    '''
137
142
    return {k: v['services'] for k, v in resource_map().iteritems()}
138
143
 
 
144
 
139
145
def register_configs():
140
146
    '''
141
147
    Returns an OSTemplateRenderer object with all required configs registered.
224
230
    except KeyError:
225
231
        return None
226
232
 
 
233
 
227
234
def public_ssh_key(user='root'):
228
235
    home = pwd.getpwnam(user).pw_dir
229
236
    try:
233
240
        return None
234
241
 
235
242
 
236
 
def initialize_ssh_keys():
237
 
    pass
 
243
def initialize_ssh_keys(user='root'):
 
244
    home_dir = pwd.getpwnam(user).pw_dir
 
245
    ssh_dir = os.path.join(home_dir, '.ssh')
 
246
    if not os.path.isdir(ssh_dir):
 
247
        os.mkdir(ssh_dir)
 
248
 
 
249
    priv_key = os.path.join(ssh_dir, 'id_rsa')
 
250
    if not os.path.isfile(priv_key):
 
251
        log('Generating new ssh key for user %s.' % user)
 
252
        cmd = ['ssh-keygen', '-q', '-N', '', '-t', 'rsa', '-b', '2048',
 
253
               '-f', priv_key]
 
254
        check_output(cmd)
 
255
 
 
256
    pub_key = '%s.pub' % priv_key
 
257
    if not os.path.isfile(pub_key):
 
258
        log('Generating missing ssh public key @ %s.' % pub_key)
 
259
        cmd = ['ssh-keygen', '-y', '-f', priv_key]
 
260
        p = check_output(cmd).strip()
 
261
        with open(pub_key, 'wb') as out:
 
262
            out.write(p)
 
263
    check_output(['chown', '-R', user, ssh_dir])
238
264
 
239
265
 
240
266
def import_authorized_keys(user='root'):
244
270
    # XXX: Should this be managed via templates + contexts?
245
271
    hosts = relation_get('known_hosts')
246
272
    auth_keys = relation_get('authorized_keys')
247
 
    if None in [hosts, auth_keys]:
 
273
    # XXX: Need to fix charm-helpers to return None for empty settings,
 
274
    #      in all cases.
 
275
    if not hosts or not auth_keys:
248
276
        return
249
277
 
250
278
    dest = os.path.join(pwd.getpwnam(user).pw_dir, '.ssh')
251
279
    log('Saving new known_hosts and authorized_keys file to: %s.' % dest)
252
280
 
253
 
    with open(os.path.join(dest, 'authorized_keys')) as _keys:
 
281
    with open(os.path.join(dest, 'authorized_keys'), 'wb') as _keys:
254
282
        _keys.write(b64decode(auth_keys))
255
 
    with open(os.path.join(dest, 'known_hosts')) as _hosts:
 
283
    with open(os.path.join(dest, 'known_hosts'), 'wb') as _hosts:
256
284
        _hosts.write(b64decode(hosts))
257
285
 
 
286
 
258
287
def configure_live_migration(configs=None):
259
288
    """
260
289
    Ensure libvirt live migration is properly configured or disabled,