Example #1
0
def run_cmd_on_server(issue_cmd, server_ip, username,
                      password, pty=True, as_sudo=False,
                      logger=None,
                      container=None,
                      detach=None,
                      shell_prefix='/bin/bash -c '
                      ):
    '''
    container : name or id of the container to run the cmd( str)
    '''
    logger = logger or contrail_logging.getLogger(__name__)
    updated_cmd = issue_cmd
    with hide('everything'):
        with settings(
            host_string='%s@%s' % (username, server_ip), password=password,
                warn_only=True, abort_on_prompts=False):
            _run = sudo if as_sudo else run
            if container:
                _run = sudo
                container_args = ''
                container_args += ' -d ' if detach else ''
                container_args += ' --privileged '
                container_args += ' -it ' if pty else ''
                container_args += container
                updated_cmd = 'docker exec %s %s \'%s\'' % (container_args,
                                                       shell_prefix,
                                                       issue_cmd)
            logger.debug('[%s]: Running cmd : %s' % (server_ip, updated_cmd))
            output = _run(updated_cmd, pty=pty)
            logger.debug('Output : %s' % (output))
            return output
Example #2
0
 def __init__(self,
              connections,
              name=None,
              namespace='default',
              csrx_version='19.2R1.8',
              csrx_path='hub.juniper.net/security/csrx',
              node_label=None,
              pod_label=None,
              match_label=None,
              fqname=None):
     self.logger = connections.logger or contrail_logging.getLogger(
         __name__)
     self.inputs = connections.inputs
     self.name = name or get_random_name('hbs')
     self.namespace = namespace
     self.k8s_client = connections.k8s_client
     self.annotations = None
     self.ds_name = None
     self.hbs_created = False
     self.node_label = node_label or {"type": "hbf"}
     self.match_label = match_label or {"type": "hbf"}
     self.pod_label = pod_label or {"type": "hbf"}
     self.csrx_version = csrx_version
     self.csrx_path = csrx_path
     self.already_exists = None
     self.leftnad = None
     self.rightnad = None
     self.connections = connections
     self.vnc_lib = connections.get_vnc_lib_h()
     self.agent_inspect = connections.agent_inspect
     self.username = "******"
     self.password = "******"
Example #3
0
def copy_file_from_server(host,
                          src_file_path,
                          dest_folder,
                          container=None,
                          logger=None):
    '''
    Can copy files using wildcard.
    Note that docker cp does not support wildcard yet
    '''
    logger = logger or contrail_logging.getLogger(__name__)
    if container:
        tmp_dest_folder = '/tmp'
    basename = os.path.basename(src_file_path)

    with settings(host_string='%s@%s' % (host['username'], host['ip']),
                  password=host['password'],
                  warn_only=True,
                  abort_on_prompts=False):
        if container:
            run('docker cp %s:%s %s' %
                (container, src_file_path, tmp_dest_folder))
        else:
            tmp_dest_folder = os.path.dirname(src_file_path)
        tmp_dest_path = '%s/%s' % (tmp_dest_folder, basename)
        get('%s/%s' % (tmp_dest_folder, basename), dest_folder)
        if container:
            run('rm -f %s/%s' % (tmp_dest_folder, basename))
        return True
Example #4
0
def sshable(host_string,
            password=None,
            gateway=None,
            gateway_password=None,
            logger=None,
            timeout=5):
    logger = logger or contrail_logging.getLogger(__name__)
    host_string_split = re.split(r"[@:]", host_string)
    host_port = host_string_split[2] if len(host_string_split) > 2 else '22'
    with hide('everything'), settings(host_string=gateway,
                                      password=gateway_password,
                                      warn_only=True):
        try:
            (ret_val, result) = safe_run('(echo > /dev/tcp/%s/%s)' %
                                         (host_string_split[1], host_port),
                                         timeout=timeout)
            if result.succeeded:
                if safe_run('(echo > /dev/tcp/%s/%s)' %
                            (host_string_split[1], host_port),
                            timeout=timeout)[1].succeeded:
                    return True
                else:
                    logger.debug("Error on ssh to %s, result: %s %s" %
                                 (host_string, result, result.__dict__))
                    return False
            else:
                logger.debug("Error on ssh to %s, result: %s %s" %
                             (host_string, result, result.__dict__))
                return False
        except CommandTimeout, e:
            logger.debug('Could not ssh to %s ' % (host_string))
            return False
Example #5
0
def stop_tcpdump_for_intf(session, pcap, logger=None):
    if not logger:
        logger = contrail_logging.getLogger(__name__)
    cmd = 'sudo kill $(ps -ef|grep tcpdump | grep pcap| awk \'{print $2}\')'
    execute_cmd(session, cmd, logger)
    sleep(2)
    return True
 def __init__(self, username, password, project_name,
              inputs=None, logger=None, auth_url=None, region_name=None,
              certfile=None, keyfile=None, cacert=None, insecure=True, domain_name=None,scope='domain'):
     self.inputs = inputs
     self.user = username
     self.passwd = password
     self.project = project_name
     self.scope = scope
     self.logger = logger or contrail_logging.getLogger(__name__)
     if inputs:
         self.auth_url = inputs.auth_url
         self.region_name = inputs.region_name
         self.domain_name = domain_name or self.inputs.admin_domain
         self.keystone_certfile = self.inputs.keystonecertfile
         self.keystone_keyfile = self.inputs.keystonekeyfile
         self.certbundle = self.inputs.certbundle
         self.insecure = self.inputs.insecure
     else:
         self.auth_url = auth_url or os.getenv('OS_AUTH_URL')
         self.region_name = region_name or os.getenv('OS_REGION_NAME')
         self.domain_name = domain_name or os.getenv('OS_DOMAIN_NAME')
         self.keystone_certfile = certfile
         self.keystone_keyfile = keyfile
         self.insecure = insecure
         self.certbundle = cacert
     self.reauth()
