resize-aws-instance 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01c74c80dde4104d8b0477a7e4d935c4053b0569
4
- data.tar.gz: 5623c4e5b45a2b10c0ef34683deac7387c31ef72
3
+ metadata.gz: a40b6b7be4e2071da0c6d91360723aa63832abc4
4
+ data.tar.gz: 5a03414dfee4015f7502188c6700b66928a46526
5
5
  SHA512:
6
- metadata.gz: 7b8894c543974775de8e3e5cf45b8de43ce8f78777fec9204d6d3c864a6c80360a2ec734f60be641d3e86609c6838b491e569a47a79aca937396a7b1fd78703a
7
- data.tar.gz: 78afc917e6ff461343b9e2b27aecbc1608928701c3efff9d6b1904df13fadb5bd6346e6f60abef5e403c0f0f4f65a128146aaada3740c99c771d9bed2f5ae8b1
6
+ metadata.gz: 4ed65501c48c2c6d4d4e6e1444c12cfbfc6bd684ec45973be1ae209e1201a0dce73e141adbda8a93556e38bdbb43bc58b17d89b821ca5ffb69e87c9d4bd4100f
7
+ data.tar.gz: 775ac82e5e32002a27ceb0477844a5bd1ce96bc25f4685eaf29340f3c9d6b08a790f3c5108566020e37d9ef4c5084874e10edaa4136c1c1d26405ce3abac659b
data/README.md CHANGED
@@ -18,15 +18,25 @@ come with a shared library. To install, simply run the following command:
18
18
 
19
19
  Simple script to resize EBS based AWS instance.
20
20
 
21
- Usage:
22
- resize_aws_instance [options]
23
- --instance-id, -i <s>: Instance ID of AWS instance
24
- --key-id, -k <s>: AWS access key ID
25
- --secret-key, -s <s>: AWS secret access key
26
- --region, -r <s>: AWS region
27
- --type, -t <s>: Target instance type
28
- --version, -v: Print version and exit
29
- --help, -h: Show this message
21
+ Usage:
22
+ resize_aws_instance [options]
23
+
24
+ Note:
25
+ The following AWS config options may be provided via environment variables:
26
+ key-id => AWS_ACCESS_KEY_ID
27
+ secret-key => AWS_SECRET_ACCESS_KEY
28
+ region => AWS_DEFAULT_REGION
29
+
30
+ Options:
31
+ --instance-id, -i <s>: AWS instance ID
32
+ --key-id, -k <s>: AWS access key ID
33
+ --secret-key, -s <s>: AWS secret access key
34
+ --region, -r <s>: AWS region
35
+ --type, -t <s>: Target instance type
36
+ --snapshot, -n <s>: Snapshot EBS volumes [none, root or comma separated
37
+ list IDs (a,b,c)] (default: none)
38
+ --version, -v: Print version and exit
39
+ --help, -h: Show this message
30
40
 
31
41
  ## Contributing
32
42
 
@@ -6,44 +6,65 @@ require 'trollop'
6
6
  opts = Trollop.options do
7
7
  version ResizeAwsInstance::VERSION
8
8
  banner <<-EOS
9
- Simple script to resize EBS based AWS instance.
9
+ Simple script to resize EBS based AWS instance.
10
10
 
11
- Usage:
12
- resize_aws_instance [options]
11
+ Usage:
12
+ resize_aws_instance [options]
13
+
14
+ Note:
15
+ The following AWS config options may be provided via environment variables:
16
+ key-id => AWS_ACCESS_KEY_ID
17
+ secret-key => AWS_SECRET_ACCESS_KEY
18
+ region => AWS_DEFAULT_REGION
19
+
20
+ Options:
13
21
  EOS
14
22
 
15
23
  opt :instance_id,
16
- 'Instance ID of AWS instance',
24
+ 'AWS instance ID',
17
25
  type: :string,
18
26
  required: true
19
27
  opt :key_id,
