Class: Kitchen::Driver::Lxd

Inherits:
Base
  • Object
show all
Includes:
HostLocator
Defined in:
lib/kitchen/driver/lxd.rb,
lib/kitchen/driver/lxd/host_locator.rb

Defined Under Namespace

Modules: HostLocator

Instance Method Summary collapse

Methods included from HostLocator

#can_rest?, #driver, #host_address, #nx_driver, #nx_transport

Constructor Details

#initialize(config = {}) ⇒ Lxd



22
23
24
25
# File 'lib/kitchen/driver/lxd.rb', line 22

def initialize(config = {})
  # pp 'Config:', config
  super
end

Instance Method Details

#create(state) ⇒ Object



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
# File 'lib/kitchen/driver/lxd.rb', line 35

def create(state)
  state[:config] = config.select { |k, _| [:server, :port, :rest_options, :image_server].include? k }
  info "Utilizing REST interface at " + host_address if respond_to?(:info) && can_rest?

  state[:username] = config[:username] if config.key? :username
  state[:container_name] = new_container_name unless state[:container_name]

  # TODO: convergent behavior on container_options change? (:profiles :config)
  state[:container_options] = container_options

  info "Container name: #{state[:container_name]}"
  driver.create_container(state[:container_name], state[:container_options])

  # Allow SSH transport on known images with sshd enabled
  # This will only work if the container is routable.  LXD does not do port forwarding (yet)
  # Which also means that you might need to do 'ssh_login: false' in the config if you're using a cloud-image and aren't routable
  # think ahead for default behaviour once LXD can port forward
  # FUTURE: If I get time I'll look into faking a port forward with something under /dev/ until then
  info "Waiting for an IP address..."
  state[:ip_address] = state[:hostname] = container_ip(state)
  if use_ssh?
    # Normalize [:ssh_login]
    config[:ssh_login] = { username: config[:ssh_login] } if config[:ssh_login].is_a? String
    config[:ssh_login] ||= {} # if config[:ssh_login] && !config.to_hash[:ssh_login].is_a?(Hash)

    state[:username] = config[:ssh_login][:username] if config[:ssh_login].key? :username
    state[:username] ||= "root"

    if (state[:username] != "root") && cloud_image?
      info "Waiting for cloud-init..."
      driver.wait_for state[:container_name], :cloud_init
    end

    setup_ssh(state[:username], config[:ssh_login][:public_key] || "#{ENV['HOME']}/.ssh/id_rsa.pub", state)
    info "SSH access enabled on #{state[:ip_address]}"
  else
    # TODO: this section is only for the base images on linuxcontainers.org... (and I still need to account for yum)
    # they need patched because they don't have wget, or anything else with which to download the chef client
    # Custom images should account for this, so I won't run this patch for them (in the name of testing speed)
    unless cloud_image?
      transport = nx_transport(state)
      transport.reset_user
      # only centos/7 and various ubuntu versions have been tested here
      #   - ubuntu non-cloud has no download utilities in order to dl the chef package so we must adapt that
      #   - centos/7 needs sudo installed, or you need to use sudo:false on the provisioner...  leaving it explicit for the user to fix
      unless transport.execute("test -d /etc/apt").error?
        info "Installing additional dependencies..."
        transport.execute("apt-get install openssl curl ca-certificates -y").error!
      end
    end
  end
end

#destroy(state) ⇒ Object



94
95
96
97
# File 'lib/kitchen/driver/lxd.rb', line 94

def destroy(state)
  info "Destroying container #{state[:container_name]}"
  driver.delete_container state[:container_name]
end

#finalize_config!(instance) ⇒ Object



88
89
90
91
92
# File 'lib/kitchen/driver/lxd.rb', line 88

def finalize_config!(instance)
  super.tap do
    instance.transport = Kitchen::Transport::Lxd.new config unless lxd_transport_selected? || use_ssh?
  end
end