Class: VagrantPlugins::OneAndOne::Action::PowerOff

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-oneandone/action/power_off.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ PowerOff

Returns a new instance of PowerOff.



7
8
9
10
11
# File 'lib/vagrant-oneandone/action/power_off.rb', line 7

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_1and1::action::power_off')
  @machine = env[:machine]
end

Instance Method Details

#call(env) ⇒ Object



13
14
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
# File 'lib/vagrant-oneandone/action/power_off.rb', line 13

def call(env)
  compute = env[:oneandone_compute]
  server = begin
             compute.servers.get(@machine.id)
           rescue
             nil
           end

  if server.nil?
    @logger.warn "Unable to find server #{env[:machine].id}..."
    env[:ui].warn(I18n.t('vagrant_1and1.errors.instance_not_found'))
    env[:machine].id = nil
  else
    @logger.info "Stopping server #{env[:machine].id}..."
    env[:ui].info(I18n.t('vagrant_1and1.stopping_server'))

    # if needed, wait until the is ready
    server.wait_for { ready? }

    if env[:force_halt]
      compute.change_status(server_id: server.id,
                            action: 'POWER_OFF', method: 'HARDWARE')
    else
      server.off
    end

    server.wait_for { ready? }

    env[:ui].info(I18n.t('vagrant_1and1.powered_off'))
  end

  @app.call(env)
end