aws_tools 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -1,6 +1,8 @@
1
1
  MIT-LICENSE
2
- Manifest
3
2
  README.rdoc
4
3
  Rakefile
4
+ aws_tools.gemspec
5
5
  lib/aws_tools.rb
6
+ lib/aws_tools/ec2.rb
6
7
  lib/aws_tools/s3.rb
8
+ Manifest
@@ -8,30 +8,12 @@ To install, run
8
8
 
9
9
  $ sudo gem install aws_tools
10
10
 
11
- = Examples
12
-
13
- require 'rubygems'
14
- require 'right_aws'
15
- require 'aws_tools'
16
-
17
- # Connect to S3 and synchronize all buckets.
18
- AwsTools::S3::Synchronize.new(RightAws::S3.new(access_key,
19
- secret_access_key,
20
- :backup_suffix => "-dr",
21
- :backup_location => :eu)
22
-
23
- View the documentation for more information.
24
-
25
11
  = Documentation
26
12
 
27
13
  You can generate documentation if you have the source by typing:
28
14
 
29
15
  rake doc
30
16
 
31
- = Release Notes
32
-
33
- First release.
34
-
35
17
  = Credits
36
18
 
37
19
  AwsTools is maintained by {Christopher Meiklejohn}[mailto:cmeik@me.com]. Contact me if you'd like to request any features or have any questions or feedback.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('aws_tools', '0.0.1') do |p|
5
+ Echoe.new('aws_tools', '0.0.2') do |p|
6
6
  p.description = "A library to help simplify common AWS tasks."
7
7
  p.url = "http://github.com/cmeiklejohn/aws_tools"
8
8
  p.author = "Christopher Meiklejohn"
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{aws_tools}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Christopher Meiklejohn"]
9
- s.date = %q{2010-02-25}
9
+ s.date = %q{2010-02-27}
10
10
  s.description = %q{A library to help simplify common AWS tasks.}
11
11
  s.email = %q{cmeik@me.com}
12
- s.extra_rdoc_files = ["README.rdoc", "lib/aws_tools.rb", "lib/aws_tools/s3.rb"]
13
- s.files = ["MIT-LICENSE", "Manifest", "README.rdoc", "Rakefile", "lib/aws_tools.rb", "lib/aws_tools/s3.rb", "aws_tools.gemspec"]
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/aws_tools.rb", "lib/aws_tools/ec2.rb", "lib/aws_tools/s3.rb"]
13
+ s.files = ["MIT-LICENSE", "README.rdoc", "Rakefile", "aws_tools.gemspec", "lib/aws_tools.rb", "lib/aws_tools/ec2.rb", "lib/aws_tools/s3.rb", "Manifest"]
14
14
  s.homepage = %q{http://github.com/cmeiklejohn/aws_tools}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Aws_tools", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
@@ -8,26 +8,18 @@
8
8
  # Copyright:: Copyright (c) 2010 Christopher Meiklejohn
9
9
  # License:: Distributes under the terms specified in the MIT-LICENSE file.
10
10
  #
11
- # Usage example:
11
+ # Documentation coming soon.
12
12
  #
13
- # # Connect to S3 and synchronize all buckets.
14
- # AwsTools::S3::Synchronize.new(RightAws::S3.new(access_key,
15
- # secret_access_key,
16
- # :backup_suffix => "-dr",
17
- # :backup_location => :eu)
18
- #
19
- #
20
- require 'rubygems'
21
- require 'right_aws'
22
-
23
13
  $:.unshift(File.dirname(__FILE__))
14
+
24
15
  require 'aws_tools/s3'
16
+ require 'aws_tools/ec2'
25
17
 
26
18
  module AwsTools #:nodoc:
27
19
  module VERSION #:nodoc:
28
20
  MAJOR = 0
29
21
  MINOR = 0
30
- TINY = 1
22
+ TINY = 2
31
23
 
32
24
  STRING = [MAJOR, MINOR, TINY].join('.')
33
25
  end
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # AwsTools::Ec2
4
+ #
5
+ # Author:: Christopher Meiklejohn (cmeik@me.com)
6
+ # Copyright:: Copyright (c) 2010 Christopher Meiklejohn
7
+ # License:: Distributes under the terms specified in the MIT-LICENSE file.
8
+ #
9
+ # Documentation coming soon.
10
+ #
11
+ module AwsTools
12
+ class Ec2
13
+ class Helpers
14
+
15
+ # SynchronizeS3Buckets
16
+ #
17
+ # Documentation coming soon.
18
+ class SynchronizeS3Buckets
19
+
20
+ # Accessors for everything we might need to specify.
21
+ # Refactor this eventually back into AwsTools::Ec2 or
22
+ # AwsTools::Ec2::Helpers
23
+ attr_accessor :min_count,
24
+ :max_count,
25
+ :security_group,
26
+ :key_name,
27
+ :addressing_type,
28
+ :instance_type,
29
+ :kernel_id,
30
+ :ramdisk_id,
31
+ :availability_zone,
32
+ :monitoring_enabled,
33
+ :subnet_id,
34
+ :disable_api_termination,
35
+ :instance_initiated_shutdown_behavior,
36
+ :block_device_mappings
37
+
38
+ # Initalizer.
39
+ #
40
+ # Required parameters:
41
+ #
42
+ # ec2: authenticated rightaws ec2 instance to launch the
43
+ # instance from
44
+ # ami: ami to use to perform the backup
45
+ #
46
+ # access_key: access key to use for s3 inside of instance
47
+ #
48
+ # secret_access_key: secret access key to use for s3 inside of
49
+ # instance
50
+ #
51
+ def initialize(ec2,
52
+ ami,
53
+ access_key,
54
+ secret_access_key,
55
+ options = {})
56
+ puts "AwsTools::Ec2::Helpers::SynchronizeS3Buckets " +
57
+ "initialize"
58
+
59
+ @ec2 = ec2
60
+ @ami = ami
61
+ @access_key = access_key
62
+ @secret_access_key = secret_access_key
63
+
64
+ options.each_pair do |key, value|
65
+ self.send("#{key}=", value)
66
+ end
67
+
68
+ setup_defaults
69
+ launch_instance
70
+ end
71
+
72
+ private
73
+
74
+ # Setup defaults for EC2 instance
75
+ #
76
+ # Refactor this eventually back into AwsTools::Ec2 or
77
+ # AwsTools::Ec2::Helpers right_was run_instances requires these
78
+ # as arguments, but launch_instances takes a option hash.
79
+ # Investigate migrating to launch_instances, which
80
+ # seems the less recommended way for launching instances
81
+ #
82
+ def setup_defaults
83
+ @min_count = 1 if @min_count.blank?
84
+ @max_count = 1 if @max_count.blank?
85
+ @security_group = 'default' if @security_group.blank?
86
+ @key_name = 'default' if @key_name.blank?
87
+ @addressing_type = 'public' if @addressing_type.blank?
88
+ @instance_type = 'm1.small' if @instance_type.blank?
89
+ # @kernel_id = nil if @kernel_id.blank?
90
+ # @ramdisk_id = nil if @ramdisk_id.blank?
91
+ # @availability_zone = nil if @availability_zone.blank?
92
+ # @monitoring_enabled = nil if @monitoring_enabled.blank?
93
+ # @subnet_id = nil if @subnet_id.blank?
94
+ # @disable_api_termination = nil
95
+ # if @disable_api_termination.blank?
96
+ # @instance_initiated_shutdown_behavior = nil
97
+ # if @instance_initiated_shutdown_behavior.blank?
98
+ # @block_device_mappings = nil if @block_device_mappings.blank?
99
+ end
100
+
101
+ # Define script data to execute
102
+ # Refactor this to use remote workling at some point
103
+ def user_data
104
+ "#!/bin/sh
105
+
106
+ /usr/bin/ruby <<EOF
107
+
108
+ require 'rubygems'
109
+ require 'right_aws'
110
+ require 'aws_tools'
111
+
112
+ AwsTools::S3::Synchronize.new(
113
+ RightAws::S3.new('#{@access_key}',
114
+ '#{@secret_access_key}'),
115
+ :backup_suffix => '-backup',
116
+ :backup_location => :'eu')
117
+
118
+ EOF
119
+
120
+ shutdown now -h
121
+ "
122
+ end
123
+
124
+ # Launch ec2 instance
125
+ def launch_instance
126
+ puts "AwsTools::Ec2::Helpers::SynchronizeS3Buckets " +
127
+ "launch_instance"
128
+
129
+ # Run the instance via right_aws
130
+ @ec2.run_instances(@ami,
131
+ @min_count,
132
+ @max_count,
133
+ @security_group,
134
+ @key_name,
135
+ user_data,
136
+ @addressing_type,
137
+ @instance_type,
138
+ @kernel_id,
139
+ @ramdisk_id,
140
+ @availability_zone,
141
+ @monitoring_enabled,
142
+ @subnet_id,
143
+ @disable_api_termination,
144
+ @instance_initiated_shutdown_behavior,
145
+ @block_device_mappings)
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -1,23 +1,48 @@
1
1
  #!/usr/bin/ruby
2
-
3
- require 'rubygems'
4
- require 'right_aws'
2
+ #
3
+ # AwsTools::S3
4
+ #
5
+ # Author:: Christopher Meiklejohn (cmeik@me.com)
6
+ # Copyright:: Copyright (c) 2010 Christopher Meiklejohn
7
+ # License:: Distributes under the terms specified in the MIT-LICENSE file.
8
+ #
9
+ # Documentation coming soon.
10
+ #
5
11
 
6
12
  module AwsTools
13
+ # S3
14
+ #
15
+ # Provides an interface to common S3 administration utilties.
7
16
  class S3
17
+
8
18
  # Bucket class.
9
19
  #
10
20
  # Knows how to perform synchronization of all assets
11
- # in one bucket.
21
+ # in one bucket. Synchronization is one way, source to destination
22
+ # only.
12
23
  class Bucket
24
+
13
25
  # Synchronize.
14
26
  #
15
27
  # Performs the synchronization of one bucket.
28
+ #
29
+ # One way synchronization, source to destination based on
30
+ # modification time.
31
+ #
16
32
  class Synchronize
17
- attr_accessor :bucket, :backup_bucket
18
33
 
19
- def initialize(options = {})
20
- puts "AwsTools::S3::Bucket::Synchronize called."
34
+ # Initializer.
35
+ #
36
+ # Required parameters:
37
+ #
38
+ # bucket: source bucket
39
+ # backup_bucket: destination bucket
40
+ #
41
+ def initialize(bucket, backup_bucket, options = {})
42
+ puts "AwsTools::S3::Bucket::Synchronize initialize"
43
+
44
+ @bucket = bucket
45
+ @backup_bucket = backup_bucket
21
46
 
22
47
  options.each_pair do |key, value|
23
48
  self.send("#{key}=", value)
@@ -30,44 +55,53 @@ module AwsTools
30
55
 
31
56
  # Peform synchronization of one bucket.
32
57
  def perform_synchronization
33
- puts "AwsTools::S3::Bucket::Synchronize synchronizing #{bucket.name} to " +
34
- "#{backup_bucket.name}"
58
+ puts "AwsTools::S3::Bucket::Synchronize " +
59
+ "perform_synchronization #{@bucket.name} " +
60
+ "#{@backup_bucket.name}"
35
61
 
36
- bucket.keys.each do |source_key|
62
+ # Iterate through the bucket.
63
+ @bucket.keys.each do |source_key|
37
64
 
38
65
  # Look for destination key.
39
- destination_key = RightAws::S3::Key.create(backup_bucket, source_key.to_s)
66
+ destination_key = RightAws::S3::Key.create(@backup_bucket,
67
+ source_key.to_s)
40
68
 
41
69
  # If it exists...
42
70
  if destination_key.exists?
43
71
 
44
- puts "AwsTools::S3::Bucket::Synchronize destination file #{source_key.to_s} " +
45
- "already exists"
72
+ puts "AwsTools::S3::Bucket::Synchronize " +
73
+ "perform_synchronization " +
74
+ "#{source_key.to_s} exists!"
46
75
 
47
76
  # Grab headers.
48
77
  source_key.head
49
78
  destination_key.head
50
79
 
51
80
  # Get last modified
52
- source_date = Time.parse(source_key.headers["last-modified"])
53
- destination_date = Time.parse(destination_key.headers["last-modified"])
81
+ source_date = Time.parse(
82
+ source_key.headers["last-modified"])
83
+ destination_date = Time.parse(
84
+ destination_key.headers["last-modified"])
54
85
 
55
86
  # Copy if the destination is older than the source
56
87
  if source_date > destination_date
57
- puts "AwsTools::S3::Bucket::Synchronize destination file #{source_key.to_s} " +
58
- "is older"
88
+ puts "AwsTools::S3::Bucket::Synchronize " +
89
+ "perform_synchronization " +
90
+ "#{source_key.to_s} older!"
59
91
 
60
92
  # Perform a copy of the object.
61
93
  source_key.copy(destination_key)
62
94
 
63
95
  # Check if it worked...
64
96
  if destination_key.exists?
65
- puts "AwsTools::S3::Bucket::Synchronize destination file #{source_key.to_s} " +
66
- "replaced with newer copy"
97
+ puts "AwsTools::S3::Bucket::Synchronize " +
98
+ "perform_synchronization " +
99
+ "#{source_key.to_s} replaced!"
67
100
  end
68
101
  else
69
- puts "AwsTools::S3::Bucket::Synchronize destination file #{source_key.to_s} " +
70
- "is up to date"
102
+ puts "AwsTools::S3::Bucket::Synchronize " +
103
+ "perform_synchronization " +
104
+ "#{source_key.to_s} current!"
71
105
  end
72
106
 
73
107
  else
@@ -77,8 +111,9 @@ module AwsTools
77
111
 
78
112
  # Check if it worked...
79
113
  if destination_key.exists?
80
- puts "AwsTools::S3::Bucket::Synchronize destination file #{source_key.to_s} " +
81
- "successfully created"
114
+ puts "AwsTools::S3::Bucket::Synchronize " +
115
+ "perform_synchronization " +
116
+ "#{source_key.to_s} created!"
82
117
  end
83
118
 
84
119
  end
@@ -93,13 +128,17 @@ module AwsTools
93
128
  #
94
129
  # Performs synchronization of every bucket in an S3 account.
95
130
  #
96
- # Assume that you will be synchronizing to a bucket with a suffix located
97
- # in another zone (since names need to be unique across all zones.
131
+ # Assume that you will be synchronizing to a bucket with a suffix
132
+ # located in another zone
133
+ # (since names need to be unique across all zones).
134
+ #
98
135
  class Synchronize
99
- attr_accessor :backup_suffix, :backup_location
136
+ attr_accessor :backup_perms,
137
+ :backup_suffix,
138
+ :backup_location
100
139
 
101
140
  def initialize(s3, options = {})
102
- puts "AwsTools::S3::Synchronize called."
141
+ puts "AwsTools::S3::Synchronize initialize"
103
142
 
104
143
  @s3 = s3
105
144
 
@@ -107,34 +146,47 @@ module AwsTools
107
146
  self.send("#{key}=", value)
108
147
  end
109
148
 
149
+ setup_defaults
110
150
  perform_synchronization
111
151
  end
112
152
 
113
- private
153
+ private
154
+
155
+ # Setup defaults for S3 backups
156
+ #
157
+ def setup_defaults
158
+ @backup_perms = "public-read" if @backup_perms.blank?
159
+ @backup_suffix = "-backup" if @backup_suffix.blank?
160
+ @backup_location = "eu" if @backup_location.blank?
161
+ end
114
162
 
115
163
  # Retrieve all buckets, and synchronize them one by one.
116
164
  def perform_synchronization
117
165
  # Retrieve bucket list.
118
166
  @s3.buckets.each do |bucket|
119
- puts "AwsTools::S3::Synchronize assesing bucket #{bucket.name}"
167
+ puts "AwsTools::S3::Synchronize " +
168
+ "perform_synchronization #{bucket.name} (#{bucket.location})"
120
169
 
121
170
  # If the bucket is located in the normal zone, assume this
122
171
  # is a bucket we want to create a backup of.
123
172
  if bucket.location.empty?
124
173
 
125
174
  # Backup bucket.
126
- backup_bucket_name = "#{bucket.name}#{backup_suffix}"
175
+ backup_bucket_name = "#{bucket.name}#{@backup_suffix}"
127
176
 
128
- puts "AwsTools::S3::Synchronize #{bucket.name} will be synchronized " +
129
- "to #{backup_bucket_name} in #{backup_location}"
177
+ puts "AwsTools::S3::Synchronize " +
178
+ "perform_synchronization #{bucket.name} " +
179
+ "(#{bucket.location}) will be " +
180
+ "synchronized to #{backup_bucket_name} " +
181
+ "(#{@backup_location})"
130
182
 
131
183
  backup_bucket = @s3.bucket("#{backup_bucket_name}",
132
184
  true,
133
- 'public-read',
134
- :location => backup_location)
185
+ @backup_perms,
186
+ :location => @backup_location)
135
187
 
136
- AwsTools::S3::Bucket::Synchronize.new(:bucket => bucket,
137
- :backup_bucket => backup_bucket)
188
+ AwsTools::S3::Bucket::Synchronize.new(bucket,
189
+ backup_bucket)
138
190
  end
139
191
  end
140
192
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Meiklejohn
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-25 00:00:00 -05:00
12
+ date: 2010-02-27 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,15 +22,17 @@ extensions: []
22
22
  extra_rdoc_files:
23
23
  - README.rdoc
24
24
  - lib/aws_tools.rb
25
+ - lib/aws_tools/ec2.rb
25
26
  - lib/aws_tools/s3.rb
26
27
  files:
27
28
  - MIT-LICENSE
28
- - Manifest
29
29
  - README.rdoc
30
30
  - Rakefile
31
+ - aws_tools.gemspec
31
32
  - lib/aws_tools.rb
33
+ - lib/aws_tools/ec2.rb
32
34
  - lib/aws_tools/s3.rb
33
- - aws_tools.gemspec
35
+ - Manifest
34
36
  has_rdoc: true
35
37
  homepage: http://github.com/cmeiklejohn/aws_tools
36
38
  licenses: []