Class: VagrantPlugins::ProfitBricks::Action::Stop

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-profitbricks/action/stop_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ Stop

Returns a new instance of Stop.



7
8
9
10
# File 'lib/vagrant-profitbricks/action/stop_server.rb', line 7

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

Instance Method Details

#call(env) ⇒ Object



12
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
46
47
# File 'lib/vagrant-profitbricks/action/stop_server.rb', line 12

def call(env)
  compute = env[:profitbricks_compute]
  server_id = env[:machine].id
  datacenter_id = env[:machine].provider_config.datacenter_id

  server = begin
             compute.servers.get(datacenter_id, server_id)
           rescue
             nil
           end

  if server.nil?
    @logger.warn "Unable to find server #{server_id}..."
    env[:ui].warn(I18n.t('vagrant_profitbricks.errors.server_not_found'))
    env[:machine].id = nil
  else
    @logger.info "Stopping server #{server_id}..."
    env[:ui].info(I18n.t('vagrant_profitbricks.stopping_server'))
  end
  stopped = begin
    server.stop
  rescue
    false
  end
  if stopped
    30.times do |_t|
      begin
        s = compute.servers.get(datacenter_id, server_id)
        break if s.vm_state == 'SHUTOFF'
        sleep 10
      end
    end
  end

  @app.call(env)
end