From 1b8e4ce12d749141581ca880a13a71314acbbf79 Mon Sep 17 00:00:00 2001 From: CNlucius Date: Tue, 30 Aug 2016 09:33:44 +0800 Subject: Modify sfc bug for ONOS Change-Id: Ied71c7e57553a846241ec826c8607ac1bb84e1e9 Signed-off-by: CNlucius --- .../roles/onos_cluster/files/setup_networks/log.py | 41 ++++++++++++ .../onos_cluster/files/setup_networks/net_init | 20 ++++++ .../files/setup_networks/setup_networks.py | 73 ++++++++++++++++++++++ .../roles/onos_cluster/tasks/main.yml | 69 ++++++++++++++++++-- .../roles/onos_cluster/templates/keepalived.conf | 47 ++++++++++++++ .../roles/onos_cluster/templates/my_configs.debian | 14 +++++ .../roles/onos_cluster/templates/network.cfg | 5 ++ 7 files changed, 265 insertions(+), 4 deletions(-) create mode 100644 deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/log.py create mode 100755 deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/net_init create mode 100644 deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/setup_networks.py create mode 100644 deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/keepalived.conf create mode 100644 deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/my_configs.debian create mode 100644 deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/network.cfg diff --git a/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/log.py b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/log.py new file mode 100644 index 00000000..fffeb589 --- /dev/null +++ b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/log.py @@ -0,0 +1,41 @@ +import logging +import os +loggers = {} +log_dir="/var/log/setup_network" +try: + os.makedirs(log_dir) +except: + pass + +def getLogger(name): + if name in loggers: + return loggers[name] + + logger = logging.getLogger(name) + logger.setLevel(logging.DEBUG) + + # create file handler which logs even debug messages + log_file = "%s/%s.log" % (log_dir, name) + try: + os.remove(log_file) + except: + pass + + fh = logging.FileHandler(log_file) + fh.setLevel(logging.DEBUG) + + # create console handler with a higher log level + ch = logging.StreamHandler() + ch.setLevel(logging.ERROR) + + # create formatter and add it to the handlers + formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + ch.setFormatter(formatter) + fh.setFormatter(formatter) + + # add the handlers to logger + logger.addHandler(ch) + logger.addHandler(fh) + + loggers[name] = logger + return logger diff --git a/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/net_init b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/net_init new file mode 100755 index 00000000..c27a8bf8 --- /dev/null +++ b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/net_init @@ -0,0 +1,20 @@ +#!/bin/bash +## BEGIN INIT INFO +# Provides: anamon.init +# Default-Start: 3 5 +# Default-Stop: 0 1 2 4 6 +# Required-Start: $network +# Short-Description: Starts the cobbler anamon boot notification program +# Description: anamon runs the first time a machine is booted after +# installation. +## END INIT INFO + +# +# anamon.init: Starts the cobbler post-install boot notification program +# +# chkconfig: 35 0 6 +# +# description: anamon runs the first time a machine is booted after +# installation. +# +python /opt/setup_networks/setup_networks.py diff --git a/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/setup_networks.py b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/setup_networks.py new file mode 100644 index 00000000..e58d6c72 --- /dev/null +++ b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/files/setup_networks/setup_networks.py @@ -0,0 +1,73 @@ +import yaml +import netaddr +import os +import log as logging + +LOG = logging.getLogger("net-init") +config_path = os.path.join(os.path.dirname(__file__), "network.cfg") + +def setup_bondings(bond_mappings): + print bond_mappings + +def add_vlan_link(interface, ifname, vlan_id): + LOG.info("add_vlan_link enter") + cmd = "ip link add link %s name %s type vlan id %s; " % (ifname, interface, vlan_id) + cmd += "ip link set %s up; ip link set %s up" % (interface, ifname) + LOG.info("add_vlan_link: cmd=%s" % cmd) + os.system(cmd) + +def add_ovs_port(ovs_br, ifname, uplink, vlan_id=None): + LOG.info("add_ovs_port enter") + cmd = "ovs-vsctl --may-exist add-port %s %s" % (ovs_br, ifname) + if vlan_id: + cmd += " tag=%s" % vlan_id + cmd += " -- set Interface %s type=internal;" % ifname + cmd += "ip link set dev %s address `ip link show %s |awk '/link\/ether/{print $2}'`;" \ + % (ifname, uplink) + cmd += "ip link set %s up;" % ifname + LOG.info("add_ovs_port: cmd=%s" % cmd) + os.system(cmd) + +def setup_intfs(sys_intf_mappings, uplink_map): + LOG.info("setup_intfs enter") + for intf_name, intf_info in sys_intf_mappings.items(): + if intf_info["type"] == "vlan": + add_vlan_link(intf_name, intf_info["interface"], intf_info["vlan_tag"]) + elif intf_info["type"] == "ovs": + add_ovs_port( + intf_info["interface"], + intf_name, + uplink_map[intf_info["interface"]], + vlan_id=intf_info.get("vlan_tag")) + else: + pass + +def setup_ips(ip_settings, sys_intf_mappings): + LOG.info("setup_ips enter") + for intf_info in ip_settings.values(): + network = netaddr.IPNetwork(intf_info["cidr"]) + if sys_intf_mappings[intf_info["name"]]["type"] == "ovs": + intf_name = intf_info["name"] + else: + intf_name = intf_info["alias"] + cmd = "ip addr add %s/%s brd %s dev %s;" \ + % (intf_info["ip"], intf_info["netmask"], str(network.broadcast),intf_name) + if "gw" in intf_info: + cmd += "route del default;" + cmd += "ip route add default via %s dev %s" % (intf_info["gw"], intf_name) + LOG.info("setup_ips: cmd=%s" % cmd) + os.system(cmd) + +def main(config): + uplink_map = {} + setup_bondings(config["bond_mappings"]) + for provider_net in config["provider_net_mappings"]: + uplink_map[provider_net['name']] = provider_net['interface'] + + setup_intfs(config["sys_intf_mappings"], uplink_map) + setup_ips(config["ip_settings"], config["sys_intf_mappings"]) + +if __name__ == "__main__": + os.system("service openvswitch-switch status|| service openvswitch-switch start") + config = yaml.load(open(config_path)) + main(config) diff --git a/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/tasks/main.yml b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/tasks/main.yml index bda2ba30..6b619057 100755 --- a/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/tasks/main.yml +++ b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/tasks/main.yml @@ -40,13 +40,74 @@ - name: Stop the Open vSwitch service and clear existing OVSDB shell: > - ovs-vsctl del-br br-int ; - ovs-vsctl del-br br-tun ; - ovs-vsctl del-manager ; - ip link delete onos_port1 type veth peer name onos_port2; + service openvswitch-switch stop ; + rm -rf /var/log/openvswitch/* ; + rm -rf /etc/openvswitch/conf.db ; + service openvswitch-switch start ; when: groups['onos']|length !=0 ignore_errors: True +################################################################## +########### Recover External network ################# +################################################################## + +- name: add ovs bridge + openvswitch_bridge: bridge={{ item["name"] }} state=present + with_items: "{{ network_cfg['provider_net_mappings'] }}" + when: item["type"] == "ovs" and groups['onos']|length !=0 + +- name: add ovs uplink + openvswitch_port: bridge={{ item["name"] }} port={{ item["interface"] }} state=present + with_items: "{{ network_cfg['provider_net_mappings'] }}" + when: item["type"] == "ovs" and groups['onos']|length !=0 + +- name: add ovs uplink + shell: ip link set {{ item["interface"] }} up + with_items: "{{ network_cfg['provider_net_mappings'] }}" + when: item["type"] == "ovs" and groups['onos']|length !=0 + +- name: ensure script dir exist + shell: mkdir -p /opt/setup_networks + when: groups['onos']|length !=0 + +- name: copy scripts + copy: src={{ item }} dest=/opt/setup_networks + with_items: + - setup_networks/log.py + - setup_networks/setup_networks.py + when: groups['onos']|length !=0 + +- name: copy boot scripts + copy: src={{ item }} dest=/etc/init.d/ mode=0755 + with_items: + - setup_networks/net_init + when: groups['onos']|length !=0 + +- name: copy config files + template: src=network.cfg dest=/opt/setup_networks + when: groups['onos']|length !=0 + +- name: make sure python lib exist + action: "{{ ansible_pkg_mgr }} name={{ item }} state=present" + with_items: + - python-yaml + - python-netaddr + when: groups['onos']|length !=0 + +- name: run scripts + shell: python /opt/setup_networks/setup_networks.py + when: groups['onos']|length !=0 + +- name: add to boot scripts + service: name=net_init enabled=yes + when: groups['onos']|length !=0 +################################################################## + +- name: restart keepalived to recover external IP + shell: service keepalived restart + when: inventory_hostname in groups['onos'] + ignore_errors: True + - name: Install ONOS Cluster on Controller include: onos_controller.yml when: inventory_hostname in groups['onos'] and onos_sfc == "Disable" diff --git a/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/keepalived.conf b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/keepalived.conf new file mode 100644 index 00000000..4ccf1c43 --- /dev/null +++ b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/keepalived.conf @@ -0,0 +1,47 @@ +global_defs { + router_id {{ inventory_hostname }} +} + +vrrp_sync_group VG1 { + group { + internal_vip + public_vip + } +} + +vrrp_instance internal_vip { + interface {{ internal_vip.interface }} + virtual_router_id {{ vrouter_id_internal }} + state BACKUP + nopreempt + advert_int 1 + priority {{ 50 + (host_index[inventory_hostname] * 50) }} + + authentication { + auth_type PASS + auth_pass 1234 + } + + virtual_ipaddress { + {{ internal_vip.ip }}/{{ internal_vip.netmask }} dev {{ internal_vip.interface }} + } +} + +vrrp_instance public_vip { + interface br-ex + virtual_router_id {{ vrouter_id_public }} + state BACKUP + nopreempt + advert_int 1 + priority {{ 50 + (host_index[inventory_hostname] * 50) }} + + authentication { + auth_type PASS + auth_pass 4321 + } + + virtual_ipaddress { + {{ network_cfg.public_vip.ip }}/{{ network_cfg.public_vip.netmask }} dev br-ex + } + +} diff --git a/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/my_configs.debian b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/my_configs.debian new file mode 100644 index 00000000..5ab1519b --- /dev/null +++ b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/my_configs.debian @@ -0,0 +1,14 @@ +{%- for alias, intf in host_ip_settings.items() %} + +auto {{ alias }} +iface {{ alias }} inet static + address {{ intf["ip"] }} + netmask {{ intf["netmask"] }} +{% if "gw" in intf %} + gateway {{ intf["gw"] }} +{% endif %} +{% if intf["name"] == alias %} + pre-up ip link set {{ sys_intf_mappings[alias]["interface"] }} up + pre-up ip link add link {{ sys_intf_mappings[alias]["interface"] }} name {{ alias }} type vlan id {{ sys_intf_mappings[alias]["vlan_tag"] }} +{% endif %} +{% endfor %} diff --git a/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/network.cfg b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/network.cfg new file mode 100644 index 00000000..75ba90cb --- /dev/null +++ b/deploy/adapters/ansible/openstack_mitaka/roles/onos_cluster/templates/network.cfg @@ -0,0 +1,5 @@ +bond_mappings: {{ network_cfg["bond_mappings"] }} +ip_settings: {{ ip_settings[inventory_hostname] }} +sys_intf_mappings: {{ sys_intf_mappings }} +provider_net_mappings: {{ network_cfg["provider_net_mappings"] }} + -- cgit 1.2.3-korg