require 'hyperkit' require 'gogetit/util' module Gogetit class GogetLXD include Gogetit::Util attr_reader :config, :logger, :conn, :maas def initialize(conf, maas, logger) @config = conf @conn = Hyperkit::Client.new( api_endpoint: config[:lxd][:url], verify_ssl: false ) @maas = maas @logger = logger end def list logger.info("Calling <#{__method__.to_s}>") conn.containers end def container_exists?(name) logger.info("Calling <#{__method__.to_s}>") list.each do |c| return true if c == name end false end def get_state(name) conn.container(name)[:status] end def wait_until_state(name, state) logger.info("Calling <#{__method__.to_s}> for being #{state}..") until get_state(name) == state sleep 3 end end 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) logger.info("#{name} has been created.") true end 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 end end