Module: GaptoolBaseHelpers

Defined in:
lib/helpers/gaptool-base.rb

Instance Method Summary collapse

Instance Method Details

#gt_securitygroup(role, environment, zone, groupname = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/helpers/gaptool-base.rb', line 22

def gt_securitygroup(role, environment, zone, groupname=nil)
  AWS.config(:access_key_id => $redis.hget('config', 'aws_id'), :secret_access_key => $redis.hget('config', 'aws_secret'), :ec2_endpoint => "ec2.#{zone.chop}.amazonaws.com")
  @ec2 = AWS::EC2.new
  groupname = groupname || "#{role}-#{environment}"
  default_list = [ 22 ]
  @ec2.security_groups.each do |group|
    if group.name == groupname
      return group.id
    end
  end
  internet = ['0.0.0.0/0']
  sg = @ec2.security_groups.create(groupname)
  sg.authorize_ingress :tcp, 22, *internet
  return sg.id
end

#hash2redis(key, hash) ⇒ Object



3
4
5
6
7
# File 'lib/helpers/gaptool-base.rb', line 3

def hash2redis(key, hash)
  hash.keys.each do |hkey|
    $redis.hset key, hkey, hash[hkey]
  end
end

#putkey(host) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/helpers/gaptool-base.rb', line 9

def putkey(host)
  @key = OpenSSL::PKey::RSA.new 2048
  @pubkey = "#{@key.ssh_type} #{[@key.to_blob].pack('m0')} GAPTOOL_GENERATED_KEY"
  ENV['SSH_AUTH_SOCK'] = ''
  Net::SSH.start(host, 'admin', :key_data => [$redis.hget('config', 'gaptoolkey')], :config => false, :keys_only => true, :paranoid => false) do |ssh|
    ssh.exec! "grep -v GAPTOOL_GENERATED_KEY ~/.ssh/authorized_keys > /tmp/pubkeys"
    ssh.exec! "cat /tmp/pubkeys > ~/.ssh/authorized_keys"
    ssh.exec! "rm /tmp/pubkeys"
    ssh.exec! "echo #{@pubkey} >> ~/.ssh/authorized_keys"
  end
  return @key.to_pem
end