Module: Chef::Knife::JobHelpers

Included in:
JobStart
Defined in:
lib/chef/knife/job_helpers.rb

Instance Method Summary collapse

Instance Method Details

#file_helper(file_name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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) ⇒ Object



132
133
134
135
136
137
138
139
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object



83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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