Class: AWS_SSH::EC2

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_ssh/ec2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEC2

Returns a new instance of EC2.



22
23
24
25
# File 'lib/aws_ssh/ec2.rb', line 22

def initialize
  self.env_error_check
  self.default_values
end

Instance Attribute Details

#apiObject

class values aws api object



14
15
16
# File 'lib/aws_ssh/ec2.rb', line 14

def api
  @api
end

#LogLevelObject

Returns the value of attribute LogLevel.



10
11
12
# File 'lib/aws_ssh/ec2.rb', line 10

def LogLevel
  @LogLevel
end

#ProxyCommandObject

Returns the value of attribute ProxyCommand.



11
12
13
# File 'lib/aws_ssh/ec2.rb', line 11

def ProxyCommand
  @ProxyCommand
end

#seperatorObject

value used as seperater in the tags



20
21
22
# File 'lib/aws_ssh/ec2.rb', line 20

def seperator
  @seperator
end

#ssh_config_fileObject

auto generated ssh config_file



18
19
20
# File 'lib/aws_ssh/ec2.rb', line 18

def ssh_config_file
  @ssh_config_file
end

#ssh_key_suffixesObject

suffix of the key to use



16
17
18
# File 'lib/aws_ssh/ec2.rb', line 16

def ssh_key_suffixes
  @ssh_key_suffixes
end

#StrictHostKeyCheckingObject

Returns the value of attribute StrictHostKeyChecking.



8
9
10
# File 'lib/aws_ssh/ec2.rb', line 8

def StrictHostKeyChecking
  @StrictHostKeyChecking
end

#UserObject

ssh config file values



7
8
9
# File 'lib/aws_ssh/ec2.rb', line 7

def User
  @User
end

#UserKnownHostsFileObject

Returns the value of attribute UserKnownHostsFile.



9
10
11
# File 'lib/aws_ssh/ec2.rb', line 9

def UserKnownHostsFile
  @UserKnownHostsFile
end

Instance Method Details

#default_valuesObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aws_ssh/ec2.rb', line 33

def default_values
  @User = "deploy"
  @StrictHostKeyChecking = "no"
  @UserKnownHostsFile = "/dev/null"
  @LogLevel = "quiet"
  @ProxyCommand = AWS_SSH::SSH.proxy

  @seperator = "."
  @ssh_key_suffixes = [".pem", ""]
  @api = AWS::EC2.new(access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: ENV['AWS_REGION'])
end

#env_error_checkObject



27
28
29
30
31
# File 'lib/aws_ssh/ec2.rb', line 27

def env_error_check
  if ENV['AWS_ACCESS_KEY_ID'].nil? || ENV['AWS_SECRET_ACCESS_KEY'].nil? || ENV['AWS_REGION'].nil?
    raise MissingAWS_CONFIG, "Missing AWS config data from the ENV."
  end
end

#hostsObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/aws_ssh/ec2.rb', line 86

def hosts
  hosts = {}
  self.instances.select!{|i| i.status.to_s == "running"}.each do |i|
    info = self.instance_data(i)
    if !info[:key_file].nil?
      self.instance_names(info).each{ |h| hosts[h] = info}
    end
  end
  return hosts
end

#instance_data(instance) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/aws_ssh/ec2.rb', line 56

def instance_data(instance)
  tags = instance.tags.to_h
  key_file = nil
  base = ENV['HOME']+"/.ssh/"+instance.key_name
  @ssh_key_suffixes.each{|k| if key_file.nil? && File.exists?(base+k) then key_file = base+k end }
  return {
    :name => if tags["Name"].nil? then nil else tags["Name"].to_s.strip end,
    :user => if tags["user"].nil? then @User else tags["user"].to_s.strip end,
    :envs => if tags["environment"].nil? then nil else tags["environment"].split(@seperator) end,
    :services => if tags["service"].nil? then nil else tags["service"].split(@seperator) end,
    :ip => instance.private_ip_address,
    :key => instance.key_name,
    :key_file => key_file
  }
end

#instance_names(info) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/aws_ssh/ec2.rb', line 72

def instance_names(info)
  names = []
  if ! info[:name].nil? then names.push(info[:name]) end
  if ! info[:envs].nil? && ! info[:services].nil?
    info[:envs].each do |e|
      info[:services].each do |s|
        names.push("#{e}_#{s}")
        names.push("#{s}_#{e}")
      end
    end
  end
  return names
end

#instancesObject



46
47
48
49
50
51
52
53
# File 'lib/aws_ssh/ec2.rb', line 46

def instances
  instances = []
  # normal instances
  @api.instances.each{|i| instances.push(i)}
  # vpc based insatnces
  @api.vpcs.each{|v| v.instances.each{|vi| instances.push(vi)} }
  return instances
end

#writeObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/aws_ssh/ec2.rb', line 98

def write
  # loop over hash and generate string
  config = "\n\n# Custom generated list from AWS on #{Time.now}\n\n"
  dir = ENV['HOME']+"/.ssh/"
  sshfile = dir+AWS_SSH::HOSTS_FILE
  File.open(sshfile, "w"){ |file| file.write(config)}
  self.hosts.each do |name, host|
    config += "Host #{name}\n"
    config += "  HostName #{host[:ip].to_s}\n"
    config += "  User #{host[:user].to_s}\n"
    config += "  IdentityFile #{host[:key_file]}\n"
    config += "  StrictHostKeyChecking #{@StrictHostKeyChecking}\n"
    config += "  UserKnownHostsFile #{@UserKnownHostsFile}\n"
    config += "  LogLevel #{@LogLevel}\n"
    config += if ! @ProxyCommand.nil? then "  ProxyCommand #{@ProxyCommand}\n" else "" end
    config += "\n\n"
  end
  # write to file
  File.open(sshfile, "w"){ |file| file.write(config)}
end