Class: Chef::Knife::ZcloudjpMachineStart

Inherits:
Chef::Knife show all
Includes:
ZcloudjpBase
Defined in:
lib/chef/knife/zcloudjp_machine_start.rb

Instance Method Summary collapse

Methods included from ZcloudjpBase

included

Instance Method Details

#check_current_state(connection, machine_uuid) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/knife/zcloudjp_machine_start.rb', line 50

def check_current_state(connection,machine_uuid)
  response = connection.get do |req|
    req.url "/machines/#{machine_uuid}.json"
    req.headers['Content-Type'] = 'application/json'
    req.headers['X-API-KEY'] = Chef::Config[:knife][:zcloudjp_api_token]
  end

  Chef::Log.debug(response.inspect)
  case response.status
  when 200
    # do nothing.
  when 404
    ui.warn("The machine #{config[:machine_uuid]} was not found.")
    exit
  else
    ui.fatal("Exit", "Unknown Error occured in API response.")
    exit
  end

  machine = JSON.parse(response.body, :symbolized_names =>true )
  machine['state']
end

#runObject



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chef/knife/zcloudjp_machine_start.rb', line 34

def run
  $stdout.sync = true

  unless config[:machine_uuid]
    ui.error("You have not provided a machine uuid. Please note the short option for this value is '-n'.")
    exit 1
  end
  body = Hash.new()

  Chef::Log.debug("Start machine.")
  Chef::Log.debug(body)
  

  locate_config_value(:zcloudjp_api_url)
  connection = Faraday.new(:url => locate_config_value(:zcloudjp_api_url), :ssl => {:verify => false}, :headers => {"User-Agent" => "Knife-Zcloudjp/#{::Knife::Zcloudjp::VERSION}"})
 
  def check_current_state(connection,machine_uuid)
    response = connection.get do |req|
      req.url "/machines/#{machine_uuid}.json"
      req.headers['Content-Type'] = 'application/json'
      req.headers['X-API-KEY'] = Chef::Config[:knife][:zcloudjp_api_token]
    end

    Chef::Log.debug(response.inspect)
    case response.status
    when 200
      # do nothing.
    when 404
      ui.warn("The machine #{config[:machine_uuid]} was not found.")
      exit
    else
      ui.fatal("Exit", "Unknown Error occured in API response.")
      exit
    end

    machine = JSON.parse(response.body, :symbolized_names =>true )
    machine['state']
  end

  ## Exit if already runnning.
  if check_current_state(connection, config[:machine_uuid]) == "running" then
    ui.info("The machine #{config[:machine_uuid]} is already running.")
    exit
  end

  # Start Machine
  response = connection.post do |req|
    req.url "/machines/#{config[:machine_uuid]}/start.json"
    req.headers['Content-Type'] = 'application/json'
    req.headers['X-API-KEY'] = Chef::Config[:knife][:zcloudjp_api_token]
    req.body = body.to_json
  end

  ## Wait
  ui.info("Waiting state of machine changed to running... (Timeout: #{config[:timeout]} seconds)")
  config[:timeout].to_i.times do |idx|
    if check_current_state(connection, config[:machine_uuid]) == "running" then
      break
    elsif (idx + 1) == config[:timeout].to_i
      ui.warn("Timed out. Please check later.")
      exit 1
    else
        sleep 1
    end
  end

  ## print current status
  response = connection.get do |req|
    req.url "/machines/#{config[:machine_uuid]}.json"
    req.headers['Content-Type'] = 'application/json'
    req.headers['X-API-KEY'] = Chef::Config[:knife][:zcloudjp_api_token]
    req.body = body.to_json
  end

  machine = JSON.parse(response.body, :symbolized_names =>true )

  msg_pair("ID", machine['id'])
  msg_pair("ip", machine['ips'].last)
  msg_pair("type", machine['type'])
  msg_pair("dataset", machine['dataset'])
  msg_pair("state", machine['state'])
end