File: nova_manage.rb

package info (click to toggle)
puppet-module-nova 25.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,100 kB
  • sloc: ruby: 11,433; python: 38; sh: 10; makefile: 10
file content (145 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/nova')
require 'uri'

Puppet::Type.type(:nova_cell_v2).provide(
  :nova_manage,
  :parent => Puppet::Provider::Nova
) do

  desc "Manage nova cellv2 cells"

  mk_resource_methods

  def initialize(value={})
    super(value)
    @property_flush = {}
  end

  def self.instances
    cells_list = nova_manage_request("cell_v2", "list_cells", "--verbose") 

    cells_list.split("\n")[3..-2].collect do |cell|
      $name, $uuid, $transport_url, $database_connection = cell.split('|')[1..-1].map{ |x| x.strip}
      default_transport_url = defaults(is_cell0($uuid))[:transport_url]
      default_database_connection = defaults(is_cell0($uuid))[:database_connection]

      if $transport_url == default_transport_url
        $transport_url = 'default'
      end
      if $database_connection == default_database_connection
        $database_connection = 'default'
      end
      new(
        :name                => $name,
        :uuid                => $uuid,
        :transport_url       => $transport_url,
        :database_connection => $database_connection,
        :ensure              => :present
      )
    end
  end

  def self.prefetch(resources)
    instances.each do |prov|
      if resource = resources[prov.name]
        resource.provider = prov
      end
    end
  end

  @cell0_uuid = '00000000-0000-0000-0000-000000000000'

  def self.is_cell0(uuid)
    uuid == @cell0_uuid
  end

  def self.defaults(cell0=false)
    conf = nova_conf
    if cell0
      database_uri = URI.parse(conf['database']['connection'].strip)
      database_uri.path += '_cell0'
      {
        :transport_url       => 'none:///',
        :database_connection => database_uri.to_s
      }
    else
      {
        :transport_url       => conf['DEFAULT']['transport_url'].strip,
        :database_connection => conf['database']['connection'].strip,
      }
    end
  end

  def is_cell0?
    self.class.is_cell0(@property_hash[:uuid])
  end

  def exists?
    @property_hash[:ensure] == :present
  end

  def create
    optional_opts = []
    unless resource[:name] == 'None'
      optional_opts.push('--name').push(resource[:name])
    end
    {
      :transport_url       => '--transport-url',
      :database_connection => '--database_connection'
    }.each do |param, opt|
      if resource[param] and resource[param] != 'default'
        optional_opts.push(opt).push(resource[param])
      end
    end
    cell_uuid = nova_manage_request('cell_v2', 'create_cell',
      optional_opts, "--verbose"
    )
    @property_hash = {
      :uuid                => cell_uuid.strip(),
      :ensure              => :present,
      :transport_url       => resource[:transport_url],
      :database_connection => resource[:database_connection]
    }
  end

  def transport_url=(value)
    @property_flush[:transport_url] = value
  end

  def database_connection=(value)
    @property_flush[:database_connection] = value
  end

  def destroy
    @property_hash.clear
  end

  def flush
    @property_hash.update(@property_flush)
    if @property_flush[:ensure] == :absent
      nova_manage_request("cell_v2", "delete_cell", "--cell_uuid", @property_hash[:uuid])
    elsif @property_flush[:transport_url] or @property_flush[:database_connection]
      opts = []
      if not @property_flush[:transport_url]
        # Must pass this even when not changed or it will change to the value in nova.conf
        @property_flush[:transport_url] = @property_hash[:transport_url]
      end
      if @property_flush[:transport_url] == 'default'
        @property_flush[:transport_url] = self.class.defaults(is_cell0?)[:transport_url]
      end
      opts.push('--transport-url').push(@property_flush[:transport_url])

      if not @property_flush[:database_connection]
        # Must pass this even when not changed or it will change to the value in nova.conf
        @property_flush[:database_connection] = @property_hash[:database_connection]
      end
      if @property_flush[:database_connection] == 'default'
        @property_flush[:database_connection] = self.class.defaults(is_cell0?)[:database_connection]
      end
      opts.push('--database_connection').push(@property_flush[:database_connection])

      nova_manage_request("cell_v2", "update_cell", "--cell_uuid", @property_hash[:uuid], opts)
    end
    @property_flush = {}
  end
end