Class: VagrantPlugins::OpenStack::Action::CreateOrchestrationStack

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-openstack-plugin/action/create_orchestration_stack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateOrchestrationStack



9
10
11
12
13
# File 'lib/vagrant-openstack-plugin/action/create_orchestration_stack.rb', line 9

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new(
    "vagrant_openstack::action::create_orchestration_stack")
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
87
88
89
90
91
92
93
# File 'lib/vagrant-openstack-plugin/action/create_orchestration_stack.rb', line 15

def call(env)
  # Get the config.
  config = env[:machine].provider_config

  # Are we going to handle orchestration stacks?
  if not config.orchestration_stack_name
    return @app.call(env)
  end

  # Create new fog orchestration service.
  env[:openstack_orchestration] = Fog::Orchestration.new(
    env[:fog_openstack_params])

  # Check if stack is already created.
  env[:openstack_orchestration].list_stacks.body['stacks'].each do |stack|
    if config.orchestration_stack_name == stack['stack_name']
      return @app.call(env)
    end
  end

  # To avoid confusion, only one source for orchestration template
  # should be set.
  if [config.orchestration_cfn_template,
      config.orchestration_cfn_template_file,
      config.orchestration_cfn_template_url].count(nil) != 2
    raise Errors::OrchestrationTemplateError,
      :err => 'One source for orchestration template should be specified.'
  end

  # Prepare parameters for new orchestration stack.
  # TODO: configurable parameters
  stack_params = {
    :disable_rollback   => false,
    :timeout_in_minutes => 5,
  }

  # Set template source.
  if config.orchestration_cfn_template
    stack_params[:template] = config.orchestration_cfn_template
  elsif config.orchestration_cfn_template_file
    if not File.exist?(config.orchestration_cfn_template_file)
      raise Errors::OrchestrationNoTemplateFileError,
        :fname => config.orchestration_cfn_template_file
    end

    # Load template file content. Newlines can cause parse error of
    # input JSON string.
    stack_params[:template] = ''
    File.open(config.orchestration_cfn_template_file) { |file|
      file.each_line do |line|
        stack_params[:template] << line
      end
    }
  else
    stack_params[:template_url] = config.orchestration_cfn_template_url
  end

  # Set template parameters.
  stack_params[:parameters] = config.orchestration_cfn_template_parameters

  # Create new stack.
  env[:ui].info(I18n.t("vagrant_openstack.creating_orchestration_stack"))
  stack = env[:openstack_orchestration].create_stack(
    config.orchestration_stack_name, stack_params)

  # Write UUID of newly created stack into file for later use (stack removal).
  created_stacks_fname = env[:machine].data_dir + 'orchestration_stacks'
  message = 'Saving information about created orchestration stack '
  message << "#{config.orchestration_stack_name}, "
  message << "UUID=#{stack.body['stack']['id']} "
  message << "to file #{created_stacks_fname}."
  @logger.info(message)

  File.open(created_stacks_fname, 'a') do |file|
    file.puts stack.body['stack']['id']
  end

  @app.call(env)
end