Skip to content

Commit

Permalink
Netns change for LBAAS config generation on controller
Browse files Browse the repository at this point in the history
1. building haproxy.conf file from json file is removed since
   config construction is done in controller
2. netns script would handle directory creation and deletion
   for lbaas based on the lbass type and lb_id from the
   keyvaluepair config file

Change-Id: I74fc8ce33f9038ead078ecbab5555a774837f532
Partial-Bug: #1557222
  • Loading branch information
ymariappan committed Apr 8, 2016
1 parent cb23029 commit cf8be92
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,56 @@
import subprocess
import haproxy_config

HAPROXY_DIR = "/var/lib/contrail/loadbalancer"
HAPROXY_PROCESS = 'haproxy'
HAPROXY_PROCESS_CONF = HAPROXY_PROCESS + ".conf"
SUPERVISOR_BASE_DIR = '/etc/contrail/supervisord_vrouter_files/lbaas-haproxy-'

def delete_haproxy_dir(base_dir, loadbalancer_id):
dir_name = base_dir + "/" + loadbalancer_id
cmd = "rm -rf " + dir_name
cmd_list = shlex.split(cmd)
p = subprocess.Popen(cmd_list)
p.communicate()

def create_haproxy_dir(base_dir, loadbalancer_id):
dir_name = base_dir + "/" + loadbalancer_id
cmd = "mkdir -p " + dir_name
cmd_list = shlex.split(cmd)
p = subprocess.Popen(cmd_list)
p.communicate()
return dir_name

def get_haproxy_config_file(cfg_file, dir_name):
f = open(cfg_file)
content = f.read()
f.close()
kvps = content.split(':::::')
for kvp in kvps or []:
KeyValue = kvp.split('::::')
if (KeyValue[0] == 'haproxy_config'):
break;
haproxy_cfg_file = dir_name + "/" + HAPROXY_PROCESS_CONF
f = open(haproxy_cfg_file, 'w+')
f.write(KeyValue[1])
f.close()
return haproxy_cfg_file

def get_pid_file_from_conf_file(conf_file):
dir_name = os.path.dirname(conf_file)
sout = os.path.split(dir_name)
pid_file = sout[0] + "/" + sout[1] + ".haproxy.pid"
pid_file = dir_name + "/" + "haproxy.pid"
return pid_file

def delete_haproxy_pid_file(conf_file):
pid_file = get_pid_file_from_conf_file(conf_file)
if os.path.isfile(pid_file):
cmd = "rm " + pid_file
cmd_list = shlex.split(cmd)
subprocess.Popen(cmd_list)
p = subprocess.Popen(cmd_list)
p.communicate()

def stop_haproxy(conf_file, daemon_mode=False):
def stop_haproxy(loadbalancer_id, daemon_mode=False):
conf_file = HAPROXY_DIR + "/" + loadbalancer_id + "/" + HAPROXY_PROCESS_CONF
try:
if daemon_mode:
_stop_haproxy_daemon(conf_file)
Expand All @@ -30,13 +64,14 @@ def stop_haproxy(conf_file, daemon_mode=False):
pass

delete_haproxy_pid_file(conf_file)
delete_haproxy_dir(HAPROXY_DIR, loadbalancer_id)


