-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from chef/ma/push-2x
Ma/push 2x
- Loading branch information
Showing
6 changed files
with
297 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,11 @@ Gem::Specification.new do |s| | |
s.platform = Gem::Platform::RUBY | ||
s.has_rdoc = true | ||
s.extra_rdoc_files = ["README.rdoc", "LICENSE"] | ||
s.summary = "Knife plugin for OPC push" | ||
s.summary = "Knife plugin for chef push" | ||
s.description = s.summary | ||
s.author = "John Keiser" | ||
s.email = "[email protected]" | ||
s.homepage = "http://www.opscode.com" | ||
s.homepage = "http://www.chef.io" | ||
|
||
# We need a more recent version of mixlib-cli in order to support --no- options. | ||
# ... but, we can live with those options not working, if it means the plugin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
# @copyright Copyright 2015 Chef Software, Inc. All Rights Reserved. | ||
# | ||
# This file is provided to you under the Apache License, | ||
# Version 2.0 (the "License"); you may not use this file | ||
# except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
|
||
class Chef | ||
class Knife | ||
module JobHelpers | ||
def self.process_search(search, nodes) | ||
node_names = [] | ||
if search | ||
q = Chef::Search::Query.new | ||
escaped_query = URI.escape(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 = 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 | ||
|
||
return node_names | ||
end | ||
|
||
def self.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) { |sum,status| | ||
nodes = job['nodes'][status] | ||
sum + (%w(new voting running).include?(status) ? 1 : 0) | ||
} | ||
if job['status'] == 'running' | ||
[false, job['status'].capitalize + " (#{in_progress}/#{total} in progress) ..."] | ||
else | ||
[true, job['status'].capitalize + '.'] | ||
end | ||
end | ||
end | ||
|
||
def self.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 | ||
|
||
def self.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 | ||
|
||
def self.run_helper(config, job_json) | ||
job_json['run_timeout'] ||= config[:run_timeout].to_i if config[:run_timeout] | ||
|
||
rest = Chef::REST.new(Chef::Config[:chef_server_url]) | ||
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 = JobHelpers.status_string(job) | ||
if state != previous_state | ||
puts state | ||
previous_state = state | ||
end | ||
end until finished | ||
job | ||
end | ||
|
||
def self.file_helper(file_name) | ||
if file_name.nil? | ||
ui.error "No file specified." | ||
show_usage | ||
exit 1 | ||
end | ||
contents = "" | ||
if File.exists?(file_name) | ||
File.open(file_name, "rb") do |file| | ||
contents = file.read | ||
end | ||
else | ||
ui.error "#{file_name} not found" | ||
exit 1 | ||
end | ||
return contents | ||
end | ||
|
||
def self.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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# @copyright Copyright 2014 Chef Software, Inc. All Rights Reserved. | ||
# | ||
# This file is provided to you under the Apache License, | ||
# Version 2.0 (the "License"); you may not use this file | ||
# except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
|
||
class Chef | ||
class Knife | ||
class JobOutput < Chef::Knife | ||
banner "knife job output <job id> <node> [<node> ...]" | ||
|
||
option :channel, | ||
:long => '--channel stdout|stderr', | ||
:default => 'stdout', | ||
:description => "Which output channel to fetch (default stdout)." | ||
|
||
def run | ||
rest = Chef::REST.new(Chef::Config[:chef_server_url]) | ||
|
||
job_id = name_args[0] | ||
channel = get_channel(config[:channel]) | ||
node = name_args[1] | ||
|
||
uri = "pushy/jobs/#{job_id}/output/#{node}/#{channel}" | ||
|
||
job = rest.get_rest(uri, false, {"Accept"=>"application/octet-stream"}) | ||
|
||
output(job) | ||
end | ||
|
||
def get_channel(channel) | ||
channel = channel || "stdout" | ||
case channel | ||
when "stdout" | ||
return channel | ||
when "stderr" | ||
return channel | ||
else | ||
raise "Invalid Format please enter stdout or stderr" | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
Oops, something went wrong.