Example #7
0
    def __init__(self,
                 connections,
                 name=None,
                 namespace='default',
                 default_backend=None,
                 rules=None,
                 tls=None,
                 metadata=None,
                 spec=None):
        self.logger = connections.logger or contrail_logging.getLogger(
            __name__)
        self.inputs = connections.inputs
        self.name = name or metadata.get('name') or get_random_name('ingress')
        self.namespace = namespace
        self.k8s_client = connections.k8s_client
        self.vnc_api_h = connections.vnc_lib
        self.metadata = {} if metadata is None else metadata
        self.spec = {} if spec is None else spec
        self.rules = [] if rules is None else rules
        self.tls = [] if tls is None else tls
        self.default_backend = {} if default_backend is None else default_backend
        self.v1_beta_h = self.k8s_client.v1_beta_h
        self.connections = connections

        self.verify_is_run = False
        self.already_exists = None
Example #8
0
    def __init__(self,
                 connections,
                 name=None,
                 namespace='default',
                 metadata={},
                 spec={},
                 shell=None):
        self.logger = connections.logger or contrail_logging.getLogger(
            __name__)
        self.inputs = connections.inputs
        self.name = name or get_random_name('pod')
        self.namespace = namespace
        self.k8s_client = connections.k8s_client
        self.metadata = metadata
        self.spec = spec
        self.already_exists = None
        self.shell = shell or '/bin/sh'
        self._shell_arg = '%s -l -c' % (self.shell)

        self.connections = connections
        self.vnc_lib = connections.get_vnc_lib_h()
        self.agent_inspect = connections.agent_inspect

        self.api_vm_obj = None
        self.vmi_objs = []
        self.tap_intfs = []
Example #9
0
 def __init__(self,
              inputs,
              username,
              password,
              project_name,
              project_id,
              vnclib=None,
              logger=None,
              auth_server_ip=None):
     self.logger = logger or contrail_logging.getLogger(__name__)
     super(OpenstackOrchestrator, self).__init__(inputs, vnclib,
                                                 self.logger)
     self.inputs = inputs
     self.quantum_h = None
     self.nova_h = None
     self.username = username
     self.password = password
     self.project_name = project_name
     self.project_id = project_id
     self.vnc_lib = vnclib
     self.auth_server_ip = auth_server_ip
     self.region_name = inputs.region_name
     if not auth_server_ip:
         self.auth_server_ip = self.inputs.auth_ip
     #for vcenter as compute
     self.vcntr_handle = self.get_vcenter_handle()
Example #10
0
 def __init__(self, host, username='******', password='******',
     logger=None, **kwargs):
     self.host = host
     self.username = username
     self.password = password
     self.handle = None
     self.logger = kwargs.get('logger', contrail_logging.getLogger(__name__))
Example #11
0
 def __init__(self, username, password, project_name,
              inputs=None, logger=None, auth_url=None, region_name=None,
              certfile=None, keyfile=None, cacert=None, insecure=True, domain_name=None,scope='domain'):
     self.inputs = inputs
     self.user = username
     self.passwd = password
     self.project = project_name
     self.scope = scope
     self.logger = logger or contrail_logging.getLogger(__name__)
     if inputs:
         self.auth_url = inputs.auth_url
         self.region_name = inputs.region_name
         self.domain_name = domain_name or self.inputs.admin_domain
         self.keystone_certfile = self.inputs.keystonecertfile
         self.keystone_keyfile = self.inputs.keystonekeyfile
         self.certbundle = self.inputs.certbundle
         self.insecure = self.inputs.insecure
         self.scope = 'project' if inputs.use_project_scoped_token else scope
     else:
         self.auth_url = auth_url or os.getenv('OS_AUTH_URL')
         self.region_name = region_name or os.getenv('OS_REGION_NAME')
         self.domain_name = domain_name or os.getenv('OS_DOMAIN_NAME')
         self.keystone_certfile = certfile
         self.keystone_keyfile = keyfile
         self.insecure = insecure
         self.certbundle = cacert
     self.reauth()
Example #12
0
 def __init__(self, input_file, report_details, logger=None):
     self.input_file = input_file
     self.build_id = None
     self.bgp_stress = False
     self.logger = logger or contrail_logging.getLogger(__name__)
     self.log_scenario = 'Sanity'
     if 'EMAIL_SUBJECT' in os.environ and os.environ['EMAIL_SUBJECT'] != '':
         self.log_scenario = os.environ.get('EMAIL_SUBJECT')
     if 'EMAIL_SUBJECT_PREFIX' in os.environ:
         self.log_scenario = '%s %s' % (
             os.environ.get('EMAIL_SUBJECT_PREFIX'), self.log_scenario)
     cwd = os.getcwd()
     log_path = ('%s' + '/logs/') % cwd
     for file in os.listdir(log_path):
         if file.startswith("results_summary") and file.endswith(".txt"):
             self.bgp_stress = True
     self.ts = self.get_os_env('SCRIPT_TS') or \
         datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
     self.core_location = self.ts
     self.jenkins_trigger = self.get_os_env('JENKINS_TRIGGERED')
     if self.jenkins_trigger:
         self.ts = self.ts + '_' + str(time.time())
     self.report_details_file = report_details
     self.distro = None
     super(ContrailReportInit, self).__init__(input_file, self.logger)
Example #13
0
 def __init__(self, input_file, report_details, logger=None):
     self.input_file = input_file
     self.build_id = None
     self.bgp_stress = False
     self.logger = logger or contrail_logging.getLogger(__name__)
     self.log_scenario = 'Sanity'
     if 'EMAIL_SUBJECT' in os.environ and os.environ['EMAIL_SUBJECT'] != '':
         self.log_scenario = os.environ.get('EMAIL_SUBJECT')
     if 'EMAIL_SUBJECT_PREFIX' in os.environ:
         self.log_scenario = '%s %s' % (os.environ.get('EMAIL_SUBJECT_PREFIX'),
                                        self.log_scenario)
     cwd = os.getcwd()
     log_path = ('%s' + '/logs/') % cwd
     for file in os.listdir(log_path):
         if file.startswith("results_summary") and file.endswith(".txt"):
             self.bgp_stress = True
     self.ts = self.get_os_env('SCRIPT_TS') or \
         datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
     self.core_location = self.ts
     self.jenkins_trigger = self.get_os_env('JENKINS_TRIGGERED')
     if self.jenkins_trigger:
         self.ts = self.ts + '_' + str(time.time())
     self.report_details_file = report_details
     self.distro = None
     super(ContrailReportInit, self).__init__(input_file, self.logger)
