Exemplo n.º 1
0
    def __init__(self, devices, options, *args, **kwargs):
        self.devices = devices
        self.options = Options(options)
        self.options.setdefault('build_path', cm.ROOT_PATH)
        self.has_essential_config = None

        super(EMInstallSoftware, self).__init__(*args, **kwargs)
Exemplo n.º 2
0
        def parse_spec(specs, alias=None):
            bits = specs.split('@', 1)
            groups = None
            if len(bits) > 1:
                groups = set(bits[1].split(','))
            bits = bits[0].split(',')

            if len(bits[0].split(':')) > 1:
                address, port = bits[0].split(':')
            else:
                address, port = bits[0], DEFAULT_PORTS['https']

            cred = Options()
            if len(bits) == 1:
                cred.common = DeviceCredential(default_username,
                                               default_password)
                device = DeviceAccess(address, {ADMIN_ROLE: cred})
            elif len(bits) == 2:
                cred.common = DeviceCredential(default_username, bits[1])
                device = DeviceAccess(address, {ADMIN_ROLE: cred})
            elif len(bits) == 3:
                cred.common = DeviceCredential(bits[1], bits[2])
                device = DeviceAccess(address, {ADMIN_ROLE: cred})
            else:
                raise ValueError('Invalid specs: %s', specs)

            device.ports['https'] = port
            device.set_groups(groups)
            return device
Exemplo n.º 3
0
        def parse_spec(specs, alias=None):
            bits = specs.split('@', 1)
            groups = None
            if len(bits) > 1:
                groups = set(bits[1].split(','))
            bits = bits[0].split(',')

            if len(bits[0].split(':')) > 1:
                address, port = bits[0].split(':')
            else:
                address, port = bits[0], DEFAULT_PORTS['https']

            cred = Options()
            if len(bits) == 1:
                cred.common = DeviceCredential(default_username, default_password)
                device = DeviceAccess(address, {ADMIN_ROLE: cred})
            elif len(bits) == 2:
                cred.common = DeviceCredential(default_username, bits[1])
                device = DeviceAccess(address, {ADMIN_ROLE: cred})
            elif len(bits) == 3:
                cred.common = DeviceCredential(bits[1], bits[2])
                device = DeviceAccess(address, {ADMIN_ROLE: cred})
            else:
                raise ValueError('Invalid specs: %s', specs)

            device.ports['https'] = port
            device.set_groups(groups)
            return device
Exemplo n.º 4
0
    def __init__(self, options, address=None, *args, **kwargs):
        self.options = Options(options)
        self.has_essential_config = None
        self.address = address

        LOG.info('Doing: %s', self.address or options.device)
        super(InstallSoftware, self).__init__(*args, **kwargs)
Exemplo n.º 5
0
    def __init__(self, options, address=None, params=None):
        self.options = Options(options)
        self.params = ' '.join(params or [])

        self.sshparams = Options(device=self.options.device,
                                 address=address, timeout=self.options.timeout,
                                 username=self.options.username,
                                 password=self.options.password)

        super(Extractor, self).__init__()
Exemplo n.º 6
0
    def __init__(self, options, address=None, ucs_name=None):
        self.options = Options(options)
        self.ucs_name = ucs_name

        self.sshparams = Options(device=self.options.device,
                                 address=address, timeout=self.options.timeout,
                                 username=self.options.username,
                                 password=self.options.password)

        super(UcsTool, self).__init__()
Exemplo n.º 7
0
    def __init__(self, options, address=None, filename=None):
        self.options = Options(options)
        self.filename = filename

        self.sshparams = Options(device=self.options.device,
                                 address=address, timeout=self.options.timeout,
                                 username=self.options.username,
                                 password=self.options.password,
                                 port=self.options.port)

        super(Tool, self).__init__()
Exemplo n.º 8
0
    def __init__(self, options, address=None):
        self.options = Options(options)

        self.sshparams = Options(device=self.options.device,
                         address=address, timeout=self.options.timeout,
                         username=self.options.root_username,
                         password=self.options.root_password)
        self.icparams = Options(device=self.options.device,
                         address=address, timeout=self.options.timeout,
                         username=self.options.admin_username,
                         password=self.options.admin_password)
        
        super(Big3dUtil, self).__init__()
Exemplo n.º 9
0
    def __init__(self, options, address=None, params=None):
        self.options = Options(options)
        self.params = params

        self.icparams = Options(device=self.options.device,
                                address=address,
                                timeout=self.options.timeout,
                                username=self.options.username,
                                password=self.options.password,
                                port=self.options.port)
        self.icparams.debug = 1 if options.verbose else 0

        super(EmpyTester, self).__init__()
Exemplo n.º 10
0
 def by_em_api(self, filename, hfiso=None):
     devices = []
     devices.append(Options(address=self.address,
                            username=self.options.admin_username,
                            password=self.options.admin_password))
     options = Options(device=self.options.em_device,
                       address=self.options.em_address,
                       admin_username=self.options.em_admin_username,
                       admin_password=self.options.em_admin_password,
                       root_username=self.options.em_root_username,
                       root_password=self.options.em_root_password,
                       essential_config=self.options.essential_config,
                       image=filename, hfimage=hfiso)
     macro = EMInstallSoftware(devices, options)
     self.has_essential_config = macro.has_essential_config
     return macro.run()
Exemplo n.º 11
0
def main():
    import optparse
    import sys

    usage = """%prog [options] <url> [url]...

  Examples:
  %prog https://10.11.41.73/1MB https://10.11.41.69/1MB -v
  """

    formatter = optparse.TitledHelpFormatter(indent_increment=2,
                                             max_help_position=60)
    p = optparse.OptionParser(usage=usage, formatter=formatter,
                              version="HTTP/S Traffic Generator %s" % __version__)
    p.add_option("-v", "--verbose", action="store_true",
                 help="Debug logging")
#    p.add_option("-s", "--stats", action="store_true", default=False,
#                 help="Show statistics when done (default: no)")
    p.add_option("-k", "--keepalive", action="store_true", default=False,
                 help="Reuse HTTP/1.1 connections for subsequent requests")

    p.add_option("-c", "--concurrency", metavar="INTEGER",
                 default=1, type="int",
                 help="Number of parallel threads (default: 10)")
    p.add_option("-n", "--requests", metavar="INTEGER",
                 default=10, type="int",
                 help="Total number of requests (default: 10)")
    p.add_option("-r", "--rate", metavar="INTEGER",
                 default=100, type="int",
                 help="Maximum bandwidth in Kbytes per sec (default: 100 KB/s)")
    p.add_option("-d", "--dns", metavar="ADDRESS",
                 default=None, type="string",
                 help="Use this DNS server to resolve hostnames.")
#    p.add_option("-p", "--pattern", metavar="STRING",
#                 default="0:10", type="string",
#                 help="[Threads delta:Sleep]... (default: 1:300:-1:300)")
    p.add_option("-t", "--timeout", metavar="SECONDS", type="float",
                 default=DEFAULT_TIMEOUT, help="Timeout (default: %d)" % DEFAULT_TIMEOUT)
    p.add_option("-l", "--limit", metavar="SECONDS", type="int",
                 default=DEFAULT_LIMIT, help="Run limit (default: %d)" % DEFAULT_LIMIT)

    options, args = p.parse_args()

    if options.verbose:
        level = logging.DEBUG
    else:
        level = logging.INFO
        logging.getLogger('f5test').setLevel(logging.INFO)
        logging.getLogger('f5test.macros').setLevel(logging.INFO)

    LOG.setLevel(level)
    logging.basicConfig(level=level)

    if not args:
        p.print_version()
        p.print_help()
        sys.exit(2)

    cs = TrafficGen(options=Options(options), urls=args)
    cs.run()
Exemplo n.º 12
0
 def setUp(self):
     super(SplitTestCase, self).setUp()
     name = re.sub('\.phase\d+.', '\._\.', self.id())
     cl = self.get_data(CHECKLIST)
     cl.setdefault(name, Options())
     self.c = cl[name]
     if self.c._has_failed:
         raise SkipTest('Test failed in preceding phase. Skipping.')
