コード例 #1
0
ファイル: osp.py プロジェクト: redhat-cip/python-dciclient
def refresh_puddles(ctx):
    versions = [
        {
            'name': '10.0',
            'urls': [
                'http://download.eng.bos.redhat.com/rcm-guest/puddles/OpenStack/10.0-RHEL-7/latest/RH7-RHOS-10.0.repo',  # noqa
             ],
            'topic_id': dci_topic.get(ctx, 'OSP10').json()['topic']['id']
        },
        {
            'name': '9.0',
            'urls': [
                'http://download.eng.bos.redhat.com/rcm-guest/puddles/OpenStack/9.0-RHEL-7/latest/RH7-RHOS-9.0.repo',  # noqa
                'http://download.eng.bos.redhat.com/rcm-guest/puddles/OpenStack/9.0-RHEL-7-director/latest/RH7-RHOS-9.0-director.repo'],  # noqa
            'topic_id': dci_topic.get(ctx, 'OSP9').json()['topic']['id']
        },
        {
            'name': '8.0',
            'urls': [
                'http://download.eng.bos.redhat.com/rel-eng/OpenStack/8.0-RHEL-7/latest/RH7-RHOS-8.0.repo',  # noqa
                'http://download.eng.bos.redhat.com/rel-eng/OpenStack/8.0-RHEL-7-director/latest/RH7-RHOS-8.0-director.repo'],  # noqa
            'topic_id': dci_topic.get(ctx, 'OSP8').json()['topic']['id']
        }
    ]

    for v in versions:
        components = get_components(v['urls'], v['topic_id'])
        for c in components:
            dci_component.create(ctx, **c)
            r = dci_component.get(ctx, c['name'], embed='file')
            if r.status_code == 401:
                continue
            if r.status_code == 404:
                print('Component %s not found' % c['name'])
                exit(1)
            component = r.json()['component']
            r_current_components = dci_component.file_list(
                ctx, id=component['id'])
            if len(r_current_components.json()['component_files']):
                print('%s already has its data. Skipped.' % component['name'])
                continue

            clean_up(component)
            prepare_archive(component)
            print('Uploading bits...')
            r = dci_component.file_upload(
                ctx, id=component['id'], file_path='archive.tar')
            if r.status_code != 201:
                print((
                    '  Upload of archive.tar has '
                    'failed for component %s: %s') % (
                        component['name'], r.text))
                exit(1)
            else:
                print('  success: %s' % r.json())
            clean_up(component)
コード例 #2
0
ファイル: dci_new_job.py プロジェクト: Spredzy/dci-ansible
def main():
    module = AnsibleModule(argument_spec=dict(
        state=dict(default='present',
                   choices=['present', 'absent'],
                   type='str'),
        login=dict(required=False, type='str'),
        password=dict(required=False, type='str'),
        topic=dict(required=False, type='str'),
        remoteci=dict(type='str'),
        url=dict(required=False, type='str'),
    ), )

    if not requests_found:
        module.fail_json(msg='The python requests module is required')

    topic_list = [module.params['topic'], os.getenv('DCI_TOPIC')]
    topic = next((item for item in topic_list if item is not None), None)

    login, password, url = get_details(module)
    if not login or not password:
        module.fail_json(msg='login and/or password have not been specified')

    ctx = dci_context.build_dci_context(url, login, password, 'ansible')

    topic_id = dci_topic.get(ctx, topic).json()['topic']['id']
    remoteci = dci_remoteci.get(ctx, module.params['remoteci']).json()
    remoteci_id = remoteci['remoteci']['id']

    dci_job.schedule(ctx, remoteci_id, topic_id=topic_id)
    jb = dci_job.get_full_data(ctx, ctx.last_job_id)
    jb['job_id'] = ctx.last_job_id

    module.exit_json(**jb)
コード例 #3
0
ファイル: api.py プロジェクト: redhat-cip/dci-downloader
def get_topic_by_id(topic_id):
    context = build_signature_context()
    c = dci_topic.get(context, topic_id)
    topic = c.json()["topic"]
    if len(topic) == 0:
        print("Ensure that topic %s exists or that you have access" % topic_id)
        print("Contact your EPM for more information.")
        return
    return topic
コード例 #4
0
def test_job_upgraded(dci_context, job_id, topic_id, product_id):
    old = topic.get(dci_context, id=topic_id).json()["topic"]
    new = topic.create(dci_context,
                       "bar_topic", ["type_1"],
                       product_id=product_id)
    t = new.json()["topic"]
    component.create(dci_context, "bar_component", "type_1", t["id"])
    topic.update(dci_context,
                 id=topic_id,
                 etag=old["etag"],
                 next_topic_id=t["id"])
    r = job.upgrade(dci_context, job_id=job_id)
    assert r.status_code == 201
    assert r.json()["job"]["previous_job_id"] == job_id
