def _setup_cico_connection(self):
     '''
     Populates self.api with the environment variables that we have. self.api will then
     be used to communicate with Duffy: requesting nodes, releasing nodes, ...
     '''
     if self.api is not None:
         return
     self.api = CicoWrapper(endpoint=os.environ['CICO_ENDPOINT'],
                            api_key=os.environ['CICO_API_KEY'])
    def take_action(self, parsed_args):
        api = CicoWrapper(endpoint=self.app.options.endpoint,
                          api_key=self.app.options.api_key)

        inventory = api.inventory(all=parsed_args.all, ssid=parsed_args.ssid)

        columns = ('host_id', 'hostname', 'ip_address', 'chassis',
                   'used_count', 'current_state', 'comment', 'distro', 'rel',
                   'centos_version', 'architecture', 'node_pool',
                   'console_port', 'flavor')

        return (columns, (utils.get_dict_properties(inventory[host], columns)
                          for host in inventory))
Exemple #3
0
    def take_action(self, parsed_args):
        api = CicoWrapper(
            endpoint=self.app.options.endpoint,
            api_key=self.app.options.api_key
        )

        hosts = api.node_done(ssid=parsed_args.ssid)

        columns = ('host_id', 'hostname', 'ip_address', 'chassis',
                   'used_count', 'current_state', 'comment', 'distro',
                   'rel', 'centos_version', 'architecture', 'node_pool')

        return (columns,
                (utils.get_dict_properties(hosts[host], columns)
                 for host in hosts))
    def take_action(self, parsed_args):
        api = CicoWrapper(endpoint=self.app.options.endpoint,
                          api_key=self.app.options.api_key)

        hosts, ssid = api.node_get(arch=parsed_args.arch,
                                   ver=parsed_args.release,
                                   count=parsed_args.count,
                                   retry_count=parsed_args.retry_count,
                                   retry_interval=parsed_args.retry_interval,
                                   flavor=parsed_args.flavor)

        columns = ('host_id', 'hostname', 'ip_address', 'chassis',
                   'used_count', 'current_state', 'comment', 'distro', 'rel',
                   'centos_version', 'architecture', 'node_pool',
                   'console_port', 'flavor')

        return (columns, (utils.get_dict_properties(hosts[host], columns)
                          for host in hosts))
Exemple #5
0
    def take_action(self, parsed_args):
        api = CicoWrapper(
            endpoint=self.app.options.endpoint,
            api_key=self.app.options.api_key
        )

        hosts, ssid = api.node_get(arch=parsed_args.arch,
                                   ver=parsed_args.release,
                                   count=parsed_args.count)
        message = "SSID for these servers: %s\n" % ssid
        sys.stdout.write(message)

        columns = ('host_id', 'hostname', 'ip_address', 'chassis',
                   'used_count', 'current_state', 'comment', 'distro',
                   'rel', 'centos_version', 'architecture', 'node_pool')

        return (columns,
                (utils.get_dict_properties(hosts[host], columns)
                 for host in hosts))
def get_host(api_key, version):
    i = 0
    while True:
        api = CicoWrapper(endpoint="http://admin.ci.centos.org:8080/",
                          api_key=api_key)
        hosts = None

        try:
            hosts, ssid = api.node_get(ver=version, retry_count=1)
        except:
            pass

        if hosts == None:
            i = i + 1
            if i > 60:
                return (None, None)
            time.sleep(i)
            continue

        for host in hosts:
            return (host, ssid)