Example #14
0
    def __init__(self,
                 connections,
                 name=None,
                 namespace='default',
                 metadata=None,
                 spec=None,
                 shell=None,
                 custom_isolation = False,
                 fq_network_name = {}):
        self.logger = connections.logger or contrail_logging.getLogger(
            __name__)
        self.inputs = connections.inputs
        self.name = name or metadata.get('name') or get_random_name('pod')
        self.namespace = namespace
        self.k8s_client = connections.k8s_client
        self.metadata = {} if metadata is None else metadata
        self.spec = {} if spec is None else spec
        self.already_exists = None
        self.shell = shell or '/bin/sh'
        self._shell_arg = '%s -l -c' % (self.shell)
        self.custom_isolation = custom_isolation
        self.fq_network_name = fq_network_name
        self.connections = connections
        self.vnc_lib = connections.get_vnc_lib_h()
        self.agent_inspect = connections.agent_inspect

        self.api_vm_obj = None
        self.vmi_objs = []
        self.tap_intfs = []
        self.host_ip = None
        self.compute_ip = None
        self.vmi_objs = []
        self.vmi_uuids = []
        self.vn_names = []
        self.vn_fq_names = []
Example #15
0
    def __init__(self,
                 config_file='/etc/kubernetes/admin.conf',
                 logger=None,
                 cluster=None):
        if cluster:
            config_file = cluster['kube_config_file']
        cfg = client.Configuration()
        config.load_kube_config(config_file=config_file,
                                client_configuration=cfg)
        cfg.assert_hostname = False
        if cluster:
            (proto, _, port) = cfg.host.split(':')
            host = proto + '://' + cluster['master_public_ip'] + ':' + port
            cfg.host = host
            cfg.verify_ssl = False
        self.api_client = client.ApiClient(cfg)
        #self.res_v1_beta1_h = client.ApiextensionsV1beta1Api(self.api_client)
        self.res_v1_obj_h = client.CustomObjectsApi(self.api_client)
        self.v1_h = client.CoreV1Api(self.api_client)
        self.v1_h.read_namespace('default')
        self.v1_beta_h = client.ExtensionsV1beta1Api(self.api_client)
        self.v1_networking = client.NetworkingV1Api(self.api_client)
        self.apps_v1_beta1_h = client.AppsV1beta1Api(self.api_client)

        self.logger = logger or contrail_logging.getLogger(__name__)
Example #16
0
 def __init__(self,
              username=None,
              password=None,
              tenant=None,
              domain_name=None,
              auth_url=None,
              insecure=True,
              region_name=None,
              cert=None,
              key=None,
              cacert=None,
              version=None,
              logger=None,
              scope='domain'):
     self.sessions = dict()
     self.logger = logger or contrail_logging.getLogger(__name__)
     self.auth_url = auth_url
     self.username = username
     self.password = password
     self.project = tenant
     self.domain_name = domain_name or 'Default'
     self.cert = cert
     self.key = key
     self.cacert = cacert
     self.region_name = region_name
     self.insecure = insecure
     self.version = self.get_version(version)
     self.scope = 'project' if self.version == '2' else scope
     self.keystone = self.get_client(self.scope)
     self.session = self.keystone.session
Example #17
0
def sshable(host_string, password=None, gateway=None, gateway_password=None,
            logger=None, timeout=5):
    logger = logger or contrail_logging.getLogger(__name__)
    host_string_split = re.split(r"[@:]", host_string)
    host_port = host_string_split[2] if len(host_string_split) > 2 else '22'
    with hide('everything'), settings(host_string=gateway,
                                      password=gateway_password,
                                      warn_only=True):
        try:
            (ret_val, result) = safe_run('(echo > /dev/tcp/%s/%s)' % (host_string_split[1],
                                                                      host_port), timeout=timeout)
            if result.succeeded:
                if safe_run('(echo > /dev/tcp/%s/%s)' % (host_string_split[1], host_port), timeout=timeout)[1].succeeded:
                    return True
                else:
                    logger.debug("Error on ssh to %s, result: %s %s" % (host_string,
                        result, result.__dict__))
                    return False
            else:
                logger.debug("Error on ssh to %s, result: %s %s" % (host_string,
                    result, result.__dict__))
                return False
        except CommandTimeout, e:
            logger.debug('Could not ssh to %s ' % (host_string))
            return False
Example #18
0
    def __init__(self,
                 connections,
                 name=None,
                 namespace='default',
                 metadata=None,
                 spec=None,
                 shell=None,
                 custom_isolation=False,
                 fq_network_name={}):
        self.logger = connections.logger or contrail_logging.getLogger(
            __name__)
        self.inputs = connections.inputs
        self.name = name or metadata.get('name') or get_random_name('pod')
        self.namespace = namespace
        self.k8s_client = connections.k8s_client
        self.metadata = {} if metadata is None else metadata
        self.spec = {} if spec is None else spec
        self.already_exists = None
        self.shell = shell or '/bin/sh'
        self._shell_arg = '%s -l -c' % (self.shell)
        self.custom_isolation = custom_isolation
        self.fq_network_name = fq_network_name
        self.connections = connections
        self.vnc_lib = connections.get_vnc_lib_h()
        self.agent_inspect = connections.agent_inspect

        self.api_vm_obj = None
        self.vmi_objs = []
        self.tap_intfs = []
        self.host_ip = None
        self.compute_ip = None
        self.vmi_objs = []
        self.vmi_uuids = []
        self.vn_names = []
        self.vn_fq_names = []
 def __init__(self, host, username='******', password='******',
     logger=None, **kwargs):
     self.host = host
     self.username = username
     self.password = password
     self.handle = None
     self.logger = kwargs.get('logger', contrail_logging.getLogger(__name__))
    def __init__(self,
                 connections,
                 name=None,
                 isolation=False,
                 custom_isolation=False,
                 ip_fabric_snat=False,
                 ip_fabric_forwarding=False,
                 fq_network_name=None):
        self.connections = connections
        self.logger = connections.logger or contrail_logging.getLogger(
            __name__)
        self.name = name or get_random_name('namespace')
        self.k8s_client = connections.k8s_client
        self.vnc_api_h = connections.vnc_lib
        self.isolation = isolation
        self.ip_fabric_snat = ip_fabric_snat
        self.ip_fabric_forwarding = ip_fabric_forwarding
        self.custom_isolation = custom_isolation
        self.fq_network_name = fq_network_name

        self.already_exists = False
        self.api_s_obj = None
        self.project_name = None
        self.project_fq_name = None
        self.inputs = self.connections.inputs
        self.project_isolation = True
        self.verify_is_run = False