コード例 #5
0
ファイル: dci_topic.py プロジェクト: goneri/dci-ansible
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(default='present',
                       choices=['present', 'absent'],
                       type='str'),
            # Authentication related parameters
            #
            login=dict(required=False, type='str'),
            password=dict(required=False, type='str'),
            url=dict(required=False, type='str'),
            # Resource related parameters
            #
            id=dict(type='str'),
            name=dict(type='str'),
            label=dict(type='str'),
        ), )

    if not dciclient_found:
        module.fail_json(msg='The python dciclient module is required')

    login, password, url = get_details(module)
    if not login or not password:
        module.fail_json(msg='login and/or password have not been specified')

    ctx = dci_context.build_dci_context(url, login, password, 'Ansible')

    # Action required: List all topics
    # Endpoint called: /topics GET via dci_topic.list()
    #
    # List all topics
    if module_params_empty(module.params):
        res = dci_topic.list(ctx)

    # Action required: Delete the topic matching topic id
    # Endpoint called: /topics/<topic_id> DELETE via dci_topic.delete()
    #
    # If the topic exists and it has been succesfully deleted the changed is
    # set to true, else if the topic does not exist changed is set to False
    elif module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_topic.delete(ctx, module.params['id'])

    # Action required: Retrieve topic informations
    # Endpoint called: /topic/<topic_id> GET via dci_topic.get()
    #
    # Get topic informations
    elif module.params[
            'id'] and not module.params['name'] and not module.params['label']:
        res = dci_topic.get(ctx, module.params['id'])

    # Action required: Update an existing topic
    # Endpoint called: /topics/<topic_id> PUT via dci_topic.update()
    #
    # Update the topic with the specified characteristics.
    elif module.params['id']:
        res = dci_topic.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 409]:
            kwargs = {
                'id': module.params['id'],
                'etag': res.json()['topic']['etag']
            }
            if module.params['name']:
                kwargs['name'] = module.params['name']
            if module.params['label']:
                kwargs['label'] = module.params['label']
            res = dci_topic.update(ctx, **kwargs)

    # Action required: Creat a topic with the specified content
    # Endpoint called: /topics POST via dci_topic.create()
    #
    # Create the new topic.
    else:
        if not module.params['name']:
            module.fail_json(msg='name parameter must be specified')

        kwargs = {'name': module.params['name']}
        if module.params['label']:
            kwargs['label'] = module.params['label']

        res = dci_topic.create(ctx, **kwargs)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code == 409:
            result = dci_topic.get(ctx, module.params['name']).json()
            result['changed'] = False
        else:
            result['changed'] = True
    except:
        result = {}
        result['changed'] = True

    module.exit_json(**result)
コード例 #6
0
ファイル: topic.py プロジェクト: asergienk/python-dciclient
def show(context, args):
    return topic.get(context, args.id)
コード例 #7
0
ファイル: dci_job.py プロジェクト: Spredzy/dci-ansible
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(default='present',
                       choices=['present', 'absent'],
                       type='str'),
            # Authentication related parameters
            #
            login=dict(required=False, type='str'),
            password=dict(required=False, type='str'),
            url=dict(required=False, type='str'),
            # Resource related parameters
            #
            id=dict(type='str'),
            topic=dict(required=False, type='str'),
            remoteci=dict(type='str'),
            comment=dict(type='str'),
            status=dict(type='str'),
            configuration=dict(type='dict'),
        ), )

    if not dciclient_found:
        module.fail_json(msg='The python dciclient module is required')

    topic_list = [module.params['topic'], os.getenv('DCI_TOPIC')]
    topic = next((item for item in topic_list if item is not None), None)

    login, password, url = get_details(module)
    if not login or not password:
        module.fail_json(msg='login and/or password have not been specified')

    ctx = dci_context.build_dci_context(url, login, password, 'Ansible')

    # Action required: Delete the job matching the job id
    # Endpoint called: /jobs/<job_id> DELETE via dci_job.delete()
    #
    # If the job exist and it has been succesfully deleted the changed is
    # set to true, else if the file does not exist changed is set to False
    if module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_job.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 422]:
            kwargs = {
                'id': module.params['id'],
                'etag': res.json()['job']['etag']
            }
            res = dci_job.delete(ctx, **kwargs)

    # Action required: Retrieve job informations
    # Endpoint called: /jobs/<job_id> GET via dci_job.get()
    #
    # Get job informations
    elif module.params[
            'id'] and not module.params['comment'] and not module.params[
                'status'] and not module.params['configuration']:
        res = dci_job.get(ctx, module.params['id'])

    # Action required: Update an existing job
    # Endpoint called: /jobs/<job_id> PUT via dci_job.update()
    #
    # Update the job with the specified characteristics.
    elif module.params['id']:
        res = dci_job.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 422]:
            kwargs = {
                'id': module.params['id'],
                'etag': res.json()['job']['etag']
            }
            if module.params['comment']:
                kwargs['comment'] = module.params['comment']
            if module.params['status']:
                kwargs['status'] = module.params['status']
            if module.params['configuration']:
                kwargs['configuration'] = module.params['configuration']
            res = dci_job.update(ctx, **kwargs)

    # Action required: Schedule a new job
    # Endpoint called: /jobs/schedule POST via dci_job.schedule()
    #
    # Schedule a new job against the DCI Control-Server
    else:
        topic_id = dci_topic.get(ctx, topic).json()['topic']['id']
        remoteci = dci_remoteci.get(ctx, module.params['remoteci']).json()
        remoteci_id = remoteci['remoteci']['id']

        res = dci_job.schedule(ctx, remoteci_id, topic_id=topic_id)
        if res.status_code not in [400, 401, 404, 422]:
            res = dci_job.get_full_data(ctx, ctx.last_job_id)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code in [400, 401, 422]:
            result['changed'] = False
        else:
            result['changed'] = True
    except AttributeError:
        # Enter here if new job has been schedule, return of get_full_data is already json.
        result = res
        result['changed'] = True
        result['job_id'] = ctx.last_job_id
    except:
        result = {}
        result['changed'] = True

    module.exit_json(**result)
