Sha256: 5685e54e418fc3788d8a4bb89ac69e623ceea6f9e4a55f5711bcc191a936fa59

Contents?: true

Size: 1.77 KB

Versions: 7

Compression:

Stored size: 1.77 KB

Contents

require 'yaml'
require 'stringio'
require 'ec2ssh/dsl'

module Ec2ssh
  class Migrator
    def initialize(dotfile_path)
      @dotfile_path = dotfile_path
    end

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

    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

    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 <<-END

# Ignore unnamed instances
reject {|instance| !instance.tags['Name'] }

# You can use methods of AWS::EC2::Instance.
# See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
host_line <<EOS
Host <%= tags['Name'] %>.<%= availability_zone %>
  HostName <%= dns_name || private_ip_address %>
EOS
      END

      out.puts
      out.puts dotfile_str.gsub(/^/m, '# ')

      out.string
    end

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

    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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ec2ssh-3.1.1 lib/ec2ssh/migrator.rb
ec2ssh-3.1.0 lib/ec2ssh/migrator.rb
ec2ssh-3.1.0.rc2 lib/ec2ssh/migrator.rb
ec2ssh-3.1.0.rc1 lib/ec2ssh/migrator.rb
ec2ssh-3.0.3 lib/ec2ssh/migrator.rb
ec2ssh-3.0.2 lib/ec2ssh/migrator.rb
ec2ssh-3.0.1 lib/ec2ssh/migrator.rb