Skip to content

Commit

Permalink
+ hosts export w/ organization
Browse files Browse the repository at this point in the history
+ export / import puppet facts
+ added examples dir
+ subscriptions can have optional Organization column to be filled w/ --organization
+ content-view-filters export date based errata
+ read from http/file/stdin
+ removed gettext from gemspec (prevented installation)
  • Loading branch information
Tom McKay committed Sep 4, 2015
1 parent 3036db5 commit 6f221b5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 34 deletions.
3 changes: 3 additions & 0 deletions examples/RHEL_Releases/subscriptions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name,Count,Organization,Manifest File,Content Set,Arch,Release
Red Hat Enterprise Linux Server,1,,,Red Hat Enterprise Linux 7 Server (RPMs),x86_64,7Server
Red Hat Enterprise Linux Server,1,,,Red Hat Enterprise Linux 6 Server (RPMs),x86_64,6Server
5 changes: 2 additions & 3 deletions hammer_cli_csv.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Gem::Specification.new do |spec|

spec.name = "hammer_cli_csv"
spec.version = HammerCLICsv.version
spec.authors = ["Tom McKay"]
spec.authors = ["Thomas McKay"]
spec.email = ["[email protected]"]
spec.homepage = "http://github.com/Katello/hammer-cli-csv"
spec.license = "GPL-2"
spec.license = "GPL-3"

spec.platform = Gem::Platform::RUBY
spec.summary = "CSV commands for Hammer"
Expand All @@ -20,7 +20,6 @@ Gem::Specification.new do |spec|
spec.require_paths = %w(lib)

spec.add_dependency('hammer_cli_katello')
spec.add_dependency("gettext", "~> 2.0")
spec.add_development_dependency("rubocop", "0.24.1")

end
5 changes: 3 additions & 2 deletions lib/hammer_cli_csv/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'apipie-bindings'
require 'hammer_cli'
require 'json'
require 'open-uri'
require 'csv'
require 'hammer_cli_csv/csv'

Expand Down Expand Up @@ -105,11 +106,11 @@ def labelize(name)
def thread_import(return_headers = false, filename=nil, name_column=nil)
filename ||= option_file || '/dev/stdin'
csv = []
CSV.foreach(filename, {
CSV.new(open(filename), {
:skip_blanks => true,
:headers => :first_row,
:return_headers => return_headers
}) do |line|
}).each do |line|
csv << line
end
lines_per_thread = csv.length / option_threads.to_i + 1
Expand Down
10 changes: 9 additions & 1 deletion lib/hammer_cli_csv/content_view_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ def export
end
rules.delete!("\n")
when /erratum/
rules = CSV.generate do |column|
rule = filter['rules'][0]
conditions = []
conditions << "start = #{DateTime.parse(rule['start_date']).strftime('%F')}" if rule['start_date']
conditions << "end = #{DateTime.parse(rule['end_date']).strftime('%F')}" if rule['end_date']
conditions += rule['types']
column << conditions
end
rules.delete!("\n")
when /package_group/
else
raise "Unknown filter rule type '#{filter['type']}'"
end
#puts "#{filter['type']} -> #{rule}"

name = contentview['name']
repositories = export_column(filter, 'repositories', 'name')
Expand Down
6 changes: 5 additions & 1 deletion lib/hammer_cli_csv/hosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class HostsCommand < BaseCommand
def export
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
csv << [NAME, COUNT, ORGANIZATION, LOCATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE, MACADDRESS, DOMAIN, PARTITIONTABLE]
@api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
search_options = {:per_page => 999999}
search_options['search'] = "organization = #{option_organization}" if option_organization
@api.resource(:hosts).call(:index, search_options)['results'].each do |host|
host = @api.resource(:hosts).call(:show, {'id' => host['id']})
raise "Host 'id=#{host['id']}' not found" if !host || host.empty?

Expand Down Expand Up @@ -47,6 +49,8 @@ def import
end

def create_hosts_from_csv(line)
return if option_organization && line[ORGANIZATION] != option_organization

line[COUNT].to_i.times do |number|
name = namify(line[NAME], number)
if !@existing.include? name
Expand Down
61 changes: 34 additions & 27 deletions lib/hammer_cli_csv/puppet_facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,57 @@ class PuppetFactsCommand < BaseCommand
command_name 'puppet-facts'
desc 'import or export puppet facts'

ORGANIZATION = 'Organization'
FACTS = 'Puppet Facts'

SEPARATOR = ' = '

def export
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
headers = [NAME, COUNT]
# Extracted facts are always based upon the first host found, otherwise this would be an intensive
# method to gather all the possible column names
any_host = @api.resource(:hosts).call(:index, {:per_page => 1})['results'][0]
headers += @api.resource(:puppetfactss).call(:index, {
'host_id' => any_host['name'],
'per_page' => 999999
})['results'][any_host['name']].keys
csv << headers
csv << [NAME, COUNT, ORGANIZATION, FACTS]

@api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
line = [host['name'], 1]
facts = @api.resource(:puppetfactss).call(:index, {'host_id' => host['name'], 'per_page' => 999999})[host['name']]
search_options = {:per_page => 999999}
search_options['search'] = "organization = #{option_organization}" if option_organization
@api.resource(:hosts).call(:index, search_options)['results'].each do |host|
facts = @api.resource(:fact_values).call(:index, {
'search' => "host = #{host['name']}",
'per_page' => 999999
})['results']
facts = @api.resource(:fact_values).call(:index, {
'search' => "host = #{host['name']}",
'per_page' => 999999
})['results'][host['name']]
facts ||= {}
headers[2..-1].each do |fact_name|
line << facts[fact_name] || ''

values = CSV.generate do |column|
column << facts.collect do |fact_name, fact_value|
"#{fact_name}#{SEPARATOR}#{fact_value}"
end
end
csv << line
values.delete!("\n")

csv << [host['name'], 1, host['organization_name'], values]
end
end
end

def import
@headers = nil

thread_import(true) do |line|
create_puppetfacts_from_csv(line)
end
end

def create_puppetfacts_from_csv(line)
if @headers.nil?
@headers = line
return
end
return if option_organization && line[ORGANIZATION] != option_organization

line[COUNT].to_i.times do |number|
name = namify(line[NAME], number)
print "Updating puppetfacts '#{name}'..." if option_verbose?
facts = line.to_hash
facts.delete(NAME)
facts.delete(COUNT)
facts = {}
collect_column(line[FACTS]) do |fact|
(fact_name, fact_value) = fact.split(SEPARATOR)
facts[fact_name] = fact_value
end

# Namify the values if the host name was namified
if name != line[NAME]
Expand All @@ -57,9 +64,9 @@ def create_puppetfacts_from_csv(line)
end

@api.resource(:hosts).call(:facts, {
'name' => name,
'facts' => facts
})
'name' => name,
'facts' => facts
})
print "done\n" if option_verbose?
end
rescue RuntimeError => e
Expand Down
3 changes: 3 additions & 0 deletions lib/hammer_cli_csv/subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def import
end

def enable_products_from_csv(line)
organization = line[ORGANIZATION] || option_organization
raise "Organization is required in either input CSV or by option --organization" if organization.nil? || organization.empty?
line[ORGANIZATION] = organization
return if option_organization && line[ORGANIZATION] != option_organization

results = @api.resource(:products).call(:index, {
Expand Down

0 comments on commit 6f221b5

Please sign in to comment.