def run(replay, fuzz_job_id, replay_job_id=None):
    cli = RaftCLI()
    substitutions = {
        '{defaults.deploymentName}': cli.definitions.deployment,
        '{jobRunId}': fuzz_job_id
    }
    replay_job_config = RaftJobConfig(file_path=replay,
                                      substitutions=substitutions)

    print('Replay')
    isIdle = False
    for task in replay_job_config.config['tasks']:
        isIdle = isIdle or task['isIdling']

    if isIdle and replay_job_id:
        cli.update_job(replay_job_id, replay_job_config)
        print(f'Idle Job: {replay_job_id}')
    else:
        # create new fuzz job configuration
        replay_job_id = cli.new_job(replay_job_config)
        if isIdle:
            print(f'New Idle Job: {replay_job_id}')
        else:
            print(f'New Job: {replay_job_id}')

    if not isIdle:
        # wait for job ID from fuzz_job to finish the run
        cli.poll(replay_job_id['jobId'])
Ejemplo n.º 2
0
def run(args):
    def validate(defaults):
        s = defaults.get('subscription')
        d = defaults.get('deploymentName')
        r = defaults.get('region')
        return (s and d and r)

    defaults_path = args['defaults_context_path']
    defaults_json = args['defaults_context_json']

    if defaults_json:
        print(f"Loading defaults from command line: {defaults_json}")
        defaults = json.loads(defaults_json)
        if not validate(defaults):
            print(defaults_help)
            return
    # check if defaults.json is set in the context and it exists
    elif os.path.isfile(defaults_path):
        with open(defaults_path, 'r') as d:
            defaults = json.load(d)
            if not validate(defaults):
                print(defaults_help)
                return
    else:
        with open(defaults_path, 'w') as d:
            d.write(fresh_defaults)
        print(defaults_help)
        return

    defaults['secret'] = args.get('secret')
    if 'metricsOptIn' not in defaults:
        defaults['metricsOptIn'] = True

    if defaults.get('useAppInsights') is None:
        defaults['useAppInsights'] = True

    cli_action = args.get('logout')
    service_action = args.get('service-action')
    job_action = args.get('job-action')
    webhook_action = args.get('webhook-action')

    if cli_action == 'logout':
        raft_sdk.raft_common.delete_token_cache()
    elif service_action:
        service_cli = RaftServiceCLI(defaults, defaults_path,
                                     args.get('secret'))
        if service_action == 'restart':
            service_cli.restart()
        elif service_action == 'info':
            info = service_cli.service_info()
            print(info)
        elif service_action == 'deploy':
            skip_sp_deployment = args.get('skip_sp_deployment')
            service_cli.deploy(
                args['sku'], skip_sp_deployment and skip_sp_deployment is True)
        elif service_action == 'upload-tools':
            utils_file_share = f'{uuid.uuid4()}'
            service_cli.upload_utils(utils_file_share,
                                     args.get('custom_tools_path'))
            service_cli.restart()
        else:
            raise Exception(f'Unhandled service argument: {service_action}')

    elif job_action:
        cli = RaftCLI()
        if job_action == 'create':
            json_config_path = args.get('file')
            if json_config_path is None:
                ArgumentRequired('--file')

            substitutionDictionary = {}
            substitutionParameter = args.get('substitute')
            if substitutionParameter:
                substitutionDictionary = json.loads(substitutionParameter)

            job_config = RaftJobConfig(file_path=json_config_path,
                                       substitutions=substitutionDictionary)

            print(job_config.config)
            duration = args.get('duration')
            if duration:
                job_config.config['duration'] = duration

            metadata = args.get('metadata')
            if metadata:
                job_config.config['metadata'] = json.loads(metadata)

            newJob = cli.new_job(job_config, args.get('region'))
            poll_interval = args.get('poll')
            if poll_interval:
                print(newJob)
                cli.poll(newJob['jobId'], poll_interval)
            else:
                print(newJob)

        elif job_action == 'status':
            job_id = args.get('job_id')
            if job_id is None:
                ArgumentRequired('--job-id')
            job_status = cli.job_status(job_id)
            poll_interval = args.get('poll')
            cli.print_status(job_status)
            if poll_interval:
                cli.poll(job_id, poll_interval)

        elif job_action == 'list':
            jobs_list = cli.list_jobs(args['look_back_hours'])
            sorted = {}
            for job in jobs_list:
                if sorted.get(job['jobId']):
                    sorted[job['jobId']].append(job)
                else:
                    sorted[job['jobId']] = [job]

            for s in sorted:
                cli.print_status(sorted[s])
                print()

            print(f"Total number of jobs: {len(sorted)}")

        elif job_action == 'update':
            json_config_path = args.get('file')
            if json_config_path is None:
                ArgumentRequired('--file')

            substitutionDictionary = {}
            substitutionParameter = args.get('substitute')
            if substitutionParameter:
                substitutionDictionary = json.loads(substitutionParameter)

            job_update = cli.update_job(
                args.get('job_id'),
                RaftJobConfig(file_path=json_config_path,
                              substitutions=substitutionDictionary))
            print(job_update)

        elif job_action == 'results':
            job_id = args.get('job_id')
            if job_id is None:
                ArgumentRequired('--job-id')
            url = cli.result_url(job_id)
            print(url)

        elif job_action == 'delete':
            job_id = args.get('job_id')
            if job_id is None:
                ArgumentRequired('--job-id')
            job_delete = cli.delete_job(job_id)
            print(job_delete)

    elif webhook_action:
        cli = RaftCLI()
        if webhook_action == 'events':
            webhook_events = cli.list_available_webhooks_events()
            print(webhook_events)

        elif webhook_action == 'create':
            name_parameter = args.get('name')
            if name_parameter is None:
                ArgumentRequired('--name')

            event_parameter = args.get('event')
            if event_parameter is None:
                ArgumentRequired('--event')

            uri_parameter = args.get('url')
            if uri_parameter is None:
                ArgumentRequired('--url')

            webhook_create_or_update = cli.set_webhooks_subscription(
                name_parameter, event_parameter, uri_parameter)
            print(webhook_create_or_update)

        elif webhook_action == 'test':
            name_parameter = args.get('name')
            if name_parameter is None:
                ArgumentRequired('--name')

            event_parameter = args.get('event')
            if event_parameter is None:
                ArgumentRequired('--event')

            webhook_test = cli.test_webhook(name_parameter, event_parameter)
            print(webhook_test)

        elif webhook_action == 'delete':
            name_parameter = args.get('name')
            if name_parameter is None:
                ArgumentRequired('--name')

            event_parameter = args.get('event')
            if event_parameter is None:
                ArgumentRequired('--event')

            webhook_delete = cli.delete_webhook(name_parameter,
                                                event_parameter)
            print(webhook_delete)

        elif webhook_action == 'list':
            name_parameter = args.get('name')
            if name_parameter is None:
                ArgumentRequired('--name')

            # the event name is not required.
            # If not supplied all events will be listed.
            event_parameter = args.get('event')

            webhooks_list = cli.list_webhooks(name_parameter, event_parameter)
            print(webhooks_list)

        else:
            raise Exception('Expected arguments could not be found in args')