Class: Knife::Server::Ec2SecurityGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/knife/server/ec2_security_group.rb

Overview

Sets up EC2 security groups for a Chef Server.

Instance Method Summary collapse

Constructor Details

#initialize(connection, ui) ⇒ Ec2SecurityGroup

Returns a new instance of Ec2SecurityGroup.



24
25
26
27
# File 'lib/knife/server/ec2_security_group.rb', line 24

def initialize(connection, ui)
  @aws  = connection
  @ui   = ui
end

Instance Method Details

#configure_chef_server_group(group_name, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/knife/server/ec2_security_group.rb', line 29

def configure_chef_server_group(group_name, options = {})
  group = find_or_create(group_name, options)

  ip_permissions.each do |p|
    if permission_exists?(group, p)
      @ui.msg "Inbound security group rule " \
        "#{p[:proto]}(#{p[:from]} -> #{p[:to]}) exists"
    else
      @ui.msg "Creating inbound security group rule for " \
        "#{p[:proto]}(#{p[:from]} -> #{p[:to]})"
      options = permission_options(group, p)
      @aws.authorize_security_group_ingress(group.name, options)
    end
  end
end

#find_or_create(name, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/knife/server/ec2_security_group.rb', line 45

def find_or_create(name, options = {})
  group = @aws.security_groups.find { |g| g.name == name }

  if group.nil?
    @ui.msg "Creating EC2 security group '#{name}'"
    @aws.create_security_group(name, options[:description])
    group = @aws.security_groups.find { |g| g.name == name }
  else
    @ui.msg "EC2 security group '#{name}' exists"
  end

  group
end