Exemple #7
0
def main():
    argument_spec = dict(
        action=dict(required=True, choices=['get', 'done', 'list']),
        arch=dict(default='x86_64', choices=['i386', 'x86_64', 'aarch64',
                                             'ppc64le']),
        flavor=dict(default='small', choices=['tiny', 'small', 'medium',
                                              'lram.tiny', 'lram.small',
                                              'xram.tiny', 'xram.small',
                                              'xram.medium', 'xram.large']),
        release=dict(default='7', choices=['5', '6', '7']),
        count=dict(default=1, type='int'),
        retry_count=dict(default=1, type='int'),
        retry_interval=dict(default=10, type='int'),
        endpoint=dict(default='http://admin.ci.centos.org:8080/'),
        api_key=dict(default=None, no_log=True),
        ssid=dict(default=None),
    )
    module = AnsibleModule(argument_spec)

    if not HAS_CICO:
        module.fail_json(msg='cicoclient is required for this module.')

    action = module.params['action']
    arch = module.params['arch']
    release = module.params['release']
    count = module.params['count']
    retry_count = module.params['retry_count']
    retry_interval = module.params['retry_interval']
    endpoint = module.params['endpoint']
    api_key = module.params['api_key']
    ssid = module.params['ssid']
    flavor = module.params['flavor']

    if action == 'done' and ssid is None:
        module.fail_json(msg='A SSID is required when releasing nodes.')

    try:
        api = CicoWrapper(
            endpoint=endpoint,
            api_key=api_key
        )

        if api.api_key is None:
            module.fail_json(msg='An API key is required for this module.')

        if action == 'get':
            hosts, new_ssid = api.node_get(arch=arch, ver=release, count=count,
                                           retry_count=retry_count,
                                           retry_interval=retry_interval,
                                           flavor=flavor)
            data = {
                'message': 'Requested servers successfully',
                'hosts': hosts,
                'ssid': new_ssid
            }
            module.exit_json(changed=True, results=data)

        if action == 'done':
            hosts = api.node_done(ssid=ssid)
            data = {
                'message': 'Released servers successfully',
                'hosts': hosts
            }
            module.exit_json(changed=True, results=data)

        if action == 'list':
            hosts = api.inventory(ssid=ssid)

            data = {
                'message': 'Listed servers successfully',
                'hosts': hosts
            }
            module.exit_json(changed=True, results=data)

    except Exception as e:
        module.fail_json(msg=e.message)
Exemple #8
0
def main():
    argument_spec = dict(
        action=dict(required=True, choices=['get', 'done', 'list']),
        arch=dict(default='x86_64', choices=['i386', 'x86_64']),
        release=dict(default='7', choices=['5', '6', '7']),
        count=dict(default=1, type='int'),
        retry_count=dict(default=1, type='int'),
        retry_interval=dict(default=10, type='int'),
        endpoint=dict(default='http://admin.ci.centos.org:8080/'),
        api_key=dict(default=os.getenv('CICO_API_KEY', None), no_log=True),
        ssid=dict(default=None),
    )
    module = AnsibleModule(argument_spec)

    if not HAS_CICO:
        module.fail_json(msg='cicoclient is required for this module.')

    action = module.params['action']
    arch = module.params['arch']
    release = module.params['release']
    count = module.params['count']
    retry_count = module.params['retry_count']
    retry_interval = module.params['retry_interval']
    endpoint = module.params['endpoint']
    api_key = module.params['api_key']
    ssid = module.params['ssid']

    # Pre-flight validation
    if api_key is None:
        module.fail_json(msg='An API key is required for this module.')

    if action == 'done' and ssid is None:
        module.fail_json(msg='A SSID is required when releasing nodes.')

    try:
        api = CicoWrapper(
            endpoint=endpoint,
            api_key=api_key
        )

        if action == 'get':
            hosts, new_ssid = api.node_get(arch=arch, ver=release, count=count,
                                           retry_count=retry_count,
                                           retry_interval=retry_interval)
            data = {
                'message': 'Requested servers successfully',
                'hosts': hosts,
                'ssid': new_ssid
            }
            module.exit_json(changed=True, results=data)

        if action == 'done':
            hosts = api.node_done(ssid=ssid)
            data = {
                'message': 'Released servers successfully',
                'hosts': hosts
            }
            module.exit_json(changed=True, results=data)

        if action == 'list':
            hosts = api.inventory(ssid=ssid)

            data = {
                'message': 'Listed servers successfully',
                'hosts': hosts
            }
            module.exit_json(changed=True, results=data)

    except Exception as e:
        module.fail_json(msg=e.message)