Example #21
0
def run_cmd_on_server(issue_cmd,
                      server_ip,
                      username,
                      password,
                      pty=True,
                      as_sudo=False,
                      logger=None,
                      container=None,
                      detach=None,
                      shell_prefix='/bin/bash -c '):
    '''
    container : name or id of the container to run the cmd( str)
    '''
    logger = logger or contrail_logging.getLogger(__name__)
    updated_cmd = issue_cmd
    with hide('everything'):
        with settings(host_string='%s@%s' % (username, server_ip),
                      password=password,
                      warn_only=True,
                      abort_on_prompts=False):
            _run = sudo if as_sudo else run
            if container:
                _run = sudo
                container_args = ''
                container_args += ' -d ' if detach else ''
                container_args += ' --privileged '
                container_args += ' -it ' if pty else ''
                container_args += container
                updated_cmd = 'docker exec %s %s \'%s\'' % (
                    container_args, shell_prefix, issue_cmd)
            logger.debug('[%s]: Running cmd : %s' % (server_ip, updated_cmd))
            output = _run(updated_cmd, pty=pty)
            logger.debug('Output : %s' % (output))
            return output
Example #22
0
 def __init__(self,
              connections,
              name=None,
              namespace='default',
              metadata=None,
              spec=None):
     self.logger = connections.logger or contrail_logging.getLogger(
         __name__)
     self.name = name or metadata.get('name') or get_random_name('service')
     self.namespace = namespace
     self.k8s_client = connections.k8s_client
     self.vnc_api_h = connections.vnc_lib
     self.metadata = {} if metadata is None else metadata
     self.spec = {} if spec is None else spec
     self.v1_networking = self.k8s_client.v1_networking
     self.agent_inspect = connections.agent_inspect
     self.connections = connections
     self.inputs = connections.inputs
     if self.inputs.slave_orchestrator == 'kubernetes':
         prefix = 'default-policy-management:%s' % self.connections.project_name
         self.k8s_default_network_policies = [prefix + '-allowall',
                                         prefix + '-Ingress',
                                         prefix + '-denyall']
         self.k8s_defaut_aps = prefix
     else:
         self.k8s_default_network_policies = ['default-policy-management:k8s-allowall',
                                             'default-policy-management:k8s-Ingress',
                                             'default-policy-management:k8s-denyall']
         self.k8s_defaut_aps = "default-policy-management:k8s"
     
     self.already_exists = None
Example #23
0
    def __init__(self,
                 connections,
                 name=None,
                 namespace='default',
                 default_backend=None,
                 rules=None,
                 tls=None,
                 metadata=None,
                 spec=None):
        self.logger = connections.logger or contrail_logging.getLogger(
            __name__)
        self.inputs = connections.inputs
        self.name = name or metadata.get('name') or get_random_name('ingress')
        self.namespace = namespace
        self.k8s_client = connections.k8s_client
        self.vnc_api_h = connections.vnc_lib
        self.metadata = {} if metadata is None else metadata
        self.spec = {} if spec is None else spec
        self.rules = [] if rules is None else rules
        self.tls = [] if tls is None else tls
        self.default_backend = {} if default_backend is None else default_backend
        self.v1_beta_h = self.k8s_client.v1_beta_h
        self.connections = connections

        self.verify_is_run = False
        self.already_exists = None
Example #24
0
def stop_tcpdump_for_intf(session, pcap, logger=None):
    if not logger:
        logger = contrail_logging.getLogger(__name__)
    cmd = 'sudo kill $(ps -ef|grep tcpdump | grep pcap| awk \'{print $2}\')'
    execute_cmd(session, cmd, logger)
    sleep(2)
    return True
Example #25
0
 def setUp(self):
     super(VncLibFixture, self).setUp()
     if not self.connections:
         self.logger = self.logger or contrail_logging.getLogger(__name__)
         self.vnc_api_h = VncApi(username=self.username,
                                 password=self.password,
                                 tenant_name=self.project_name,
                                 domain_name=self.orch_domain,
                                 api_server_host=self.cfgm_ip,
                                 api_server_port=self.api_server_port,
                                 auth_host=self.auth_server_ip,
                                 auth_port=self.auth_port,
                                 api_server_use_ssl=self.use_ssl,
                                 auth_url=self.authn_url)
         if self.orchestrator == 'openstack':
             self.auth_client = OpenstackAuth(self.username,
                                              self.password,
                                              self.project_name,
                                              domain_name=self.orch_domain,
                                              auth_url=self.auth_url,
                                              certfile=self.certfile,
                                              keyfile=self.keyfile,
                                              cacert=self.cacert,
                                              insecure=self.insecure,
                                              logger=self.logger,
                                              scope='project')
         elif self.orchestrator == 'vcenter':
             self.auth_client = VcenterAuth(self.username, self.password,
                                            self.project_name, self.inputs)
     if self.orch:
         self.vnc_h = self.orch.vnc_h
     else:
         self.vnc_h = ContrailVncApi(self.vnc_api_h, self.logger)
