aboutsummaryrefslogtreecommitdiffstats
path: root/core/vswitch_controller_pvp.py
blob: c02863238a0373f6af790a808967090d9353c53f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright 2015 Intel Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""VSwitch controller for Physical to VM to Physical deployment
"""

import logging

from core.vswitch_controller import IVswitchController

class VswitchControllerPVP(IVswitchController):
    """VSwitch controller for PVP deployment scenario.

    Attributes:
        _vswitch_class: The vSwitch class to be used.
        _vswitch: The vSwitch object controlled by this controller
        _deployment_scenario: A string describing the scenario to set-up in the
            constructor.
    """
    def __init__(self, vswitch_class):
        """Initializes up the prerequisites for the PVP deployment scenario.

        :vswitch_class: the vSwitch class to be used.
        """
        self._logger = logging.getLogger(__name__)
        self._vswitch_class = vswitch_class
        self._vswitch = vswitch_class()
        self._deployment_scenario = "PVP"
        self._logger.debug('Creation using ' + str(self._vswitch_class))

    def setup(self):
        """
        Sets up the switch for the particular deployment scenario passed in to
        the constructor.
        """
        # TODO call IVSwitch methods to configure VSwitch for PVP scenario.
        self._logger.debug('Setup using ' + str(self._vswitch_class))

    def stop(self):
        """
        Tears down the switch created in setup().
        """
        # TODO call IVSwitch methods to stop VSwitch for PVP scenario.
        self._logger.debug('Stop using ' + str(self._vswitch_class))

    def get_vswitch(self):
        """See IVswitchController for description
        """
        return self._vswitch

    def get_ports_info(self):
        """See IVswitchController for description
        """
        self._logger.debug('get_ports_info  using ' + str(self._vswitch_class))
        return []
weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
#!/usr/bin/env python
#
# Copyright (c) 2015 Ericsson
# jose.lausuch@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
#

import re, json, os, urllib2, argparse, logging, shutil, subprocess, yaml, sys, getpass
import functest_utils
from git import Repo
from os import stat
from pwd import getpwuid
from neutronclient.v2_0 import client as neutronclient

actions = ['start', 'check', 'clean']
parser = argparse.ArgumentParser()
parser.add_argument("action", help="Possible actions are: '{d[0]}|{d[1]}|{d[2]}' ".format(d=actions))
parser.add_argument("-d", "--debug", help="Debug mode",  action="store_true")
parser.add_argument("-f", "--force", help="Force",  action="store_true")
args = parser.parse_args()


""" logging configuration """
logger = logging.getLogger('config_functest')
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
if args.debug:
    ch.setLevel(logging.DEBUG)
else:
    ch.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

REPOS_DIR=os.environ['repos_dir']
FUNCTEST_REPO=REPOS_DIR+'/functest/'
if not os.path.exists(FUNCTEST_REPO):
    logger.error("Functest repository directory not found '%s'" % FUNCTEST_REPO)
    exit(-1)
sys.path.append(FUNCTEST_REPO + "testcases/")

with open("/home/opnfv/functest/conf/config_functest.yaml") as f:
    functest_yaml = yaml.safe_load(f)
f.close()


""" global variables """
# Directories
RALLY_DIR = FUNCTEST_REPO + functest_yaml.get("general").get("directories").get("dir_rally")
RALLY_REPO_DIR = functest_yaml.get("general").get("directories").get("dir_repo_rally")
RALLY_INSTALLATION_DIR = functest_yaml.get("general").get("directories").get("dir_rally_inst")
RALLY_RESULT_DIR = functest_yaml.get("general").get("directories").get("dir_rally_res")
VPING_DIR = FUNCTEST_REPO + functest_yaml.get("general").get("directories").get("dir_vping")
VIMS_TEST_DIR = functest_yaml.get("general").get("directories").get("dir_repo_vims_test")
ODL_DIR = FUNCTEST_REPO + functest_yaml.get("general").get("directories").get("dir_odl")
DATA_DIR = functest_yaml.get("general").get("directories").get("dir_functest_data")

# Tempest/Rally configuration details
DEPLOYMENT_MAME = functest_yaml.get("rally").get("deployment_name")
RALLY_COMMIT = functest_yaml.get("general").get("repositories").get("rally_commit")

#Image (cirros)
IMAGE_FILE_NAME = functest_yaml.get("general").get("openstack").get("image_file_name")
IMAGE_PATH = DATA_DIR + "/" + IMAGE_FILE_NAME

# NEUTRON Private Network parameters
NEUTRON_PRIVATE_NET_NAME = functest_yaml.get("general"). \
    get("openstack").get("neutron_private_net_name")
NEUTRON_PRIVATE_SUBNET_NAME = functest_yaml.get("general"). \
    get("openstack").get("neutron_private_subnet_name")
NEUTRON_PRIVATE_SUBNET_CIDR = functest_yaml.get("general"). \
    get("openstack").get("neutron_private_subnet_cidr")
NEUTRON_ROUTER_NAME = functest_yaml.get("general"). \
    get("openstack").get("neutron_router_name")

creds_neutron = functest_utils.get_credentials("neutron")
neutron_client = neutronclient.Client(**creds_neutron)

def action_start():
    """
    Start the functest environment installation
    """
    if not functest_utils.check_internet_connectivity():
        logger.error("There is no Internet connectivity. Please check the network configuration.")
        exit(-1)

    if action_check():
        logger.info("Functest environment already installed. Nothing to do.")
        exit(0)

    else:
        # Clean in case there are left overs
        logger.debug("Cleaning possible functest environment leftovers.")
        action_clean()
        logger.info("Starting installation of functest environment")

        private_net = functest_utils.get_private_net(neutron_client)
        if private_net is None:
            # If there is no private network in the deployment we create one
            if not create_private_neutron_net(neutron_client):
                logger.error("There has been a problem while creating the functest network.")
                action_clean()
                exit(-1)
        else:
            logger.info("Private network '%s' already existing in the deployment."
                 % private_net['name'])

        logger.info("Installing Rally...")
        if not install_rally():
            logger.error("There has been a problem while installing Rally.")
            action_clean()
            exit(-1)

        logger.info("Installing Ruby libraries for vIMS testcase...")
        # Install ruby libraries for vims test-case
        script = 'source /etc/profile.d/rvm.sh; '
        script += 'cd ' + VIMS_TEST_DIR + '; '
        script += 'rvm autolibs enable ;'
        script += 'rvm install 1.9.3; '
        script += 'rvm use 1.9.3;'
        script += 'bundle install'

        logger_debug = None
        CI_DEBUG = os.environ.get("CI_DEBUG")
        if CI_DEBUG == "true" or CI_DEBUG == "True":
            logger_debug = logger

        cmd = "/bin/bash -c '" + script + "'"
        functest_utils.execute_command(cmd, logger = logger_debug, exit_on_error=False)

        install_promise(logger_debug)

        # Create result folder under functest if necessary
        if not os.path.exists(RALLY_RESULT_DIR):
            os.makedirs(RALLY_RESULT_DIR)

        try:
            logger.info("CI: Generate the list of executable tests.")
            runnable_test = functest_utils.generateTestcaseList(functest_yaml)
            logger.info("List of runnable tests generated: %s" % runnable_test)
        except:
            logger.error("Impossible to generate the list of runnable tests")

        exit(0)


def action_check():
    """
    Check if the functest environment is properly installed
    """
    errors_all = False
    errors = False
    logger.info("Checking current functest configuration...")

    logger.debug("Checking script directories...")

    dirs = [RALLY_DIR, RALLY_INSTALLATION_DIR, VPING_DIR, ODL_DIR]
    for dir in dirs:
        if not os.path.exists(dir):
            logger.debug("The directory '%s' does NOT exist." % dir)
            errors = True
            errors_all = True
        else:
            logger.debug("   %s found" % dir)
    if not errors:
        logger.debug("...OK")
    else:
        logger.debug("...FAIL")


    logger.debug("Checking Rally deployment...")
    if not check_rally():
        logger.debug("   Rally deployment NOT installed.")
        errors_all = True
        logger.debug("...FAIL")
    else:
        logger.debug("...OK")

    logger.debug("Checking Image...")
    errors = False
    if not os.path.isfile(IMAGE_PATH):
        logger.debug("   Image file '%s' NOT found." % IMAGE_PATH)
        errors = True
        errors_all = True
    else:
        logger.debug("   Image file found in %s" % IMAGE_PATH)


    if not errors:
        logger.debug("...OK")
    else:
        logger.debug("...FAIL")

    #TODO: check OLD environment setup
    return not errors_all



def action_clean():
    """
    Clean the existing functest environment
    """
    logger.info("Removing current functest environment...")
    if os.path.exists(RALLY_INSTALLATION_DIR):
        logger.debug("Removing Rally installation directory %s" % RALLY_INSTALLATION_DIR)
        shutil.rmtree(RALLY_INSTALLATION_DIR,ignore_errors=True)

    if os.path.exists(RALLY_RESULT_DIR):
        logger.debug("Removing Result directory")
        shutil.rmtree(RALLY_RESULT_DIR,ignore_errors=True)

    logger.debug("Cleaning up the OpenStack deployment...")
    cmd='python ' + FUNCTEST_REPO + \
        '/testcases/VIM/OpenStack/CI/libraries/clean_openstack.py --debug'
    functest_utils.execute_command(cmd,logger)
    logger.info("Functest environment clean!")



def install_rally():
    if check_rally():
        logger.info("Rally is already installed.")
    else:
        logger.debug("Executing %s/install_rally.sh..." %RALLY_REPO_DIR)
        install_script = RALLY_REPO_DIR + "/install_rally.sh --yes"
        cmd = 'sudo ' + install_script
        if os.environ.get("CI_DEBUG") == "false":
            functest_utils.execute_command(cmd)
        else:
            functest_utils.execute_command(cmd,logger)

        logger.debug("Creating Rally environment...")
        cmd = "rally deployment create --fromenv --name="+DEPLOYMENT_MAME
        functest_utils.execute_command(cmd,logger)

        logger.debug("Installing tempest...")
        cmd = "rally verify install"
        functest_utils.execute_command(cmd,logger)

        cmd = "rally deployment check"
        functest_utils.execute_command(cmd,logger)
        #TODO: check that everything is 'Available' and warn if not

        cmd = "rally show images"
        functest_utils.execute_command(cmd,logger)

        cmd = "rally show flavors"
        functest_utils.execute_command(cmd,logger)

    return True

def install_promise(logger_debug):
    logger.info("Installing dependencies for Promise testcase...")
    current_dir = os.getcwd()
    os.chdir(REPOS_DIR+'/promise/')

    cmd = 'curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -'
    functest_utils.execute_command(cmd,logger = logger_debug, exit_on_error=False)

    cmd = 'sudo apt-get install -y nodejs'
    functest_utils.execute_command(cmd,logger = logger_debug, exit_on_error=False)

    cmd = 'sudo npm -g install npm@latest'
    functest_utils.execute_command(cmd,logger = logger_debug, exit_on_error=False)

    cmd = 'npm install'
    functest_utils.execute_command(cmd,logger = logger_debug, exit_on_error=False)
    os.chdir(current_dir)


def check_rally():
    """
    Check if Rally is installed and properly configured
    """
    if os.path.exists(RALLY_INSTALLATION_DIR):
        logger.debug("   Rally installation directory found in %s" % RALLY_INSTALLATION_DIR)
        FNULL = open(os.devnull, 'w');
        cmd="rally deployment list | grep "+DEPLOYMENT_MAME
        logger.debug('   Executing command : {}'.format(cmd))
        p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=FNULL);
        #if the command does not exist or there is no deployment
        line = p.stdout.readline()
        if line == "":
            logger.debug("   Rally deployment NOT found")
            return False
        logger.debug("   Rally deployment found")
        return True
    else:
        return False


def create_private_neutron_net(neutron):
    neutron.format = 'json'
    logger.info('Creating neutron network %s...' % NEUTRON_PRIVATE_NET_NAME)
    network_id = functest_utils. \
        create_neutron_net(neutron, NEUTRON_PRIVATE_NET_NAME)

    if not network_id:
        return False
    logger.debug("Network '%s' created successfully" % network_id)

    logger.info('Updating neutron network %s...' % NEUTRON_PRIVATE_NET_NAME)
    if functest_utils.update_neutron_net(neutron, network_id, shared=True):
        logger.debug("Network '%s' updated successfully" % network_id)
    else:
        logger.info('Updating neutron network %s failed' % network_id)

    logger.debug('Creating Subnet....')
    subnet_id = functest_utils. \
        create_neutron_subnet(neutron,
                              NEUTRON_PRIVATE_SUBNET_NAME,
                              NEUTRON_PRIVATE_SUBNET_CIDR,
                              network_id)
    if not subnet_id:
        return False
    logger.debug("Subnet '%s' created successfully" % subnet_id)
    logger.debug('Creating Router...')
    router_id = functest_utils. \
        create_neutron_router(neutron, NEUTRON_ROUTER_NAME)

    if not router_id:
        return False

    logger.debug("Router '%s' created successfully" % router_id)
    logger.debug('Adding router to subnet...')

    result = functest_utils.add_interface_router(neutron, router_id, subnet_id)

    if not result:
        return False

    logger.debug("Interface added successfully.")
    network_dic = {'net_id': network_id,
                   'subnet_id': subnet_id,
                   'router_id': router_id}
    return True


def main():
    if not (args.action in actions):
        logger.error('argument not valid')
        exit(-1)


    if not functest_utils.check_credentials():
        logger.error("Please source the openrc credentials and run the script again.")
        #TODO: source the credentials in this script
        exit(-1)


    if args.action == "start":
        action_start()

    if args.action == "check":
        if action_check():
            logger.info("Functest environment correctly installed")
        else:
            logger.info("Functest environment not found or faulty")

    if args.action == "clean":
        if args.force :
            action_clean()
        else :
            while True:
                print("Are you sure? [y|n]")
                answer = raw_input("")
                if answer == "y":
                    action_clean()
                    break
                elif answer == "n":
                    break
                else:
                    print("Invalid option.")
    exit(0)


if __name__ == '__main__':
    main()