def all_hosts_done(api_key):
    api = CicoWrapper(endpoint="http://admin.ci.centos.org:8080/",
                      api_key=api_key)
    hosts = api.inventory()
    for host in hosts:
        host_done(api_key, hosts[host].get('comment'))
def host_done(api_key, ssid):
    api = CicoWrapper(endpoint="http://admin.ci.centos.org:8080/",
                      api_key=api_key)
    api.node_done(ssid=ssid)
    eprint("Duffy: Host with ssid %s marked as done" % ssid)
class DuffyLibrary(object):
    def __init__(self):
        # The connection to Duffy
        self.api = None
        # List of the populated Duffy nodes
        self.nodes = []
        self.ssid = []
        self.exec_nodes = []
        self.exit_codes = []

    def _setup_cico_connection(self):
        '''
        Populates self.api with the environment variables that we have. self.api will then
        be used to communicate with Duffy: requesting nodes, releasing nodes, ...
        '''
        if self.api is not None:
            return
        self.api = CicoWrapper(endpoint=os.environ['CICO_ENDPOINT'],
                               api_key=os.environ['CICO_API_KEY'])

    def populate_a_duffy_node(self, **kwargs):
        self._setup_cico_connection()
        self.populate_duffy_nodes(1, **kwargs)

    @keyword('Populate ${count:\d} Duffy nodes')
    def populate_duffy_nodes(self, count=None, **kwargs):
        self._setup_cico_connection()
        assert len(self.nodes) == 0, 'Nodes have already been populated'
        nodes, self.ssid = self.api.node_get(count=count,
                                             retry_count=10,
                                             retry_interval=60,
                                             **kwargs)
        self.nodes = nodes.values()

    def release_the_duffy_nodes(self):
        if self.ssid:
            self.api.node_done(ssid=self.ssid)

    @single_node
    def on_the_duffy_node(self):
        self.exec_nodes = self.nodes

    def get_glob_file(self, filename):
        files = glob.glob(filename)
        assert len(files) == 1, "Expected 1 file, found %s: %s" % (len(files),
                                                                   files)
        return files[0]

    def on_the_first_duffy_node(self):
        self.exec_nodes = [self.nodes[0]]

    def on_the_duffy_nodes(self):
        self.exec_nodes = self.nodes

    def i_try_to_run_locally(self, *args, **kwargs):
        self.exit_codes = [run_command(*args, **kwargs).returncode]

    def i_run_locally(self, *args, **kwargs):
        process = run_command(*args, **kwargs)
        rc = process.returncode
        assert rc == 0, 'Failed to run %s' % args
        return process

    def i_run(self, *args):
        self._exec_ssh_command(*args)

    def i_fetch_the_srpm(self):
        self._exec_sftp_get_command('SRPMS/*.src.rpm', os.environ['WORKSPACE'])

    def i_rsync_the_workspace(self):
        for node in self.exec_nodes:
            rsync_command = ['rsync']
            rsync_command.append('-e')
            rsync_command.append(
                'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -l root'
            )
            rsync_command.append('-rlpt')
            rsync_command.append(os.environ['WORKSPACE'] + '/')
            rsync_command.append(node['ip_address'] + ':ws')
            rc = run_command(*rsync_command).returncode
            assert rc == 0, 'Failed to run %s' % rsync_command

    def get_task_id_from_a_cbs_output(self, process):
        lines = process.stdout
        for l in lines.split('\n'):
            if l.startswith('Created task:'):
                return l.split(' ')[2]

    def it_returns(self, value):
        for code in self.exit_codes:
            assert code == int(
                value), 'Should have returned %s, returned %s.' % (code, value)

    def i_add_a_cbs_yum_repo(self, reponame):
        with NamedTemporaryFile() as f:
            f.write("[%s]\n" % reponame)
            f.write("baseurl=http://cbs.centos.org/repos/%s/$basearch/os/\n" %
                    reponame)
            #FIXME
            f.write("gpgcheck=0\n")
            f.flush()
            self._exec_sftp_send_command(f.name,
                                         '/etc/yum.repos.d/%s.repo' % reponame)

    def _exec_sftp_send_command(self, *args):
        for node in self.exec_nodes:
            scp_command = ['scp']
            scp_command.extend(['-o', 'UserKnownHostsFile=/dev/null'])
            scp_command.extend(['-o', 'StrictHostKeyChecking=no'])
            scp_command.append(args[0])
            scp_command.append('%s@%s:%s' %
                               ('root', node['ip_address'], args[1]))
            rc = run_command(*scp_command).returncode
            assert rc == 0, 'Failed to run %s' % scp_command

    def _exec_sftp_get_command(self, *args):
        for node in self.exec_nodes:
            scp_command = ['scp']
            scp_command.extend(['-o', 'UserKnownHostsFile=/dev/null'])
            scp_command.extend(['-o', 'StrictHostKeyChecking=no'])
            scp_command.append('%s@%s:%s' %
                               ('root', node['ip_address'], args[0]))
            scp_command.append(args[1])
            rc = run_command(*scp_command).returncode
            assert rc == 0, 'Failed to run %s' % scp_command

    def _exec_ssh_command(self, *args):
        exit_codes = []
        for node in self.exec_nodes:
            ssh_command = ['ssh']
            ssh_command.append(node['ip_address'])
            ssh_command.append('-t')
            ssh_command.extend(['-o', 'UserKnownHostsFile=/dev/null'])
            ssh_command.extend(['-o', 'StrictHostKeyChecking=no'])
            ssh_command.extend(['-l', 'root'])
            ssh_command.extend(args)
            rc = run_command(*ssh_command).returncode
            assert rc == 0, 'Failed to run %s' % ssh_command
            exit_codes.append(rc)
        self.exit_codes = exit_codes
