~hopem/charms/trusty/nova-compute/fix-config-restart-svc-race

« back to all changes in this revision

Viewing changes to hooks/nova_compute_utils.py

  • Committer: james.page at ubuntu
  • Date: 2015-04-23 13:42:35 UTC
  • mfrom: (126.1.4 nova-compute)
  • Revision ID: james.page@ubuntu.com-20150423134235-nbzrw5r6zgtdwe1n
[zulcss,james-page,r=gnuoy] Add support for lxd hypervisor type on vivid.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
from base64 import b64decode
6
6
from copy import deepcopy
7
 
from subprocess import check_call, check_output
 
7
from subprocess import check_call, check_output, CalledProcessError
8
8
 
9
9
from charmhelpers.fetch import (
10
10
    apt_update,
34
34
)
35
35
 
36
36
from charmhelpers.core.templating import render
 
37
from charmhelpers.core.decorators import retry_on_exception
37
38
from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
38
39
from charmhelpers.contrib.openstack import templating, context
39
40
from charmhelpers.contrib.openstack.alternatives import install_alternative
122
123
    'quantum-server',
123
124
]
124
125
 
 
126
DEFAULT_INSTANCE_PATH = '/var/lib/nova/instances'
125
127
NOVA_CONF_DIR = "/etc/nova"
126
128
QEMU_CONF = '/etc/libvirt/qemu.conf'
127
129
LIBVIRTD_CONF = '/etc/libvirt/libvirtd.conf'
130
132
NOVA_CONF = '%s/nova.conf' % NOVA_CONF_DIR
131
133
 
132
134
BASE_RESOURCE_MAP = {
133
 
    QEMU_CONF: {
134
 
        'services': ['libvirt-bin'],
135
 
        'contexts': [],
136
 
    },
137
 
    LIBVIRTD_CONF: {
138
 
        'services': ['libvirt-bin'],
139
 
        'contexts': [NovaComputeLibvirtContext()],
140
 
    },
141
 
    LIBVIRT_BIN: {
142
 
        'services': ['libvirt-bin'],
143
 
        'contexts': [NovaComputeLibvirtContext()],
144
 
    },
145
 
    LIBVIRT_BIN_OVERRIDES: {
146
 
        'services': ['libvirt-bin'],
147
 
        'contexts': [NovaComputeLibvirtOverrideContext()],
148
 
    },
149
135
    NOVA_CONF: {
150
136
        'services': ['nova-compute'],
151
137
        'contexts': [context.AMQPContext(ssl_dir=NOVA_CONF_DIR),
171
157
    },
172
158
}
173
159
 
 
160
LIBVIRT_RESOURCE_MAP = {
 
161
    QEMU_CONF: {
 
162
        'services': ['libvirt-bin'],
 
163
        'contexts': [],
 
164
    },
 
165
    LIBVIRTD_CONF: {
 
166
        'services': ['libvirt-bin'],
 
167
        'contexts': [NovaComputeLibvirtContext()],
 
168
    },
 
169
    LIBVIRT_BIN: {
 
170
        'services': ['libvirt-bin'],
 
171
        'contexts': [NovaComputeLibvirtContext()],
 
172
    },
 
173
    LIBVIRT_BIN_OVERRIDES: {
 
174
        'services': ['libvirt-bin'],
 
175
        'contexts': [NovaComputeLibvirtOverrideContext()],
 
176
    },
 
177
}
 
178
LIBVIRT_RESOURCE_MAP.update(BASE_RESOURCE_MAP)
 
179
 
174
180
CEPH_SECRET = '/etc/ceph/secret.xml'
175
181
 
176
182
CEPH_RESOURCES = {
212
218
    'xen': ['nova-compute-xen'],
213
219
    'uml': ['nova-compute-uml'],
214
220
    'lxc': ['nova-compute-lxc'],
 
221
    'lxd': ['nova-compute-lxd'],
215
222
}
216
223
 
217
224
# Maps virt-type config to a libvirt URI.
230
237
    hook execution.
231
238
    '''
232
239
    # TODO: Cache this on first call?
233
 
    resource_map = deepcopy(BASE_RESOURCE_MAP)
 
240
    if config('virt-type').lower() == 'lxd':
 
241
        resource_map = deepcopy(BASE_RESOURCE_MAP)
 
242
    else:
 
243
        resource_map = deepcopy(LIBVIRT_RESOURCE_MAP)
234
244
    net_manager = network_manager()
235
245
    plugin = neutron_plugin()
236
246
 
554
564
    check_call(cmd)
555
565
 
556
566
 
 
567
def configure_lxd(user='nova'):
 
568
    ''' Configure lxd use for nova user '''
 
569
    if lsb_release()['DISTRIB_CODENAME'].lower() < "vivid":
 
570
        raise Exception("LXD is not supported for Ubuntu "
 
571
                        "versions less than 15.04 (vivid)")
 
572
 
 
573
    configure_subuid(user='nova')
 
574
    configure_lxd_daemon(user='nova')
 
575
 
 
576
    service_restart('nova-compute')
 
577
 
 
578
 
 
579
def configure_lxd_daemon(user):
 
580
    add_user_to_group(user, 'lxd')
 
581
    service_restart('lxd')
 
582
    # NOTE(jamespage): Call list function to initialize cert
 
583
    lxc_list(user)
 
584
 
 
585
 
 
586
@retry_on_exception(5, base_delay=2, exc_type=CalledProcessError)
 
587
def lxc_list(user):
 
588
    cmd = ['sudo', '-u', user, 'lxc', 'list']
 
589
    check_call(cmd)
 
590
 
 
591
 
 
592
def configure_subuid(user):
 
593
    cmd = ['usermod', '-v', '100000-200000', '-w', '100000-200000', user]
 
594
    check_call(cmd)
 
595
 
 
596
 
557
597
def enable_shell(user):
558
598
    cmd = ['usermod', '-s', '/bin/bash', user]
559
599
    check_call(cmd)