module Chef::Knife::JobHelpers
Public Instance Methods
file_helper(file_name)
click to toggle source
# File lib/chef/knife/job_helpers.rb, line 114 def file_helper(file_name) if file_name.nil? ui.error "No file specified." show_usage exit 1 end contents = "" if File.exist?(file_name) File.open(file_name, "rb") do |file| contents = file.read end else ui.error "#{file_name} not found" exit 1 end contents end
get_env(config)
click to toggle source
# File lib/chef/knife/job_helpers.rb, line 132 def get_env(config) env = {} begin env = config[:with_env] ? JSON.parse(config[:with_env]) : {} rescue Exception => e Chef::Log.info("Can't parse environment as JSON") end end
get_quorum(quorum, total_nodes)
click to toggle source
# File lib/chef/knife/job_helpers.rb, line 68 def get_quorum(quorum, total_nodes) unless qmatch = /^(\d+)(\%?)$/.match(quorum) raise "Invalid Format please enter integer or percent" end num = qmatch[1] case qmatch[2] when "%" then ((num.to_f / 100) * total_nodes).ceil else num.to_i end end
process_search(search, nodes)
click to toggle source
# File lib/chef/knife/job_helpers.rb, line 23 def process_search(search, nodes) node_names = [] if search q = Chef::Search::Query.new escaped_query = Addressable::URI.encode_component(search, Addressable::URI::CharacterClasses::QUERY) begin nodes = q.search(:node, escaped_query).first rescue Net::HTTPClientException => 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 = nodes 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 node_names end
run_helper(config, job_json)
click to toggle source
# File lib/chef/knife/job_helpers.rb, line 93 def run_helper(config, job_json) 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(config[:poll_interval].to_f) putc(".") job = rest.get_rest(job_uri) finished, state = status_string(job) if state != previous_state puts state previous_state = state end end until finished job end
status_code(job)
click to toggle source
# File lib/chef/knife/job_helpers.rb, line 83 def status_code(job) if job["status"] == "complete" && job["nodes"].keys.all? do |key| key == "succeeded" || key == "nacked" || key == "unavailable" end 0 else 1 end end
status_string(job)
click to toggle source
# File lib/chef/knife/job_helpers.rb, line 48 def status_string(job) case job["status"] when "new" [false, "Initialized."] when "voting" [false, job["status"].capitalize + "."] else total = job["nodes"].values.inject(0) { |sum, nodes| sum + nodes.length } in_progress = job["nodes"].keys.inject(0) do |sum, status| nodes = job["nodes"][status] sum + (%w{new voting running}.include?(status) ? 1 : 0) end if job["status"] == "running" [false, job["status"].capitalize + " (#{in_progress}/#{total} in progress) ..."] else [true, job["status"].capitalize + "."] end end end