Exemplo n.º 13
0
    def __init__(self, options, emaddress=None, bigipaddress=None):
        self.options = Options(options)

        self.emparams = Options(device=self.options.emdevice,
                         address=emaddress, timeout=self.options.timeout,
                         username=self.options.em_root_username,
                         password=self.options.em_root_password)
        self.emicparams = Options(device=self.options.emdevice,
                         address=emaddress, timeout=self.options.timeout,
                         username=self.options.em_admin_username,
                         password=self.options.em_admin_password)
        self.bigipparams = Options(device=self.options.bigipdevice,
                         address=bigipaddress, timeout=self.options.timeout,
                         username=self.options.bigip_admin_username,
                         password=self.options.bigip_admin_password)

        super(DeviceCloner, self).__init__()
Exemplo n.º 14
0
    def run(self, tmp=None, task_vars=None):
        if task_vars is None:
            task_vars = dict()

        for arg in self._task.args:
            if arg not in self.VALID_ARGS:
                return {"failed": True, "msg": "'%s' is not a valid option for f5_unmerge" % arg}

        result = super(ActionModule, self).run(tmp, task_vars)
        #result['_ansible_verbose_override'] = True

        options = Options()

        options.port = self._task.args.get('port')
        options.username = self._task.args.get('username')
        options.password = self._task.args.get('password')
        options.verbose = self._task.args.get('verbose')
        options.timeout = int(self._task.args.get('timeout', 60))
        address = self._task.args.get('address')
        filename = self._task.args.get('filename')

        macro = Tool(options, address, filename)
        result['output'] = macro.run()

        return result
Exemplo n.º 15
0
    def run(self, tmp=None, task_vars=None):
        if task_vars is None:
            task_vars = dict()

        for arg in self._task.args:
            if arg not in self.VALID_ARGS:
                return {"failed": True, "msg": "'%s' is not a valid option in f5_config" % arg}

        result = super(ActionModule, self).run(tmp, task_vars)
        result['_ansible_verbose_override'] = True

        options = Options()
        address = self._task.args.get('address')
        if VAR_F5TEST_CONFIG in task_vars:
            irack = task_vars[VAR_F5TEST_CONFIG].irack
            if irack:
                options.irack_address = irack.address
                options.irack_username = irack.username
                options.irack_apikey = irack.apikey
        options.update(self._task.args)

        macro = TmosConfig(address=address, specs=options)
        result['output'] = macro.run()

        return result
Exemplo n.º 16
0
    def __init__(self, options, address=None):
        self.options = Options(options)

        if self.options.device:
            self.address = ConfigInterface().get_device_address(options.device)
        else:
            self.address = address

        LOG.info('Doing: %s', self.address)
        super(KeySwap, self).__init__()
Exemplo n.º 17
0
    def __init__(self, options, authorities=None, peers=None, groups=None):
        self.options = Options(options)
        self.authorities_spec = list(authorities or [])
        self.peers_spec = list(peers or [])
        self.cas = []
        self.peers = []
        self.groups_specs = groups or [DEFAULT_DG]
        self.sync_device = None  # Used only when do_config_all() is called.

        super(FailoverMacro, self).__init__()
Exemplo n.º 18
0
    def __init__(self, options, address=None):
        self.options = Options(options)

        self.address = ConfigInterface().get_device_address(options.device) \
                       if self.options.device else address

        if self.options.alias is None:
            self.options.alias = []

        super(WebCert, self).__init__()
Exemplo n.º 19
0
    def __init__(self, options, method, address=None, params=None):
        self.options = Options(options)
        self.method = method
        if isinstance(params, basestring):
            self.params = []
            if params.strip():
                for name, value in parser(params):
                    self.params.append("%s=%r" % (name, value))
        else:
            self.params = params or []

        self.icparams = Options(device=self.options.device,
                                address=address,
                                timeout=self.options.timeout,
                                username=self.options.username,
                                password=self.options.password,
                                port=self.options.port)
        self.icparams.debug = 1 if options.verbose else 0

        super(Ictester, self).__init__()
Exemplo n.º 20
0
 def wait_for_conflicts():
     resp = self.api.get(task.selfLink)
     if (resp.status not in ('STARTED',) and
             resp.currentStep in ('PENDING_CONFLICTS', 'PENDING_CHILD_CONFLICTS')):
         LOG.info('Conflicts detected, setting resolution: %s' % resolution)
         payload = Options(status='STARTED',
                           conflicts=resp.conflicts[:])
         for conflict in payload.conflicts:
             conflict.resolution = resolution
         resp = self.api.patch(task.selfLink, payload=payload)
     return resp
Exemplo n.º 21
0
    def __init__(self, options, urls):
        options.setdefault('timeout', DEFAULT_TIMEOUT)
        options.setdefault('limit', DEFAULT_LIMIT)
        options.setdefault('concurrency', 1)
        options.setdefault('rate', 100)
        options.setdefault('requests', 1)
        options.setdefault('keepalive', False)
        self.options = Options(options)
        self.urls = urls

        self.stats = Options()
        self.stats.requests = {}
        self.stats.requests.successful = 0
        self.stats.requests.failed = 0
        self.stats.time = {}
        self.stats.time.delta = 0
        self.stats.time.req_per_sec = 0
        self.stats.data = {}
        self.stats.data.total = 0

        super(TrafficGen, self).__init__()
Exemplo n.º 22
0
        def add_float_selfips(self, device, param):
            # TODO: Need to put logic here to check if an IP is within a network
            node_name = param.node_name
            graph_name = param.graph_name
            contract_name = param.contract_name
            network_folder = self.find('*[@key="Network"]')

            # Add external float selfip
            param = Options(key="ExternalSelfIP", name="ExternalFloat",
                            node_name=node_name,
                            graph_name=graph_name,
                            contract_name=contract_name)
            folder_params = FolderInst(param)
            params = []
            params.append(Options(key="Floating", name="Floating", value='YES'))
            params.append(Options(key="PortLockdown", name="PortLockdown",
                                  value='DEFAULT'))
            params.append(Options(key="SelfIPAddress", name="SelfIPAddress",
                                  value=device.specs.external.float1))
            params.append(Options(key="SelfIPNetmask", name="SelfIPNetmask",
                                  value=device.specs.external.netmask))
            folder_params.add_params(params)
            network_folder.append(folder_params)

            # Add internal float selfip
            param = Options(key="InternalSelfIP", name="InternalFloat",
                            node_name=node_name,
                            graph_name=graph_name,
                            contract_name=contract_name)
            folder_params = FolderInst(param)
            params = []
            params.append(Options(key="Floating", name="Floating", value='YES'))
            params.append(Options(key="PortLockdown", name="PortLockdown",
                                  value='DEFAULT'))
            params.append(Options(key="SelfIPAddress", name="SelfIPAddress",
                                  value=device.specs.internal.float1))
            params.append(Options(key="SelfIPNetmask", name="SelfIPNetmask",
                                  value=device.specs.internal.netmask))
            folder_params.add_params(params)
            network_folder.append(folder_params)
Exemplo n.º 23
0
 def set_network_relation(self, param):
     network_name = self.find('*[@key="Network"]').get('name')
     param = Options(key="NetworkRelation", name="NetworkRelation",
                     node_name=param.node_name,
                     graph_name=param.graph_name,
                     contract_name=param.contract_name)
     network_relation = FolderInst(param)
     config = ET.Element('vnsCfgRelInst')
     config.set('name', 'NetworkRel')
     config.set('key', 'NetworkRel')
     config.set('targetName', network_name)
     network_relation.append(config)
     self.append(network_relation)
Exemplo n.º 24
0
    def __init__(self, name, options=None, *args, **kwargs):
        super(BridgeDomain, self).__init__('fvBD', *args, **kwargs)
        o = Options(options)

        if o.context:
            self.set('name', name)
            context = ET.Element('fvRsCtx')
            context.set('tnFvCtxName', options.context)
            self.append(context)

        if o.subnet:
            subnet = ET.Element('fvSubnet', attrib={'scope': 'private'})
            subnet.set('ip', options.subnet)
            self.append(subnet)
