Class: Puppet::Provider::Nova

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.auth_endpointObject



61
62
63
# File 'lib/puppet/provider/nova.rb', line 61

def self.auth_endpoint
  @auth_endpoint ||= get_auth_endpoint
end

.auth_nova(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puppet/provider/nova.rb', line 65

def self.auth_nova(*args)
  q = nova_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']
  }
  begin
    withenv authenv do
      nova(args)
    end
  rescue Exception => e
    if (e.message =~ /\[Errno 111\] Connection refused/) or
        (e.message =~ /\(HTTP 400\)/)
      sleep 10
      withenv authenv do
        nova(args)
      end
    else
     raise(e)
    end
  end
end

.cliout2list(output) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/puppet/provider/nova.rb', line 131

def self.cliout2list(output)
  #don't proceed with empty output
  if output.empty?
    return []
  end
  lines = []
  output.each_line do |line|
    #ignore lines starting with '+'
    if not line.match("^\\+")
      #split line at '|' and remove useless information
      line = line.gsub(/^\| /, "").gsub(/ \|$/, "").gsub(/[\n]+/, "")
      line = line.split("|").map do |el|
        el.strip().gsub(/^-$/, "")
      end
      #check every element for list
      line = line.map do |el|
        el = str2list(el)
      end
      lines.push(line)
    end
  end
  #create a list of hashes and return the list
  hash_list = []
  header = lines[0]
  lines[1..-1].each do |line|
    hash_list.push(Hash[header.zip(line)])
  end
  return hash_list
end

.conf_filenameObject



7
8
9
# File 'lib/puppet/provider/nova.rb', line 7

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

.get_auth_endpointObject



56
57
58
59
# File 'lib/puppet/provider/nova.rb', line 56

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

.get_nova_credentialsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet/provider/nova.rb', line 40

def self.get_nova_credentials
  #needed keys for authentication
  auth_keys = ['auth_host', 'auth_port', 'auth_protocol',
               'admin_tenant_name', 'admin_user', 'admin_password']
  conf = nova_conf
  if conf and conf['keystone_authtoken'] and
      auth_keys.all?{|k| !conf['keystone_authtoken'][k].nil?}
    return Hash[ auth_keys.map \
                 { |k| [k, conf['keystone_authtoken'][k].strip] } ]
  else
    raise(Puppet::Error, "File: #{conf_filename} does not contain all " +
          "required sections.  Nova types will not work if nova is not " +
          "correctly configured.")
  end
end

.nova_aggregate_resources_attr(id) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/puppet/provider/nova.rb', line 185

def self.nova_aggregate_resources_attr(id)
  #run command to get details for given Id
  cmd_output = auth_nova("aggregate-details", id)
  list = cliout2list(cmd_output)[0]
  if ! list["Hosts"].is_a?(Array)
    if list["Hosts"] == ""
      list["Hosts"] = []
    else
      list["Hosts"] = [ list["Hosts"] ]
    end
  end
  return list
end

.nova_aggregate_resources_get_name_by_id(name) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/puppet/provider/nova.rb', line 174

def self.nova_aggregate_resources_get_name_by_id(name)
  #find the id by the given name
  nova_aggregate_resources_ids.each do |entry|
    if entry["Name"] == name
      return entry["Id"]
    end
  end
  #name not found
  return nil
end

.nova_aggregate_resources_idsObject



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/puppet/provider/nova.rb', line 161

def self.nova_aggregate_resources_ids
  #produce a list of hashes with Id=>Name pairs
  lines = []
  #run command
  cmd_output = auth_nova("aggregate-list")
  #parse output
  hash_list = cliout2list(cmd_output)
  #only interessted in Id and Name
  hash_list.map{ |e| e.delete("Availability Zone")}
  hash_list.map{ |e| e['Id'] = e['Id'].to_i}
return hash_list
end

.nova_confObject



25
26
27
28
29
30
# File 'lib/puppet/provider/nova.rb', line 25

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

.nova_credentialsObject



32
33
34
# File 'lib/puppet/provider/nova.rb', line 32

def self.nova_credentials
  @nova_credentials ||= get_nova_credentials
end

.resetObject



94
95
96
97
# File 'lib/puppet/provider/nova.rb', line 94

def self.reset
  @nova_conf = nil
  @nova_credentials = nil
end

.str2hash(s) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/puppet/provider/nova.rb', line 99

def self.str2hash(s)
  #parse string
  if s.include? "="
    k, v = s.split("=", 2)
    return {k.gsub(/'/, "") => v.gsub(/'/, "")}
  else
    return s.gsub(/'/, "")
  end
end

.str2list(s) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/puppet/provider/nova.rb', line 109

def self.str2list(s)
  #parse string
  if s.include? ","
    if s.include? "="
      new = {}
    else
      new = []
    end
    s.split(",").each do |el|
      ret = str2hash(el.strip())
      if s.include? "="
        new.update(ret)
      else
        new.push(ret)
      end
    end
    return new
  else
    return str2hash(s.strip())
  end
end

.withenv(hash, &block) ⇒ Object



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

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_nova(*args) ⇒ Object



90
91
92
# File 'lib/puppet/provider/nova.rb', line 90

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

#nova_credentialsObject



36
37
38
# File 'lib/puppet/provider/nova.rb', line 36

def nova_credentials
  self.class.nova_credentials
end