Example #26
0
    def __init__(self,
                 connections,
                 name=None,
                 namespace='default',
                 metadata=None,
                 spec=None):
        self.logger = connections.logger or contrail_logging.getLogger(
            __name__)
        self.name = name or metadata.get('name') or get_random_name('service')
        self.namespace = namespace
        self.k8s_client = connections.k8s_client
        self.vnc_api_h = connections.vnc_lib
        self.metadata = {} if metadata is None else metadata
        self.spec = {} if spec is None else spec
        self.v1_networking = self.k8s_client.v1_networking
        self.agent_inspect = connections.agent_inspect
        self.connections = connections
        self.inputs = connections.inputs
        self.k8s_default_network_policies = [
            'default-policy-management:k8s-allowall',
            'default-policy-management:k8s-Ingress',
            'default-policy-management:k8s-denyall'
        ]
        self.k8s_defaut_aps = "default-policy-management:k8s"

        self.already_exists = None
Example #27
0
def run_fab_cmd_on_node(host_string,
                        password,
                        cmd,
                        as_sudo=False,
                        timeout=120,
                        as_daemon=False,
                        raw=False,
                        warn_only=True,
                        logger=None):
    """
    Run fab command on a node. Usecase : as part of script running on cfgm node, can run a cmd on VM from compute node

    If raw is True, will return the fab _AttributeString object itself without removing any unwanted output
    """
    logger = logger or contrail_logging.getLogger(__name__)
    cmd = _escape_some_chars(cmd)
    (username, host_ip) = host_string.split('@')
    copy_fabfile_to_agent()
    cmd_args = '-u %s -p "%s" -H %s -D --hide status,user,running' % (
        username, password, host_ip)
    if warn_only:
        cmd_args += ' -w '
    cmd_str = 'fab %s ' % (cmd_args)
    if as_daemon:
        cmd_str += '--no-pty '
        cmd = 'nohup ' + cmd + ' &'
    if username == 'root':
        as_sudo = False
    elif username == 'cirros':
        cmd_str += ' -s "/bin/sh -l -c" '
    if as_sudo:
        cmd_str += 'sudo_command:\"%s\"' % (cmd)
    else:
        cmd_str += 'command:\"%s\"' % (cmd)
    # Sometimes, during bootup, there could be some intermittent conn. issue
    logger.debug(cmd_str)
    tries = 1
    output = None
    while tries > 0:
        if timeout:
            try:
                output = sudo(cmd_str, timeout=timeout)
                logger.debug(output)
            except CommandTimeout:
                return output
        else:
            output = run(cmd_str)
        if ((output) and ('Fatal error' in output)):
            tries -= 1
            time.sleep(5)
        else:
            break
    # end while

    if not raw:
        real_output = remove_unwanted_output(output)
    else:
        real_output = output
    return real_output
Example #28
0
    def __init__(self, connections, name=None):
        self.connections = connections
        self.logger = connections.logger or contrail_logging.getLogger(__name__)
        self.name = name or get_random_name('namespace')
        self.k8s_client = connections.k8s_client
        self.vnc_api_h = connections.vnc_lib

        self.already_exists = False
Example #29
0
def main():
    from common import contrail_test_init
    inputs = contrail_test_init.ContrailTestInit(sys.argv[1])
    host = sys.argv[2] if len(sys.argv) > 2 else None
    role = sys.argv[3] if len(sys.argv) > 3 else None
    service = sys.argv[4] if len(sys.argv) > 4 else None
    logger = contrail_logging.getLogger('contrail_status')
    contrail_status(inputs, host, role, service, logger=logger)
Example #30
0
def main():
    from common import contrail_test_init
    inputs = contrail_test_init.ContrailTestInit(sys.argv[1])
    host = sys.argv[2] if len(sys.argv) > 2 else None
    role = sys.argv[3] if len(sys.argv) > 3 else None
    service = sys.argv[4] if len(sys.argv) > 4 else None
    logger = contrail_logging.getLogger('contrail_status')
    contrail_status(inputs, host, role, service, logger=logger)
Example #31
0
def start_tcpdump_for_intf(ip, username, password, interface, filters='-v', logger=None):
    if not logger:
        logger = contrail_logging.getLogger(__name__)
    session = ssh(ip, username, password)
    pcap = '/tmp/%s_%s.pcap' % (interface, get_random_name())
    cmd = 'tcpdump -ni %s -U %s -w %s' % (interface, filters, pcap)
    execute_cmd(session, cmd, logger)
    return (session, pcap)
Example #32
0
    def __init__(self, config_file='/etc/kubernetes/admin.conf', logger=None):
        self.api_client = config.new_client_from_config(config_file)
        self.api_client.config.assert_hostname = False
        self.v1_h = client.CoreV1Api(self.api_client)
        self.v1_h.read_namespace('default')
        self.v1_beta_h = client.ExtensionsV1beta1Api(self.api_client)

        self.logger = logger or contrail_logging.getLogger(__name__)
def start_tcpdump_for_intf(ip, username, password, interface, filters='-v', logger=None):
    if not logger:
        logger = contrail_logging.getLogger(__name__)
    session = ssh(ip, username, password)
    pcap = '/tmp/%s_%s.pcap' % (interface, get_random_name())
    cmd = 'tcpdump -nni %s -U %s -w %s' % (interface, filters, pcap)
    execute_cmd(session, cmd, logger)
    return (session, pcap)
