示例#1
0
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)
示例#2
0
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)
示例#3
0
def test_get_full_data(job_id, dci_context):
    full_data_job = job.get_full_data(dci_context, job_id)
    assert full_data_job['remoteci']['data'] == {'remoteci': 'remoteci'}
    assert full_data_job['jobdefinition']['name'] == 'tname'
    assert full_data_job['components'][0]['name'] == 'hihi'
示例#4
0
def test_get_full_data(job_id, dci_context):
    full_data_job = job.get_full_data(dci_context, job_id)
    assert full_data_job['remoteci'] == {'remoteci': 'remoteci'}
    assert full_data_job['test'] == {'test': 'test'}
    assert full_data_job['components'] == [{'component': 'component'}]
# Our DCI connection
dci_context = dci_context.build_dci_context(
    'http://127.0.0.1',
    'remoteci_1',
    'welcome')

# RemoteCI id and Topic id -- probably better to be dynamically passed in
dci_context.remoteci_id = 'fd6c285c-fa57-4aa8-a8b3-c68a4acdfa9c'
dci_context.topic_id = 'fe145e49-992a-4843-a44f-b058c7a05261'

# schedule the job and pull down data
dci_context.job_id = dci_job.schedule(dci_context,
                                      remoteci_id=dci_context.remoteci_id,
                                      topic_id=dci_context.topic_id).json()['job']['id']

job_full_data = dci_job.get_full_data(dci_context, dci_context.job_id)

# create initial jobstate of pre-run
jobstate = dci_jobstate.create(dci_context, 'pre-run', 'Initializing the environment', dci_context.job_id)
print "This is where we'd do some stuff to init the environment"

# update the jobstate to start the job run
dci_jobstate.create(dci_context, 'running', 'Running the test', dci_context.job_id)
jobstate_id = dci_context.last_jobstate_id
result = execute_testing()

# read our testing log and push to the DCI control server
home = expanduser('~')
with io.open(home + '/.ansible/logs/run.log', encoding='utf-8') as f:
    content = f.read(20 * 1024 * 1024) # default file size is 20MB
    dci_file.create(dci_context, home + '/.ansible/logs/run.log', content, 'text/plain', jobstate_id)