class Chef::Knife::ScalewayServerPower

Public Instance Methods

run() click to toggle source
# File lib/chef/knife/scaleway_droplet_power.rb, line 32
def run
  $stdout.sync = true

  validate!

  unless locate_config_value(:action)
    ui.error('Action cannot be empty. => -a <action>')
    exit 1
  end

  unless locate_config_value(:id)
    ui.error('ID cannot be empty. => -I <id>')
    exit 1
  end

  case locate_config_value(:action)
  when /(on)/i
    poweron = true
    result = Scaleway::Server.action(locate_config_value(:id), 'poweron')
  when /(off)/i
    poweroff = true
    result = Scaleway::Server.action(locate_config_value(:id), 'poweroff')
  else
    ui.error 'Bad Action: Use on/off.'
    exit 1
  end

  unless result.task.description
    #ui.error JSON.parse(result)['message']
    exit 1
  end

  wait_for_status(result, status: 'running') if poweron
  wait_for_status(result, status: 'stopped') if poweroff
end