def start_update_haproxy(conf_file, netns, daemon_mode=False,
keystone_auth_conf_file=None):
pool_id = os.path.split(os.path.dirname(conf_file))[1]
haproxy_cfg_file = haproxy_config.build_config(conf_file, \
keystone_auth_conf_file)
def start_update_haproxy(loadbalancer_id, cfg_file,
netns, daemon_mode=False, keystone_auth_conf_file=None):
pool_id = loadbalancer_id
dir_name = create_haproxy_dir(HAPROXY_DIR, loadbalancer_id)
haproxy_cfg_file = get_haproxy_config_file(cfg_file, dir_name)
try:
if daemon_mode:
_start_haproxy_daemon(pool_id, netns, haproxy_cfg_file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import requests
import json
import os
import shlex


from linux import ip_lib
import haproxy_process
Expand All @@ -54,9 +56,9 @@ class NetnsManager(object):
RIGH_DEV_PREFIX = 'gw-'
TAP_PREFIX = 'veth'
PORT_TYPE = 'NameSpacePort'
LBAAS_PROCESS = 'haproxy'
BASE_URL = "http://localhost:9091/port"
HEADERS = {'content-type': 'application/json'}
LBAAS_DIR = "/var/lib/contrail/loadbalancer"

def __init__(self, vm_uuid, nic_left, nic_right, other_nics=None,
root_helper='sudo', cfg_file=None, update=False,
Expand Down Expand Up @@ -88,6 +90,7 @@ def __init__(self, vm_uuid, nic_left, nic_right, other_nics=None,
self.cfg_file = cfg_file
self.update = update
self.gw_ip = gw_ip
self.loadbalancer_id = loadbalancer_id
self.keystone_auth_cfg_file = keystone_auth_cfg_file

def _get_tap_name(self, uuid_str):
Expand Down Expand Up @@ -133,13 +136,50 @@ def set_snat(self):
self.SNAT_RT_TABLES_ID, 'via', self.gw_ip,
'dev', str(self.nic_left['name'])])

def find_lbaas_type(self, cfg_file):
lbaas_type = '';
if not os.path.exists(cfg_file):
return lbaas_type
f = open(cfg_file)
content = f.read()
f.close()
kvps = content.split(':::::')
for kvp in kvps or []:
lbaas_type = kvp.split('::::')[0]
if (lbaas_type == 'haproxy_config'):
break;
return lbaas_type

def move_cfg_file_to_lbaas_dir(self, cfg_file):
dir_name = self.LBAAS_DIR;
if not os.path.exists(dir_name):
cmd = "mkdir -p " + dir_name
cmd_list = shlex.split(cmd)
p = subprocess.Popen(cmd_list)
p.communicate()
cmd = "mv " + cfg_file + " " + dir_name
cmd_list = shlex.split(cmd)
p = subprocess.Popen(cmd_list)
p.communicate();
return dir_name + '/' + os.path.basename(cfg_file)

def remove_cfg_file(self, cfg_file):
cmd = "rm " + cfg_file
cmd_list = shlex.split(cmd)
p = subprocess.Popen(cmd_list)
p.communicate()

def set_lbaas(self):
if not self.ip_ns.netns.exists(self.namespace):
self.create()

haproxy_process.start_update_haproxy(self.cfg_file, self.namespace, True,
self.keystone_auth_cfg_file)

lbaas_type = self.find_lbaas_type(self.cfg_file)
if (lbaas_type == ''):
raise ValueError('LBAAS_TYPE does not exist %s' % self.cfg_file)
self.cfg_file = self.move_cfg_file_to_lbaas_dir(self.cfg_file)
if (lbaas_type == 'haproxy_config'):
haproxy_process.start_update_haproxy(self.loadbalancer_id, self.cfg_file,
self.namespace, True, self.keystone_auth_cfg_file)
try:
self.ip_ns.netns.execute(['route', 'add', 'default', 'gw', self.gw_ip])
except RuntimeError:
Expand All @@ -149,12 +189,17 @@ def release_lbaas(self):
if not self.ip_ns.netns.exists(self.namespace):
raise ValueError('Need to create the network namespace before '
'relasing lbaas')

haproxy_process.stop_haproxy(self.cfg_file, True)
cfg_file = self.LBAAS_DIR + "/" + self.loadbalancer_id + ".conf"
lbaas_type = self.find_lbaas_type(cfg_file)
if (lbaas_type == ''):
return
elif (lbaas_type == 'haproxy_config'):
haproxy_process.stop_haproxy(self.loadbalancer_id, True)
try:
self.ip_ns.netns.execute(['route', 'del', 'default'])
except RuntimeError:
pass
self.remove_cfg_file(cfg_file)

def destroy(self):
if not self.ip_ns.netns.exists(self.namespace):
Expand Down

0 comments on commit cf8be92

Please sign in to comment.