Exemplo n.º 25
0
 def prep(self):
     super(GetDiscoveredServicesList, self).prep()
     # This doesn't work in v5.5.0
     #f = Options(filter="product eq 'BIG-IP' and address eq '%s'" % self.device.get_discover_address())
     f = Options(filter="product eq 'BIG-IP' and deviceUri eq 'https://%s:443'" % self.device.get_discover_address())
     resp = self.api.get(MachineIdResolver.URI, odata_dict=f)
     items = resp['items']
     for item in items:
         LOG.debug("Looking for {0} EQ to {1}".format(self.device.get_discover_address(), item.address))
         if self.device.get_discover_address() == item.address:
             self.resp = item
             break
     if self.resp is None:
         LOG.warning("No machineid reference found for {0}".format(self.device.get_discover_address()))
Exemplo n.º 26
0
    def set_connections(self, tenant_name):
        attrib = Options(unicastRoute='yes', connType='external', adjType='L2')
        graph_name = self.get('name')
        node_name = self.find('vnsAbsNode').get('name')
        provider_name = self.find('vnsAbsTermNodeProv').get('name')
        consumer_name = self.find('vnsAbsTermNodeCon').get('name')
        connectors = self.findall('*vnsAbsFuncConn')
        for connector in connectors:
            if 'mConn-external' in connector.find('vnsRsMConnAtt').get('tDn'):
                external_connector_name = connector.get('name')
            elif 'mConn-internal' in connector.find('vnsRsMConnAtt').get(
                    'tDn'):
                internal_connector_name = connector.get('name')

        # Add external connection
        external_connection = ET.Element('vnsAbsConnection', attrib=attrib)
        external_connection.set('name', 'C1')

        tdn = 'uni/tn-%s/AbsGraph-%s/AbsNode-%s/AbsFConn-%s' % (
            tenant_name, graph_name, node_name, external_connector_name)
        connection_to_connector = ET.Element('vnsRsAbsConnectionConns')
        connection_to_connector.set('tDn', tdn)
        external_connection.append(connection_to_connector)

        tdn = 'uni/tn-%s/AbsGraph-%s/AbsTermNodeCon-%s/AbsTConn' % (
            tenant_name, graph_name, consumer_name)
        connection_to_connector = ET.Element('vnsRsAbsConnectionConns')
        connection_to_connector.set('tDn', tdn)
        external_connection.append(connection_to_connector)

        # Add internal connection
        internal_connection = ET.Element('vnsAbsConnection', attrib=attrib)
        internal_connection.set('name', 'C2')

        tdn = 'uni/tn-%s/AbsGraph-%s/AbsNode-%s/AbsFConn-%s' % (
            tenant_name, graph_name, node_name, internal_connector_name)
        connection_to_connector = ET.Element('vnsRsAbsConnectionConns')
        connection_to_connector.set('tDn', tdn)
        internal_connection.append(connection_to_connector)

        tdn = 'uni/tn-%s/AbsGraph-%s/AbsTermNodeProv-%s/AbsTConn' % (
            tenant_name, graph_name, provider_name)
        connection_to_connector = ET.Element('vnsRsAbsConnectionConns')
        connection_to_connector.set('tDn', tdn)
        internal_connection.append(connection_to_connector)

        self.append(external_connection)
        self.append(internal_connection)
Exemplo n.º 27
0
    def __init__(self, address, specs, *args, **kwargs):
        self.specs = specs

        options = Options(ssh_port=int(specs.get('ssh_port', 22)),
                          ssl_port=int(specs.get('ssl_port', 443)),
                          partitions=int(specs.get('partitions', DEFAULT_PARTITIONS)),
                          node_start=specs.get('node_start', DEFAULT_NODE_START),
                          node_count=int(specs.get('node_count', DEFAULT_NODES)),
                          pool_count=int(specs.get('pool_count', DEFAULT_POOLS)),
                          pool_members=int(specs.get('pool_members', DEFAULT_MEMBERS)),
                          vip_start=specs.get('vip_start'),
                          vip_count=int(specs.get('vip_count', DEFAULT_VIPS)),
                          username=specs.get('username'),
                          password=specs.get('password'),
                          license=specs.get('license'),
                          provision=specs.get('provision'),
                          clean=specs.get('clean', False),
                          timeout=specs.get('timeout'),
                          verify=specs.get('verify'),
                          verbose=specs.get('verbose'),
                          hostname=specs.get('hostname'),
                          mgmtip=specs.get('mgmtip'),
                          mgmtgw=specs.get('mgmtgw'),
                          selfip_internal=specs.get('selfip_internal'),
                          selfip_external=specs.get('selfip_external'),
                          vlan_internal_name=specs.get('vlan_internal_name',
                                                       'internal'),
                          vlan_external_name=specs.get('vlan_external_name',
                                                       'external'),
                          vlan_internal=specs.get('vlan_internal'),
                          vlan_external=specs.get('vlan_external'),
                          irack_address=specs.get('irack_address'),
                          irack_username=specs.get('irack_username'),
                          irack_apikey=specs.get('irack_apikey'),
                          no_irack=specs.get('no_irack', False),
                          force_license=specs.get('force_license', False),
                          license_only=specs.get('license_only', False)
                          )
        super(TmosConfig, self).__init__(options, address, *args, **kwargs)
Exemplo n.º 28
0
import os
import socket
import sys
import random
from f5test.interfaces.rest.emapi import EmapiInterface
from f5test.interfaces.rest.core import AUTH
from f5test.interfaces.rest.emapi.objects.bigip import AuthzRoles
from f5test.interfaces.rest.emapi.objects.shared import Echo

__version__ = '1.0'
LOG = logging.getLogger(__name__)

F5TEST_SUBJECT = Options(CN='NotSet',
                         emailAddress='*****@*****.**',
                         OU='Enterprise Manager',
                         O='F5 Networks',
                         L='Seattle',
                         ST='Washington',
                         C='US'
                         )

MAXINT = 4294967295
DEFAULT_TIMEOUT = 300