コード例 #8
0
def main(argv=None):
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
    parser = argparse.ArgumentParser()
    parser.add_argument('--topic')
    parser.add_argument('--config', default='/etc/dci/dci_agent.yaml')
    parser.add_argument('--version',
                        action='version',
                        version=('dci-agent %s' % version))
    args = parser.parse_args(argv)

    dci_conf = load_config(args.config)
    ctx = get_dci_context(**dci_conf['auth'])
    topic_name = args.topic if args.topic else dci_conf['topic']
    topic = dci_topic.get(ctx, topic_name).json()['topic']
    remoteci = dci_remoteci.get(ctx, dci_conf['remoteci']).json()['remoteci']
    r = dci_job.schedule(ctx, remoteci['id'], topic_id=topic['id'])
    if r.status_code == 412:
        logging.info('Nothing to do')
        exit(0)
    elif r.status_code != 201:
        logging.error('Unexpected code: %d' % r.status_code)
        logging.error(r.text)
        exit(1)
    components = dci_job.get_components(ctx,
                                        ctx.last_job_id).json()['components']
    logging.debug(components)

    try:
        prepare_local_mirror(ctx, dci_conf['mirror']['directory'],
                             dci_conf['mirror']['url'], components)
        dci_jobstate.create(ctx, 'pre-run', 'director node provisioning',
                            ctx.last_job_id)
        for c in dci_conf['hooks']['provisioning']:
            dci_helper.run_command(ctx, c, shell=True)
        init_undercloud_host(dci_conf['undercloud_ip'],
                             dci_conf['key_filename'])
        dci_jobstate.create(ctx, 'running', 'undercloud deployment',
                            ctx.last_job_id)
        for c in dci_conf['hooks']['undercloud']:
            dci_helper.run_command(ctx, c, shell=True)
        dci_jobstate.create(ctx, 'running', 'overcloud deployment',
                            ctx.last_job_id)
        for c in dci_conf['hooks']['overcloud']:
            dci_helper.run_command(ctx, c, shell=True)
        dci_tripleo_helper.run_tests(ctx,
                                     undercloud_ip=dci_conf['undercloud_ip'],
                                     key_filename=dci_conf['key_filename'],
                                     remoteci_id=remoteci['id'],
                                     stack_name=dci_conf.get(
                                         'stack_name', 'overcloud'))
        final_status = 'success'
        backtrace = ''
        msg = ''
    except Exception as e:
        final_status = 'failure'
        backtrace = traceback.format_exc()
        msg = str(e)
        pass

    # Teardown should happen even in case of failure and should not make the
    # agent run fail.
    try:
        teardown_commands = dci_conf['hooks'].get('teardown')
        if teardown_commands:
            dci_jobstate.create(ctx, 'post-run', 'teardown', ctx.last_job_id)
            for c in teardown_commands:
                dci_helper.run_command(ctx, c, shell=True)
    except Exception as e:
        backtrace_teardown = str(e) + '\n' + traceback.format_exc()
        logging.error(backtrace_teardown)
        dci_file.create(ctx,
                        'backtrace_teardown',
                        backtrace_teardown,
                        mime='text/plain',
                        jobstate_id=ctx.last_jobstate_id)
        pass

    dci_jobstate.create(ctx, final_status, msg, ctx.last_job_id)
    logging.info('Final status: ' + final_status)
    if backtrace:
        logging.error(backtrace)
        dci_file.create(ctx,
                        'backtrace',
                        backtrace,
                        mime='text/plain',
                        jobstate_id=ctx.last_jobstate_id)
    sys.exit(0 if final_status == 'success' else 1)