Class: Chef::Provisioning::FogDriver::Providers::XenServer

Inherits:
Driver
  • Object
show all
Defined in:
lib/chef/provisioning/fog_driver/providers/xenserver.rb

Constant Summary

Constants inherited from Driver

Driver::DEFAULT_OPTIONS, Driver::RETRYABLE_ERRORS, Driver::RETRYABLE_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Driver

__new__, #allocate_machine, #allocate_machines, canonicalize_url, #compute, #compute_options, #connect_to_machine, #create_volume, #destroy_machine, #destroy_volume, from_provider, from_url, #image_for, inherited, #initialize, new, #provider, provider_class_for, #ready_machine, register_provider_class, #stop_machine, #transport_for

Constructor Details

This class inherits a constructor from Chef::Provisioning::FogDriver::Driver

Class Method Details

.compute_options_for(provider, id, config) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef/provisioning/fog_driver/providers/xenserver.rb', line 25

def self.compute_options_for(provider, id, config)
  new_compute_options = {}
  new_compute_options[:provider] = provider
  new_config = { driver_options: { compute_options: new_compute_options } }
  new_defaults = {
    driver_options: { compute_options: {} },
    machine_options: { bootstrap_options: { affinity: id } }
  }
  result = Cheffish::MergedConfig.new(new_config, config, new_defaults)

  new_compute_options[:xenserver_url] = id if id && id != ""
  credential = Fog.credentials

  new_compute_options[:xenserver_username] ||= credential[:xenserver_username]
  new_compute_options[:xenserver_password] ||= credential[:xenserver_password]
  new_compute_options[:xenserver_url] ||= credential[:xenserver_url]
  new_compute_options[:xenserver_timeout] ||= 300
  new_compute_options[:xenserver_redirect_to_master] ||= true

  id = result[:driver_options][:compute_options][:xenserver_url]

  [result, id]
end

Instance Method Details

#bootstrap_options_for(machine_spec, machine_options) ⇒ Object



13
14
15
16
17
# File 'lib/chef/provisioning/fog_driver/providers/xenserver.rb', line 13

def bootstrap_options_for(machine_spec, machine_options)
  bootstrap_options = super
  bootstrap_options[:tags] = bootstrap_options[:tags].map { |k, v| "#{k}: #{v}" }
  bootstrap_options
end

#converge_floating_ips(action_handler, machine_spec, machine_options, server) ⇒ Object



150
151
152
# File 'lib/chef/provisioning/fog_driver/providers/xenserver.rb', line 150

def converge_floating_ips(action_handler, machine_spec, machine_options, server)
  # XenServer does not have floating IPs
end

#create_many_servers(num_servers, bootstrap_options, parallelizer) ⇒ Object



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
# File 'lib/chef/provisioning/fog_driver/providers/xenserver.rb', line 70

