Class: VagrantPlugins::Rimu::Actions::Create

Inherits:
AbstractAction show all
Includes:
Vagrant::Util::Retryable, SshUtils
Defined in:
lib/vagrant-rimu/actions/create.rb

Instance Method Summary collapse

Methods included from SshUtils

#public_key, #upload_key

Methods inherited from AbstractAction

#call

Constructor Details

#initialize(app, env) ⇒ Create

Returns a new instance of Create.



14
15
16
17
18
# File 'lib/vagrant-rimu/actions/create.rb', line 14

def initialize(app, env)
  @app = app
  @machine = env[:machine]
  @logger = Log4r::Logger.new('vagrant::rimu::create')
end

Instance Method Details

#execute(env) ⇒ Object

rubocop:disable Metrics/AbcSize



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
# File 'lib/vagrant-rimu/actions/create.rb', line 21

def execute(env)
  client = env[:rimu_api]
  env[:ui].info I18n.t('vagrant_rimu.creating')
  params = {
    :billing_oid => @machine.provider_config.billing_id,
    :dc_location => @machine.provider_config.data_centre,
    :host_server_oid => @machine.provider_config.host_server_id,
    :instantiation_options => {
        :domain_name => @machine.provider_config.host_name,
        :password => @machine.provider_config.root_password,
        :distro => @machine.provider_config.distro_code,
        :control_panel => @machine.provider_config.control_panel,
    },
    :instantiation_via_clone_options => {
        :domain_name => @machine.provider_config.host_name,
        :vps_order_oid_to_clone => @machine.provider_config.vps_to_clone,
    },
    :ip_request => {
        :extra_ip_reason => @machine.provider_config.extra_ip_reason,
        :num_ips => @machine.provider_config.num_ips,
        :requested_ips => @machine.provider_config.private_ips,
    },
    :is_just_minimal_init => @machine.provider_config.minimal_init,
    :vps_parameters => {
        :disk_space_mb => @machine.provider_config.disk_space_mb,
        :memory_mb => @machine.provider_config.memory_mb,
        :disk_space_2_mb => @machine.provider_config.disk_space_2_mb,
    },
    :vps_type => @machine.provider_config.vps_type,
  }
  params.delete(:instantiation_via_clone_options) if @machine.provider_config.vps_to_clone.nil?
  params.delete(:instantiation_options) if params.has_key?(:instantiation_via_clone_options)
  if @machine.provider_config.root_password
    root_pass = @machine.provider_config.root_password
  else
    root_pass = Digest::SHA2.new.update(@machine.provider_config.api_key).to_s
  end
  if params.has_key?(:instantiation_options)
    params[:instantiation_options][:password] = root_pass
  end

  begin
    result = client.servers.create(params)
  rescue ::Rimu::RimuAPI::RimuRequestError, ::Rimu::RimuAPI::RimuResponseError => e
    raise Errors::ApiError, {:stderr=>e}
  end

  @machine.id = result.order_oid
  env[:ui].info I18n.t('vagrant_rimu.ip_address', {:ip => result.allocated_ips["primary_ip"]})

  switch_user = @machine.provider_config.setup?
  user = @machine.config.ssh.username
  if switch_user
    @machine.config.ssh.username = 'root'
    @machine.config.ssh.password = root_pass
  end
  if params.has_key?(:instantiation_options)
    retryable(:tries => 120, :sleep => 10) do
      next if env[:interrupted]
      raise 'not ready' unless @machine.communicate.ready?
    end
  end

  # upload root ssh key
  upload_key(env)

  @machine.config.ssh.username = user

  @app.call(env)
end

#recover(env) ⇒ Object

rubocop:enable Metrics/AbcSize



93
94
95
96
97
98
# File 'lib/vagrant-rimu/actions/create.rb', line 93

def recover(env)
  return if env['vagrant.error'].is_a?(Vagrant::Errors::VagrantError)
  if @machine.state.id != :not_created
    terminate(env)
  end
end

#terminate(env) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/vagrant-rimu/actions/create.rb', line 100

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