Class: VagrantPlugins::Scaleway::Action::CreateServer

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-scaleway/action/create_server.rb

Overview

This creates the configured server.

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ CreateServer



11
12
13
14
# File 'lib/vagrant-scaleway/action/create_server.rb', line 11

def initialize(app, _env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_scaleway::action::create_server')
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-scaleway/action/create_server.rb', line 16

def call(env)
  # Initialize metrics if they haven't been
  env[:metrics] ||= {}

  config = env[:machine].provider_config

  bootscript      = config.bootscript
  commercial_type = config.commercial_type
  image           = config.image
  name            = config.name
  security_group  = config.security_group
  tags            = config.tags
  volumes         = config.volumes.map.with_index(1).to_h.invert

  env[:ui].info(I18n.t('vagrant_scaleway.creating_server'))
  env[:ui].info(" -- Bootscript: #{bootscript}") if bootscript
  env[:ui].info(" -- Commercial Type: #{commercial_type}")
  env[:ui].info(" -- Image: #{image}")
  env[:ui].info(" -- Name: #{name}")
  env[:ui].info(" -- Security Group: #{security_group}") if security_group
  env[:ui].info(" -- Tags: #{tags}") unless tags.empty?
  env[:ui].info(" -- Volumes: #{volumes}") unless volumes.empty?

  options = {
    name:            name,
    image:           image,
    volumes:         volumes,
    commercial_type: commercial_type,
    tags:            tags
  }

  options[:bootscript] = bootscript if bootscript
  options[:security_group] = security_group if security_group

  begin
    server = env[:scaleway_compute].servers.create(options)
  rescue Fog::Scaleway::Compute::Error => e
    raise Errors::FogError, message: e.message
  rescue Excon::Errors::HTTPStatusError => e
    raise Errors::InternalFogError,
          error: e.message,
          response: e.response.body
  end

  @logger.info("Machine '#{name}' created.")

  # Immediately save the ID since it is created at this point.
  env[:machine].id = server.id

  # destroy the server if we were interrupted
  destroy(env) if env[:interrupted]

  @app.call(env)
end

#destroy(env) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/vagrant-scaleway/action/create_server.rb', line 77

def destroy(env)
  destroy_env = env.dup
  destroy_env.delete(:interrupted)
  destroy_env[:config_validate] = false
  destroy_env[:force_confirm_destroy] = true
  env[:action_runner].run(Action.action_destroy, destroy_env)
end

#recover(env) ⇒ Object



71
72
73
74
75
# File 'lib/vagrant-scaleway/action/create_server.rb', line 71

def recover(env)
  return if env['vagrant.error'].is_a?(Vagrant::Errors::VagrantError)

  destroy(env) if env[:machine].provider.state.id != :not_created
end