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



58
59
60
61
62
63
64
65
# File 'lib/ec2ssh/command/init.rb', line 58

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



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

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
51
52
53
54
55
56
# File 'lib/ec2ssh/command/init.rb', line 33

def write_dotfile_example
  example = "path '\#{ENV['HOME']}/.ssh/config'\naws_keys(\n  default: {\n    access_key_id: ENV['AWS_ACCESS_KEY_ID'],\n    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']\n  },\n  # my_key1: { access_key_id: '...', secret_access_key: '...' }, ...\n)\nregions ENV['AWS_REGION'] || ENV['AMAZON_REGION'] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1'\n# Enable regions as you like\n# 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)\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 <<END\nHost <%= tags['Name'] %>.<%= availability_zone %>\n  HostName <%= dns_name || private_ip_address %>\nEND\n  DOTFILE\n\n  File.open(dotfile_path, 'w') {|f| f.write example }\nend\n"