class VagrantPlugins::Openstack::Action::CreateStack

Public Class Methods

new(app, _env) click to toggle source
# File lib/vagrant-openstack-provider/action/create_stack.rb, line 18
def initialize(app, _env)
  @app = app
  @logger = Log4r::Logger.new('vagrant_openstack::action::create_stack')
end

Public Instance Methods

execute(env) click to toggle source
# File lib/vagrant-openstack-provider/action/create_stack.rb, line 23
def execute(env)
  if @@is_created
    @app.call(env)
    return
  end

  @logger.info 'Start create stacks action'

  config = env[:machine].provider_config

  heat = env[:openstack_client].heat

  config.stacks.each do |stack|
    env[:ui].info(I18n.t('vagrant_openstack.create_stack'))
    env[:ui].info(" -- Stack Name : #{stack[:name]}")
    env[:ui].info(" -- Template   : #{stack[:template]}")

    create_opts = {
      name: stack[:name],
      template: YAML.load_file(stack[:template])
    }

    stack_id = heat.create_stack(env, create_opts)

    file_path = "#{env[:machine].data_dir}/stack_#{stack[:name]}_id"
    File.write(file_path, stack_id)

    waiting_for_stack_to_be_created(env, stack[:name], stack_id)
  end unless config.stacks.nil?

  @@is_created = true
  @app.call(env)
end

Private Instance Methods

waiting_for_stack_to_be_created(env, stack_name, stack_id, retry_interval = 3) click to toggle source
# File lib/vagrant-openstack-provider/action/create_stack.rb, line 59
def waiting_for_stack_to_be_created(env, stack_name, stack_id, retry_interval = 3)
  @logger.info "Waiting for the stack with id #{stack_id} to be built..."
  env[:ui].info(I18n.t('vagrant_openstack.waiting_for_stack'))
  config = env[:machine].provider_config
  Timeout.timeout(config.stack_create_timeout, Errors::Timeout) do
    stack_status = 'CREATE_IN_PROGRESS'
    until stack_status == 'CREATE_COMPLETE'
      @logger.debug('Waiting for stack to be CREATED')
      stack_status = env[:openstack_client].heat.get_stack_details(env, stack_name, stack_id)['stack_status']
      fail Errors::StackStatusError, stack: stack_id if stack_status == 'CREATE_FAILED'
      sleep retry_interval
    end
  end
end