Class: Chef::Knife::JobStart

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/job_start.rb

Instance Method Summary collapse

Instance Method Details

#runObject



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
# File 'lib/chef/knife/job_start.rb', line 53

def run
  @node_names = []

  job_name = @name_args[0]
  if job_name.nil?
    ui.error "No job specified."
    show_usage
    exit 1
  end

  if config[:search]
    q = Chef::Search::Query.new
    @escaped_query = URI.escape(config[:search],
                               Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
    begin
      nodes = q.search(:node, @escaped_query).first
    rescue Net::HTTPServerException => e
      msg Chef::JSONCompat.from_json(e.response.body)['error'].first
      ui.error("knife search failed: #{msg}")
      exit 1
    end
    nodes.each { |node| @node_names << node.name }
  else
    @node_names = name_args[1,name_args.length-1]
  end

  if @node_names.empty?
    ui.error "No nodes to run job on. Specify nodes as arguments or use -s to specify a search query."
    exit 1
  end

  rest = Chef::REST.new(Chef::Config[:chef_server_url])

  job_json = {
    'command' => job_name,
    'nodes' => @node_names,
    'quorum' => get_quorum(config[:quorum], @node_names.length)
  }
  job_json['run_timeout'] = config[:run_timeout].to_i if config[:run_timeout]
  result = rest.post_rest('pushy/jobs', job_json)
  job_uri = result['uri']
  puts "Started.  Job ID: #{job_uri[-32,32]}"
  exit(0) if config[:nowait]
  previous_state = "Initialized."
  begin
    sleep(0.1)
    job = rest.get_rest(job_uri)
    finished, state = status_string(job)
    if state != previous_state
      puts state
      previous_state = state
    end
  end until finished

  output(job)

  exit(status_code(job))
end