bucket_list 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/bucket_list CHANGED
@@ -13,14 +13,17 @@ require 'optparse'
13
13
  options = {}
14
14
 
15
15
  option_parser = OptionParser.new do |opts|
16
-
17
- # Allow app to take a comma-separated list of hashes
18
- # E.g. bucket:file,bucket:file
19
16
  opts.accept(Hash) do |string|
20
17
  hash = {}
21
18
  string.split(',').each do |pair|
22
19
  key,value = pair.split(/:/)
23
- hash[key] = value
20
+ if hash.key?(key)
21
+ orig_value = hash[key]
22
+ new_value = [hash[key], value]
23
+ hash[key] = new_value
24
+ else
25
+ hash[key] = value
26
+ end
24
27
  end
25
28
  hash
26
29
  end
@@ -33,7 +36,7 @@ option_parser = OptionParser.new do |opts|
33
36
  options[:delete_bucket] = bucket_name.to_s
34
37
  end
35
38
 
36
- opts.on('-u', '--upload BUCKET_NAME:FILE',Hash, 'Upload FILE to BUCKET_NAME') do |hash|
39
+ opts.on('-u', '--upload BUCKET_NAME:FILE', Hash, 'Upload FILE to BUCKET_NAME') do |hash|
37
40
  options[:upload] = hash
38
41
  end
39
42
 
@@ -56,7 +59,9 @@ end
56
59
 
57
60
  option_parser.parse!
58
61
 
59
- bucket_list = BucketList.new
62
+ unless options[:help]
63
+ bucket_list = BucketList.new
64
+ end
60
65
 
61
66
  if options[:create_bucket]
62
67
  puts "[INFO] About to create a new S3 bucket named '#{options[:create_bucket]}'"
@@ -90,8 +95,15 @@ end
90
95
 
91
96
  if options[:upload]
92
97
  options[:upload].each_pair do |bucket_name, file|
93
- puts "[INFO] Uploading '#{file}' to the '#{bucket_name}' bucket"
94
- bucket_list.upload_file_to_bucket(bucket_name, file)
98
+ if file.class.eql?(Array)
99
+ file.each do |f|
100
+ puts "[INFO] Uploading '#{f}' to the '#{bucket_name}' bucket"
101
+ bucket_list.upload_file_to_bucket(bucket_name, f)
102
+ end
103
+ else
104
+ puts "[INFO] Uploading '#{file}' to the '#{bucket_name}' bucket"
105
+ bucket_list.upload_file_to_bucket(bucket_name, file)
106
+ end
95
107
  end
96
108
  end
97
109
 
data/config/config.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  # bucket_list config file
2
2
 
3
- version: 0.1.0
3
+ version: 0.2.0
@@ -21,7 +21,20 @@ class BucketList
21
21
  end
22
22
  end
23
23
 
24
- def bucket_exists?(bucket_name)
24
+ def bucket_exists?(bucket_name, bucket_should_exist=true)
25
+ timeout = 10
26
+ polling_interval = 0.5
27
+ time_limit = Time.now + timeout
28
+
29
+ while Time.now < time_limit
30
+ if bucket_should_exist
31
+ break if @s3.buckets[bucket_name].exists?
32
+ else
33
+ break if !@s3.buckets[bucket_name].exists?
34
+ end
35
+ sleep polling_interval
36
+ end
37
+
25
38
  @s3.buckets[bucket_name].exists?
26
39
  end
27
40
 
@@ -36,9 +49,8 @@ class BucketList
36
49
  end
37
50
 
38
51
  def create_bucket(bucket_name)
39
- if !bucket_exists?(bucket_name)
52
+ if !bucket_exists?(bucket_name, false)
40
53
  @s3.buckets.create(bucket_name)
41
- wait_for_bucket(bucket_name, true)
42
54
  puts "[INFO] Bucket created"
43
55
  else
44
56
  puts "[ERROR] Sorry, a bucket named '#{bucket_name}' already exists"
@@ -52,13 +64,26 @@ class BucketList
52
64
  end
53
65
 
54
66
  def delete_bucket(bucket_name)
55
- if bucket_exists?(bucket_name)
56
- bucket = s3.buckets[bucket_name]
57
- bucket.delete!
58
- wait_for_bucket(bucket_name, false)
59
- puts "[INFO] The bucket '#{bucket_name}' has been deleted"
60
- else
61
- puts "[ERROR] Sorry, a bucket named '#{bucket_name}' does not exist"
67
+ bucket = s3.buckets[bucket_name]
68
+ bucket_deleted = false
69
+ timeout = false
70
+ attempts = 0
71
+ max_attempts = 10
72
+ until bucket_deleted || (attempts >= max_attempts)
73
+ begin
74
+ bucket.delete!
75
+ bucket_deleted = true
76
+ puts "[INFO] The bucket '#{bucket_name}' has been deleted"
77
+ rescue AWS::S3::Errors::NoSuchBucket => error
78
+ puts "[ERROR] #{error}"
79
+ puts "[INFO] Trying again... (#{attempts.to_i + 1}/#{max_attempts} remaining)"
80
+ attempts = attempts + 1
81
+ sleep 1
82
+ next
83
+ rescue AWS::S3::Errors::AccessDenied => error
84
+ puts "[ERROR] #{error}"
85
+ exit 1
86
+ end
62
87
  end
63
88
  end
64
89
 
@@ -120,15 +145,4 @@ class BucketList
120
145
  end
121
146
  end
122
147
 
123
- def wait_for_bucket(bucket_name, exist_bool)
124
- if exist_bool
125
- until bucket_exists?(bucket_name) do
126
- sleep 1
127
- end
128
- else
129
- until !bucket_exists?(bucket_name) do
130
- sleep 1
131
- end
132
- end
133
- end
134
148
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bucket_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-27 00:00:00.000000000 Z
12
+ date: 2012-09-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
@@ -97,4 +97,3 @@ signing_key:
97
97
  specification_version: 3
98
98
  summary: Command line app for talking to AWS S3
99
99
  test_files: []
100
- has_rdoc: