def build_and_run_client()

in deploy/deploy_agent.py [0:0]


def build_and_run_client(iotClient, iotConfig):
    #create test policy
    try:
        iotClient.get_policy(
            policyName=iotConfig['devicePolicy']
        )
    except iotClient.exceptions.ResourceNotFoundException as e:
        logging.info('Create test policy %s', iotConfig['devicePolicy'])
        iotClient.create_policy(
            policyName=iotConfig['devicePolicy'],
            policyDocument=json.dumps({
                'Version': '2012-10-17',
                'Statement': [{
                    'Effect': 'Allow',
                    'Action': 'iot:*',
                    'Resource': '*'
                }]
            })
        )
    # create test thing group
    try:
        iotClient.describe_thing_group(thingGroupName=iotConfig['thingGroup'])
    except iotClient.exceptions.ResourceNotFoundException as e:
        logging.info('Create test thing group %s', iotConfig['thingGroup'])
        iotClient.create_thing_group(thingGroupName=iotConfig['thingGroup'])

    # create thing
    client_id = str(uuid.uuid4())
    thing_name = 'stressTest_%s' % client_id
    iotClient.create_thing(thingName=thing_name)
    iotClient.add_thing_to_thing_group(
        thingGroupName=iotConfig['thingGroup'], thingName=thing_name
    )
    resp = iotClient.create_keys_and_certificate(setAsActive=True)
    certificateArn = resp['certificateArn']
    thing_cert = '%s/%s.pem.crt' % (iotConfig['thingCertDir'], client_id)
    thing_key = '%s/%s.pem.key' % (iotConfig['thingCertDir'], client_id)
    with open(thing_cert, 'w') as f:
        f.write(resp['certificatePem'])
    with open(thing_key, 'w') as f:
        f.write(resp['keyPair']['PrivateKey'])
    rootCAPath = '%s/%s' % (iotConfig['thingCertDir'], os.path.basename(iotConfig['rootCA']))
    copyfile(iotConfig['rootCA'], rootCAPath)
    iotClient.attach_thing_principal(
        thingName=thing_name,
        principal=certificateArn
    )
    iotClient.attach_policy(
        policyName=iotConfig['devicePolicy'],
        target=certificateArn
    )
    endpoint = iotClient.describe_endpoint(endpointType='iot:Data-ATS')['endpointAddress']

    # change config
    configs_map = {
        'AWS_IOT_MQTT_HOST':            'AWS_IOT_MQTT_HOST              "%s"' % endpoint,
        'AWS_IOT_MQTT_CLIENT_ID':       'AWS_IOT_MQTT_CLIENT_ID         "%s"' % client_id,
        'AWS_IOT_MY_THING_NAME':        'AWS_IOT_MY_THING_NAME          "%s"' % thing_name,
        'AWS_IOT_ROOT_CA_FILENAME':     'AWS_IOT_ROOT_CA_FILENAME       "%s"' % os.path.basename(rootCAPath),
        'AWS_IOT_CERTIFICATE_FILENAME': 'AWS_IOT_CERTIFICATE_FILENAME   "%s"' % os.path.basename(thing_cert),
        'AWS_IOT_PRIVATE_KEY_FILENAME': 'AWS_IOT_PRIVATE_KEY_FILENAME   "%s"' % os.path.basename(thing_key)
    }
    for line in fileinput.input(iotConfig['iotConfigPath'], inplace=True):
        for src, dest in configs_map.items():
            if src in line:
                line = re.sub(src + '.*', dest, line)
                break
        sys.stdout.write(line)

    # build
    work_dir = os.getcwd()
    sample_dir = os.path.dirname(iotConfig['iotConfigPath'])
    sample_name = os.path.basename(sample_dir)
    os.chdir(sample_dir)
    os.system('make')
    os.rename(sample_name, client_id)

    # run
    os.chdir(work_dir)
    exefile_path = os.path.join(work_dir, sample_dir, client_id)
    p = Process(target=run_client, args=(exefile_path,))
    p.start()
    p.join()

    return client_id