20
28
  'AWS access key ID',
21
- default: ENV['AWS_ACCESS_KEY_ID'],
22
29
  type: :string
23
30
  opt :secret_key,
24
31
  'AWS secret access key',
25
- default: ENV['AWS_SECRET_ACCESS_KEY'],
26
32
  type: :string
27
33
  opt :region,
28
34
  'AWS region',
29
- default: ENV['AWS_DEFAULT_REGION'],
30
35
  type: :string
31
36
  opt :type,
32
37
  'Target instance type',
33
38
  type: :string,
34
39
  required: true
40
+ opt :snapshot,
41
+ 'Snapshot EBS volumes [none, root or comma separated list IDs (a,b,c)]',
42
+ type: :string,
43
+ default: 'none'
44
+ end
45
+
46
+ opts[:key_id] ||= ENV['AWS_ACCESS_KEY_ID']
47
+ opts[:secret_key] ||= ENV['AWS_SECRET_ACCESS_KEY']
48
+ opts[:region] ||= ENV['AWS_DEFAULT_REGION']
49
+
50
+ if opts[:key_id].nil?
51
+ Trollop.die :key_id, 'must be provided or set via AWS_ACCESS_KEY_ID'
52
+ end
53
+ if opts[:secret_key].nil?
54
+ Trollop.die :secret_key, 'must be provided or set via AWS_SECRET_ACCESS_KEY'
55
+ end
56
+ if opts[:region].nil?
57
+ Trollop.die :region, 'must be provided or set via AWS_DEFAULT_REGION'
35
58
  end
36
- Trollop.die :key_id, 'unset' if opts[:key_id].nil?
37
- Trollop.die :secret_key, 'unset' if opts[:secret_key].nil?
38
- Trollop.die :region, 'unset' if opts[:region].nil?
39
59
 
40
60
  begin
41
61
  instance = ResizeAwsInstance.new(
42
62
  opts[:key_id],
43
63
  opts[:secret_key],
44
- opts[:region], opts[:instance_id]
64
+ opts[:region],
65
+ opts[:instance_id]
45
66
  )
46
- instance.resize(opts[:type])
67
+ instance.resize(opts[:type], opts[:snapshot].split(','))
47
68
  puts "[#{Time.now}] Resize complete."
48
69
  rescue => e
49
70
  puts "[#{Time.now}] #{e}"
@@ -1,4 +1,4 @@
1
1
  # ResizeAwsInstance::VERSION
2
2
  class ResizeAwsInstance
3
- VERSION = '0.1.1'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -17,9 +17,12 @@ class ResizeAwsInstance
17
17
  fail 'FATAL: Non EBS root volume.'
18
18
  end
19
19
 
20
- def resize(type)
21
- fail 'Instance already target type.' if @instance.instance_type == type
20
+ def resize(type, snapshot)
21
+ if @instance.instance_type == type
22
+ fail 'FATAL: Instance already target type.'
23
+ end
22
24
  stop
25
+ backup(snapshot) unless snapshot == ['none']
23
26
  change_type(type)
24
27
  start
25
28
  @instance.instance_type
@@ -28,24 +31,67 @@ class ResizeAwsInstance
28
31
  private
29
32
 
30
33
  def stop
34
+ @instance.stop
31
35
  loop do
32
- @instance.stop
33
- break if @instance.status == :stopped
36
+ return true if @instance.status == :stopped
34
37
  puts "[#{Time.now}] Waiting for instance to stop..."
35
38
  sleep 5
36
39
  end
37
40
  end
38
41
 
