forked from joemiller/aws-cf-private-streaming-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf-download-distribution.rb
executable file
·281 lines (245 loc) · 7.71 KB
/
cf-download-distribution.rb
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/ruby
#
# == Synopsis
#
# cf-download-distribution: Manipulate Amazon Cloudfront Download Distributions
#
# == Usage
#
# cf-download-distribution.rb [OPTIONS] [command] [args]
#
# == Commands
# list
# List all Download Distributions
#
# get [aws_id]
# Get details about the Download Distribution identified by [aws_id].
#
# create [bucket]
# Create new Download Distribution using S3 origin bucket [bucket]. CNAMEs
# can optionally be specified with multiple --cname options, and a comment can
# be applied with --comment option
#
# delete [aws_id] [e_tag]
# Delete the Download Distribution identified by [aws_id] and [e_tag]. A
# distribution must first be disabled before it can be deleted. Use 'get'
# to retrieve a distribution's e_tag.
#
# modify [aws_id]
# Modify attributes on the Download Distribution identified by [aws_id]. Must
# be used in conjunction with at least one of the following options:
# --comment, --enabled, --oai, --trusted-signer, --cname
#
# wait [aws_id]
# Loop until a Download Distribution specified by [aws_id] enters the 'deployed'
# state. You could use this in scripts if you need to know when a
# distribution becomes available for use.
#
# == OPTIONS
# -h, --help:
# show help
#
# -c, --cname [cname]:
# Use this CNAME on the bucket (can be used with 'create' and 'modify' commands).
# Multiple --cname options can be used. When used with modify command, will
# overwrite all existing CNAMEs
#
# -o, --oai [origin-access-identity]:
# Use with 'modify' command to set the Origin Access Identity on a Download
# Distribution.
#
# -e, --enable:
# Use with 'modify' command to enable a Download Distribution.
#
# -d, --disable:
# Use with 'modify' command to disable a Download Distribution.
#
# -t, --trusted-signer [aws_account_id | self]:
# Use with 'modify' command to set trusted signers on a Download Distribution.
# Can be used multiple times to set multiple signers. This will overwrite all
# existing trusted signers. Use 'self' for [aws_account_id] to refer to the
# parent account of the Download Distribution.
#
# -m, --comment ['some descriptive test']:
# Set 'comment' on the distribution
#
# -k, --key [AWS_ACCESS_KEY_ID]
# Amazon AWS ACCESS KEY ID (can also be set in environment variable 'AWS_ACCESS_KEY_ID')
#
# -s, --seckey [AWS_SECRET_ACCESS_KEY]
# Amazon AWS SECRET ACCESS KEY (can also be set in environment variable 'AWS_SECRET_ACCESS_KEY')
# joe miller, <[email protected]>, 10/30/2010
require 'rubygems'
require 'right_aws'
require 'getoptlong'
require 'rdoc/usage'
require 'ap'
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--cname', '-c', GetoptLong::REQUIRED_ARGUMENT ],
[ '--oai', '-o', GetoptLong::REQUIRED_ARGUMENT ],
[ '--enable', '-e', GetoptLong::NO_ARGUMENT ],
[ '--disable', '-d', GetoptLong::NO_ARGUMENT ],
[ '--trusted-signer', '-t', GetoptLong::REQUIRED_ARGUMENT ],
[ '--comment', '-m', GetoptLong::REQUIRED_ARGUMENT ],
[ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT ],
[ '--seckey', '-s', GetoptLong::REQUIRED_ARGUMENT ]
)
key = ENV['AWS_ACCESS_KEY_ID']
seckey = ENV['AWS_SECRET_ACCESS_KEY']
if !key || !seckey
puts "Please set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables, or use the -k/-s parameters."
exit 1
end
cnames = []
signers = []
oai = nil
enabled = nil
comment = nil
opts.each do |opt, arg|
case opt
when '--help'
RDoc::usage
when '--cname'
cnames.push arg
when '--comment'
comment = arg
when '--key'
key = arg
when '--seckey'
seckey = arg
when '--oai'
oai = arg
when '--trusted-signer'
signers.push arg
when '--enable'
enabled = true
when '--disable'
enabled = false
end
end
command = ARGV.shift
args = ARGV
log = Logger.new(STDERR)
log.level = Logger::FATAL
### connect to amazon cloudfront
cf = RightAws::AcfInterface.new(key, seckey,
{:logger => log} )
if command == 'list'
dists = cf.list_distributions
dists.each do |dist|
ap dist, :indent => -2
end
elsif command == 'get'
if args.length < 1
puts "'get' requires 1 arg (try --help)"
exit 1
end
aws_id = args.shift
begin
result = cf.get_distribution(aws_id)
ap result, :indent => -2
rescue RightAws::AwsError => e
e.errors.each do |code, msg|
puts "Error (#{code}): #{msg}"
end
exit 1
end
elsif command == 'delete'
if args.length < 2
puts "'delete' requires 2 args (try --help)"
exit 1
end
aws_id = args.shift
etag = args.shift
begin
result = cf.delete_distribution(aws_id, etag)
rescue RightAws::AwsError => e
e.errors.each do |code, msg|
puts "Error (#{code}): #{msg}"
end
exit 1
end
if result == true
puts "Delete successful"
else
puts "Delete failed"
end
elsif command == 'create'
if args.length < 1
puts "'create' requires 1 arg (try --help)"
exit 1
end
## the CF api expects a canonical bucket name for the origin bucket,
## eg "mybucket.s3.amazonaws.com".
bucket = args.shift
unless bucket =~ /s3\.amazonaws\.com$/
bucket = bucket + '.s3.amazonaws.com'
end
begin
config = Hash.new
config[:s3_origin] ||= Hash.new
config[:comment] = comment if comment
config[:trusted_signers] = signers if signers.length > 0
config[:cnames] = cnames if cnames.length > 0
config[:enabled] = enabled if enabled != nil
config[:s3_origin][:dns_name] = bucket if bucket
config[:s3_origin][:origin_access_identity] = "origin-access-identity/cloudfront/#{oai}" if oai
result = cf.create_distribution(config)
rescue RightAws::AwsError => e
e.errors.each do |code, msg|
puts "Error (#{code}): #{msg}"
end
exit 1
end
## success
puts
puts
puts
puts
puts "Success!"
puts "domain_name: #{result[:domain_name]}"
puts "aws_id: #{result[:aws_id]}"
exit 0
elsif command == 'modify'
if args.length < 1
puts "'create' requires 1 arg (try --help)"
exit 1
end
aws_id = args.shift
begin
config = cf.get_distribution_config(aws_id)
config[:s3_origin] ||= Hash.new
config[:comment] = comment if comment
config[:trusted_signers] = signers if signers.length > 0
config[:cnames] = cnames if cnames.length > 0
config[:enabled] = enabled if enabled != nil
# config[:s3_origin][:dns_name] = bucket if bucket # Can't change bucket name after create?
config[:s3_origin][:origin_access_identity] = "origin-access-identity/cloudfront/#{oai}" if oai
result = cf.set_distribution_config(aws_id, config)
rescue RightAws::AwsError => e
e.errors.each do |code, msg|
puts "Error (#{code}): #{msg}"
end
exit 1
end
if result == true
puts "Success!"
else
puts "Unknown error occurred"
end
elsif command == 'wait'
if args.length < 1
puts "'wait' requires 1 arg (try --help)"
exit 1
end
aws_id = args.shift
until cf.get_distribution(aws_id)[:status] == 'Deployed'
puts "Waiting for download distribution #{aws_id} to become 'Deployed' .."
sleep 5
end
else
puts "no command given (try --help)"
exit 1
end
exit 0