예제 #1
0
    def setup(self):
        if not self.api.exists('/var/local/ucs/{}.ucs'.format(self.ucs_name)):
            raise RuntimeError('UCS not found on the target.')
        LOG.info('Processing...')
        self.api.run(r'cd {} && tar xf /var/local/ucs/{}.ucs'.format(UCS_TMP, self.ucs_name))

        # Replace all encrypted secrets which will fail to load anyway.
        self.api.run(r"sed -ibak '/^\s*#/!s/secret.*$/secret default/g' {}/config/bigip.conf".format(UCS_TMP))

        # Adding the license back
        self.api.run(r"cp -f /config/bigip.license {}/config/".format(UCS_TMP))

        # Preserve:
        # - management IP
        # - management route
        # Note: DHCP is not supported
        text = self.api.sftp().open('/config/bigip_base.conf').read()
        config = tmsh.parser(text)
        with self.api.sftp().open('{}/config/bigip_base.conf'.format(UCS_TMP), 'at+') as f:
            f.write(config.match('sys management-ip .*').dumps())
            f.write(config.match('sys management-route /Common/default').dumps())
            bit = config.match('sys global-settings')
            bit['sys global-settings']['mgmt-dhcp'] = 'disabled'
            bit['sys global-settings']['gui-security-banner-text'] = 'Welcome to the BIG-IP Configuration Utility. Configuration imported by UCS Tool 1.0'
            f.write(bit.dumps())
        
        self.api.run(r'tar -C {} --xform s:"^./":: -czf /var/local/ucs/{}_imported.ucs .'.format(UCS_TMP, self.ucs_name))
        #print config
        if self.options.load:
            LOG.info('Loading UCS...')
            self.api.run('tmsh load sys ucs {}_imported no-platform-check no-license'.format(self.ucs_name))

        LOG.info('Done.')
예제 #2
0
파일: unmerge.py 프로젝트: xiaotdl/nosest
    def setup(self):
        if not self.api.exists(self.filename):
            raise RuntimeError('File %s not found on the target.' % self.filename)
        LOG.info('Processing...')

        with self.api.sftp().open(self.filename) as f:
            result = parser(f.read())
            shell = self.api.interactive()
            shell.expect_exact(PROMPT)
            shell.sendline('tmsh')
            shell.expect_exact(PROMPT)
            leftovers = set()

            for path in reversed(result.keys()):
                if path.startswith('ltm pool'):
                    for member in result[path]['members'].keys():
                        if member.count('.') == 1:  # 2002::1.http
                            leftovers.add('ltm node {}'.format(member.split('.')[0]))
                        elif member.count(':') == 1:  # 1.1.1.1:21
                            leftovers.add('ltm node {}'.format(member.split(':')[0]))
                        else:
                            raise ValueError(member)
                shell.sendline(TMSH_DELETE.format(path))
                shell.expect_exact(PROMPT)
                LOG.info(shell.before)

            for path in leftovers:
                shell.sendline(TMSH_DELETE.format(path))
                shell.expect_exact(PROMPT)
                LOG.info(shell.before)

        LOG.info('Done.')
예제 #3
0
파일: extractor.py 프로젝트: jonozzz/nosest
    def setup(self):
        output = SCF_OUTPUT
        if self.options.cache:
            if self.api.exists(output):
                text = self.api.run('cat {0}'.format(output)).stdout
            else:
                text = self.dump_config(output)
        else:
            text = self.dump_config(output)

        if self.sshifc and not self.options.cache:
            self.api.run('rm -f {0}*'.format(output)).stdout

        LOG.info('Parsing...')
        config = tmsh.parser(text)
        LOG.debug(config)
        all_keys = list(config.keys())
        LOG.info('Last key: %s', all_keys[-1])
        all_ids = {}
        for x in all_keys:
            k = shlex.split(x)[-1]
            if k in all_ids:
                all_ids[k].append(config.glob(x))
            else:
                all_ids[k] = [config.glob(x)]
        vip = config.match("^{}$".format(self.params))
        if not vip:
            raise Exception('No objects found matching "%s"' % self.params)

        def rec2(root, deps=OrderedDict()):
            deps.update(root)

            def guess_key(k, d):
                this_id = re.split('[:]', k, 1)[0]
                if not this_id.startswith('/'):
                    this_id = '/Common/%s' % this_id

                if k.startswith('/'):
                    folder = k.rsplit('/', 1)[0]
                    if folder in all_ids:
                        for x in all_ids[folder]:
                            d.update(x)

                if this_id in all_ids:
                    for x in all_ids[this_id]:
                        d.update(x)

                # ipv6.port
                if re.search(':.*\.[^\.]*$', k):
                    this_id = re.split('[\.]', k, 1)[0]
                    this_id = '/Common/%s' % this_id
                    if this_id in all_ids:
                        for x in all_ids[this_id]:
                            d.update(x)

            def rec(root, deps=None):
                if deps is None:
                    deps = {}
                if isinstance(root, dict):
                    for k, v in root.items():
                        guess_key(k, deps)
                        rec(v, deps)
                elif isinstance(root, (set, list, tuple)):
                    for v in root:
                        rec(v, deps)
                else:
                    root = str(root)
                    assert isinstance(root, str), root
                    guess_key(root, deps)
                return deps
            d = rec(root)
            if d:
                # Try to avoid circular dependencies
                list(map(lambda x: d.pop(x), [x for x in d if x in deps]))
                deps.update(d)
                rec2(d, deps)
            return deps

        ret = rec2(vip)
        # Sort keys
        if self.options.sort:
            ret = tmsh.GlobDict(sorted(iter(ret.items()), key=lambda x: x[0]))
        return tmsh.dumps(ret)