コード例 #1
0
def create_stack_instances(req_params):
    state = CloudFormationRegion.get()
    set_name = req_params.get('StackSetName')
    stack_set = [
        sset for sset in state.stack_sets.values()
        if sset.stack_set_name == set_name
    ]
    if not stack_set:
        return not_found_error('Stack set named "%s" does not exist' %
                               set_name)
    stack_set = stack_set[0]
    op_id = req_params.get('OperationId') or short_uid()
    sset_meta = stack_set.metadata
    accounts = extract_url_encoded_param_list(req_params, 'Accounts.member.%s')
    accounts = accounts or extract_url_encoded_param_list(
        req_params, 'DeploymentTargets.Accounts.member.%s')
    regions = extract_url_encoded_param_list(req_params, 'Regions.member.%s')
    stacks_to_await = []
    for account in accounts:
        for region in regions:
            # deploy new stack
            LOG.debug('Deploying instance for stack set "%s" in region "%s"' %
                      (set_name, region))
            cf_client = aws_stack.connect_to_service('cloudformation',
                                                     region_name=region)
            kwargs = select_attributes(sset_meta,
                                       'TemplateBody') or select_attributes(
                                           sset_meta, 'TemplateURL')
            stack_name = 'sset-%s-%s' % (set_name, account)
            result = cf_client.create_stack(StackName=stack_name, **kwargs)
            stacks_to_await.append((stack_name, region))
            # store stack instance
            instance = {
                'StackSetId': sset_meta['StackSetId'],
                'OperationId': op_id,
                'Account': account,
                'Region': region,
                'StackId': result['StackId'],
                'Status': 'CURRENT',
                'StackInstanceStatus': {
                    'DetailedStatus': 'SUCCEEDED'
                }
            }
            instance = StackInstance(instance)
            stack_set.stack_instances.append(instance)
    # wait for completion of stack
    for stack in stacks_to_await:
        aws_stack.await_stack_completion(stack[0], region_name=stack[1])
    # record operation
    operation = {
        'OperationId': op_id,
        'StackSetId': stack_set.metadata['StackSetId'],
        'Action': 'CREATE',
        'Status': 'SUCCEEDED'
    }
    stack_set.operations[op_id] = operation
    result = {'OperationId': op_id}
    return result
コード例 #2
0
def create_stack_instances(req_params):
    state = CloudFormationRegion.get()
    set_name = req_params.get("StackSetName")
    stack_set = [
        sset for sset in state.stack_sets.values()
        if sset.stack_set_name == set_name
    ]
    if not stack_set:
        return not_found_error('Stack set named "%s" does not exist' %
                               set_name)
    stack_set = stack_set[0]
    op_id = req_params.get("OperationId") or short_uid()
    sset_meta = stack_set.metadata
    accounts = extract_url_encoded_param_list(req_params, "Accounts.member.%s")
    accounts = accounts or extract_url_encoded_param_list(
        req_params, "DeploymentTargets.Accounts.member.%s")
    regions = extract_url_encoded_param_list(req_params, "Regions.member.%s")
    stacks_to_await = []
    for account in accounts:
        for region in regions:
            # deploy new stack
            LOG.debug('Deploying instance for stack set "%s" in region "%s"',
                      set_name, region)
            cf_client = aws_stack.connect_to_service("cloudformation",
                                                     region_name=region)
            kwargs = select_attributes(sset_meta,
                                       "TemplateBody") or select_attributes(
                                           sset_meta, "TemplateURL")
            stack_name = "sset-%s-%s" % (set_name, account)
            result = cf_client.create_stack(StackName=stack_name, **kwargs)
            stacks_to_await.append((stack_name, region))
            # store stack instance
            instance = {
                "StackSetId": sset_meta["StackSetId"],
                "OperationId": op_id,
                "Account": account,
                "Region": region,
                "StackId": result["StackId"],
                "Status": "CURRENT",
                "StackInstanceStatus": {
                    "DetailedStatus": "SUCCEEDED"
                },
            }
            instance = StackInstance(instance)
            stack_set.stack_instances.append(instance)
    # wait for completion of stack
    for stack in stacks_to_await:
        aws_stack.await_stack_completion(stack[0], region_name=stack[1])
    # record operation
    operation = {
        "OperationId": op_id,
        "StackSetId": stack_set.metadata["StackSetId"],
        "Action": "CREATE",
        "Status": "SUCCEEDED",
    }
    stack_set.operations[op_id] = operation
    result = {"OperationId": op_id}
    return result
コード例 #3
0
ファイル: provider.py プロジェクト: localstack/localstack
    def create_stack_instances(
        self,
        context: RequestContext,
        request: CreateStackInstancesInput,
    ) -> CreateStackInstancesOutput:
        state = CloudFormationRegion.get()

        set_name = request.get("StackSetName")
        stack_set = [sset for sset in state.stack_sets.values() if sset.stack_set_name == set_name]

        if not stack_set:
            return not_found_error(f'Stack set named "{set_name}" does not exist')

        stack_set = stack_set[0]
        op_id = request.get("OperationId") or short_uid()
        sset_meta = stack_set.metadata
        accounts = request["Accounts"]
        regions = request["Regions"]

        stacks_to_await = []
        for account in accounts:
            for region in regions:
                # deploy new stack
                LOG.debug('Deploying instance for stack set "%s" in region "%s"', set_name, region)
                cf_client = aws_stack.connect_to_service("cloudformation", region_name=region)
                kwargs = select_attributes(sset_meta, ["TemplateBody"]) or select_attributes(
                    sset_meta, ["TemplateURL"]
                )
                stack_name = f"sset-{set_name}-{account}"
                result = cf_client.create_stack(StackName=stack_name, **kwargs)
                stacks_to_await.append((stack_name, region))
                # store stack instance
                instance = {
                    "StackSetId": sset_meta["StackSetId"],
                    "OperationId": op_id,
                    "Account": account,
                    "Region": region,
                    "StackId": result["StackId"],
                    "Status": "CURRENT",
                    "StackInstanceStatus": {"DetailedStatus": "SUCCEEDED"},
                }
                instance = StackInstance(instance)
                stack_set.stack_instances.append(instance)

        # wait for completion of stack
        for stack in stacks_to_await:
            aws_stack.await_stack_completion(stack[0], region_name=stack[1])

        # record operation
        operation = {
            "OperationId": op_id,
            "StackSetId": stack_set.metadata["StackSetId"],
            "Action": "CREATE",
            "Status": "SUCCEEDED",
        }
        stack_set.operations[op_id] = operation

        return CreateStackInstancesOutput(OperationId=op_id)