Example #34
0
    def __init__(self, ini_file, report_details, logger=None):
        self.build_id = None
        self.bgp_stress = False
        self.logger = logger or contrail_logging.getLogger(__name__)
        self.config = ConfigParser.ConfigParser()
        self.config.read(ini_file)
        self.orch = read_config_option(self.config, 'Basic', 'orchestrator',
                                       'openstack')
        self.prov_file = read_config_option(self.config,
                                            'Basic', 'provFile', None)
        self.log_scenario = read_config_option(self.config,
                                               'Basic', 'logScenario', 'Sanity')
        if 'EMAIL_SUBJECT' in os.environ and os.environ['EMAIL_SUBJECT'] != '':
            self.log_scenario = os.environ.get('EMAIL_SUBJECT')
        if 'EMAIL_SUBJECT_PREFIX' in os.environ:
            self.log_scenario = '%s %s' % (os.environ.get('EMAIL_SUBJECT_PREFIX'),
                                           self.log_scenario)
        self.ext_rtr = read_config_option(
            self.config, 'router', 'router_info', 'None')
        self.ui_browser = read_config_option(self.config,
                                             'ui', 'browser', None)
        cwd = os.getcwd()
        log_path = ('%s' + '/logs/') % cwd
        for file in os.listdir(log_path):
            if file.startswith("results_summary") and file.endswith(".txt"):
                self.bgp_stress = True

        # Web Server related details
        self.web_server = read_config_option(self.config,
                                             'WebServer', 'host', None)
        self.web_server_user = read_config_option(self.config,
                                                  'WebServer', 'username', None)
        self.web_server_password = read_config_option(self.config,
                                                      'WebServer', 'password', None)
        self.web_server_report_path = read_config_option(self.config,
                                                         'WebServer', 'reportPath', None)
        self.web_server_log_path = read_config_option(self.config,
                                                      'WebServer', 'logPath', None)
        self.web_root = read_config_option(self.config,
                                           'WebServer', 'webRoot', None)
        # Mail Setup
        self.smtpServer = read_config_option(self.config,
                                             'Mail', 'server', None)
        self.smtpPort = read_config_option(self.config,
                                           'Mail', 'port', '25')
        self.mailTo = read_config_option(self.config,
                                         'Mail', 'mailTo', None)
        self.mailSender = read_config_option(self.config,
                                             'Mail', 'mailSender', '*****@*****.**')
        self.ts = self.get_os_env('SCRIPT_TS') or \
            datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
        self.core_location = self.ts
        self.single_node = self.get_os_env('SINGLE_NODE_IP')
        self.jenkins_trigger = self.get_os_env('JENKINS_TRIGGERED')
        if self.jenkins_trigger:
            self.ts = self.ts + '_' + str(time.time())
        self.report_details_file = report_details
        self.distro = None
Example #35
0
    def __init__(self, ini_file, report_details, logger=None):
        self.build_id = None
        self.bgp_stress = False
        self.logger = logger or contrail_logging.getLogger(__name__)
        self.config = ConfigParser.ConfigParser()
        self.config.read(ini_file)
        self.orch = read_config_option(self.config, 'Basic', 'orchestrator',
                                       'openstack')
        self.prov_file = read_config_option(self.config,
                                            'Basic', 'provFile', None)
        self.log_scenario = read_config_option(self.config,
                                               'Basic', 'logScenario', 'Sanity')
        if 'EMAIL_SUBJECT' in os.environ and os.environ['EMAIL_SUBJECT'] != '':
            self.log_scenario = os.environ.get('EMAIL_SUBJECT')
        if 'EMAIL_SUBJECT_PREFIX' in os.environ:
            self.log_scenario = '%s %s' % (os.environ.get('EMAIL_SUBJECT_PREFIX'),
                                           self.log_scenario)
        self.ext_rtr = read_config_option(
            self.config, 'router', 'router_info', 'None')
        self.ui_browser = read_config_option(self.config,
                                             'ui', 'browser', None)
        cwd = os.getcwd()
        log_path = ('%s' + '/logs/') % cwd
        for file in os.listdir(log_path):
            if file.startswith("results_summary") and file.endswith(".txt"):
                self.bgp_stress = True

        # Web Server related details
        self.web_server = read_config_option(self.config,
                                             'WebServer', 'host', None)
        self.web_server_user = read_config_option(self.config,
                                                  'WebServer', 'username', None)
        self.web_server_password = read_config_option(self.config,
                                                      'WebServer', 'password', None)
        self.web_server_report_path = read_config_option(self.config,
                                                         'WebServer', 'reportPath', None)
        self.web_server_log_path = read_config_option(self.config,
                                                      'WebServer', 'logPath', None)
        self.web_root = read_config_option(self.config,
                                           'WebServer', 'webRoot', None)
        # Mail Setup
        self.smtpServer = read_config_option(self.config,
                                             'Mail', 'server', None)
        self.smtpPort = read_config_option(self.config,
                                           'Mail', 'port', '25')
        self.mailTo = read_config_option(self.config,
                                         'Mail', 'mailTo', None)
        self.mailSender = read_config_option(self.config,
                                             'Mail', 'mailSender', '*****@*****.**')
        self.ts = self.get_os_env('SCRIPT_TS') or \
            datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
        self.core_location = self.ts
        self.single_node = self.get_os_env('SINGLE_NODE_IP')
        self.jenkins_trigger = self.get_os_env('JENKINS_TRIGGERED')
        if self.jenkins_trigger:
            self.ts = self.ts + '_' + str(time.time())
        self.report_details_file = report_details
        self.distro = None
Example #36
0
    def __init__(self, config_file='/etc/kubernetes/admin.conf', logger=None):
        self.api_client = config.new_client_from_config(config_file)
        self.api_client.configuration.assert_hostname = False
        self.v1_h = client.CoreV1Api(self.api_client)
        self.v1_h.read_namespace('default')
        self.v1_beta_h = client.ExtensionsV1beta1Api(self.api_client)
        self.apps_v1_beta1_h = client.AppsV1beta1Api(self.api_client)

        self.logger = logger or contrail_logging.getLogger(__name__)
Example #37
0
 def __init__(self, vub, logger=LOG, args=None, use_admin_auth=False):
     self.log = logger
     self._vub = vub
     self.more_logger = contrail_logging.getLogger('introspect',
                                                   log_to_console=False)
     # Since introspect log is a single file, need locks
     self.lock = threading.Lock()
     if args:
         pass
Example #38
0
 def __init__(self, auth_h=None, **kwargs):
     self.inputs = kwargs.get('inputs')
     self.logger = kwargs.get('logger') or self.inputs.logger if self.inputs \
                       else contrail_logging.getLogger(__name__)
     self.region_name = kwargs.get('region_name') or \
                        inputs.region_name if self.inputs else None
     if not auth_h:
         auth_h = self.get_auth_h(**kwargs)
     self.auth_h = auth_h
 def __init__(self, vub, logger=LOG, args=None, use_admin_auth=False):
     self.log = logger
     self._vub = vub
     self.more_logger = contrail_logging.getLogger('introspect',
                                                   log_to_console=False)
     # Since introspect log is a single file, need locks
     self.lock = threading.Lock()
     if args:
         pass
