Class: Ec2ssh::Command::Init

Inherits:
Base
  • Object
show all
Defined in:
lib/ec2ssh/command/init.rb

Instance Attribute Summary

Attributes inherited from Base

#cli

Instance Method Summary collapse

Methods inherited from Base

#dotfile, #dotfile_path, #ssh_config_path

Constructor Details

#initialize(cli) ⇒ Init

Returns a new instance of Init.



8
9
10
# File 'lib/ec2ssh/command/init.rb', line 8

def initialize(cli)
  super
end

Instance Method Details

#init_dotfileObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ec2ssh/command/init.rb', line 21

def init_dotfile
  if File.exist?(dotfile_path)
    cli.yellow "Warning: #{dotfile_path} already exists."
    return
  end

  write_dotfile_example

  cli.green "Generated #{dotfile_path}"
  cli.yellow "Please check and edit #{dotfile_path} before run `ec2ssh update`"
end

#init_ssh_configObject



52
53
54
55
56
57
58
59
# File 'lib/ec2ssh/command/init.rb', line 52

def init_ssh_config
  if ssh_config.mark_exist?
    raise MarkAlreadyExists
  else
    ssh_config.append_mark!
    cli.green "Added mark to #{ssh_config_path}"
  end
end

#runObject



12
13
14
15
16
17
18
19
# File 'lib/ec2ssh/command/init.rb', line 12

def run
  begin
    init_ssh_config
  rescue DotfileNotFound
    init_dotfile
    retry
  end
end

#ssh_configObject



61
62
63
# File 'lib/ec2ssh/command/init.rb', line 61

def ssh_config
  @ssh_config ||= SshConfig.new(ssh_config_path)
end

#write_dotfile_exampleObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ec2ssh/command/init.rb', line 33

def write_dotfile_example
  example = <<-DOTFILE
path '#{ENV['HOME']}/.ssh/config'
profiles 'default', 'myprofile'
regions ENV['AWS_REGION'] || ENV['AMAZON_REGION'] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
# Enable regions as you like
# regions *%w(ap-northeast-1 ap-southeast-1 ap-southeast-2 eu-west-1 sa-east-1 us-east-1 us-west-1 us-west-2)

# You can use methods of AWS::EC2::Instance.
# See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
host_line <<END
Host <%= tag('Name') %>.<%= placement.availability_zone %>
  HostName <%= public_dns_name || private_ip_address %>
END
  DOTFILE

  File.open(dotfile_path, 'w') {|f| f.write example }
end