ROOTCA_CRT = """-----BEGIN CERTIFICATE-----
MIICxTCCAi6gAwIBAgIBATANBgkqhkiG9w0BAQUFADCBnTEWMBQGA1UEAxMNRjUg
RU0gVGVzdGluZzEcMBoGCSqGSIb3DQEJARYNZW10ZXN0QGY1LmNvbTEbMBkGA1UE
CxMSRW50ZXJwcmlzZSBNYW5hZ2VyMRQwEgYDVQQKEwtGNSBOZXR3b3JrczEQMA4G
A1UEBxMHU2VhdHRsZTETMBEGA1UECBMKV2FzaGluZ3RvbjELMAkGA1UEBhMCVVMw
HhcNMTAwMzA1MDIyMjAxWhcNMjAwMzAyMDIyMjAxWjCBnTEWMBQGA1UEAxMNRjUg
RU0gVGVzdGluZzEcMBoGCSqGSIb3DQEJARYNZW10ZXN0QGY1LmNvbTEbMBkGA1UE
CxMSRW50ZXJwcmlzZSBNYW5hZ2VyMRQwEgYDVQQKEwtGNSBOZXR3b3JrczEQMA4G
Exemplo n.º 29
0
    def __init__(self, options, action):
        self.options = Options(options)
        self.action = action

        if options.env:
            venv_dir = options.env
            bin_dir = os.path.join(venv_dir, 'bin')
        else:
            bin_dir = os.path.dirname(os.path.realpath(inspect.stack()[-1][1]))
            venv_dir = os.path.dirname(bin_dir)

        LOG.info('Using sandbox directory: %s', venv_dir)
        params = Options()
        params.display = options.display
        
        jar_file = os.path.join(bin_dir, SELENIUM_JAR)
        params.jar = "-jar %s" % jar_file
        assert os.path.exists(jar_file), '%s not found' % jar_file
        
        var_run = os.path.join(venv_dir, 'var', 'run')
        var_log = os.path.join(venv_dir, 'var', 'log')
        if not os.path.exists(var_run):
            os.makedirs(var_run)
        
        if not os.path.exists(var_log):
            os.makedirs(var_log)
        
        params.dirs = dict(run=var_run,
                           log=var_log,
                           bin=bin_dir)
        
        params.pids = dict(xvfb='xvfb.pid',
                           selenium='selenium.pid', 
                           selenium_node='selenium_node.pid',
                           selenium_hub='selenium_hub.pid')

        params.logs = dict(xvfb='xvfb.log',
                           selenium='selenium.log', 
                           selenium_node='selenium_node.log',
                           selenium_hub='selenium_hub.log')

        if self.options.hub:
            params.hub = "-hub http://%s:4444/grid/register" % self.options.hub
            if not self.options.role:
                self.options.role = 'node'
        else:
            params.hub = ''

        if self.options.role:
            if self.options.role == 'hub':
                self.options.no_xvfb = True
            
            if self.options.role == 'node' and not self.options.hub:
                raise ValueError('--hub needs to be specified')
            
            params.role = "-role %s" % self.options.role
        else:
            params.role = ''
        
        self.params = params
        super(SeleniumRC, self).__init__()
Exemplo n.º 30
0
from f5test.interfaces.selenium import SeleniumInterface
from f5test.interfaces.selenium.driver import By
import getpass
import logging
import os
import pprint

__version__ = '0.3'
LOG = logging.getLogger(__name__)
DEFAULT_USER = os.environ.get('LOGNAME')

BANNED_FEATURES = set(('Appliance Mode', 'TrustedIP Subscription',
                       'IP Intelligence Subscription', 'IPI Subscription',
                       'App Mode', '1 Gbps - 3 Gbps Upgrade'))

MAP = Options()
MAP.eval = {}
MAP.eval.bigip = {}
MAP.eval.bigip['VE'] = 'F5-BIG-LTM-VE-1G-LIC'
MAP.eval.bigip['VE3'] = 'F5-BIG-LTM-VE-3G-LIC'
MAP.eval.bigip['AWS'] = 'F5-BIG-LTM-AWS-1G-LIC'
#MAP.eval.bigip['POOL'] = 'F5-BIG-LTMPL2-1G-100-LIC'
MAP.eval.bigip['1600'] = 'F5-BIG-LTM-1600-4G-LIC'
MAP.eval.bigip['2000'] = 'F5-BIG-LTM-2000S-LIC'
MAP.eval.bigip['2200'] = 'F5-BIG-LTM-2200S-LIC'
MAP.eval.bigip['3600'] = 'F5-BIG-LTM-3600-4G-LIC'
MAP.eval.bigip['3900'] = 'F5-BIG-LTM-3900-8G-LIC'
MAP.eval.bigip['4000'] = 'F5-BIG-LTM-4200V-LIC'
MAP.eval.bigip['5000'] = 'F5-BIG-LTM-5200V-LIC'
MAP.eval.bigip['6400'] = 'F5-BIG-LTM-6400-LIC'
MAP.eval.bigip['6800'] = 'F5-BIG-LTM-6800-LIC'
Exemplo n.º 31
0
def process_stages(stages, section, context, stop_on_error=True):
    if not stages:
        LOG.debug('No stages found.')
        return

    # Replicate the "_enabled" flag.
    carry_flag(stages)

    # Build the stage map with *ALL* defined stage classes in this file.
    stages_map = {}
    for value in globals().values():
        if inspect.isclass(value) and issubclass(value, Stage) and value != Stage:
            stages_map[value.name] = value

    # Focus only on our stages section
    for key in section.split('.'):
        stages = stages.get(key, Options())

    # Sort stages by priority attribute and stage name.
    stages = sorted(stages.iteritems(), key=lambda x: (isinstance(x[1], dict) and
                                                      x[1].get(PRIORITY_KEY,
                                                               DEFAULT_PRIORITY),
                                                      x[0]))

    config = ConfigInterface().config
    # Group stages of the same type. The we spin up one thread per stage in a
    # group and wait for threads within a group to finish.
    sg_dict = {}
    sg_list = []
    for name, specs in stages:
        if not specs or name.startswith('_'):
            continue
        assert TYPE_KEY in specs, "%s stage is invalid. No type specified." % name

        specs = Options(specs)
        key = specs.get(GROUP_KEY, "{0}-{1}".format(name, specs[TYPE_KEY]))

        group = sg_dict.get(key)
        if not group:
            sg_dict[key] = []
            sg_list.append(sg_dict[key])
        sg_dict[key].append((name, specs))

    LOG.debug("sg_list: %s", sg_list)
    for stages in sg_list:
        q = Queue()
        pool = []
        for stage in stages:
            description, specs = stage
            if not specs or not _bool(specs.get(ENABLE_KEY)):
                continue

            LOG.info("Processing stage: %s", description)
            # items() reverts <Options> to a simple <dict>
            specs = Options(specs)
            if not stages_map.get(specs[TYPE_KEY]):
                LOG.warning("Stage '%s' (%s) not defined.", description, specs[TYPE_KEY])
                continue

            stage_class = stages_map[specs[TYPE_KEY]]
            parameters = specs.get(PARAMETERS_KEY) or Options()
            parameters._context = context

            devices = expand_devices(specs)
            if devices is None:
                stage_class(parameters).run()
            elif devices == []:
                LOG.error("Stage %s requires devices but found none" % description)
            else:
                if not devices:
                    LOG.warning('No devices found for stage %s', description)
                for device in devices:
                    stage = stage_class(device, parameters)
                    name = '%s :: %s' % (description, device.alias) if device else description
                    t = MacroThread(stage, q, name=name, config=config)
                    t.start()
                    pool.append(t)
                    if not stage_class.parallelizable or not specs.get('parallelizable', True):
                        t.join()

                    # Cap the number parallel threads
                    if len(pool) >= specs.get('threads', MAX_THREADS):
                        map(lambda x: x.join(), pool)
                        pool[:] = []

        LOG.debug('Waiting for threads...')
        for t in pool:
            t.join()

        if not q.empty():
            stages = []
            while not q.empty():
                ret = q.get(block=False)
                thread, exc_info = ret.popitem()
                stages.append((thread, exc_info))
                LOG.error('Exception while "%s"', thread.getName())
                for line in traceback.format_exception(*exc_info):
                    LOG.error(line.strip())

            if stop_on_error:
                raise StageError(stages)
Exemplo n.º 32
0
    def __init__(self, options, ifc=None):
        self.options = Options(options)
        self.ifc = ifc
        self._ourifc = False

        super(LicenseGenerator, self).__init__()
Exemplo n.º 33
0
from f5test.interfaces.selenium import SeleniumInterface
from f5test.interfaces.selenium.driver import By
import getpass
import logging
import os
import pprint

__version__ = '0.3'
LOG = logging.getLogger(__name__)
DEFAULT_USER = os.environ.get('LOGNAME')

BANNED_FEATURES = set(('Appliance Mode', 'TrustedIP Subscription',
                       'IP Intelligence Subscription', 'IPI Subscription',
                       'App Mode', '1 Gbps - 3 Gbps Upgrade'))

MAP = Options()
MAP.eval = {}
MAP.eval.bigip = {}
MAP.eval.bigip['VE'] = 'F5-BIG-LTM-VE-1G-LIC'
MAP.eval.bigip['VE3'] = 'F5-BIG-LTM-VE-3G-LIC'
MAP.eval.bigip['AWS'] = 'F5-BIG-LTM-AWS-1G-LIC'
#MAP.eval.bigip['POOL'] = 'F5-BIG-LTMPL2-1G-100-LIC'
MAP.eval.bigip['1600'] = 'F5-BIG-LTM-1600-4G-LIC'
MAP.eval.bigip['2000'] = 'F5-BIG-LTM-2000S-LIC'
MAP.eval.bigip['2200'] = 'F5-BIG-LTM-2200S-LIC'
MAP.eval.bigip['3600'] = 'F5-BIG-LTM-3600-4G-LIC'
MAP.eval.bigip['3900'] = 'F5-BIG-LTM-3900-8G-LIC'
MAP.eval.bigip['4000'] = 'F5-BIG-LTM-4200V-LIC'
MAP.eval.bigip['5000'] = 'F5-BIG-LTM-5200V-LIC'
MAP.eval.bigip['6400'] = 'F5-BIG-LTM-6400-LIC'
MAP.eval.bigip['6800'] = 'F5-BIG-LTM-6800-LIC'
Exemplo n.º 34
0
class InstallSoftware(Macro):

    def __init__(self, options, address=None, *args, **kwargs):
        self.options = Options(options)
        self.has_essential_config = None
        self.address = address

        LOG.info('Doing: %s', self.address or options.device)
        super(InstallSoftware, self).__init__(*args, **kwargs)

    def by_em_api(self, filename, hfiso=None):
        devices = []
        devices.append(Options(address=self.address,
                               username=self.options.admin_username,
                               password=self.options.admin_password))
        options = Options(device=self.options.em_device,
                          address=self.options.em_address,
                          admin_username=self.options.em_admin_username,
                          admin_password=self.options.em_admin_password,
                          root_username=self.options.em_root_username,
                          root_password=self.options.em_root_password,
                          essential_config=self.options.essential_config,
                          image=filename, hfimage=hfiso)
        macro = EMInstallSoftware(devices, options)
        self.has_essential_config = macro.has_essential_config
        return macro.run()

#    def by_em_ui(self):
#        return
#
#    def by_bigpipe(self):
#        return

    def _initialize_big3d(self):
        LOG.info('Initializing big3d...')
        sshifc = SSHInterface(address=self.address, port=self.options.ssh_port)

        with sshifc:
            ssh = sshifc.api
            ssh.run('bigstart stop big3d;'
                    'rm -f /shared/bin/big3d;'
                    'test -f /usr/sbin/big3d.default && cp -f /usr/sbin/big3d.default /usr/sbin/big3d;'
                    'bigstart start big3d')

    def _initialize_em(self):
        LOG.info('Initializing EM...')
        sshifc = SSHInterface(address=self.address, port=self.options.ssh_port)
        timeout = self.options.timeout
        with sshifc:
            ssh = sshifc.api
            ssh.run('bigstart stop;'
                    '/etc/em/f5em_db_setup.sh;'
                    'rm -f /shared/em/mysql_shared_db/f5em_extern/*;'
                    '/etc/em/f5em_extern_db_setup.sh;'
                    'bigstart start')
            SCMD.ssh.FileExists('/etc/em/.initial_boot', ifc=sshifc).\
                run_wait(lambda x: x is False,
                         progress_cb=lambda x: 'EM still not initialized...',
                         timeout=timeout)

    def _wait_after_reboot(self, essential):
        if essential:
            ssh = SSHInterface(address=self.address, port=self.options.ssh_port)
        else:
            ssh = SSHInterface(address=self.address,
                               username=self.options.root_username,
                               password=self.options.root_password,
                               port=self.options.ssh_port)

        timeout = self.options.timeout
        try:
            SCMD.ssh.GetPrompt(ifc=ssh).\
                run_wait(lambda x: x not in ('INOPERATIVE', '!'), timeout=timeout,
                         timeout_message="Timeout ({0}s) waiting for a non-inoperative prompt.")
            SCMD.ssh.FileExists('/var/run/mcpd.pid', ifc=ssh).\
                run_wait(lambda x: x,
                         progress_cb=lambda x: 'mcpd not up...',
                         timeout=timeout)
            SCMD.ssh.FileExists('/var/run/mprov.pid', ifc=ssh).\
                run_wait(lambda x: x is False,
                         progress_cb=lambda x: 'mprov still running...',
                         timeout=timeout)
            SCMD.ssh.FileExists('/var/run/grub.conf.lock', ifc=ssh).\
                run_wait(lambda x: x is False,
                         progress_cb=lambda x: 'grub.lock still running...',
                         timeout=timeout)
            version = SCMD.ssh.get_version(ifc=ssh)
        finally:
            ssh.close()
        return version

    def by_image2disk(self, filename, hfiso=None):
        iso_version = cm.version_from_metadata(filename)

        if hfiso:
            hfiso_version = cm.version_from_metadata(hfiso)
            reboot = False
        else:
            hfiso_version = None
            reboot = True

        LOG.debug('iso: %s', iso_version)

        base = os.path.basename(filename)
        essential = self.options.essential_config
        timeout = self.options.timeout

        if self.options.format_partitions or self.options.format_volumes:
            reboot = True

        with SSHInterface(address=self.address,
                          username=self.options.root_username,
                          password=self.options.root_password,
                          timeout=timeout, port=self.options.ssh_port) as sshifc:
            ssh = sshifc.api
            version = SCMD.ssh.get_version(ifc=sshifc)
            LOG.info('running on %s', version)

            if version > 'bigip 9.6.0':
                try:
                    ret = SCMD.tmsh.list('sys cluster', ifc=sshifc)
                except:
                    ret = None
                if ret:
                    raise NotImplementedError('Due to potential complications image2disk '
                                              'installations on clustered '
                                              'systems are not supported by this tool '
                                              'and should be done by hand. Sorry!')

            if not essential and abs(iso_version) < abs(version) or \
               iso_version.product != version.product:
                LOG.warning('Enforcing --esential-config')
                essential = True

            if essential:
                lines = ssh.run('ls ' + SHARED_IMAGES).stdout.split()
                images = [x for x in lines if '.iso' in x]

                hfbase = os.path.basename(hfiso) if hfiso else None
                for image in images:
                    if base != image and hfbase != image:
                        LOG.info('Deleting image: %s' % image)
                        ssh.run('rm -rf %s/%s' % (SHARED_IMAGES, image))

            # XXX: Image checksum is not verified!!
            if (base not in ssh.run('ls ' + SHARED_IMAGES).stdout.split()):
                LOG.info('Importing iso %s', filename)
                SCMD.ssh.scp_put(ifc=sshifc, source=filename, nokex=False)

            filename = os.path.join(SHARED_IMAGES, base)

            if self.options.format_volumes:
                fmt = 'lvm'
            elif self.options.format_partitions:
                fmt = 'partitions'
            else:
                fmt = None

            def log_progress(stdout, stderr):
                output = ''
                if stdout:
                    output += stdout
                if stderr:
                    output += '\n'
                    output += stderr

                # An in-house grep.
                for line in output.splitlines():
                    line = line.strip()
                    if line and not line.startswith('info: '):
                        LOG.debug(line)

            try:
                audit = SCMD.ssh.audit_software(version=version, ifc=sshifc)
                volume = get_inactive_volume(audit)
            except:
                volume = 'HD1.1'
                LOG.warning('Assuming destination slot %s', volume)
            LOG.info('Installing %s on %s...', iso_version, volume)
            SCMD.ssh.install_software(version=version, ifc=sshifc,
                                      repository=filename, format=fmt,
                                      essential=essential, volume=volume,
                                      progress_cb=log_progress,
                                      reboot=reboot,
                                      repo_version=iso_version)

        if reboot:
            # Grab a new iControl handle that uses the default admin credentials.
            self._wait_after_reboot(essential)

        if hfiso:
            if essential:
                sshifc = SSHInterface(address=self.address, timeout=timeout,
                                      port=self.options.ssh_port)
            else:
                sshifc = SSHInterface(address=self.address, timeout=timeout,
                                      username=self.options.root_username,
                                      password=self.options.root_password,
                                      port=self.options.ssh_port)

            with sshifc:
                version = SCMD.ssh.get_version(ifc=sshifc)
                LOG.info('running on %s', version)
                if reboot:
                    audit = SCMD.ssh.audit_software(version=version, ifc=sshifc)
                    volume = get_inactive_volume(audit)
                    LOG.info('Installing image on %s...', volume)
                    SCMD.ssh.install_software(version=version, ifc=sshifc,
                                              repository=filename, reboot=False,
                                              essential=essential, volume=volume,
                                              progress_cb=log_progress,
                                              repo_version=iso_version)
                hfbase = os.path.basename(hfiso)
                if (hfbase not in sshifc.api.run('ls ' + SHARED_IMAGES).stdout.split()):
                    LOG.info('Importing hotfix %s', hfiso)
                    SCMD.ssh.scp_put(ifc=sshifc, source=hfiso, nokex=not reboot)
                hfiso = os.path.join(SHARED_IMAGES, hfbase)

                LOG.info('Installing hotfix on %s...', volume)
                SCMD.ssh.install_software(version=version, ifc=sshifc,
                                          repository=hfiso, is_hf=True,
                                          essential=essential, volume=volume,
                                          progress_cb=log_progress,
                                          repo_version=hfiso_version,
                                          reboot=False)
                LOG.info('Rebooting...')
                SCMD.ssh.switchboot(ifc=sshifc, volume=volume)
                SCMD.ssh.reboot(ifc=sshifc)

        # Grab a new iControl handle that uses the default admin credentials.
        current_version = self._wait_after_reboot(essential)
        expected_version = hfiso_version or iso_version

        if expected_version != current_version:
            raise InstallFailed('Version expected: %s but found %s' %
                                (expected_version, current_version))

        if essential:
            self._initialize_big3d()

        if essential and current_version.product.is_em:
            self._initialize_em()

        self.has_essential_config = essential

#    def by_tmsh(self):
#        return

    def copy_only(self, filename, hfiso=None):
        timeout = self.options.timeout

        def is_available(items):
            all_count = len(items)
            return sum(bool(x['verified']) for x in items) == all_count

        base = os.path.basename(filename)
        LOG.info('Importing base iso %s', base)
        SCMD.ssh.scp_put(address=self.address,
                         username=self.options.root_username,
                         password=self.options.root_password,
                         port=self.options.ssh_port,
                         source=filename, nokex=False, timeout=timeout)

        if hfiso:
            hfbase = os.path.basename(hfiso)
            LOG.info('Importing hotfix iso %s', hfbase)
            SCMD.ssh.scp_put(address=self.address,
                             username=self.options.root_username,
                             password=self.options.root_password,
                             port=self.options.ssh_port,
                             source=hfiso, nokex=False)

    def by_icontrol(self, filename, hfiso=None):
        iso_version = cm.version_from_metadata(filename)
        timeout = self.options.timeout
        if hfiso:
            hfiso_version = cm.version_from_metadata(hfiso)
        else:
            hfiso_version = None

        LOG.debug('iso: %s', iso_version)

        icifc = IcontrolInterface(address=self.address,
                                  username=self.options.admin_username,
                                  password=self.options.admin_password,
                                  port=self.options.ssl_port)
        ic = icifc.open()
        running_volume = ICMD.software.get_active_volume(ifc=icifc)
        assert running_volume != self.options.volume, \
            "Can't install on the active volume"

        version = ICMD.system.get_version(ifc=icifc)
        base = os.path.basename(filename)

        LOG.debug('running: %s', version)
        essential = self.options.essential_config
        if not essential and abs(iso_version) < abs(version):
            LOG.warning('Enforcing --esential-config')
            essential = True

        LOG.info('Setting the global DB vars...')
        ic.Management.Partition.set_active_partition(active_partition='Common')
        ic.Management.DBVariable.modify(variables=[
            {'name': 'LiveInstall.MoveConfig',
             'value': essential and 'disable' or 'enable'},
            {'name': 'LiveInstall.SaveConfig',
             'value': essential and 'disable' or 'enable'}
        ])
        # =======================================================================
        # Copy the ISO over to the device in /shared/images if it's not already
        # in the software repository.
        # =======================================================================
        images = ICMD.software.get_software_image(ifc=icifc)
        haz_it = any(filter(lambda x: x['verified'] and
                            x['product'] == iso_version.product.to_tmos and
                            x['version'] == iso_version.version and
                            x['build'] == iso_version.build, images))

        volume = self.options.volume or ICMD.software.get_inactive_volume(ifc=icifc)
        LOG.info('Preparing volume %s...', volume)
        ICMD.software.clear_volume(volume=volume, ifc=icifc)

        def is_available(items):
            all_count = len(items)
            return sum(bool(x['verified']) for x in items) == all_count

        is_clustered = ic.System.Cluster.is_clustered_environment()

        LOG.info('Timeout: %d', timeout)

        if essential:
            with SSHInterface(address=self.address,
                              username=self.options.root_username,
                              password=self.options.root_password,
                              timeout=timeout, port=self.options.ssh_port) as sshifc:
                ssh = sshifc.api
                lines = ssh.run('ls ' + SHARED_IMAGES).stdout.split()
                images = [x for x in lines if '.iso' in x]

            hfbase = os.path.basename(hfiso) if hfiso else None
            for image in images:
                if base != image and hfbase != image:
                    # If the image is a hotfix image
                    if 'hotfix' in image.lower():
                        LOG.info('Deleting hotfix image: %s' % image)
                        ICMD.software.delete_software_image(image, is_hf=True,
                                                            ifc=icifc)

                    # Otherwise assume it is a base image
                    else:
                        LOG.info('Deleting base image: %s' % image)
                        ICMD.software.delete_software_image(image, ifc=icifc)

        if not haz_it:
            LOG.info('Importing base iso %s', base)
            SCMD.ssh.scp_put(address=self.address,
                             username=self.options.root_username,
                             password=self.options.root_password,
                             port=self.options.ssh_port,
                             source=filename, nokex=False, timeout=timeout)

            LOG.info('Wait for image to be imported %s', base)
            ICMD.software.GetSoftwareImage(filename=base, ifc=icifc) \
                .run_wait(is_available, timeout=timeout,
                          timeout_message="Timeout ({0}s) while waiting for the software image to be imported.")

        if hfiso:
            images = ICMD.software.get_software_image(ifc=icifc, is_hf=True)
            haz_it = any(filter(lambda x: x['verified'] and
                                x['product'] == hfiso_version.product.to_tmos and
                                x['version'] == hfiso_version.version and
                                x['build'] == hfiso_version.build, images))

            if not haz_it:
                hfbase = os.path.basename(hfiso)
                LOG.info('Importing hotfix iso %s', hfiso)
                SCMD.ssh.scp_put(address=self.address,
                                 username=self.options.root_username,
                                 password=self.options.root_password,
                                 port=self.options.ssh_port,
                                 source=hfiso, nokex=False)

                LOG.info('Wait for image to be imported %s', hfbase)
                ICMD.software.GetSoftwareImage(filename=hfbase, ifc=icifc, is_hf=True) \
                    .run_wait(is_available, timeout_message="Timeout ({0}s) while waiting for the hotfix image to be imported.")

        def is_still_removing(items):
            return not any(filter(lambda x: x['status'].startswith('removing'),
                                  items))

        def is_still_installing(items):
            return not any(filter(lambda x: x['status'].startswith('installing') or
                                  x['status'].startswith('waiting') or
                                  x['status'].startswith('testing') or
                                  x['status'] in ('audited', 'auditing',
                                                  'upgrade needed'),
                                  items))

        volumes = ICMD.software.get_software_status(ifc=icifc)
        assert is_still_installing(volumes), "An install is already in " \
                                             "progress on another slot: %s" % volumes

        ICMD.software.GetSoftwareStatus(volume=volume, ifc=icifc) \
                     .run_wait(is_still_removing,
                               # CAVEAT: tracks progress only for the first blade
                               progress_cb=lambda x: x[0]['status'],
                               timeout=timeout)

        LOG.info('Installing %s...', iso_version)

        ICMD.software.install_software(hfiso_version or iso_version,
                                       volume=volume, ifc=icifc)

        ret = ICMD.software.GetSoftwareStatus(volume=volume, ifc=icifc) \
            .run_wait(is_still_installing,
                      # CAVEAT: tracks progress only for the first blade
                      progress_cb=lambda x: x[0]['status'],
                      timeout=timeout,
                      timeout_message="Timeout ({0}s) while waiting software install to finish.",
                      stabilize=10)

        LOG.info('Resetting the global DB vars...')
        ic.Management.DBVariable.modify(variables=[
            {'name': 'LiveInstall.MoveConfig',
             'value': essential and 'enable' or 'disable'},
            {'name': 'LiveInstall.SaveConfig',
             'value': essential and 'enable' or 'disable'}
        ])

        if sum(x['status'] == 'complete' for x in ret) != len(ret):
            raise InstallFailed('Install did not succeed: %s' % ret)

        LOG.info('Setting the active boot location %s.', volume)
        if is_clustered:
            # ===================================================================
            # Apparently on chassis systems the device is rebooted automatically
            # upon setting the active location, just like `b software desired
            # HD1.N active enable`.
            # ===================================================================
            uptime = ic.System.SystemInfo.get_uptime()
            ic.System.SoftwareManagement.set_cluster_boot_location(location=volume)
            time.sleep(60)
        else:
            ic.System.SoftwareManagement.set_boot_location(location=volume)
            LOG.info('Rebooting...')
            uptime = ICMD.system.reboot(ifc=icifc)

        # Grab a new iControl handle that uses the default admin credentials.
        if essential:
            icifc.close()
            icifc = IcontrolInterface(address=self.address,
                                      port=self.options.ssl_port)
            icifc.open()

        if uptime:
            ICMD.system.HasRebooted(uptime, ifc=icifc).run_wait(timeout=timeout)
            LOG.info('Device is rebooting...')

        LOG.info('Wait for box to be ready...')
        ICMD.system.IsServiceUp('MCPD', ifc=icifc).\
            run_wait(timeout=timeout,
                     timeout_message="Timeout ({0}s) while waiting for MCPD to come up")
        ICMD.system.IsServiceUp('TMM', ifc=icifc).\
            run_wait(timeout_message="Timeout ({0}s) while waiting for TMM to come up")

        ICMD.management.GetDbvar('Configsync.LocalConfigTime', ifc=icifc).\
            run_wait(lambda x: int(x) > 0,
                     progress_cb=lambda x: 'waiting configsync...',
                     timeout=timeout)
        ICMD.system.FileExists('/var/run/mprov.pid', ifc=icifc).\
            run_wait(lambda x: x is False,
                     progress_cb=lambda x: 'mprov still running...',
                     timeout=timeout)
        ICMD.system.FileExists('/var/run/grub.conf.lock', ifc=icifc).\
            run_wait(lambda x: x is False,
                     progress_cb=lambda x: 'grub.lock still present...',
                     timeout=timeout)

        current_version = ICMD.system.get_version(ifc=icifc)
        expected_version = hfiso_version or iso_version
        try:
            if expected_version != current_version:
                raise InstallFailed('Version expected: %s but found %s' %
                                    (expected_version, current_version))
        finally:
            icifc.close()

        # Will use SSH!
        if essential:
            self._initialize_big3d()

        if essential and current_version.product.is_em:
            self._initialize_em()

        self.has_essential_config = essential

    def prep(self):
        LOG.debug('prepping for install')

    def set_defaults(self):
        if self.options.device:
            device = ConfigInterface().get_device(self.options.device)
            self.address = device.get_address()
            self.options.admin_username = device.get_admin_creds().username
            self.options.admin_password = device.get_admin_creds().password
            self.options.root_username = device.get_root_creds().username
            self.options.root_password = device.get_root_creds().password
            self.options.ssl_port = device.ports.get('https', 443)
            self.options.ssh_port = device.ports.get('ssh', 22)

        self.options.setdefault('admin_username', ADMIN_USERNAME)
        self.options.setdefault('admin_password', ADMIN_PASSWORD)
        self.options.setdefault('root_username', ROOT_USERNAME)
        self.options.setdefault('root_password', ROOT_PASSWORD)
        self.options.setdefault('build_path', cm.ROOT_PATH)

    def _find_iso(self):
        identifier = self.options.pversion
        build = self.options.pbuild

        if identifier:
            identifier = str(identifier)

        if build:
            build = str(build)

        # BotD mode - All constants hardcoded here for now.
        if build and build.lower() in ('bod', 'botd', 'auto'):
            assert self.options.product, "A product identifier needs to be specified for BotD mode."

            if self.options.phf:
                LOG.warning("Hotfix parameter ignored in BotD mode.")

            with RestInterface(address=BOTD_HOST, proto='http') as restifc:
                r = restifc.api
                LOG.info('Querying build of the day...')
                response = r.get(BOTD_PATH)
                botd_product = self.options.product.upper()

                if self.options.phf:
                    botd_branch = "%s-%s" % (identifier, self.options.phf)
                else:
                    botd_branch = identifier

                build = response.products[botd_product].branches[botd_branch].build_id
                LOG.info("Found: %s", build)

        if self.options.image:
            filename = self.options.image.strip()
        else:
            base_build = None if self.options.phf else build
            filename = cm.isofile(identifier=identifier, build=base_build,
                                  product=self.options.product,
                                  root=self.options.build_path)

        if self.options.hfimage:
            hfiso = self.options.hfimage.strip()
        elif self.options.phf:
            hfiso = cm.isofile(identifier=identifier, build=build,
                               hotfix=self.options.phf,
                               product=self.options.product,
                               root=self.options.build_path)
        else:
            hfiso = None

        return filename, hfiso

    def setup(self):
        # Set the admin and root usernames and passwords
        self.set_defaults()

        if self.options.image:
            title = 'Installing custom base image on %s' % self.address
        else:
            title = 'Installing %s %s on %s' % (self.options.product,
                                                self.options.pversion,
                                                self.address)
        LOG.info(title)
        filename, hfiso = self._find_iso()
        iso_version = cm.version_from_metadata(filename)

        if self.options.copy_only:
            self.copy_only(filename, hfiso)
            return

        if self.options.format_partitions or self.options.format_volumes:
            with SSHInterface(address=self.address,
                              username=self.options.root_username,
                              password=self.options.root_password,
                              port=self.options.ssh_port) as sshifc:
                version = SCMD.ssh.get_version(ifc=sshifc)
        else:
            with IcontrolInterface(address=self.address,
                                   username=self.options.admin_username,
                                   password=self.options.admin_password,
                                   port=self.options.ssl_port) as icifc:
                version = ICMD.system.get_version(ifc=icifc)

        if (iso_version.product.is_bigip and iso_version >= 'bigip 10.0.0' or
            iso_version.product.is_em and iso_version >= 'em 2.0.0' or
                iso_version.product.is_bigiq):
            if self.options.format_partitions or self.options.format_volumes or \
               (version.product.is_bigip and version < 'bigip 10.0.0' or
                    version.product.is_em and version < 'em 2.0.0'):
                ret = self.by_image2disk(filename, hfiso)
            else:
                ret = self.by_icontrol(filename, hfiso)
        elif (iso_version.product.is_bigip and iso_version < 'bigip 9.6.0' or
              iso_version.product.is_em and iso_version < 'em 2.0.0'):
            assert self.options.em_address, "--em-address is needed for legacy installations."
            ret = self.by_em_api(filename, hfiso)
        else:
            raise VersionNotSupported('%s is not supported' % iso_version)

        LOG.debug('done')
        return ret
Exemplo n.º 35
0
class EMInstallSoftware(Macro):
    """Use an EM to install software.

    install_options = dict(uid, slot_uid, install_location, boot_location,
                           format, reboot, essential)
    device = dict(address='1.1.1.1', username='******', password='******',
                  install_options)
    options = dict(iso, hfiso, include_pk, continue_on_error, task_name)

    @param devices: [device1, device2, ...]
    @type devices: array
    @param options: EM address, credentials and other installation options.
    @param options: AttrDict
    """
    def __init__(self, devices, options, *args, **kwargs):
        self.devices = devices
        self.options = Options(options)
        self.options.setdefault('build_path', cm.ROOT_PATH)
        self.has_essential_config = None

        super(EMInstallSoftware, self).__init__(*args, **kwargs)

    def by_api(self):
        o = self.options
        timeout = o.timeout

        identifier = self.options.pversion
        build = self.options.pbuild

        if identifier:
            identifier = str(identifier)
            if build:
                build = str(build)

        if self.options.image:
            filename = self.options.image
        else:
            filename = cm.isofile(identifier=identifier, build=build,
                                  product=o.product, root=o.build_path)

        if self.options.hfimage:
            hfiso = self.options.hfimage
        elif self.options.phf:
            hfiso = cm.isofile(identifier=identifier, build=build,
                               hotfix=o.phf,
                               product=o.product,
                               root=o.build_path)
        else:
            hfiso = None

        iso_version = cm.version_from_metadata(filename)
        if (iso_version.product.is_bigip and iso_version >= 'bigip 10.0.0' or
                iso_version.product.is_em and iso_version >= 'em 2.0.0'):
            raise VersionNotSupported('Only legacy images supported through EMInstaller.')

        emifc = EMInterface(device=o.device, address=o.address,
                            username=o.admin_username, password=o.admin_password)
        emifc.open()

        with SSHInterface(device=o.device, address=o.address,
                          username=o.root_username, password=o.root_password,
                          port=self.options.ssh_port) as ssh:
            status = SCMD.ssh.get_prompt(ifc=ssh)
            if status in ['LICENSE EXPIRED', 'REACTIVATE LICENSE']:
                SCMD.ssh.relicense(ifc=ssh)
            elif status in ['LICENSE INOPERATIVE', 'NO LICENSE']:
                raise MacroError('Device at %s needs to be licensed.' % ssh)

            reachable_devices = [x['access_address'] for x in
                                 EMSQL.device.get_reachable_devices(ifc=ssh)]
            for x in self.devices:
                x.address = net.resolv(x.address)

            to_discover = [x for x in self.devices
                           if x.address not in reachable_devices]

            if to_discover:
                uid = EMAPI.device.discover(to_discover, ifc=emifc)
                task = EMSQL.device.GetDiscoveryTask(uid, ifc=ssh) \
                            .run_wait(lambda x: x['status'] != 'started',
                                      timeout=timeout,
                                      progress_cb=lambda x: 'discovery: %d%%' % x.progress_percent)
                assert task['error_count'] == 0, 'Discovery failed: %s' % task
            targets = []
            for device in self.devices:
                mgmtip = device.address
                version = EMSQL.device.get_device_version(mgmtip, ifc=ssh)
                if not o.essential_config and abs(iso_version) < abs(version):
                    LOG.warning('Enforcing --esential-config')
                    o.essential_config = True

                device_info = EMSQL.device.get_device_info(mgmtip, ifc=ssh)
                active_slot = EMSQL.device.get_device_active_slot(mgmtip,
                                                                  ifc=ssh)
                targets.append(dict(device_uid=device_info['uid'],
                                    slot_uid=active_slot['uid']))

            image_list = EMSQL.software.get_image_list(ifc=ssh)
            if iso_version not in image_list:
                base = os.path.basename(filename)
                destination = '%s.%d' % (os.path.join(SHARED_TMP, base), os.getpid())
                LOG.info('Importing base iso %s', base)
                SCMD.ssh.scp_put(device=o.device, address=o.address,
                                 destination=destination,
                                 username=self.options.root_username,
                                 password=self.options.root_password,
                                 port=self.options.ssh_port,
                                 source=filename, nokex=False)

                imuid = EMAPI.software.import_image(destination, ifc=emifc)
            else:
                imuid = image_list[iso_version]
                LOG.info('Image already imported: %d', imuid)

            if hfiso:
                hf_list = EMSQL.software.get_hotfix_list(ifc=ssh)
                hfiso_version = cm.version_from_metadata(hfiso)
                if hfiso_version not in hf_list:
                    hfbase = os.path.basename(hfiso)
                    destination = '%s.%d' % (os.path.join(SHARED_TMP, hfbase), os.getpid())
                    LOG.info('Importing hotfix iso %s', hfbase)
                    SCMD.ssh.scp_put(device=o.device, address=o.address,
                                     destination=destination,
                                     username=self.options.root_username,
                                     password=self.options.root_password,
                                     port=self.options.ssh_port,
                                     source=hfiso, nokex=False)
                    hfuid = EMAPI.software.import_image(destination, ifc=emifc)
                else:
                    hfuid = hf_list[hfiso_version]
            else:
                hfuid = None

            EMSQL.software.get_hotfix_list(ifc=ssh)

            EMSQL.device.CountActiveTasks(ifc=ssh) \
                        .run_wait(lambda x: x == 0, timeout=timeout,
                                  progress_cb=lambda x: 'waiting for other tasks')

            LOG.info('Installing %s...', iso_version)
            ret = EMAPI.software.install_image(targets, imuid, hfuid, o, ifc=emifc)
            ret = EMSQL.software.GetInstallationTask(ret['uid'], ifc=ssh).\
                run_wait(lambda x: x['status'] != 'started',
                         progress_cb=lambda x: 'install: %d%%' % x.progress_percent,
                         timeout=o.timeout)

        LOG.info('Deleting %d device(s)...', len(targets))
        EMAPI.device.delete(uids=[x['device_uid'] for x in targets], ifc=emifc)
        emifc.close()

        messages = []
        for d in ret['details']:
            if int(d['error_code']):
                messages.append("%(display_device_address)s:%(error_message)s" % d)
            if int(d['hf_error_code'] or 0):
                messages.append("%(display_device_address)s:%(hf_error_message)s" % d)
        if messages:
            raise InstallFailed('Install did not succeed: %s' %
                                ', '.join(messages))

        self.has_essential_config = o.essential_config
        return ret

    def setup(self):
        if not self.devices:
            LOG.info('No devices to install')
            return
        if self.options.image:
            title = 'Installing custom base image on %s through %s' % (self.devices,
                                                                       self.options.device)
        else:
            title = 'Installing %s %s on %s through %s' % (self.options.product,
                                                           self.options.pversion,
                                                           self.devices,
                                                           self.options.device)
        LOG.info(title)
        return self.by_api()
Exemplo n.º 36
0
        def convert_device(string):
            default_admin_username = self.options.admin_username
            default_admin_password = self.options.admin_password
            default_root_username = self.options.root_username
            default_root_password = self.options.root_password

            bits = string.split(',')

            if len(bits[0].split(':')) > 1:
                address, discover_address = bits[0].split(':')
            else:
                address, discover_address = bits[0], None
            specs = Options()
            specs['discover address'] = discover_address

            admin_cred = Options()
            root_cred = Options()
            if len(bits) == 1:
                admin_cred.common = DeviceCredential(default_admin_username,
                                                     default_admin_password)
                root_cred.common = DeviceCredential(default_root_username,
                                                    default_root_password)
            elif len(bits) == 3:
                admin_cred.common = DeviceCredential(bits[1], bits[2])
                root_cred.common = DeviceCredential(default_root_username,
                                                    default_root_password)
            elif len(bits) == 5:
                admin_cred.common = DeviceCredential(bits[1], bits[2])
                root_cred.common = DeviceCredential(bits[3], bits[4])
            else:
                raise ValueError('Invalid specs: %s', string)

            creds = {ADMIN_ROLE: admin_cred, ROOT_ROLE: root_cred}
            device = DeviceAccess(address, credentials=creds, specs=specs)

            for spec in self.options.https_ports.split(','):
                if len(spec.split(':')) != 2 and spec:
                    raise ValueError('Invalid https input: %s', spec)

            for spec in self.options.ssh_ports.split(','):
                if len(spec.split(':')) != 2 and spec:
                    raise ValueError('Invalid ssh input: %s', spec)

            def create_port_specs(string):
                ret = dict()
                if string:
                    for x in string.split(','):
                        x = x.strip().split(':')
                        ret.update({x[0]: x[1]})
                return ret

            https_by_address = create_port_specs(self.options.https_ports)
            ssh_by_address = create_port_specs(self.options.ssh_ports)

            device.ports['https'] = https_by_address.get(address,
                                                         DEFAULT_PORTS['https'])
            device.ports['ssh'] = ssh_by_address.get(address,
                                                     DEFAULT_PORTS['ssh'])
            device.alias = "device-%s" % device.address

            return device
Exemplo n.º 37
0
def run_module():
    # define the available arguments/parameters that a user can pass to
    # the module
    module_args = dict(
        #name=dict(type='str', required=True),
        #new=dict(type='bool', required=False, default=False),
        address=dict(type='str', required=True),
        admin_username=dict(type='str', required=False),
        admin_password=dict(type='str', required=False, no_log=True),
        root_username=dict(type='str', required=False),
        root_password=dict(type='str', required=False, no_log=True),
        product=dict(type='str', required=True),
        version=dict(type='str', required=True),
        build=dict(type='str', required=False),
        hf=dict(type='str', required=False),
        essential=dict(type='bool', required=False, default=False),
        image=dict(type='str', required=False),
        hfimage=dict(type='str', required=False),
        timeout=dict(type='int', required=False, default=1200),
    )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=True, original_message='', message='')

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    # if the user is working with this module in only check mode we do not
    # want to make any changes to the environment, just return the current
    # state with no modifications
    if module.check_mode:
        return result

    options = Options()
    options.admin_username = module.params['admin_username']
    options.admin_password = module.params['admin_password']
    options.root_username = module.params['root_username']
    options.root_password = module.params['root_password']
    options.product = module.params['product']
    options.pversion = module.params['version']
    options.pbuild = module.params['build']
    options.phf = module.params['hf']
    options.essential_config = module.params['essential']
    options.image = module.params['image']
    options.hfimage = module.params['hfimage']
    options.timeout = module.params['timeout']
    address = module.params['address']

    level = logging.INFO
    LOG.setLevel(level)
    logging.basicConfig(level=level)

    cs = InstallSoftware(options=options, address=address)
    result['output'] = cs.run()

    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    module.exit_json(**result)