forked from SUSE-Enceladus/ec2imgutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2listimg
executable file
·188 lines (172 loc) · 5.39 KB
/
ec2listimg
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python3
# Copyright 2020 SUSE LLC
#
# This file is part of ec2imgutils
#
# ec2imgutils is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ec2imgutils is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ec2publishimg. If not, see <http://www.gnu.org/licenses/>.
import argparse
import os
import sys
import ec2imgutils.ec2utils as utils
import ec2imgutils.ec2listimg as ec2lsimg
from ec2imgutils.ec2imgutilsExceptions import (
EC2AccountException,
EC2ListImgException
)
# Set up command line argument parsing
argparse = argparse.ArgumentParser(description='List account owned images')
argparse.add_argument(
'-a', '--account',
dest='account_name',
help='Account to use',
metavar='ACCOUNT_NAME',
)
argparse.add_argument(
'--access-id',
dest='access_key',
help='AWS access key (Optional)',
metavar='AWS_ACCESS_KEY'
)
argparse.add_argument(
'-f', '--file',
default=os.sep.join(['~', '.ec2utils.conf']),
dest='config_file_path',
help='Path to configuration file, default ~/.ec2utils.conf (Optional)',
metavar='CONFIG_FILE'
)
publish_image_condition_group = argparse.add_mutually_exclusive_group()
publish_image_condition_group.add_argument(
'--image-id',
dest='image_id',
help='The AMI ID of the image to be published (Optional)',
metavar='AMI_ID'
)
publish_image_condition_group.add_argument(
'--image-name',
dest='image_name',
help='The image name of the image to be published (Optional)',
metavar='IMAGE_NAME'
)
help_msg = 'An image name fragment to match the image name of the image to be '
help_msg += 'published (Optional)'
publish_image_condition_group.add_argument(
'--image-name-frag',
dest='image_name_frag',
help=help_msg,
metavar='IMAGE_NAME_FRAGMENT'
)
help_msg = 'A regular expression to match the image name of the image to be '
help_msg += 'published (Optional)'
publish_image_condition_group.add_argument(
'--image-name-match',
dest='image_name_match',
help=help_msg,
metavar='REGEX'
)
help_msg = 'Comma separated list of regions for publishing, all integrated '
help_msg += 'regions if not given (Optional)'
argparse.add_argument(
'-r', '--regions',
dest='regions',
help=help_msg,
metavar='EC2_REGIONS'
)
argparse.add_argument(
'-s', '--secret-key',
dest='secret_key',
help='AWS secret access key (Optional)',
metavar='AWS_SECRET_KEY'
)
help_msg = 'Set vebosety level 0 (default) image name, 1 image name and ID, '
help_msg += '2 full description'
argparse.add_argument(
'--verbose',
type=int,
default=0,
dest='verbose',
)
argparse.add_argument(
'--version',
action='version',
version=utils.get_version(),
help='Program version'
)
args = argparse.parse_args()
logger = utils.get_logger(args.verbose)
config_file = os.path.expanduser(args.config_file_path)
config = None
if not os.path.isfile(config_file):
print('Configuration file "%s" not found.' % config_file)
sys.exit(1)
try:
config = utils.get_config(config_file)
except Exception as e:
print(format(e), file=sys.stderr)
sys.exit(1)
access_key = args.access_key
if not access_key:
try:
access_key = utils.get_from_config(args.account_name,
config,
None,
'access_key_id',
'--access-id')
except EC2AccountException as e:
print(format(e), file=sys.stderr)
sys.exit(1)
if not access_key:
print('Could not determine account access key', file=sys.stderr)
sys.exit(1)
secret_key = args.secret_key
if not secret_key:
try:
secret_key = utils.get_from_config(args.account_name,
config,
None,
'secret_access_key',
'--secret-key')
except EC2AccountException as e:
print(format(e), file=sys.stderr)
sys.exit(1)
if not secret_key:
print('Could not determine account secret access key', file=sys.stderr)
sys.exit(1)
if not args.regions:
regions = utils.get_regions(args, access_key, secret_key)
else:
regions = args.regions.split(',')
lister = ec2lsimg.EC2ListImage(
access_key=access_key,
image_id=args.image_id,
image_name=args.image_name,
image_name_fragment=args.image_name_frag,
image_name_match=args.image_name_match,
secret_key=secret_key,
log_callback=logger,
verbose=args.verbose
)
if len(regions) > 1 or args.verbose:
lister.set_indent(4)
try:
for region in regions:
if len(regions) > 1 and not args.verbose:
print('Using EC2 region: %s' % region)
lister.set_region(region)
lister.output_image_list()
except EC2ListImgException as e:
print(format(e), file=sys.stderr)
sys.exit(1)
except Exception as e:
print(format(e), file=sys.stderr)
sys.exit(1)