Module: Vagrant::Aws::Stack

Defined in:
lib/vagrant/aws/stack.rb,
lib/vagrant/aws/stack/version.rb

Constant Summary collapse

AWS_BINARY =
`which aws`.chomp
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.get_records(opts) ⇒ Object

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vagrant/aws/stack.rb', line 89

def self.get_records(opts)
  raise ArgumentError, "must provide :id_field_name" unless opts[:id_key] 
  raise ArgumentError, "must provide :id_field_name" unless opts[:data]

  id_key = opts[:id_key]
  name = opts[:logical_id]

  results = Array.new

  opts[:data].each do |data|

    next unless data["Tags"]     

    stack = get_tag(data["Tags"],"aws:cloudformation:stack-name") unless @stack_id.nil?
    logical_id = get_tag(data["Tags"],"aws:cloudformation:logical-id")

    next unless "#{stack}" == "#{@stack_id}"

    if name.nil?
      results << { :id => data[id_key], :stack => stack, :name => logical_id } 
      next
    end

    if name.is_a?(Array)
      name.each do |cur_name|
        next unless logical_id == cur_name
        results << { :id => data[id_key], :stack => stack, :name => logical_id }
      end
      next
    end

    next unless logical_id == name

    return { :id => data[id_key], :stack => stack, :name => logical_id }

  end
  return results
end

.get_sg(name = nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/vagrant/aws/stack.rb', line 51

def self.get_sg(name=nil)
  sgs = Array.new
  @sg_data = run_aws_cmd('ec2 describe-security-groups')["SecurityGroups"] unless @sg_data
  sgs = get_records(:id_key => "GroupId", :logical_id => name, :data => @sg_data )

  return sgs
end

.get_sg_by_name(name = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/vagrant/aws/stack.rb', line 59

def self.get_sg_by_name(name=nil)
  sgs = Array.new
  @sg_data = run_aws_cmd('ec2 describe-security-groups')["SecurityGroups"] unless @sg_data
  get_records(:id_key => "GroupId", :logical_id => name, :data => @sg_data ).each do |sg|
    sgs << sg[:id]
  end

  return sgs
end

.get_subnet(name = nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/vagrant/aws/stack.rb', line 69

def self.get_subnet(name=nil)

  @subnet_data = run_aws_cmd('ec2 describe-subnets')["Subnets"] unless @subnet_data
  subnets = get_records(:id_key => "SubnetId", :logical_id => name, :data => @subnet_data ) 

  return subnets
end

.get_subnet_by_name(name = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant/aws/stack.rb', line 77

def self.get_subnet_by_name(name=nil)
  @subnet_data = run_aws_cmd('ec2 describe-subnets')["Subnets"] unless @subnet_data
  subnets =  get_records(:id_key => "SubnetId", :logical_id => name, :data => @subnet_data )
  if subnets.is_a?(Array)
    raise Vagrant::Errors::VagrantError, "no subnets found with those ids!"
    return subnets.map! { |opt| opt[:id] }
  end

  return subnets[:id]

end

.get_tag(tags, tag_key) ⇒ Object

Raises:

  • (ArgumentError)


128
129
130
131
132
133
134
135
136
137
# File 'lib/vagrant/aws/stack.rb', line 128

def self.get_tag(tags,tag_key) 
  raise ArgumentError, "must provide the actual keys hash!" unless tags.is_a?(Array)
  return false unless tags
  tags.each do |tag|
    if tag['Key'] == tag_key
      return tag['Value']
    end
  end
  return false
end

.get_vpc(name = nil) ⇒ Object



45
46
47
48
49
# File 'lib/vagrant/aws/stack.rb', line 45

def self.get_vpc(name=nil)
  @vpc_data = run_aws_cmd('ec2 describe-vpcs')["Vpcs"] unless @vpc_data
  vpcs = get_records(:id_key => "VpcId", :logical_id => name, :data => @vpc_data )
  return vpcs
end

.run_aws_cmd(cmd = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant/aws/stack.rb', line 21

def self.run_aws_cmd(cmd=nil)

  result = nil

  json = %x[#{AWS_BINARY} #{cmd} 2>&1]

  status = $?

    if status.exitstatus != 0
      json.gsub! /\n|\r/,""
      logger.error "error: #{status.exitstatus} - #{json}"
      return
  end
  if json and json != ""
    begin
      result = JSON.parse( json )
    rescue JSON::ParserError => err
      logger.error "Unable to parse result after command #{cmd} #{json} - #{err}"
    end
  end

  return result
end

.stack_idObject



17
18
19
# File 'lib/vagrant/aws/stack.rb', line 17

def self.stack_id
  return @stack_id
end

.stack_id=(id) ⇒ Object



13
14
15
# File 'lib/vagrant/aws/stack.rb', line 13

def self.stack_id=(id)
  @stack_id = id
end