Example #40
0
 def __init__(self, auth_h=None, **kwargs):
     self.inputs = kwargs.get('inputs')
     self.logger = kwargs.get('logger') or self.inputs.logger if self.inputs \
                       else contrail_logging.getLogger(__name__)
     self.region_name = kwargs.get('region_name') or \
                        inputs.region_name if self.inputs else None
     if not auth_h:
         auth_h = self.get_auth_h(**kwargs)
     self.auth_h = auth_h
Example #41
0
def fab_put_file_to_vm(host_string, password, src, dest, logger=None):
    logger = logger or contrail_logging.getLogger(__name__)
    copy_fabfile_to_agent()
    (username, host_ip) = host_string.split('@')
    cmd_str = 'fab -u %s -p "%s" -H %s -D -w --hide status,user,running fput:\"%s\",\"%s\"' % (
        username, password, host_ip, src, dest)
    logger.debug(cmd_str)
    output = run(cmd_str)
    real_output = remove_unwanted_output(output)
Example #42
0
def web_invoke(httplink, logger = None):
    logger = logger or contrail_logging.getLogger(__name__)
    output = None
    try:
        cmd = 'curl ' + httplink
        output = subprocess.check_output(cmd, shell=True)
    except Exception, e:
        output = None
        logger.debug(e)
        return output
Example #43
0
def wait_for_ssh_on_node(host_string, password=None, logger=None):
    logger = logger or contrail_logging.getLogger(__name__)
    try:
        with settings(host_string=host_string, password=password):
            fab_connections.connect(host_string)
    except Exception, e:
        # There can be different kinds of exceptions. Catch all
        logger.debug('Host: %s, password: %s Unable to connect yet. Got: %s' % (
            host_string, password, e))
        return False
Example #44
0
def check_pcap_file_exists(session, pcap, expect=True):
    cmd = 'ls -d /tmp/* | grep -w %s ' % (pcap)
    out, err = execute_cmd_out(session, cmd)
    out = bool(out)
    logger = contrail_logging.getLogger(__name__)
    if expect and not out:
        logger.warn("Pcap file not created yet..waiting")
    if expect and out or not expect and not out:
        return True
    return False
Example #45
0
def wait_for_ssh_on_node(host_string, password=None, logger=None):
    logger = logger or contrail_logging.getLogger(__name__)
    try:
        with settings(host_string=host_string, password=password):
            fab_connections.connect(host_string)
    except Exception, e:
        # There can be different kinds of exceptions. Catch all
        logger.debug('Host: %s, password: %s Unable to connect yet. Got: %s' %
                     (host_string, password, e))
        return False
Example #46
0
def check_pcap_file_exists(session, pcap, expect=True):
    cmd = 'ls -d /tmp/* | grep -w %s ' % (pcap)
    out, err = execute_cmd_out(session, cmd)
    out = bool(out)
    logger = contrail_logging.getLogger(__name__)
    if expect and not out:
        logger.warn("Pcap file not created yet..waiting")
    if expect and out or not expect and not out:
        return True
    return False
Example #47
0
def web_invoke(httplink, logger=None):
    logger = logger or contrail_logging.getLogger(__name__)
    output = None
    try:
        cmd = 'curl ' + httplink
        output = subprocess.check_output(cmd, shell=True)
    except Exception, e:
        output = None
        logger.debug(e)
        return output
Example #48
0
def fab_put_file_to_vm(host_string, password, src, dest,
                       logger=None):
    logger = logger or contrail_logging.getLogger(__name__)
    copy_fabfile_to_agent()
    (username, host_ip) = host_string.split('@')
    cmd_str = 'fab -u %s -p "%s" -H %s -D -w --hide status,user,running fput:\"%s\",\"%s\"' % (
        username, password, host_ip, src, dest)
    logger.debug(cmd_str)
    output = run(cmd_str)
    real_output = remove_unwanted_output(output)
Example #49
0
 def __init__(self, auth_h=None, **kwargs):
     self.obj = None
     inputs = kwargs.get('inputs')
     self.logger = kwargs.get('logger') or inputs.logger if inputs \
                       else contrail_logging.getLogger(__name__)
     self.region_name = kwargs.get('region_name') or \
                        inputs.region_name if inputs else None
     if not auth_h:
         auth_h = self.get_auth_h(**kwargs)
     self.auth_h = auth_h
     self.project_id = get_plain_uuid(auth_h.get_project_id())
Example #50
0
 def setUpClass(cls):
     if hasattr(super(BaseTestCase, cls), 'setUpClass'):
         super(BaseTestCase, cls).setUpClass()
     cls.setUpClassCalled = True
     
     if 'TEST_CONFIG_FILE' in os.environ :
         cls.input_file= os.environ.get('TEST_CONFIG_FILE')
     else:
         cls.input_file= 'sanity_params.ini'
     cls.logger = contrail_logging.getLogger(cls.__name__)
     cls.inputs = ContrailTestInit(cls.input_file,logger = cls.logger)
Example #51
0
def run_fab_cmd_on_node(host_string, password, cmd, as_sudo=False, timeout=120, as_daemon=False, raw=False,
                        warn_only=True,
                        logger=None):
    """
    Run fab command on a node. Usecase : as part of script running on cfgm node, can run a cmd on VM from compute node

    If raw is True, will return the fab _AttributeString object itself without removing any unwanted output
    """
    logger = logger or contrail_logging.getLogger(__name__)
    cmd = _escape_some_chars(cmd)
    (username, host_ip) = host_string.split('@')
    copy_fabfile_to_agent()
    cmd_args = '-u %s -p "%s" -H %s -D --hide status,user,running' % (username,
                password, host_ip)
    if warn_only:
        cmd_args+= ' -w '
    cmd_str = 'fab %s ' % (cmd_args)
    if as_daemon:
        cmd_str += '--no-pty '
        cmd = 'nohup ' + cmd + ' &'
    if username == 'root':
        as_sudo = False
    elif username == 'cirros':
        cmd_str += ' -s "/bin/sh -l -c" '
    if as_sudo:
        cmd_str += 'sudo_command:\"%s\"' % (cmd)
    else:
        cmd_str += 'command:\"%s\"' % (cmd)
    # Sometimes, during bootup, there could be some intermittent conn. issue
    logger.debug(cmd_str)
    tries = 1
    output = None
    while tries > 0:
        if timeout:
            try:
                output = sudo(cmd_str, timeout=timeout)
                logger.debug(output)
            except CommandTimeout:
                return output
        else:
            output = run(cmd_str)
        if ((output) and ('Fatal error' in output)):
            tries -= 1
            time.sleep(5)
        else:
            break
    # end while

    if not raw:
        real_output = remove_unwanted_output(output)
    else:
        real_output = output
    return real_output
