ec2ssh 3.1.1 → 5.1.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,113 +0,0 @@
1
- require 'spec_helper'
2
- require 'ec2ssh/command/migrate'
3
-
4
- describe Ec2ssh::Command::Migrate do
5
- include FakeFS::SpecHelpers
6
-
7
- describe '#run' do
8
- let(:cli) do
9
- double(:cli,
10
- options: options, yes?: nil,
11
- red: nil, yellow: nil, green: nil)
12
- end
13
- let(:command) do
14
- described_class.new cli
15
- end
16
- let(:options) do
17
- double(:options, dotfile: '/dotfile')
18
- end
19
-
20
- before do
21
- File.open('/dotfile', 'w') {|f| f.write dotfile_str }
22
- end
23
-
24
- around do |example|
25
- silence_stdout { example.run }
26
- end
27
-
28
- context 'version 2' do
29
- let(:dotfile_str) { <<-END }
30
- ---
31
- path: /path/to/ssh/config
32
- aws_keys:
33
- key1:
34
- access_key_id: ACCESS_KEY1
35
- secret_access_key: SECRET1
36
- END
37
-
38
- context 'yes' do
39
- before do
40
- expect(cli).to receive(:yes?).and_return(true)
41
- Timecop.freeze(Time.utc(2014, 1, 1)) do
42
- command.run
43
- end
44
- end
45
-
46
- it do
47
- expect(File.read('/dotfile')).to eq <<-END
48
- path '/path/to/ssh/config'
49
- aws_keys(
50
- key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' }
51
- )
52
-
53
- # Ignore unnamed instances
54
- reject {|instance| !instance.tags['Name'] }
55
-
56
- # You can use methods of AWS::EC2::Instance.
57
- # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
58
- host_line <<EOS
59
- Host <%= tags['Name'] %>.<%= availability_zone %>
60
- HostName <%= dns_name || private_ip_address %>
61
- EOS
62
-
63
- # ---
64
- # path: /path/to/ssh/config
65
- # aws_keys:
66
- # key1:
67
- # access_key_id: ACCESS_KEY1
68
- # secret_access_key: SECRET1
69
- END
70
- end
71
-
72
- it do
73
- expect(File.read('/dotfile.20140101000000')).to eq(dotfile_str)
74
- end
75
- end
76
-
77
- context 'no' do
78
- before do
79
- expect(cli).to receive(:yes?).and_return(false)
80
- command.run
81
- end
82
-
83
- it do
84
- expect(File.read('/dotfile')).to eq(dotfile_str)
85
- end
86
- end
87
- end
88
-
89
- context 'version 3' do
90
- let(:dotfile_str) { <<-END }
91
- path '/path/to/ssh/config'
92
- aws_keys(
93
- key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' }
94
- )
95
-
96
- # You can use methods of AWS::EC2::Instance.
97
- # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
98
- host_line <<EOS
99
- Host <%= tags['Name'] %>.<%= availability_zone %>
100
- HostName <%= dns_name || private_ip_address %>
101
- EOS
102
- END
103
-
104
- before do
105
- command.run
106
- end
107
-
108
- it do
109
- expect(File.read('/dotfile')).to eq(dotfile_str)
110
- end
111
- end
112
- end
113
- end
@@ -1,64 +0,0 @@
1
- require 'spec_helper'
2
- require 'ec2ssh/migrator'
3
-
4
- describe Ec2ssh::Migrator do
5
- include FakeFS::SpecHelpers
6
-
7
- subject(:migrator) { described_class.new '/dotfile' }
8
-
9
- before do
10
- File.open('/dotfile', 'w') {|f| f.write dotfile_str }
11
- end
12
-
13
- context 'from version 2' do
14
- let(:dotfile_str) { <<-END }
15
- ---
16
- path: /path/to/ssh/config
17
- aws_keys:
18
- key1:
19
- access_key_id: ACCESS_KEY1
20
- secret_access_key: SECRET1
21
- key2:
22
- access_key_id: ACCESS_KEY2
23
- secret_access_key: SECRET2
24
- regions:
25
- - ap-northeast-1
26
- - us-east-1
27
- END
28
-
29
- let(:new_dotfile_str) { <<-END }
30
- path '/path/to/ssh/config'
31
- aws_keys(
32
- key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' },
33
- key2: { access_key_id: 'ACCESS_KEY2', secret_access_key: 'SECRET2' }
34
- )
35
- regions 'ap-northeast-1', 'us-east-1'
36
-
37
- # Ignore unnamed instances
38
- reject {|instance| !instance.tags['Name'] }
39
-
40
- # You can use methods of AWS::EC2::Instance.
41
- # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
42
- host_line <<EOS
43
- Host <%= tags['Name'] %>.<%= availability_zone %>
44
- HostName <%= dns_name || private_ip_address %>
45
- EOS
46
-
47
- # ---
48
- # path: /path/to/ssh/config
49
- # aws_keys:
50
- # key1:
51
- # access_key_id: ACCESS_KEY1
52
- # secret_access_key: SECRET1
53
- # key2:
54
- # access_key_id: ACCESS_KEY2
55
- # secret_access_key: SECRET2
56
- # regions:
57
- # - ap-northeast-1
58
- # - us-east-1
59
- END
60
-
61
- it { expect(migrator.check_version).to eq('2') }
62
- it { expect(migrator.migrate_from_2).to eq(new_dotfile_str) }
63
- end
64
- end