Class: Ec2ssh::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2ssh/migrator.rb

Instance Method Summary collapse

Constructor Details

#initialize(dotfile_path) ⇒ Migrator

Returns a new instance of Migrator.



7
8
9
# File 'lib/ec2ssh/migrator.rb', line 7

def initialize(dotfile_path)
  @dotfile_path = dotfile_path
end

Instance Method Details

#backup!Object



71
72
73
74
75
# File 'lib/ec2ssh/migrator.rb', line 71

def backup!
  backup_path = "#{@dotfile_path}.#{Time.now.strftime("%Y%m%d%H%M%S")}"
  File.open(backup_path, 'w') {|f| f.write dotfile_str }
  backup_path
end

#check_versionObject

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ec2ssh/migrator.rb', line 15

def check_version
  begin
    hash = YAML.load dotfile_str
    return '2' if hash.is_a?(Hash) && hash.keys.include?('aws_keys')
  rescue Psych::SyntaxError
  end

  begin
    Dsl::Parser.parse dotfile_str
    return '3'
  rescue DotfileSyntaxError
  end

  raise InvalidDotfile
end

#dotfile_strObject



11
12
13
# File 'lib/ec2ssh/migrator.rb', line 11

def dotfile_str
  @dotfile_str ||= File.read(@dotfile_path)
end

#migrate_from_2Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ec2ssh/migrator.rb', line 31

def migrate_from_2
  hash = YAML.load dotfile_str
  out = StringIO.new

  out.puts "path '#{hash['path']}'" if hash['path']

  out.puts 'aws_keys('
  out.puts hash['aws_keys'].map {|name, key|
    "  #{name}: { access_key_id: '#{key['access_key_id']}', secret_access_key: '#{key['secret_access_key']}' }"
  }.join(",\n")
  out.puts ')'

  if hash['regions']
    regions = hash['regions'].map{|r| "'#{r}'" }.join(', ')
    out.puts "regions #{regions}"
  end

  out.puts "\n# Ignore unnamed instances\nreject {|instance| !instance.tags['Name'] }\n\n# You can use methods of AWS::EC2::Instance.\n# See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html\nhost_line <<EOS\nHost <%= tags['Name'] %>.<%= availability_zone %>\n  HostName <%= dns_name || private_ip_address %>\nEOS\n  END\n\n  out.puts\n  out.puts dotfile_str.gsub(/^/m, '# ')\n\n  out.string\nend\n"

#replace!(new_dotfile_str) ⇒ Object



67
68
69
# File 'lib/ec2ssh/migrator.rb', line 67

def replace!(new_dotfile_str)
  File.open(@dotfile_path, 'w') {|f| f.write new_dotfile_str }
end