-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
package.py
152 lines (126 loc) · 5.02 KB
/
package.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import os
import logging
import sys
from botocore.client import Config
from awscli.customizations.cloudformation.s3uploader import S3Uploader
from awscli.customizations.cloudformation.artifact_exporter import Template
from awscli.customizations.cloudformation.yamlhelper import yaml_dump
from awscli.customizations.cloudformation import exceptions
from awscli.customizations.commands import BasicCommand
LOG = logging.getLogger(__name__)
class PackageCommand(BasicCommand):
MSG_PACKAGED_TEMPLATE_WRITTEN = (
"Successfully packaged artifacts and wrote output template "
"to file {output_file_name}."
"\n"
"Execute the following command to deploy the packaged template"
"\n"
"aws cloudformation deploy --template-file {output_file_path} "
"--stack-name <YOUR STACK NAME>"
"\n")
NAME = "package"
DESCRIPTION = BasicCommand.FROM_FILE("cloudformation",
"_package_description.rst")
ARG_TABLE = [
{
'name': 'template-file',
'required': True,
'help_text': (
'The path where your AWS CloudFormation'
' template is located.'
)
},
{
'name': 's3-bucket',
'required': True,
'help_text': (
'The name of the S3 bucket where this command uploads'
' the artifacts that are referenced in your template.'
)
},
{
'name': 's3-prefix',
'help_text': (
'A prefix name that the command adds to the'
' artifacts\' name when it uploads them to the S3 bucket.'
' The prefix name is a path name (folder name) for'
' the S3 bucket.'
)
},
{
'name': 'kms-key-id',
'help_text': (
'The ID of an AWS KMS key that the command uses'
' to encrypt artifacts that are at rest in the S3 bucket.'
)
},
{
"name": "output-template-file",
"help_text": (
"The path to the file where the command writes the"
" output AWS CloudFormation template. If you don't specify"
" a path, the command writes the template to the standard"
" output."
)
},
{
"name": "force-upload",
"action": "store_true",
"help_text": (
'Indicates whether to override existing files in the S3 bucket.'
' Specify this flag to upload artifacts even if they '
' match existing artifacts in the S3 bucket.'
)
}
]
def _run_main(self, parsed_args, parsed_globals):
s3_client = self._session.create_client(
"s3",
config=Config(signature_version='s3v4'),
region_name=parsed_globals.region,
verify=parsed_globals.verify_ssl)
template_path = parsed_args.template_file
if not os.path.isfile(template_path):
raise exceptions.InvalidTemplatePathError(
template_path=template_path)
bucket = parsed_args.s3_bucket
self.s3_uploader = S3Uploader(s3_client,
bucket,
parsed_globals.region,
parsed_args.s3_prefix,
parsed_args.kms_key_id,
parsed_args.force_upload)
output_file = parsed_args.output_template_file
exported_str = self._export(template_path)
sys.stdout.write("\n")
self.write_output(output_file, exported_str)
if output_file:
msg = self.MSG_PACKAGED_TEMPLATE_WRITTEN.format(
output_file_name=output_file,
output_file_path=os.path.abspath(output_file))
sys.stdout.write(msg)
sys.stdout.flush()
return 0
def _export(self, template_path):
template = Template(template_path, os.getcwd(), self.s3_uploader)
exported_template = template.export()
exported_str = yaml_dump(exported_template)
return exported_str
def write_output(self, output_file_name, data):
if output_file_name is None:
sys.stdout.write(data)
return
with open(output_file_name, "w") as fp:
fp.write(data)