250 lines
7.4 KiB
Python
Raw Normal View History

2012-10-26 17:56:42 +02:00
import subprocess
2013-01-18 08:51:11 +00:00
import os
import uuid
2013-03-20 16:08:54 +00:00
import base64
import apt_pkg as apt
2013-03-20 16:29:43 +00:00
from lib.utils import (
juju_log as log,
configure_source,
config_get
2013-03-20 16:29:43 +00:00
)
2012-10-26 17:56:42 +02:00
OVS = "ovs"
2012-10-26 17:56:42 +02:00
OVS_PLUGIN = \
"quantum.plugins.openvswitch.ovs_quantum_plugin.OVSQuantumPluginV2"
CORE_PLUGIN = {
OVS: OVS_PLUGIN,
2012-10-26 17:56:42 +02:00
}
OVS_PLUGIN_CONF = \
"/etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini"
PLUGIN_CONF = {
OVS: OVS_PLUGIN_CONF,
2012-10-26 17:56:42 +02:00
}
GATEWAY_PKGS = {
OVS: [
"quantum-plugin-openvswitch-agent",
"quantum-l3-agent",
"quantum-dhcp-agent",
2013-01-18 08:51:11 +00:00
'python-mysqldb',
"nova-api-metadata"
],
}
GATEWAY_AGENTS = {
OVS: [
"quantum-plugin-openvswitch-agent",
"quantum-l3-agent",
2013-01-18 08:51:11 +00:00
"quantum-dhcp-agent",
"nova-api-metadata"
],
2013-02-08 16:20:03 +00:00
}
EXT_PORT_CONF = '/etc/init/ext-port.conf'
2013-02-08 16:20:03 +00:00
2013-03-20 16:08:54 +00:00
def get_os_version(package=None):
apt.init()
cache = apt.Cache()
pkg = cache[package or 'quantum-common']
if pkg.current_ver:
return apt.upstream_version(pkg.current_ver.ver_str)
else:
return None
2013-01-18 08:51:11 +00:00
if get_os_version('quantum-common') >= "2013.1":
for plugin in GATEWAY_AGENTS:
GATEWAY_AGENTS[plugin].append("quantum-metadata-agent")
2012-10-26 17:56:42 +02:00
DB_USER = "quantum"
QUANTUM_DB = "quantum"
KEYSTONE_SERVICE = "quantum"
2013-01-18 08:51:11 +00:00
NOVA_DB_USER = "nova"
NOVA_DB = "nova"
2012-10-26 17:56:42 +02:00
QUANTUM_CONF = "/etc/quantum/quantum.conf"
L3_AGENT_CONF = "/etc/quantum/l3_agent.ini"
2012-11-05 11:59:27 +00:00
DHCP_AGENT_CONF = "/etc/quantum/dhcp_agent.ini"
2013-01-18 08:51:11 +00:00
METADATA_AGENT_CONF = "/etc/quantum/metadata_agent.ini"
NOVA_CONF = "/etc/nova/nova.conf"
2012-10-26 17:56:42 +02:00
2013-04-12 15:56:50 +01:00
RESTART_MAP = {
QUANTUM_CONF: [
'quantum-l3-agent',
'quantum-dhcp-agent',
'quantum-metadata-agent',
'quantum-plugin-openvswitch-agent'
],
DHCP_AGENT_CONF: [
'quantum-dhcp-agent'
],
L3_AGENT_CONF: [
'quantum-l3-agent'
],
METADATA_AGENT_CONF: [
'quantum-metadata-agent'
],
OVS_PLUGIN_CONF: [
'quantum-plugin-openvswitch-agent'
],
NOVA_CONF: [
'nova-api-metadata'
]
}
2012-11-05 11:59:27 +00:00
RABBIT_USER = "nova"
RABBIT_VHOST = "nova"
2012-10-26 17:56:42 +02:00
2012-12-03 15:16:55 +00:00
INT_BRIDGE = "br-int"
EXT_BRIDGE = "br-ex"
2012-10-26 17:56:42 +02:00
def add_bridge(name):
status = subprocess.check_output(["ovs-vsctl", "show"])
if "Bridge {}".format(name) not in status:
log('INFO', 'Creating bridge {}'.format(name))
2012-10-26 17:56:42 +02:00
subprocess.check_call(["ovs-vsctl", "add-br", name])
def del_bridge(name):
status = subprocess.check_output(["ovs-vsctl", "show"])
if "Bridge {}".format(name) in status:
log('INFO', 'Deleting bridge {}'.format(name))
2012-10-26 17:56:42 +02:00
subprocess.check_call(["ovs-vsctl", "del-br", name])
def add_bridge_port(name, port):
status = subprocess.check_output(["ovs-vsctl", "show"])
2012-11-05 11:59:27 +00:00
if ("Bridge {}".format(name) in status and
"Interface \"{}\"".format(port) not in status):
log('INFO',
'Adding port {} to bridge {}'.format(port, name))
2012-10-26 17:56:42 +02:00
subprocess.check_call(["ovs-vsctl", "add-port", name, port])
subprocess.check_call(["ip", "link", "set", port, "up"])
2012-10-26 17:56:42 +02:00
def del_bridge_port(name, port):
status = subprocess.check_output(["ovs-vsctl", "show"])
if ("Bridge {}".format(name) in status and
"Interface \"{}\"".format(port) in status):
log('INFO',
'Deleting port {} from bridge {}'.format(port, name))
2012-10-26 17:56:42 +02:00
subprocess.check_call(["ovs-vsctl", "del-port", name, port])
subprocess.check_call(["ip", "link", "set", port, "down"])
2013-01-18 08:51:11 +00:00
SHARED_SECRET = "/etc/quantum/secret.txt"
def get_shared_secret():
secret = None
if not os.path.exists(SHARED_SECRET):
secret = str(uuid.uuid4())
with open(SHARED_SECRET, 'w') as secret_file:
secret_file.write(secret)
else:
with open(SHARED_SECRET, 'r') as secret_file:
secret = secret_file.read().strip()
return secret
2013-02-08 16:10:48 +00:00
def flush_local_configuration():
if os.path.exists('/usr/bin/quantum-netns-cleanup'):
cmd = [
"quantum-netns-cleanup",
2013-02-18 09:14:49 +00:00
"--config-file=/etc/quantum/quantum.conf"
2013-02-08 16:10:48 +00:00
]
2013-02-18 09:14:49 +00:00
for agent_conf in ['l3_agent.ini', 'dhcp_agent.ini']:
agent_cmd = list(cmd)
agent_cmd.append('--config-file=/etc/quantum/{}'\
.format(agent_conf))
subprocess.call(agent_cmd)
2013-03-20 16:08:54 +00:00
def install_ca(ca_cert):
with open('/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt',
'w') as crt:
crt.write(base64.b64decode(ca_cert))
subprocess.check_call(['update-ca-certificates', '--fresh'])
DHCP_AGENT = "DHCP Agent"
L3_AGENT = "L3 Agent"
def reassign_agent_resources(env):
''' Use agent scheduler API to detect down agents and re-schedule '''
from quantumclient.v2_0 import client
# TODO: Fixup for https keystone
2013-03-20 16:20:51 +00:00
auth_url = 'http://%(keystone_host)s:%(auth_port)s/v2.0' % env
2013-03-20 16:08:54 +00:00
quantum = client.Client(username=env['service_username'],
password=env['service_password'],
tenant_name=env['service_tenant'],
auth_url=auth_url,
region_name=env['region'])
agents = quantum.list_agents(agent_type=DHCP_AGENT)
dhcp_agents = []
l3_agents = []
2013-03-20 16:08:54 +00:00
networks = {}
for agent in agents['agents']:
if not agent['alive']:
log('INFO', 'DHCP Agent %s down' % agent['id'])
for network in \
quantum.list_networks_on_dhcp_agent(agent['id'])['networks']:
networks[network['id']] = agent['id']
else:
dhcp_agents.append(agent['id'])
2013-03-20 16:08:54 +00:00
agents = quantum.list_agents(agent_type=L3_AGENT)
routers = {}
for agent in agents['agents']:
if not agent['alive']:
log('INFO', 'L3 Agent %s down' % agent['id'])
for router in \
quantum.list_routers_on_l3_agent(agent['id'])['routers']:
routers[router['id']] = agent['id']
else:
l3_agents.append(agent['id'])
2013-03-20 16:08:54 +00:00
index = 0
2013-03-20 16:08:54 +00:00
for router_id in routers:
agent = index % len(l3_agents)
2013-03-20 16:08:54 +00:00
log('INFO',
'Moving router %s from %s to %s' % \
(router_id, routers[router_id], l3_agents[agent]))
2013-03-20 16:08:54 +00:00
quantum.remove_router_from_l3_agent(l3_agent=routers[router_id],
router_id=router_id)
quantum.add_router_to_l3_agent(l3_agent=l3_agents[agent],
2013-03-20 16:08:54 +00:00
body={'router_id': router_id})
index += 1
2013-03-20 16:08:54 +00:00
index = 0
2013-03-20 16:08:54 +00:00
for network_id in networks:
agent = index % len(dhcp_agents)
2013-03-20 16:08:54 +00:00
log('INFO',
'Moving network %s from %s to %s' % \
(network_id, networks[network_id], dhcp_agents[agent]))
2013-03-20 16:08:54 +00:00
quantum.remove_network_from_dhcp_agent(dhcp_agent=networks[network_id],
network_id=network_id)
quantum.add_network_to_dhcp_agent(dhcp_agent=dhcp_agents[agent],
2013-03-20 16:08:54 +00:00
body={'network_id': network_id})
index += 1
def do_openstack_upgrade():
configure_source()
plugin = config_get('plugin')
pkgs = []
if plugin in GATEWAY_PKGS.keys():
2013-05-22 16:00:14 -07:00
pkgs += GATEWAY_PKGS[plugin]
if plugin == OVS:
pkgs.append('openvswitch-datapath-dkms')
cmd = ['apt-get', '-y',
'--option', 'Dpkg::Options::=--force-confold',
'--option', 'Dpkg::Options::=--force-confdef',
'install'] + pkgs
subprocess.check_call(cmd)