Class: Puppet::Provider::Neutron

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/neutron.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.auth_endpointObject



51
52
53
# File 'lib/puppet/provider/neutron.rb', line 51

def self.auth_endpoint
  @auth_endpoint ||= get_auth_endpoint
end

.auth_neutron(*args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/puppet/provider/neutron.rb', line 67

def self.auth_neutron(*args)
  q = neutron_credentials
  authenv = {
    :OS_AUTH_URL    => self.auth_endpoint,
    :OS_USERNAME    => q['admin_user'],
    :OS_TENANT_NAME => q['admin_tenant_name'],
    :OS_PASSWORD    => q['admin_password']
  }
  if q.key?('nova_region_name')
    authenv[:OS_REGION_NAME] = q['nova_region_name']
  end
  rv = nil
  timeout = 120
  end_time = Time.now.to_i + timeout
  loop do
    begin
      withenv authenv do
        rv = neutron(args)
      end
      break
    rescue Puppet::ExecutionFailure => e
      if ! e.message =~ /(\(HTTP\s+400\))|
            (400-\{\'message\'\:\s+\'\'\})|
            (\[Errno 111\]\s+Connection\s+refused)|
            (503\s+Service\s+Unavailable)|
            (504\s+Gateway\s+Time-out)|
            (\:\s+Maximum\s+attempts\s+reached)|
            (Unauthorized\:\s+bad\s+credentials)|
            (Max\s+retries\s+exceeded)/
        raise(e)
      end
      current_time = Time.now.to_i
      if current_time > end_time
        break
      else
        wait = end_time - current_time
        Puppet::debug("Non-fatal error: \"#{e.message}\"")
        notice("Neutron API not avalaible. Wait up to #{wait} sec.")
      end
      sleep(2)
      # Note(xarses): Don't remove, we know that there is one of the
      # Recoverable erros above, So we will retry a few more times
    end
  end
  return rv
end

.conf_filenameObject



6
7
8
# File 'lib/puppet/provider/neutron.rb', line 6

def self.conf_filename
  '/etc/neutron/neutron.conf'
end

.get_auth_endpointObject



55
56
57
58
# File 'lib/puppet/provider/neutron.rb', line 55

def self.get_auth_endpoint
  q = neutron_credentials
  "#{q['auth_protocol']}://#{q['auth_host']}:#{q['auth_port']}/v2.0/"
end

.get_neutron_credentialsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/puppet/provider/neutron.rb', line 28

def self.get_neutron_credentials
  auth_keys = ['auth_host', 'auth_port', 'auth_protocol',
               'admin_tenant_name', 'admin_user', 'admin_password']
  conf = neutron_conf
  if conf and conf['keystone_authtoken'] and
      auth_keys.all?{|k| !conf['keystone_authtoken'][k].nil?}
    creds = Hash[ auth_keys.map \
                 { |k| [k, conf['keystone_authtoken'][k].strip] } ]
    if conf['DEFAULT'] and !conf['DEFAULT']['nova_region_name'].nil?
      creds['nova_region_name'] = conf['DEFAULT']['nova_region_name']
    end
    return creds
  else
    raise(Puppet::Error, "File: #{conf_filename} does not contain all \
required sections.  Neutron types will not work if neutron is not \
correctly configured.")
  end
end

.get_neutron_resource_attrs(type, id) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/puppet/provider/neutron.rb', line 133

def self.get_neutron_resource_attrs(type, id)
  attrs = {}
  net = auth_neutron("#{type}-show", '--format=shell', id)
  last_key = nil
  (net.split("\n") || []).compact.collect do |line|
    if line.include? '='
      k, v = line.split('=', 2)
      attrs[k] = v.gsub(/\A"|"\Z/, '')
      last_key = k
    else
      # Handle the case of a list of values
      v = line.gsub(/\A"|"\Z/, '')
      attrs[last_key] = [attrs[last_key], v].flatten
    end
  end
  return attrs
end

.get_tenant_id(catalog, name) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/puppet/provider/neutron.rb', line 176

def self.get_tenant_id(catalog, name)
  instance_type = 'keystone_tenant'
  instance = catalog.resource("#{instance_type.capitalize!}[#{name}]")
  if ! instance
    instance = Puppet::Type.type(instance_type).instances.find do |i|
      i.provider.name == name
    end
  end
  if instance
    return instance.provider.id
  else
    fail("Unable to find #{instance_type} for name #{name}")
  end
end

.list_neutron_resources(type) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/puppet/provider/neutron.rb', line 123

def self.list_neutron_resources(type)
  ids = []
  list = auth_neutron("#{type}-list", '--format=csv',
                      '--column=id', '--quote=none')
  (list.split("\n")[1..-1] || []).compact.collect do |line|
    ids << line.strip
  end
  return ids
end

.list_router_ports(router_name_or_id) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/puppet/provider/neutron.rb', line 151

def self.list_router_ports(router_name_or_id)
  results = []
  cmd_output = auth_neutron("router-port-list",
                            '--format=csv',
                            router_name_or_id)
  if ! cmd_output
    return results
  end

  headers = nil
  CSV.parse(cmd_output) do |row|
    if headers == nil
      headers = row
    else
      result = Hash[*headers.zip(row).flatten]
      match_data = /.*"subnet_id": "(.*)", .*/.match(result['fixed_ips'])
      if match_data
        result['subnet_id'] = match_data[1]
      end
      results << result
    end
  end
  return results
end

.neutron_confObject



60
61
62
63
64
65
# File 'lib/puppet/provider/neutron.rb', line 60

def self.neutron_conf
  return @neutron_conf if @neutron_conf
  @neutron_conf = Puppet::Util::IniConfig::File.new
  @neutron_conf.read(conf_filename)
  @neutron_conf
end

.neutron_credentialsObject



24
25
26
# File 'lib/puppet/provider/neutron.rb', line 24

def self.neutron_credentials
  @neutron_credentials ||= get_neutron_credentials
end

.parse_creation_output(data) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/puppet/provider/neutron.rb', line 191

def self.parse_creation_output(data)
  hash = {}
  data.split("\n").compact.each do |line|
    if line.include? '='
      hash[line.split('=').first] = line.split('=', 2)[1].gsub(/\A"|"\Z/, '')
    end
  end
  hash
end

.resetObject



118
119
120
121
# File 'lib/puppet/provider/neutron.rb', line 118

def self.reset
  @neutron_conf        = nil
  @neutron_credentials = nil
end

.withenv(hash, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/puppet/provider/neutron.rb', line 10

def self.withenv(hash, &block)
  saved = ENV.to_hash
  hash.each do |name, val|
    ENV[name.to_s] = val
  end

  yield
ensure
  ENV.clear
  saved.each do |name, val|
    ENV[name] = val
  end
end

Instance Method Details

#auth_neutron(*args) ⇒ Object



114
115
116
# File 'lib/puppet/provider/neutron.rb', line 114

def auth_neutron(*args)
  self.class.auth_neutron(args)
end

#neutron_credentialsObject



47
48
49
# File 'lib/puppet/provider/neutron.rb', line 47

def neutron_credentials
  self.class.neutron_credentials
end