Exemple #12
0
def main():
    argument_spec = dict(
        action=dict(required=True, choices=['get', 'done', 'list']),
        arch=dict(default='x86_64', choices=['i386', 'x86_64']),
        release=dict(default='7', choices=['5', '6', '7']),
        count=dict(default='1'),
        retry_count=dict(default='1'),
        retry_interval=dict(default='10'),
        endpoint=dict(default='http://admin.ci.centos.org:8080/'),
        api_key=dict(default=os.getenv('CICO_API_KEY', None)),
        ssid=dict(default=None),
    )
    module = AnsibleModule(argument_spec)

    if not HAS_CICO:
        module.fail_json(msg='cicoclient is required for this module.')

    action = module.params['action']
    arch = module.params['arch']
    release = module.params['release']
    count = module.params['count']
    retry_count = module.params['retry_count']
    retry_interval = module.params['retry_interval']
    endpoint = module.params['endpoint']
    api_key = module.params['api_key']
    ssid = module.params['ssid']

    # Pre-flight validation
    if api_key is None:
        module.fail_json(msg='An API key is required for this module.')

    if action == 'done' and ssid is None:
        module.fail_json(msg='A SSID is required when releasing nodes.')

    try:
        api = CicoWrapper(
            endpoint=endpoint,
            api_key=api_key
        )

        if action == 'get':
            hosts, new_ssid = api.node_get(arch=arch, ver=release, count=count,
                                           retry_count=retry_count,
                                           retry_interval=retry_interval)
            data = {
                'message': 'Requested servers successfully',
                'hosts': hosts,
                'ssid': new_ssid
            }
            module.exit_json(changed=True, results=data)

        if action == 'done':
            hosts = api.node_done(ssid=ssid)
            data = {
                'message': 'Released servers successfully',
                'hosts': hosts
            }
            module.exit_json(changed=True, results=data)

        if action == 'list':
            hosts = api.inventory(ssid=ssid)

            data = {
                'message': 'Listed servers successfully',
                'hosts': hosts
            }
            module.exit_json(changed=True, results=data)

    except Exception as e:
        module.fail_json(msg=e.message)