Class: VagrantPlugins::Ec2setup::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/ec2setup/command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



18
19
20
21
22
23
24
# File 'lib/vagrant/ec2setup/command.rb', line 18

def execute
  @config = @env.config_global.ec2setup
  ENV['AWS_ACCESS_KEY_ID'] || usage
  ENV['AWS_SECRET_ACCESS_KEY'] || usage
  generate_key_pair
  generate_security_group
end

#generate_key_pairObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant/ec2setup/command.rb', line 34

def generate_key_pair
  key_pair_name    = @config.key_pair_name
  private_key_path = @config.private_key_path
  @key_pair ||= if File.exists?(private_key_path) && (key = region.key_pairs[key_pair_name]).exists?
                  key
                else
                  region.key_pairs.create(key_pair_name).tap{|k|
                    File.open(private_key_path, 'w'){|f| f.write(k.private_key) }
                  }
                end
end

#generate_security_groupObject



46
47
48
49
50
51
52
53
# File 'lib/vagrant/ec2setup/command.rb', line 46

def generate_security_group
  security_group_name = @config.security_group_name
  return if region.security_groups.filter('group-name', security_group_name).count > 0
  security_group = region.security_groups.create(security_group_name)
  [22, 80, 443].each do |port|
    security_group.authorize_ingress(:tcp, port, "0.0.0.0/0")
  end
end

#regionObject



26
27
28
29
30
31
32
# File 'lib/vagrant/ec2setup/command.rb', line 26

def region
  region_name = @config.region
  return @region if @region
  @region = ::AWS::EC2.new.regions[region_name].tap{|region|
    raise "No such region #{region_name}." unless region.exists?
  }
end

#usageObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/vagrant/ec2setup/command.rb', line 7

def usage
  puts <<-EOS.gsub(/^ {10}/, '')
    You must set AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID.
    For example into your ~/.bashrc

      export AWS_ACCESS_KEY_ID="Your Aws Access Key"
      export AWS_SECRET_ACCESS_KEY="Your Aws Access Token"
  EOS
  exit
end