Commit f9bb3376 authored by Ulrich Dangel's avatar Ulrich Dangel

Imported Upstream version 0.4.2.2

parents
#!/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.
require "optparse"
collections_filterable = [
:architectures,
:common_parameters,
:config_templates,
:domains,
:environments,
:fact_values,
:hosts,
:hostgroups,
:media,
:puppetclasses,
:reports,
:roles,
:settings,
:lookup_keys,
:dashboard,
:operatingsystems,
:subnets,
:ptables,
:users
]
collections = [
:auth_source_ldaps,
:hypervisors,
:lookup_values,
:smart_proxies,
:statistics,
:usergroups,
]
collections_not_implemented = [ :audits, :bookmarks ]
@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']
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[: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[:json] = false
opts.on( "--json", "JSON output" ) do
options[:json] = true
end
options[:yaml] = false
opts.on( "--yaml", "YAML output" ) do
options[:yaml] = true
end
options[:status] = false
opts.on( "--status", "Foreman status" ) do
options[:status] = true
end
# options[:dashboard] = false
# opts.on( "--dashboard", "Foreman dashboard" ) do |f|
# options[:dashboard] = f
# end
options[:custom] = false
opts.on( "--custom COLLECTION", "Custom COLLECTION string, see: http://theforeman.org/projects/foreman/wiki/API" ) do |f|
options[:custom] = f
end
collections_filterable.each do |collection|
options[collection] = false
opts.on("--#{collection} [filter]", "Retrieve a list of #{collection}") do |f|
options[collection] = f || true
end
end
collections.each do |collection|
options[collection] = false
opts.on("--#{collection}", "Retrieve a list of #{collection}") do
options[collection] = true
end
end
collections_not_implemented.each do |collection|
options[collection] = false
opts.on("--#{collection}", "Not implemented") do
options[collection] = true
end
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 precedence over ENVIRONMENT VARIABLES"
puts ""
puts " FILTERS:"
puts ""
puts " Please see:"
puts " http://theforeman.org/projects/foreman/wiki/Search_API"
puts ""
puts " Examples:"
puts " --hosts \"domain = domain.com AND class = squid\""
puts " --hosts \"domain = domain.com AND facts.architecture = x86_64 AND \\"
puts " class = module::class"
puts " --classes \"name = squid\""
puts " --domains \"name = domain.com\""
puts ""
exit 1
end
end.parse!
if options.values.uniq == [false]
warn "Use -h, --help for usage."
exit 1
end
require "rubygems"
require "rest_client"
require "json"
require "yaml"
@foreman_user = options.delete(:username) if options[:username]
@foreman_pass = options.delete(:password) if options[:password]
@foreman_url = options.delete(:server) if options[:server]
@usejson = options.delete(:json) if options[:json]
@useyaml = options.delete(:yaml) if options[:yaml]
@custom = options.delete(:custom) if options[:custom]
@status = options.delete(:status) if options[:status]
RestClient.log = 'stdout' if options.delete(:debug)
def get_collection(path, options = {})
JSON.parse(RestClient::Request.new({:method => :get,
:url => "#{@foreman_url}/#{path.to_s.chomp('/')}",
:user => @foreman_user, :password => @foreman_pass,
:headers => { :accept => :json, :content_type => :json }}.merge(options)).execute.to_s)
end
def search_collection(path,search)
get_collection(path, :url => "#{@foreman_url}/#{path}?search=#{URI.encode(search)}")
end
def format
return "json" if @usejson
return "yaml" if @useyaml
end
def print_response response
puts(case format
when "json"
JSON.pretty_generate(response)
when "yaml"
YAML.dump(response)
else
r = response.first
if r.is_a?(Hash)
# we simply return host list
if r.first[0] == "host"
response.map{|h| h["host"]["name"]}
else
response.map{|o| o.inspect}
end
else
response.join("\n")
end
end)
end
options.each do |collection, filter|
next unless filter
if filter == true
print_response(get_collection(collection))
else
print_response(search_collection(collection, filter))
end
end
print_response(get_collection(@custom)) if @custom
puts JSON.pretty_generate(get_collection("status")) if @status
# -*- encoding: utf-8 -*-
$:.push('lib')
require "foremancli/version"
Gem::Specification.new do |s|
s.name = "foremancli"
s.version = Foremancli::VERSION.dup
s.date = "2012-05-01"
s.summary = "This is the CLI for Foreman, which is a provisioning tool and node classifier for sysadmins."
s.email = "brian.gupta@brandorr.com"
s.homepage = "https://github.com/ohadlevy/foreman/blob/master/extras/cli/foremancli"
s.authors = ['Brian Gupta']
s.description = <<-EOF
This is the CLI for Foreman, which is a provisioning tool and node classifier for sysadmins.
EOF
dependencies = [
[:runtime, "rest-client", "~> 1.4"],
[:runtime, "json", "~> 1.4"],
]
s.files = Dir['**/*']
s.test_files = Dir['test/**/*'] + Dir['spec/**/*']
s.executables = Dir['bin/*'].map { |f| File.basename(f) }
s.require_paths = ["lib"]
## Make sure you can build the gem on older versions of RubyGems too:
s.rubygems_version = "1.3.7"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.specification_version = 3 if s.respond_to? :specification_version
dependencies.each do |type, name, version|
if s.respond_to?("add_#{type}_dependency")
s.send("add_#{type}_dependency", name, version)
else
s.add_dependency(name, version)
end
end
end
module Foremancli
VERSION = '0.4.2.2'
end
--- !ruby/object:Gem::Specification
name: foremancli
version: !ruby/object:Gem::Version
hash: 99
prerelease:
segments:
- 0
- 4
- 2
- 2
version: 0.4.2.2
platform: ruby
authors:
- Brian Gupta
autorequire:
bindir: bin
cert_chain: []
date: 2012-05-01 00:00:00 Z
dependencies:
- !ruby/object:Gem::Dependency
name: rest-client
prerelease: false
requirement: &id001 !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
hash: 7
segments:
- 1
- 4
version: "1.4"
type: :runtime
version_requirements: *id001
- !ruby/object:Gem::Dependency
name: json
prerelease: false
requirement: &id002 !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
hash: 7
segments:
- 1
- 4
version: "1.4"
type: :runtime
version_requirements: *id002
description: |
This is the CLI for Foreman, which is a provisioning tool and node classifier for sysadmins.
email: brian.gupta@brandorr.com
executables:
- foremancli
extensions: []
extra_rdoc_files: []
files:
- bin/foremancli
- foremancli-0.4.1.gem
- foremancli-0.4.2.1.gem
- foremancli.gemspec
- lib/foremancli/version.rb
homepage: https://github.com/ohadlevy/foreman/blob/master/extras/cli/foremancli
licenses: []
post_install_message:
rdoc_options: []
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ">="
- !ruby/object:Gem::Version
hash: 3
segments:
- 0
version: "0"
required_rubygems_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ">="
- !ruby/object:Gem::Version
hash: 3
segments:
- 0
version: "0"
requirements: []
rubyforge_project:
rubygems_version: 1.8.24
signing_key:
specification_version: 3
summary: This is the CLI for Foreman, which is a provisioning tool and node classifier for sysadmins.
test_files: []
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment