Sha256: 1b687e440281b99ade84b81fd20b2ec584ab7c20f995017df5ca4f9b34a4db94

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gogetit-0.2.1 lib/providers/lxd.rb