42
+ def backup(snapshot)
43
+ case snapshot
44
+ when ['root']
45
+ name = @instance.root_device_name
46
+ volume = @instance.attachments[name].volume
47
+ create_snapshot(name, volume)
48
+ else
49
+ threads = []
50
+ snapshot.each do |id|
51
+ # AWS advertises devices as /dev/sdN and Linux sees them as
52
+ # /dev/xvdN. In case this ever changes, this should match
53
+ # either pattern.
54
+ attachments = instance.attachments.select do |k, _|
55
+ k.match(%r{/dev/(s|xv)d#{id}\d*})
56
+ end
57
+ attachments.keys.each do |attachment|
58
+ volume = @instance.attachments[attachment].volume
59
+ threads << Thread.new { create_snapshot(attachment, volume) }
60
+ end
61
+ end
62
+ threads.each { |t| t.join }
63
+ end
64
+ end
65
+
66
+ def create_snapshot(name, vol)
67
+ desc = "#{@instance.id} - #{name} - #{Time.now}"
68
+ snap = vol.create_snapshot(desc)
69
+ loop do
70
+ case snap.status
71
+ when :pending
72
+ puts "[#{Time.now}] Snapshot pending [#{name}]: #{snap.progress || 0}%"
73
+ when :completed
74
+ puts "[#{Time.now}] Snapshot complete [#{name}]"
75
+ return true
76
+ when :error
77
+ fail "FATAL: Failed to create snapshot: #{desc}"
78
+ else
79
+ puts "Snapshot status unknown [#{name}]: #{snap.status}"
80
+ end
81
+ sleep 5
82
+ end
83
+ end
84
+
39
85
  def change_type(type)
40
86
  @instance.instance_type = type
41
87
  rescue AWS::EC2::Errors::Client::InvalidParameterValue
42
- raise "Invalid instance type: #{type}"
88
+ raise "FATAL: Invalid instance type: #{type}"
43
89
  end
44
90
 
45
91
  def start
92
+ @instance.start
46
93
  loop do
47
- @instance.start
48
- break if @instance.status == :running
94
+ return true if @instance.status == :running
49
95
  puts "[#{Time.now}] Waiting for instance to start..."
50
96
  sleep 5
51
97
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resize-aws-instance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tucker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-11 00:00:00.000000000 Z
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -127,12 +127,16 @@ summary: "# ResizeAwsInstance This is a quick Ruby scipt to simplify the proces
127
127
  i-xxxxxxxx ## Installation This is packaged as a gem to simplify the process of
128
128
  installation. It does not come with a shared library. To install, simply run the
129
129
  following command: $ gem install resize_aws_instance ## Usage Simple script to
130
- resize EBS based AWS instance. Usage: resize_aws_instance [options] --instance-id,
131
- -i <s>: Instance ID of AWS instance --key-id, -k <s>: AWS access key ID --secret-key,
132
- -s <s>: AWS secret access key --region, -r <s>: AWS region --type, -t <s>: Target
133
- instance type --version, -v: Print version and exit --help, -h: Show this message
134
- \ ## Contributing 1. Fork it ( https://github.com/[my-github-username]/resize-aws-instance/fork
135
- ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your
136
- changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin
137
- my-new-feature`) 5. Create a new Pull Request"
130
+ resize EBS based AWS instance. Usage: resize_aws_instance [options] Note: The
131
+ following AWS config options may be provided via environment variables: key-id =>
132
+ AWS_ACCESS_KEY_ID secret-key => AWS_SECRET_ACCESS_KEY region => AWS_DEFAULT_REGION
133
+ \ Options: --instance-id, -i <s>: AWS instance ID --key-id, -k <s>: AWS access
134
+ key ID --secret-key, -s <s>: AWS secret access key --region, -r <s>: AWS region
135
+ --type, -t <s>: Target instance type --snapshot, -n <s>: Snapshot EBS volumes
136
+ [none, root or comma separated list IDs (a,b,c)] (default: none) --version, -v:
137
+ \ Print version and exit --help, -h: Show this message ## Contributing 1. Fork
138
+ it ( https://github.com/[my-github-username]/resize-aws-instance/fork ) 2. Create
139
+ your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git
140
+ commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`)
141
+ 5. Create a new Pull Request"
138
142
  test_files: []