def create_many_servers(num_servers, bootstrap_options, parallelizer)
  parallelizer.parallelize(1.upto(num_servers)) do |_i|
    compute.default_template = bootstrap_options[:template] if bootstrap_options[:template]
    raise "No server can be created without a template, please set a template name as bootstrap_options" unless compute.default_template
    server = compute.default_template.clone bootstrap_options[:name]

    if bootstrap_options[:affinity]
      host = compute.hosts.all.select { |h| h.address == bootstrap_options[:affinity] }.first
      unless host
        raise "Host with ID #{bootstrap_options[:affinity]} not found."
      end
      server.affinity = host.reference
    end

    unless bootstrap_options[:memory].nil?
      mem = (bootstrap_options[:memory].to_i * 1024 * 1024).to_s
      server.set_attribute "memory_limits", mem, mem, mem, mem
    end

    unless bootstrap_options[:cpus].nil?
      cpus = (bootstrap_options[:cpus]).to_s
      server.set_attribute "VCPUs_max", cpus
      server.set_attribute "VCPUs_at_startup", cpus
    end

    # network configuration through xenstore
    attrs = {}
    unless bootstrap_options[:network].nil?
      network = bootstrap_options[:network]
      net_names = network[:vifs]
      if net_names
        server.vifs.each(&:destroy)
        compute.networks.select { |net| Array(net_names).include? net.name }.each do |net|
          compute.vifs.create vm: server, network: net, device: "0"
        end
      end
      attrs["vm-data/ip"] = network[:vm_ip] if network[:vm_ip]
      attrs["vm-data/gw"] = network[:vm_gateway] if network[:vm_gateway]
      attrs["vm-data/nm"] = network[:vm_netmask] if network[:vm_netmask]
      attrs["vm-data/ns"] = network[:vm_dns] if network[:vm_dns]
      attrs["vm-data/dm"] = network[:vm_domain] if network[:vm_domain]
      server.set_attribute "xenstore_data", attrs unless attrs.empty?
    end

    userdevice = 1
    (bootstrap_options[:additional_disks] || {}).each do |name, data|
      sr_name = data[:sr]
      storage_repository = compute.storage_repositories.find { |sr| sr.name == sr_name }
      raise "You must specify sr name to add additional disks" unless storage_repository
      raise "You must specify size to add additional disk" unless data[:size]

      gb   = 1_073_741_824
      size = data[:size].to_i * gb

      vdi_params = { name: name }
      vdi_params[:storage_repository] = storage_repository
      vdi_params[:description] == data[:description] if data[:description]
      vdi_params[:virtual_size] = size.to_s
      vdi = compute.vdis.create vdi_params

      compute.vbds.create vm: server, vdi: vdi, userdevice: userdevice.to_s, bootable: false
      userdevice += 1
    end

    server.provision
    yield server if block_given?
    server
  end.to_a
end

#creatorObject



9
10
11
# File 'lib/chef/provisioning/fog_driver/providers/xenserver.rb', line 9

def creator
  compute_options[:xenserver_username]
end

#server_for(machine_spec) ⇒ Object



49
50
51
52
53
# File 'lib/chef/provisioning/fog_driver/providers/xenserver.rb', line 49

def server_for(machine_spec)
  if machine_spec.reference
    compute.servers.get(compute.get_by_uuid(machine_spec.reference["server_id"], "VM"))
  end
end

#servers_for(specs_and_options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chef/provisioning/fog_driver/providers/xenserver.rb', line 55

def servers_for(specs_and_options)
  result = {}
  specs_and_options.each do |machine_spec, _machine_options|
    if machine_spec.reference
      if machine_spec.reference["driver_url"] != driver_url
        raise "Switching a machine's driver from #{machine_spec.reference['driver_url']} to #{driver_url} for is not currently supported!  Use machine :destroy and then re-create the machine on the new driver."
      end
      result[machine_spec] = compute.servers.get(compute.get_by_uuid(machine_spec.reference["server_id"], "VM"))
    else
      result[machine_spec] = nil
    end
  end
  result
end

#ssh_options_for(_machine_spec, machine_options, _server) ⇒ Object



19
20
21
22
23
# File 'lib/chef/provisioning/fog_driver/providers/xenserver.rb', line 19

def ssh_options_for(_machine_spec, machine_options, _server)
  { auth_methods: ["password"],
    timeout: (machine_options[:ssh_timeout] || 600),
    password: machine_options[:ssh_password] }.merge(machine_options[:ssh_options] || {})
end

#start_server(action_handler, machine_spec, server) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/chef/provisioning/fog_driver/providers/xenserver.rb', line 140

def start_server(action_handler, machine_spec, server)
  if server.state == "Halted"
    action_handler.perform_action "start machine #{machine_spec.name} (#{server.id} on #{driver_url})" do
      server.start
      machine_spec.reference["started_at"] = Time.now.to_i
    end
    machine_spec.save(action_handler)
  end
end