Skip to content

Instantly share code, notes, and snippets.

@bgupta
Created October 24, 2011 23:43
Show Gist options
  • Save bgupta/1310758 to your computer and use it in GitHub Desktop.
Save bgupta/1310758 to your computer and use it in GitHub Desktop.
ssh_using_foreman
#!/usr/bin/env ruby
# Copyright (C) 2011 Vijay Brian Gupta brian.gupta@brandorr.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
# v0.1 May 27, 2011 - Supports --hostlist
# v0.9 Oct 7, 2011 - Supports filters, and SSL and basicauth, and url, user and password as options.
require "rubygems"
require "time"
require "openssl"
require "rest_client"
require "json"
require "socket"
require "open-uri"
require "optparse"
require "yaml"
class NilClass; def empty?; true; end; end
begin
require 'rubygems'
require 'net/ssh/multi'
rescue LoadError => detail
warn "failed to load ssh_multi library - try: gem install net-ssh-multi"
exit 1
end
options = {}
OptionParser.new do|opts|
opts.banner = "Usage: " + File.basename($0) + " [options] ..."
options[:debug] = false
opts.on( "-d", "--debug", "Output more information" ) do
options[:debug] = true
end
options[:command] = false
opts.on( "-c", "--command CMD", "command to execute" ) do|f|
options[:command] = f
end
options[:username] = false
opts.on( '-u', '--user USER', "Foreman user") do|f|
options[:username] = f
end
options[:password] = false
opts.on( "-p", "--pass PASSWORD", "Foreman password" ) do|f|
options[:password] = f
end
options[:server] = false
opts.on( "-s", "--server URL", "Foreman Server URL" ) do|f|
options[:server] = f
end
options[:sshuser] = false
opts.on( '-l', '--sshuser USER', "SSH user") do|f|
options[:sshuser] = f
end
options[:facts] = {}
opts.on( '-f', '--facts fact=x,fact=y..', "one or more facts to filter the host list") do|facts|
facts.split(",").each do |g|
name, value = g.split("=")
options[:facts].merge!({ name => value })
end
end
options[:klass] = false
opts.on( '-k', '--puppetclass CLASSA,CLASSB', "one or more classes to filter the host list") do|f|
options[:klass] = f.split(",")
end
options[:state] = "active"
opts.on( '-t', '--state STATE', "") do|f|
options[:state] = f
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
puts ""
puts " ENVIRONMENT VARIABLES:"
puts ""
puts " FOREMAN_SERVER Foreman Server URL"
puts " FOREMAN_USER Foreman user"
puts " FOREMAN_PASSWORD Foreman password"
puts ""
puts " CLI options take precendence over ENVIRONMENT VARIABLES"
puts ""
exit
end
end.parse!
puts "Use -h, --help for usage." if options.values.uniq == [false]
@foreman_user = ENV['FOREMAN_USER'] if ENV['FOREMAN_USER']
@foreman_pass = ENV['FOREMAN_PASSWORD'] if ENV['FOREMAN_PASSWORD']
@foreman_url = ENV['FOREMAN_SERVER'] if ENV['FOREMAN_SERVER']
@foreman_user = options.delete(:username) if options[:username]
@foreman_pass = options.delete(:password) if options[:password]
@foreman_url = options.delete(:server) if options[:server]
RestClient.log = 'stdout' if options.delete(:debug)
def get_collection(path)
response = RestClient::Request.new(:method => :get,
:url => @foreman_url + "/" + path.to_s,
:user => @foreman_user, :password => @foreman_pass,
:headers => { :accept => :json, :content_type => :json }).execute
results = JSON.parse(response.to_str)
end
def search_collection(path,search)
response = RestClient::Request.new(:method => :get,
:url => @foreman_url + "/" + path.to_s + "?search=" + search,
:user => @foreman_user, :password => @foreman_pass,
:headers => { :accept => :json, :content_type => :json }).execute
results = JSON.parse(response.to_str)
end
puts "About to execute: #{options[:command]}"
#query = {"state" => options[:state]}
#query.merge!({"fact" => options[:facts]}) unless options[:facts].empty?
#query.merge!({"class" => options[:klass]}) unless options[:klass].empty?
klasses = options[:klass]
facts = options[:facts]
querystring = ""
klasses.each { |x| querystring << "class = #{x} and " }
facts.each { |k,v| querystring << "facts.#{k} = \"#{v}\" and " }
querystring.chomp!(" and ")
puts querystring
#hosts = [ "metro.brandorr.com"]
#hosts = get_collection("hosts")
#query =
hosts = search_collection("hosts",CGI::escape(" " + querystring))
if hosts.nil?
warn "unable to find any hosts"
exit 1
end
# the rest is commented out while debugging the search query.
#puts "on the following #{hosts.size} hosts: #{hosts.join(" ")}"
#puts "ctrl-c to abort or any key to continue"
#$stdin.gets
#ssh_options = {:user => options[:user], :auth_methods => "publickey"}
#Net::SSH::Multi.start(:on_error => :warn) do |session|
# hosts.each { |s| session.use s, ssh_options }
# session.exec options[:command]
# session.loop
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment