Class: Gogetit::GogetLXD

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/providers/lxd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#get_gateway, #ping_available?, #recognize_env, #ssh_available?, #symbolize_keys

Constructor Details

#initialize(conf, maas, logger) ⇒ GogetLXD

Returns a new instance of GogetLXD.



10
11
12
13
14
15
16
17
18
# File 'lib/providers/lxd.rb', line 10

def initialize(conf, maas, logger)
  @config = conf
  @conn = Hyperkit::Client.new(
      api_endpoint: config[:lxd][:url],
      verify_ssl: false
    )
  @maas = maas
  @logger = logger
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/providers/lxd.rb', line 8

def config
  @config
end

#connObject (readonly)

Returns the value of attribute conn.



8
9
10
# File 'lib/providers/lxd.rb', line 8

def conn
  @conn
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/providers/lxd.rb', line 8

def logger
  @logger
end

#maasObject (readonly)

Returns the value of attribute maas.



8
9
10
# File 'lib/providers/lxd.rb', line 8

def maas
  @maas
end

Instance Method Details

#container_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/providers/lxd.rb', line 25

def container_exists?(name)
  logger.info("Calling <#{__method__.to_s}>")
  list.each do |c|
    return true if c == name
  end
  false
end

#create(name, args = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/providers/lxd.rb', line 57

def create(name, args = {})
  logger.info("Calling <#{__method__.to_s}>")
  if container_exists?(name) or maas.domain_name_exists?(name)
    puts "Container #{name} already exists!"
    return false
  end

  args[:alias] ||= config[:lxd][:default_alias]
  args[:profiles] ||= config[:lxd][:profiles]
  args[:sync] ||= true
  conn.create_container(name, args)
  conn.start_container(name, :sync=>"true")

  fqdn = name + '.' + maas.get_domain
  wait_until_available(fqdn)
  logger.info("#{name} has been created.")
  true
end

#destroy(name, args = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/providers/lxd.rb', line 76

def destroy(name, args = {})
  logger.info("Calling <#{__method__.to_s}>")
  args[:sync] ||= true
  if get_state(name) == 'Running'
    conn.stop_container(name, args)
  end
  wait_until_state(name, 'Stopped')
  conn.delete_container(name, args)
  maas.delete_dns_record(name)
  logger.info("#{name} has been destroyed.")
  true
end

#get_state(name) ⇒ Object



33
34
35
# File 'lib/providers/lxd.rb', line 33

def get_state(name)
  conn.container(name)[:status]
end

#listObject



20
21
22
23
# File 'lib/providers/lxd.rb', line 20

def list
  logger.info("Calling <#{__method__.to_s}>")
  conn.containers
end

#wait_until_available(fqdn) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/providers/lxd.rb', line 44

def wait_until_available(fqdn)
  until ping_available?(fqdn)
    logger.info("Calling <#{__method__.to_s}> for ping to be ready..")
    sleep 3
  end
  logger.info("#{fqdn} is now available to ping..")
  until ssh_available?(fqdn, 'ubuntu')
    logger.info("Calling <#{__method__.to_s}> for ssh to be ready..")
    sleep 3
  end
  logger.info("#{fqdn} is now available to ssh..")
end

#wait_until_state(name, state) ⇒ Object



37
38
39
40
41
42
# File 'lib/providers/lxd.rb', line 37

def wait_until_state(name, state)
  logger.info("Calling <#{__method__.to_s}> for being #{state}..")
  until get_state(name) == state
    sleep 3
  end
end