ec2ssh 3.1.0.rc1 → 5.0.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.
@@ -1,34 +0,0 @@
1
- require 'ec2ssh/exceptions'
2
- require 'ec2ssh/command'
3
- require 'ec2ssh/migrator'
4
-
5
- module Ec2ssh
6
- module Command
7
- class Migrate < Base
8
- def initialize(cli)
9
- super
10
- end
11
-
12
- def run
13
- migrator = Migrator.new dotfile_path
14
- version = migrator.check_version
15
- case version
16
- when '2'
17
- cli.red "Current dotfile version is #{version} (#{dotfile_path})"
18
- new_dotfile_str = migrator.migrate_from_2
19
- cli.red "Ec2ssh is migrating your dotfile to version 3 style as follows:"
20
- cli.yellow new_dotfile_str
21
- if cli.yes? "Are you sure to migrate #{dotfile_path} to version 3 style? (y/n)"
22
- backup_path = migrator.backup!
23
- puts "Creating backup as #{backup_path}"
24
- migrator.replace! new_dotfile_str
25
- cli.green 'Migrated successfully.'
26
- end
27
- when '3'
28
- cli.green "Current dotfile version is #{version} (#{dotfile_path})"
29
- cli.green 'Your dotfile is up-to-date.'
30
- end
31
- end
32
- end
33
- end
34
- end
@@ -1,77 +0,0 @@
1
- require 'yaml'
2
- require 'stringio'
3
- require 'ec2ssh/dsl'
4
-
5
- module Ec2ssh
6
- class Migrator
7
- def initialize(dotfile_path)
8
- @dotfile_path = dotfile_path
9
- end
10
-
11
- def dotfile_str
12
- @dotfile_str ||= File.read(@dotfile_path)
13
- end
14
-
15
- def check_version
16
- begin
17
- hash = YAML.load dotfile_str
18
- return '2' if hash.is_a?(Hash) && hash.keys.include?('aws_keys')
19
- rescue Psych::SyntaxError
20
- end
21
-
22
- begin
23
- Dsl::Parser.parse dotfile_str
24
- return '3'
25
- rescue DotfileSyntaxError
26
- end
27
-
28
- raise InvalidDotfile
29
- end
30
-
31
- def migrate_from_2
32
- hash = YAML.load dotfile_str
33
- out = StringIO.new
34
-
35
- out.puts "path '#{hash['path']}'" if hash['path']
36
-
37
- out.puts 'aws_keys('
38
- out.puts hash['aws_keys'].map {|name, key|
39
- " #{name}: { access_key_id: '#{key['access_key_id']}', secret_access_key: '#{key['secret_access_key']}' }"
40
- }.join(",\n")
41
- out.puts ')'
42
-
43
- if hash['regions']
44
- regions = hash['regions'].map{|r| "'#{r}'" }.join(', ')
45
- out.puts "regions #{regions}"
46
- end
47
-
48
- out.puts <<-END
49
-
50
- # Ignore unnamed instances
51
- reject {|instance| !instance.tags['Name'] }
52
-
53
- # You can use methods of AWS::EC2::Instance.
54
- # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
55
- host_line <<EOS
56
- Host <%= tags['Name'] %>.<%= availability_zone %>
57
- HostName <%= dns_name || private_ip_address %>
58
- EOS
59
- END
60
-
61
- out.puts
62
- out.puts dotfile_str.gsub(/^/m, '# ')
63
-
64
- out.string
65
- end
66
-
67
- def replace!(new_dotfile_str)
68
- File.open(@dotfile_path, 'w') {|f| f.write new_dotfile_str }
69
- end
70
-
71
- def backup!
72
- backup_path = "#{@dotfile_path}.#{Time.now.strftime("%Y%m%d%H%M%S")}"
73
- File.open(backup_path, 'w') {|f| f.write dotfile_str }
74
- backup_path
75
- end
76
- end
77
- end
@@ -1,111 +0,0 @@
1
- require 'spec_helper'
2
- require 'ec2ssh/command/migrate'
3
-
4
- describe Ec2ssh::Command::Migrate do
5
- describe '#run' do
6
- let(:cli) do
7
- double(:cli,
8
- options: options, yes?: nil,
9
- red: nil, yellow: nil, green: nil)
10
- end
11
- let(:command) do
12
- described_class.new cli
13
- end
14
- let(:options) do
15
- double(:options, dotfile: '/dotfile')
16
- end
17
-
18
- before do
19
- File.open('/dotfile', 'w') {|f| f.write dotfile_str }
20
- end
21
-
22
- around do |example|
23
- silence_stdout { example.run }
24
- end
25
-
26
- context 'version 2' do
27
- let(:dotfile_str) { <<-END }
28
- ---
29
- path: /path/to/ssh/config
30
- aws_keys:
31
- key1:
32
- access_key_id: ACCESS_KEY1
33
- secret_access_key: SECRET1
34
- END
35
-
36
- context 'yes' do
37
- before do
38
- expect(cli).to receive(:yes?).and_return(true)
39
- Timecop.freeze(Time.utc(2014, 1, 1)) do
40
- command.run
41
- end
42
- end
43
-
44
- it do
45
- expect(File.read('/dotfile')).to eq <<-END
46
- path '/path/to/ssh/config'
47
- aws_keys(
48
- key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' }
49
- )
50
-
51
- # Ignore unnamed instances
52
- reject {|instance| !instance.tags['Name'] }
53
-
54
- # You can use methods of AWS::EC2::Instance.
55
- # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
56
- host_line <<EOS
57
- Host <%= tags['Name'] %>.<%= availability_zone %>
58
- HostName <%= dns_name || private_ip_address %>
59
- EOS
60
-
61
- # ---
62
- # path: /path/to/ssh/config
63
- # aws_keys:
64
- # key1:
65
- # access_key_id: ACCESS_KEY1
66
- # secret_access_key: SECRET1
67
- END
68
- end
69
-
70
- it do
71
- expect(File.read('/dotfile.20140101000000')).to eq(dotfile_str)
72
- end
73
- end
74
-
75
- context 'no' do
76
- before do
77
- expect(cli).to receive(:yes?).and_return(false)
78
- command.run
79
- end
80
-
81
- it do
82
- expect(File.read('/dotfile')).to eq(dotfile_str)
83
- end
84
- end
85
- end
86
-
87
- context 'version 3' do
88
- let(:dotfile_str) { <<-END }
89
- path '/path/to/ssh/config'
90
- aws_keys(
91
- key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' }
92
- )
93
-
94
- # You can use methods of AWS::EC2::Instance.
95
- # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
96
- host_line <<EOS
97
- Host <%= tags['Name'] %>.<%= availability_zone %>
98
- HostName <%= dns_name || private_ip_address %>
99
- EOS
100
- END
101
-
102
- before do
103
- command.run
104
- end
105
-
106
- it do
107
- expect(File.read('/dotfile')).to eq(dotfile_str)
108
- end
109
- end
110
- end
111
- end
@@ -1,62 +0,0 @@
1
- require 'spec_helper'
2
- require 'ec2ssh/migrator'
3
-
4
- describe Ec2ssh::Migrator do
5
- subject(:migrator) { described_class.new '/dotfile' }
6
-
7
- before do
8
- File.open('/dotfile', 'w') {|f| f.write dotfile_str }
9
- end
10
-
11
- context 'from version 2' do
12
- let(:dotfile_str) { <<-END }
13
- ---
14
- path: /path/to/ssh/config
15
- aws_keys:
16
- key1:
17
- access_key_id: ACCESS_KEY1
18
- secret_access_key: SECRET1
19
- key2:
20
- access_key_id: ACCESS_KEY2
21
- secret_access_key: SECRET2
22
- regions:
23
- - ap-northeast-1
24
- - us-east-1
25
- END
26
-
27
- let(:new_dotfile_str) { <<-END }
28
- path '/path/to/ssh/config'
29
- aws_keys(
30
- key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' },
31
- key2: { access_key_id: 'ACCESS_KEY2', secret_access_key: 'SECRET2' }
32
- )
33
- regions 'ap-northeast-1', 'us-east-1'
34
-
35
- # Ignore unnamed instances
36
- reject {|instance| !instance.tags['Name'] }
37
-
38
- # You can use methods of AWS::EC2::Instance.
39
- # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
40
- host_line <<EOS
41
- Host <%= tags['Name'] %>.<%= availability_zone %>
42
- HostName <%= dns_name || private_ip_address %>
43
- EOS
44
-
45
- # ---
46
- # path: /path/to/ssh/config
47
- # aws_keys:
48
- # key1:
49
- # access_key_id: ACCESS_KEY1
50
- # secret_access_key: SECRET1
51
- # key2:
52
- # access_key_id: ACCESS_KEY2
53
- # secret_access_key: SECRET2
54
- # regions:
55
- # - ap-northeast-1
56
- # - us-east-1
57
- END
58
-
59
- it { expect(migrator.check_version).to eq('2') }
60
- it { expect(migrator.migrate_from_2).to eq(new_dotfile_str) }
61
- end
62
- end