Example #52
0
 def __init__(self,openstack_ip,username,password):
     import logging
     from fabric.api import env, run, local
     from fabric.operations import get, put, reboot
     from fabric.context_managers import settings, hide
     from fabric.exceptions import NetworkError
     from fabric.contrib.files import exists
     self.logger = contrail_logging.getLogger(__name__)
     self.openstack_ip = openstack_ip
     self.host_data = dict()
     nested_set(self.host_data,[self.openstack_ip,'username'],username) 
     nested_set(self.host_data,[self.openstack_ip,'password'],password) 
Example #53
0
 def __init__(self, connections=None, auth_h=None, **kwargs):
     self.heat_api_version = '1'
     self.logger = kwargs.get('logger') or connections.logger if connections \
                   else contrail_logging.getLogger(__name__)
     auth_h = auth_h or connections.auth if connections else None
     if not auth_h:
         auth_h = self.get_auth_h(**kwargs)
     self.auth = auth_h
     self.obj = None
     self.certfile = kwargs.get('certfile') or auth_h.keystone_certfile
     self.cacert = kwargs.get('cacert') or auth_h.certbundle
     self.keyfile = kwargs.get('keyfile') or auth_h.keystone_keyfile
     self.insecure = kwargs.get('insecure') or auth_h.insecure
Example #54
0
def fab_check_ssh(host_string, password,
                  logger=None):
    logger = logger or contrail_logging.getLogger(__name__)
    copy_fabfile_to_agent()
    (username, host_ip) = host_string.split('@')
    cmd_str = 'fab -u %s -p "%s" -H %s -D -w --hide status,user,running verify_socket_connection:22' % (
        username, password, host_ip)
    logger.debug(cmd_str)
    output = run(cmd_str)
    logger.debug(output)
    if 'True' in output:
        return True
    return False
    def __init__(self, username=None, password=None, tenant=None,
                 auth_url=None, token=None, endpoint=None,
                 insecure=True, region_name=None,
                 logger=None):

        self.logger = logger or contrail_logging.getLogger(__name__)
        if token:
            self.keystone = keystoneclient.Client(
                token=token, endpoint=endpoint)
        else:
            self.keystone = keystone_client.Client(
                username=username, password=password, tenant_name=tenant, auth_url=auth_url,
                insecure=insecure, region_name=region_name or 'RegionOne')
Example #56
0
 def __init__(self, inputs, auth_h, region_name=None, vnclib=None, logger=None):
     self.logger = logger or contrail_logging.getLogger(__name__)
     super(OpenstackOrchestrator, self).__init__(inputs, vnclib, self.logger)
     self.auth_h = auth_h
     self.inputs = inputs
     self.quantum_h = None
     self.nova_h = None
     self.glance_h = None
     self.ironic_h = None
     self.vnc_lib = vnclib
     self.region_name = region_name or inputs.region_name if inputs else None
     #for vcenter as compute
     self.vcntr_handle = self.get_vcenter_handle()
Example #57
0
 def __init__(
         self,
         auth_h=None,
         **kwargs):
     self.obj = None
     inputs = kwargs.get('inputs')
     self.logger = kwargs.get('logger') or inputs.logger if inputs \
                       else contrail_logging.getLogger(__name__)
     self.region_name = kwargs.get('region_name') or \
                        inputs.region_name if inputs else None
     if not auth_h:
         auth_h = self.get_auth_h(**kwargs)
     self.auth_h = auth_h
     self.project_id = get_plain_uuid(auth_h.get_project_id())
def compare_args(key, a, b, exp_name='expected', act_name='actual',
                 logger=None):
    ''' For a given key, compare values a, b got from 2 different databases.
    If instance is dict and not matching, call compare_rules_list to get details'''
    if not logger:
        logger = contrail_logging.getLogger(__name__)
    ret = None
    if a != b:
        ret = key + " not matching --->expected: " + \
            str(a) + " --->got: " + str(b)
    if a != b and isinstance(a, dict):
        ret = compare_rules_list(a, b, exp_name, act_name, logger)
    if a != b and isinstance(a, list):
        ret = compare_rules_list(a, b, exp_name, act_name, logger)
    return ret
Example #59
0
 def __init__(self, user, passwd, project_name,
              inputs=None, logger=None, auth_url=None, region_name=None):
     self.inputs = inputs
     self.user = user
     self.passwd = passwd
     self.project = project_name
     self.logger = logger or contrail_logging.getLogger(__name__)
     self.insecure = bool(os.getenv('OS_INSECURE',True))
     if inputs:
         self.auth_url = inputs.auth_url
         self.region_name = inputs.region_name
     else:
         self.auth_url = auth_url or os.getenv('OS_AUTH_URL')
         self.region_name = region_name or os.getenv('OS_REGION_NAME')
     self.reauth()
Example #60
0
def is_nh_of_local_interface(inspect_h, nh, intf_name, logger=None):
    '''
    '''
    logger = logger or contrail_logging.getLogger(__name__)
    if is_nh_local(nh):
        vif_index = nh.get('encap_oif_id')
        vif_dict = inspect_h.get_vrouter_virtual_interface(vif_index)
        if vif_dict['name'] == intf_name:
            logger.debug('NH %s does indeed point to local interface' % (
                nh, intf_name))
            return True
        else:
            logger.debug('NH %s does not point to local interface' % (
                nh, intf_name))
            return False