2013-08-01 20:42:16 -07:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import ConfigParser
|
|
|
|
|
|
|
|
from base64 import b64encode
|
|
|
|
from collections import OrderedDict
|
|
|
|
from copy import deepcopy
|
|
|
|
|
2013-08-12 13:53:00 -07:00
|
|
|
from charmhelpers.contrib.openstack import context, templating
|
2013-08-14 09:02:36 -07:00
|
|
|
from charmhelpers.contrib.openstack.neutron import (
|
|
|
|
network_manager, neutron_plugin_attribute)
|
2013-08-01 20:42:16 -07:00
|
|
|
|
2013-08-19 12:16:37 -07:00
|
|
|
from charmhelpers.contrib.hahelpers.cluster import eligible_leader
|
|
|
|
|
2013-08-01 20:42:16 -07:00
|
|
|
from charmhelpers.contrib.openstack.utils import (
|
2013-08-19 12:16:37 -07:00
|
|
|
configure_installation_source,
|
2013-09-05 16:53:31 -07:00
|
|
|
get_host_ip,
|
2013-09-05 18:39:48 -07:00
|
|
|
get_hostname,
|
2013-08-19 12:16:37 -07:00
|
|
|
get_os_codename_install_source,
|
2013-09-05 16:53:31 -07:00
|
|
|
is_ip,
|
2013-08-12 13:53:00 -07:00
|
|
|
os_release,
|
2013-08-12 10:44:10 -07:00
|
|
|
save_script_rc as _save_script_rc)
|
|
|
|
|
2013-09-20 17:29:50 +01:00
|
|
|
from charmhelpers.fetch import (
|
2013-08-19 12:16:37 -07:00
|
|
|
apt_install,
|
|
|
|
apt_update,
|
|
|
|
)
|
2013-08-01 20:42:16 -07:00
|
|
|
|
|
|
|
from charmhelpers.core.hookenv import (
|
|
|
|
config,
|
2013-08-10 09:49:47 +01:00
|
|
|
log,
|
2013-09-05 16:53:31 -07:00
|
|
|
relation_get,
|
2013-08-01 20:42:16 -07:00
|
|
|
relation_ids,
|
2013-08-10 09:49:47 +01:00
|
|
|
remote_unit,
|
2013-08-12 13:53:00 -07:00
|
|
|
INFO,
|
2013-08-10 09:49:47 +01:00
|
|
|
ERROR,
|
2013-08-01 20:42:16 -07:00
|
|
|
)
|
|
|
|
|
2013-08-10 14:35:17 +01:00
|
|
|
|
2013-08-01 20:42:16 -07:00
|
|
|
import nova_cc_context
|
|
|
|
|
|
|
|
TEMPLATES = 'templates/'
|
|
|
|
|
|
|
|
CLUSTER_RES = 'res_nova_vip'
|
|
|
|
|
|
|
|
# removed from original: python-mysqldb python-keystone charm-helper-sh
|
|
|
|
BASE_PACKAGES = [
|
|
|
|
'apache2',
|
|
|
|
'haproxy',
|
2013-08-19 15:05:54 -07:00
|
|
|
'python-keystoneclient',
|
2013-08-01 20:42:16 -07:00
|
|
|
'uuid',
|
|
|
|
]
|
|
|
|
|
|
|
|
BASE_SERVICES = [
|
|
|
|
'nova-api-ec2',
|
|
|
|
'nova-api-os-compute',
|
|
|
|
'nova-objectstore',
|
|
|
|
'nova-cert',
|
|
|
|
'nova-scheduler',
|
|
|
|
]
|
|
|
|
|
|
|
|
API_PORTS = {
|
|
|
|
'nova-api-ec2': 8773,
|
|
|
|
'nova-api-os-compute': 8774,
|
|
|
|
'nova-api-os-volume': 8776,
|
|
|
|
'nova-objectstore': 3333,
|
2013-08-10 14:35:17 +01:00
|
|
|
'neutron-server': 9696,
|
2013-08-01 20:42:16 -07:00
|
|
|
'quantum-server': 9696,
|
|
|
|
}
|
|
|
|
|
2013-09-25 14:10:26 +01:00
|
|
|
NOVA_CONF = '/etc/nova/nova.conf'
|
|
|
|
NOVA_API_PASTE = '/etc/nova/api-paste.ini'
|
|
|
|
QUANTUM_CONF = '/etc/quantum/quantum.conf'
|
|
|
|
QUANTUM_API_PASTE = '/etc/quantum/api-paste.ini'
|
|
|
|
NEUTRON_CONF = '/etc/neutron/neutron.conf'
|
|
|
|
HAPROXY_CONF = '/etc/haproxy/haproxy.cfg'
|
|
|
|
APACHE_CONF = '/etc/apache2/sites-available/openstack_https_frontend'
|
2013-09-25 17:21:41 +01:00
|
|
|
APACHE_24_CONF = '/etc/apache2/sites-available/openstack_https_frontend.conf'
|
2013-10-16 12:54:16 +01:00
|
|
|
NEUTRON_DEFAULT = '/etc/default/neutron-server'
|
|
|
|
QUANTUM_DEFAULT = '/etc/default/quantum-server'
|
2013-09-25 14:10:26 +01:00
|
|
|
|
2013-08-01 20:42:16 -07:00
|
|
|
BASE_RESOURCE_MAP = OrderedDict([
|
2013-09-25 14:10:26 +01:00
|
|
|
(NOVA_CONF, {
|
2013-08-01 20:42:16 -07:00
|
|
|
'services': BASE_SERVICES,
|
|
|
|
'contexts': [context.AMQPContext(),
|
2013-08-12 13:53:00 -07:00
|
|
|
context.SharedDBContext(relation_prefix='nova'),
|
2013-08-01 20:42:16 -07:00
|
|
|
context.ImageServiceContext(),
|
2013-08-14 09:14:51 -07:00
|
|
|
context.OSConfigFlagContext(),
|
2013-10-08 17:51:27 -07:00
|
|
|
context.SubordinateConfigContext(
|
|
|
|
interface='nova-vmware',
|
|
|
|
service='nova',
|
|
|
|
config_file=NOVA_CONF,
|
|
|
|
),
|
2013-08-14 09:02:36 -07:00
|
|
|
nova_cc_context.HAProxyContext(),
|
2013-08-15 15:47:37 -07:00
|
|
|
nova_cc_context.IdentityServiceContext(),
|
2013-09-04 17:09:30 +01:00
|
|
|
nova_cc_context.VolumeServiceContext(),
|
|
|
|
nova_cc_context.NeutronCCContext()],
|
2013-08-01 20:42:16 -07:00
|
|
|
}),
|
2013-09-25 14:10:26 +01:00
|
|
|
(NOVA_API_PASTE, {
|
2013-08-01 20:42:16 -07:00
|
|
|
'services': [s for s in BASE_SERVICES if 'api' in s],
|
2013-08-15 15:47:37 -07:00
|
|
|
'contexts': [nova_cc_context.IdentityServiceContext()],
|
2013-08-01 20:42:16 -07:00
|
|
|
}),
|
2013-09-25 14:10:26 +01:00
|
|
|
(QUANTUM_CONF, {
|
2013-08-01 20:42:16 -07:00
|
|
|
'services': ['quantum-server'],
|
2013-08-10 18:35:37 +01:00
|
|
|
'contexts': [context.AMQPContext(),
|
|
|
|
nova_cc_context.HAProxyContext(),
|
2013-08-15 15:47:37 -07:00
|
|
|
nova_cc_context.IdentityServiceContext(),
|
2013-09-25 12:12:16 +01:00
|
|
|
nova_cc_context.NeutronCCContext()],
|
2013-08-01 20:42:16 -07:00
|
|
|
}),
|
2013-10-16 12:54:16 +01:00
|
|
|
(QUANTUM_DEFAULT, {
|
|
|
|
'services': ['quantum-server'],
|
|
|
|
'contexts': [nova_cc_context.NeutronCCContext()],
|
|
|
|
}),
|
2013-09-25 14:10:26 +01:00
|
|
|
(QUANTUM_API_PASTE, {
|
2013-08-01 20:42:16 -07:00
|
|
|
'services': ['quantum-server'],
|
2013-08-15 15:47:37 -07:00
|
|
|
'contexts': [nova_cc_context.IdentityServiceContext()],
|
2013-08-01 20:42:16 -07:00
|
|
|
}),
|
2013-09-25 14:10:26 +01:00
|
|
|
(NEUTRON_CONF, {
|
2013-08-10 14:35:17 +01:00
|
|
|
'services': ['neutron-server'],
|
2013-08-10 18:35:37 +01:00
|
|
|
'contexts': [context.AMQPContext(),
|
2013-08-15 15:47:37 -07:00
|
|
|
nova_cc_context.IdentityServiceContext(),
|
2013-08-12 13:53:00 -07:00
|
|
|
nova_cc_context.NeutronCCContext(),
|
2013-09-25 12:12:16 +01:00
|
|
|
nova_cc_context.HAProxyContext()],
|
2013-08-10 14:35:17 +01:00
|
|
|
}),
|
2013-10-16 12:54:16 +01:00
|
|
|
(NEUTRON_DEFAULT, {
|
|
|
|
'services': ['neutron-server'],
|
|
|
|
'contexts': [nova_cc_context.NeutronCCContext()],
|
|
|
|
}),
|
2013-09-25 14:10:26 +01:00
|
|
|
(HAPROXY_CONF, {
|
2013-08-01 20:42:16 -07:00
|
|
|
'contexts': [context.HAProxyContext(),
|
|
|
|
nova_cc_context.HAProxyContext()],
|
|
|
|
'services': ['haproxy'],
|
|
|
|
}),
|
2013-09-25 14:10:26 +01:00
|
|
|
(APACHE_CONF, {
|
2013-09-25 17:21:41 +01:00
|
|
|
'contexts': [nova_cc_context.ApacheSSLContext()],
|
|
|
|
'services': ['apache2'],
|
|
|
|
}),
|
|
|
|
(APACHE_24_CONF, {
|
2013-08-01 20:42:16 -07:00
|
|
|
'contexts': [nova_cc_context.ApacheSSLContext()],
|
|
|
|
'services': ['apache2'],
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
|
|
|
|
CA_CERT_PATH = '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt'
|
|
|
|
|
2013-08-10 09:49:47 +01:00
|
|
|
NOVA_SSH_DIR = '/etc/nova/compute_ssh/'
|
|
|
|
|
2013-08-01 20:42:16 -07:00
|
|
|
|
|
|
|
def resource_map():
|
|
|
|
'''
|
|
|
|
Dynamically generate a map of resources that will be managed for a single
|
|
|
|
hook execution.
|
|
|
|
'''
|
|
|
|
resource_map = deepcopy(BASE_RESOURCE_MAP)
|
|
|
|
|
|
|
|
if relation_ids('nova-volume-service'):
|
|
|
|
# if we have a relation to a nova-volume service, we're
|
|
|
|
# also managing the nova-volume API endpoint (legacy)
|
|
|
|
resource_map['/etc/nova/nova.conf']['services'].append(
|
|
|
|
'nova-api-os-volume')
|
|
|
|
|
2013-08-14 09:02:36 -07:00
|
|
|
net_manager = network_manager()
|
|
|
|
|
|
|
|
# pop out irrelevant resources from the OrderedDict (easier than adding
|
|
|
|
# them late)
|
|
|
|
if net_manager != 'quantum':
|
2013-08-01 20:42:16 -07:00
|
|
|
[resource_map.pop(k) for k in list(resource_map.iterkeys())
|
|
|
|
if 'quantum' in k]
|
2013-08-14 09:02:36 -07:00
|
|
|
if net_manager != 'neutron':
|
2013-08-10 14:35:17 +01:00
|
|
|
[resource_map.pop(k) for k in list(resource_map.iterkeys())
|
|
|
|
if 'neutron' in k]
|
|
|
|
|
2013-09-25 17:21:41 +01:00
|
|
|
if os.path.exists('/etc/apache2/conf-available'):
|
2013-09-25 17:24:36 +01:00
|
|
|
resource_map.pop(APACHE_CONF)
|
2013-09-25 17:21:41 +01:00
|
|
|
else:
|
2013-09-25 17:24:36 +01:00
|
|
|
resource_map.pop(APACHE_24_CONF)
|
2013-09-25 17:21:41 +01:00
|
|
|
|
2013-08-21 12:30:10 -07:00
|
|
|
# add neutron plugin requirements. nova-c-c only needs the neutron-server
|
|
|
|
# associated with configs, not the plugin agent.
|
2013-08-14 09:02:36 -07:00
|
|
|
if net_manager in ['quantum', 'neutron']:
|
|
|
|
plugin = neutron_plugin()
|
|
|
|
if plugin:
|
|
|
|
conf = neutron_plugin_attribute(plugin, 'config', net_manager)
|
|
|
|
ctxts = (neutron_plugin_attribute(plugin, 'contexts', net_manager)
|
|
|
|
or [])
|
2013-10-16 11:47:19 +01:00
|
|
|
services = neutron_plugin_attribute(plugin, 'server_services',
|
|
|
|
net_manager)
|
2013-08-14 09:02:36 -07:00
|
|
|
resource_map[conf] = {}
|
2013-10-16 11:47:19 +01:00
|
|
|
resource_map[conf]['services'] = services
|
2013-08-14 09:02:36 -07:00
|
|
|
resource_map[conf]['contexts'] = ctxts
|
|
|
|
resource_map[conf]['contexts'].append(
|
|
|
|
nova_cc_context.NeutronCCContext())
|
|
|
|
|
2013-08-15 12:49:57 -07:00
|
|
|
# nova-conductor for releases >= G.
|
|
|
|
if os_release('nova-common') not in ['essex', 'folsom']:
|
|
|
|
resource_map['/etc/nova/nova.conf']['services'] += ['nova-conductor']
|
2013-10-16 15:13:33 -07:00
|
|
|
|
|
|
|
# also manage any configs that are being updated by subordinates.
|
|
|
|
vmware_ctxt = context.SubordinateConfigContext(interface='nova-vmware',
|
|
|
|
service='nova',
|
|
|
|
config_file=NOVA_CONF)
|
|
|
|
vmware_ctxt = vmware_ctxt()
|
|
|
|
if vmware_ctxt and 'services' in vmware_ctxt:
|
|
|
|
for s in vmware_ctxt['services']:
|
|
|
|
if s not in resource_map[NOVA_CONF]['services']:
|
|
|
|
resource_map[NOVA_CONF]['services'].append(s)
|
2013-08-01 20:42:16 -07:00
|
|
|
return resource_map
|
|
|
|
|
|
|
|
|
|
|
|
def register_configs():
|
2013-08-12 13:53:00 -07:00
|
|
|
release = os_release('nova-common')
|
2013-08-01 20:42:16 -07:00
|
|
|
configs = templating.OSConfigRenderer(templates_dir=TEMPLATES,
|
|
|
|
openstack_release=release)
|
2013-08-12 10:44:10 -07:00
|
|
|
for cfg, rscs in resource_map().iteritems():
|
2013-08-01 20:42:16 -07:00
|
|
|
configs.register(cfg, rscs['contexts'])
|
|
|
|
return configs
|
|
|
|
|
|
|
|
|
|
|
|
def restart_map():
|
2013-08-19 15:36:36 -07:00
|
|
|
return OrderedDict([(cfg, v['services'])
|
|
|
|
for cfg, v in resource_map().iteritems()
|
|
|
|
if v['services']])
|
2013-08-01 20:42:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
def determine_ports():
|
|
|
|
'''Assemble a list of API ports for services we are managing'''
|
|
|
|
ports = []
|
|
|
|
for cfg, services in restart_map().iteritems():
|
|
|
|
for service in services:
|
|
|
|
try:
|
|
|
|
ports.append(API_PORTS[service])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2013-08-19 15:58:17 -07:00
|
|
|
return list(set(ports))
|
2013-08-01 20:42:16 -07:00
|
|
|
|
2013-08-10 14:35:17 +01:00
|
|
|
|
|
|
|
def api_port(service):
|
|
|
|
return API_PORTS[service]
|
|
|
|
|
2013-08-01 20:42:16 -07:00
|
|
|
|
|
|
|
def determine_packages():
|
|
|
|
# currently all packages match service names
|
|
|
|
packages = [] + BASE_PACKAGES
|
|
|
|
for k, v in resource_map().iteritems():
|
|
|
|
packages.extend(v['services'])
|
2013-10-16 12:47:26 +01:00
|
|
|
if network_manager() in ['neutron', 'quantum']:
|
|
|
|
pkgs = neutron_plugin_attribute(neutron_plugin(), 'server_packages',
|
|
|
|
network_manager())
|
2013-10-16 13:12:13 +01:00
|
|
|
packages.extend(pkgs)
|
2013-08-01 20:42:16 -07:00
|
|
|
return list(set(packages))
|
|
|
|
|
|
|
|
|
|
|
|
def save_script_rc():
|
|
|
|
env_vars = {
|
|
|
|
'OPENSTACK_PORT_MCASTPORT': config('ha-mcastport'),
|
|
|
|
'OPENSTACK_SERVICE_API_EC2': 'nova-api-ec2',
|
|
|
|
'OPENSTACK_SERVICE_API_OS_COMPUTE': 'nova-api-os-compute',
|
|
|
|
'OPENSTACK_SERVICE_CERT': 'nova-cert',
|
|
|
|
'OPENSTACK_SERVICE_CONDUCTOR': 'nova-conductor',
|
|
|
|
'OPENSTACK_SERVICE_OBJECTSTORE': 'nova-objectstore',
|
|
|
|
'OPENSTACK_SERVICE_SCHEDULER': 'nova-scheduler',
|
|
|
|
}
|
|
|
|
if relation_ids('nova-volume-service'):
|
|
|
|
env_vars['OPENSTACK_SERVICE_API_OS_VOL'] = 'nova-api-os-volume'
|
2013-08-10 14:35:17 +01:00
|
|
|
if network_manager() == 'quantum':
|
2013-08-01 20:42:16 -07:00
|
|
|
env_vars['OPENSTACK_SERVICE_API_QUANTUM'] = 'quantum-server'
|
2013-08-10 14:35:17 +01:00
|
|
|
if network_manager() == 'neutron':
|
|
|
|
env_vars['OPENSTACK_SERVICE_API_NEUTRON'] = 'neutron-server'
|
2013-08-01 20:42:16 -07:00
|
|
|
_save_script_rc(**env_vars)
|
|
|
|
|
|
|
|
|
2013-08-19 12:16:37 -07:00
|
|
|
def do_openstack_upgrade(configs):
|
|
|
|
new_src = config('openstack-origin')
|
|
|
|
new_os_rel = get_os_codename_install_source(new_src)
|
|
|
|
log('Performing OpenStack upgrade to %s.' % (new_os_rel))
|
|
|
|
|
|
|
|
configure_installation_source(new_src)
|
|
|
|
apt_update()
|
|
|
|
|
|
|
|
dpkg_opts = [
|
|
|
|
'--option', 'Dpkg::Options::=--force-confnew',
|
|
|
|
'--option', 'Dpkg::Options::=--force-confdef',
|
|
|
|
]
|
|
|
|
|
|
|
|
apt_install(packages=determine_packages(), options=dpkg_opts, fatal=True)
|
|
|
|
|
|
|
|
# set CONFIGS to load templates from new release and regenerate config
|
|
|
|
configs.set_release(openstack_release=new_os_rel)
|
|
|
|
configs.write_all()
|
|
|
|
|
|
|
|
if eligible_leader(CLUSTER_RES):
|
|
|
|
migrate_database()
|
2013-08-01 20:42:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
def volume_service():
|
|
|
|
'''Specifies correct volume API for specific OS release'''
|
2013-08-12 13:53:00 -07:00
|
|
|
os_vers = os_release('nova-common')
|
2013-08-01 20:42:16 -07:00
|
|
|
if os_vers == 'essex':
|
|
|
|
return 'nova-volume'
|
|
|
|
elif os_vers == 'folsom': # support both drivers in folsom.
|
|
|
|
if not relation_ids('cinder-volume-service'):
|
|
|
|
return 'nova-volume'
|
|
|
|
return 'cinder'
|
|
|
|
|
|
|
|
|
|
|
|
def migrate_database():
|
|
|
|
'''Runs nova-manage to initialize a new database or migrate existing'''
|
2013-08-14 09:02:36 -07:00
|
|
|
log('Migrating the nova database.', level=INFO)
|
2013-08-01 20:42:16 -07:00
|
|
|
cmd = ['nova-manage', 'db', 'sync']
|
2013-08-15 13:48:37 -07:00
|
|
|
subprocess.check_output(cmd)
|
2013-08-01 20:42:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
def auth_token_config(setting):
|
|
|
|
'''
|
|
|
|
Returns currently configured value for setting in api-paste.ini's
|
|
|
|
authtoken section, or None.
|
|
|
|
'''
|
|
|
|
config = ConfigParser.RawConfigParser()
|
|
|
|
config.read('/etc/nova/api-paste.ini')
|
|
|
|
try:
|
|
|
|
value = config.get('filter:authtoken', setting)
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
if value.startswith('%'):
|
|
|
|
return None
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def keystone_ca_cert_b64():
|
|
|
|
'''Returns the local Keystone-provided CA cert if it exists, or None.'''
|
|
|
|
if not os.path.isfile(CA_CERT_PATH):
|
|
|
|
return None
|
|
|
|
with open(CA_CERT_PATH) as _in:
|
|
|
|
return b64encode(_in.read())
|
|
|
|
|
|
|
|
|
2013-08-10 09:49:47 +01:00
|
|
|
def ssh_directory_for_unit():
|
|
|
|
remote_service = remote_unit().split('/')[0]
|
2013-08-12 13:53:00 -07:00
|
|
|
_dir = os.path.join(NOVA_SSH_DIR, remote_service)
|
|
|
|
for d in [NOVA_SSH_DIR, _dir]:
|
|
|
|
if not os.path.isdir(d):
|
|
|
|
os.mkdir(d)
|
|
|
|
for f in ['authorized_keys', 'known_hosts']:
|
|
|
|
f = os.path.join(_dir, f)
|
|
|
|
if not os.path.isfile(f):
|
|
|
|
open(f, 'w').close()
|
|
|
|
return _dir
|
2013-08-10 09:49:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
def known_hosts():
|
|
|
|
return os.path.join(ssh_directory_for_unit(), 'known_hosts')
|
|
|
|
|
|
|
|
|
|
|
|
def authorized_keys():
|
|
|
|
return os.path.join(ssh_directory_for_unit(), 'authorized_keys')
|
|
|
|
|
|
|
|
|
|
|
|
def ssh_known_host_key(host):
|
|
|
|
cmd = ['ssh-keygen', '-f', known_hosts(), '-H', '-F', host]
|
|
|
|
return subprocess.check_output(cmd).strip()
|
|
|
|
|
|
|
|
|
|
|
|
def remove_known_host(host):
|
|
|
|
log('Removing SSH known host entry for compute host at %s' % host)
|
|
|
|
cmd = ['ssh-kegen', '-f', known_hosts(), '-R', host]
|
|
|
|
subprocess.check_call(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
def add_known_host(host):
|
|
|
|
'''Add variations of host to a known hosts file.'''
|
|
|
|
cmd = ['ssh-keyscan', '-H', '-t', 'rsa', host]
|
|
|
|
try:
|
|
|
|
remote_key = subprocess.check_output(cmd).strip()
|
|
|
|
except Exception as e:
|
|
|
|
log('Could not obtain SSH host key from %s' % host, level=ERROR)
|
|
|
|
raise e
|
|
|
|
|
|
|
|
current_key = ssh_known_host_key(host)
|
|
|
|
if current_key:
|
|
|
|
if remote_key == current_key:
|
|
|
|
log('Known host key for compute host %s up to date.' % host)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
remove_known_host(host)
|
|
|
|
|
|
|
|
log('Adding SSH host key to known hosts for compute node at %s.' % host)
|
|
|
|
with open(known_hosts(), 'a') as out:
|
|
|
|
out.write(remote_key + '\n')
|
|
|
|
|
|
|
|
|
|
|
|
def ssh_authorized_key_exists(public_key):
|
|
|
|
with open(authorized_keys()) as keys:
|
|
|
|
return (' %s ' % public_key) in keys.read()
|
|
|
|
|
|
|
|
|
|
|
|
def add_authorized_key(public_key):
|
|
|
|
with open(authorized_keys(), 'a') as keys:
|
|
|
|
keys.write(public_key + '\n')
|
|
|
|
|
|
|
|
|
2013-09-05 16:53:31 -07:00
|
|
|
def ssh_compute_add(public_key):
|
|
|
|
# If remote compute node hands us a hostname, ensure we have a
|
|
|
|
# known hosts entry for its IP, hostname and FQDN.
|
|
|
|
private_address = relation_get('private-address')
|
|
|
|
hosts = [private_address]
|
2013-09-05 18:39:48 -07:00
|
|
|
|
2013-09-05 16:53:31 -07:00
|
|
|
if not is_ip(private_address):
|
|
|
|
hosts.append(get_host_ip(private_address))
|
|
|
|
hosts.append(private_address.split('.')[0])
|
2013-09-05 18:39:48 -07:00
|
|
|
else:
|
|
|
|
hn = get_hostname(private_address)
|
|
|
|
hosts.append(hn)
|
|
|
|
hosts.append(hn.split('.')[0])
|
2013-09-05 16:53:31 -07:00
|
|
|
|
2013-09-05 18:39:48 -07:00
|
|
|
for host in list(set(hosts)):
|
2013-09-05 16:53:31 -07:00
|
|
|
if not ssh_known_host_key(host):
|
|
|
|
add_known_host(host)
|
|
|
|
|
2013-08-10 09:49:47 +01:00
|
|
|
if not ssh_authorized_key_exists(public_key):
|
2013-09-05 16:53:31 -07:00
|
|
|
log('Saving SSH authorized key for compute host at %s.' %
|
|
|
|
private_address)
|
2013-08-12 13:53:00 -07:00
|
|
|
add_authorized_key(public_key)
|
2013-08-10 09:49:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
def ssh_known_hosts_b64():
|
|
|
|
with open(known_hosts()) as hosts:
|
|
|
|
return b64encode(hosts.read())
|
|
|
|
|
|
|
|
|
|
|
|
def ssh_authorized_keys_b64():
|
|
|
|
with open(authorized_keys()) as keys:
|
|
|
|
return b64encode(keys.read())
|
|
|
|
|
|
|
|
|
2013-09-05 17:28:22 -07:00
|
|
|
def ssh_compute_remove(public_key):
|
2013-08-10 09:49:47 +01:00
|
|
|
if not (os.path.isfile(authorized_keys()) or
|
|
|
|
os.path.isfile(known_hosts())):
|
|
|
|
return
|
2013-09-05 17:28:22 -07:00
|
|
|
|
2013-08-10 09:49:47 +01:00
|
|
|
with open(authorized_keys()) as _keys:
|
2013-09-05 17:28:22 -07:00
|
|
|
keys = [k.strip() for k in _keys.readlines()]
|
|
|
|
|
|
|
|
if public_key not in keys:
|
|
|
|
return
|
|
|
|
|
|
|
|
[keys.remove(key) for key in keys if key == public_key]
|
|
|
|
|
2013-08-10 09:49:47 +01:00
|
|
|
with open(authorized_keys(), 'w') as _keys:
|
|
|
|
_keys.write('\n'.join(keys))
|
2013-08-01 20:42:16 -07:00
|
|
|
|
|
|
|
|
2013-08-10 14:35:17 +01:00
|
|
|
def determine_endpoints(url):
|
|
|
|
'''Generates a dictionary containing all relevant endpoints to be
|
|
|
|
passed to keystone as relation settings.'''
|
|
|
|
region = config('region')
|
|
|
|
|
|
|
|
# TODO: Configurable nova API version.
|
2013-08-14 09:02:36 -07:00
|
|
|
nova_url = ('%s:%s/v1.1/$(tenant_id)s' %
|
2013-08-10 14:35:17 +01:00
|
|
|
(url, api_port('nova-api-os-compute')))
|
|
|
|
ec2_url = '%s:%s/services/Cloud' % (url, api_port('nova-api-ec2'))
|
2013-08-14 09:02:36 -07:00
|
|
|
nova_volume_url = ('%s:%s/v1/$(tenant_id)s' %
|
2013-08-10 14:35:17 +01:00
|
|
|
(url, api_port('nova-api-os-compute')))
|
|
|
|
neutron_url = '%s:%s' % (url, api_port('neutron-server'))
|
|
|
|
s3_url = '%s:%s' % (url, api_port('nova-objectstore'))
|
|
|
|
|
|
|
|
# the base endpoints
|
|
|
|
endpoints = {
|
|
|
|
'nova_service': 'nova',
|
|
|
|
'nova_region': region,
|
|
|
|
'nova_public_url': nova_url,
|
|
|
|
'nova_admin_url': nova_url,
|
|
|
|
'nova_internal_url': nova_url,
|
|
|
|
'ec2_service': 'ec2',
|
|
|
|
'ec2_region': region,
|
|
|
|
'ec2_public_url': ec2_url,
|
|
|
|
'ec2_admin_url': ec2_url,
|
|
|
|
'ec2_internal_url': ec2_url,
|
|
|
|
's3_service': 's3',
|
|
|
|
's3_region': region,
|
|
|
|
's3_public_url': s3_url,
|
|
|
|
's3_admin_url': s3_url,
|
|
|
|
's3_internal_url': s3_url,
|
|
|
|
}
|
|
|
|
|
|
|
|
if relation_ids('nova-volume-service'):
|
|
|
|
endpoints.update({
|
|
|
|
'nova-volume_service': 'nova-volume',
|
|
|
|
'nova-volume_region': region,
|
|
|
|
'nova-volume_public_url': nova_volume_url,
|
|
|
|
'nova-volume_admin_url': nova_volume_url,
|
|
|
|
'nova-volume_internal_url': nova_volume_url,
|
|
|
|
})
|
|
|
|
|
2013-08-10 19:06:09 +01:00
|
|
|
# XXX: Keep these relations named quantum_*??
|
2013-08-10 14:35:17 +01:00
|
|
|
if network_manager() in ['quantum', 'neutron']:
|
|
|
|
endpoints.update({
|
|
|
|
'quantum_service': 'quantum',
|
|
|
|
'quantum_region': region,
|
|
|
|
'quantum_public_url': neutron_url,
|
|
|
|
'quantum_admin_url': neutron_url,
|
|
|
|
'quantum_internal_url': neutron_url,
|
|
|
|
})
|
|
|
|
|
|
|
|
return endpoints
|
2013-08-14 09:02:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
def neutron_plugin():
|
|
|
|
# quantum-plugin config setting can be safely overriden
|
|
|
|
# as we only supported OVS in G/neutron
|
|
|
|
return config('neutron-plugin') or config('quantum-plugin')
|