Revision c2496839
Added by Thomas McKay about 8 years ago
lib/hammer_cli_csv.rb | ||
---|---|---|
25 | 25 |
require 'hammer_cli_csv/content_views' |
26 | 26 |
require 'hammer_cli_csv/content_view_filters' |
27 | 27 |
require 'hammer_cli_csv/domains' |
28 |
require 'hammer_cli_csv/export' |
|
28 | 29 |
require 'hammer_cli_csv/host_collections' |
29 | 30 |
require 'hammer_cli_csv/hosts' |
30 | 31 |
require 'hammer_cli_csv/import' |
lib/hammer_cli_csv/base.rb | ||
---|---|---|
60 | 60 |
:api_version => 2 |
61 | 61 |
}) |
62 | 62 |
|
63 |
@server_status = check_server_status |
|
64 |
|
|
63 | 65 |
option_csv_export? ? export : import |
64 | 66 |
HammerCLI::EX_OK |
65 | 67 |
end |
66 | 68 |
|
69 |
def check_server_status |
|
70 |
server = option_server || HammerCLI::Settings.get(:csv, :host) |
|
71 |
username = option_username || HammerCLI::Settings.get(:csv, :username) |
|
72 |
password = option_password || HammerCLI::Settings.get(:csv, :password) |
|
73 |
url = "#{server}/api/status" |
|
74 |
uri = URI(url) |
|
75 |
server_status = Net::HTTP.start(uri.host, uri.port, |
|
76 |
:use_ssl => uri.scheme == 'https', |
|
77 |
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http| |
|
78 |
request = Net::HTTP::Get.new uri.request_uri |
|
79 |
request.basic_auth(username, password) |
|
80 |
response = http.request(request) |
|
81 |
JSON.parse(response.body) |
|
82 |
end |
|
83 |
|
|
84 |
server_status |
|
85 |
end |
|
86 |
|
|
67 | 87 |
def namify(name_format, number = 0) |
68 | 88 |
if name_format.index('%') |
69 | 89 |
name = name_format % number |
... | ... | |
254 | 274 |
'search' => "role=\"#{role}\"" |
255 | 275 |
})['results'] |
256 | 276 |
filters.each do |filter| |
257 |
resource_type = filter['resource_type'].split(':')[-1] # To remove "Katello::" when present
|
|
277 |
resource_type = (filter['resource_type'] || '').split(':')[-1] # To remove "Katello::" when present
|
|
258 | 278 |
return filter['id'] if resource_type == resource && filter['search'] == search |
259 | 279 |
end |
260 | 280 |
|
lib/hammer_cli_csv/export.rb | ||
---|---|---|
1 |
# Copyright 2013-2014 Red Hat, Inc. |
|
2 |
# |
|
3 |
# This software is licensed to you under the GNU General Public |
|
4 |
# License as published by the Free Software Foundation; either version |
|
5 |
# 2 of the License (GPLv2) or (at your option) any later version. |
|
6 |
# There is NO WARRANTY for this software, express or implied, |
|
7 |
# including the implied warranties of MERCHANTABILITY, |
|
8 |
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should |
|
9 |
# have received a copy of GPLv2 along with this software; if not, see |
|
10 |
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. |
|
11 |
|
|
12 |
|
|
13 |
module HammerCLICsv |
|
14 |
class CsvCommand |
|
15 |
class ExportCommand < HammerCLI::Apipie::Command |
|
16 |
command_name 'export' |
|
17 |
desc 'export into directory' |
|
18 |
|
|
19 |
option %w(-v --verbose), :flag, 'be verbose' |
|
20 |
option %w(--threads), 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1 |
|
21 |
option %w(--server), 'SERVER', 'Server URL' |
|
22 |
option %w(-u --username), 'USERNAME', 'Username to access server' |
|
23 |
option %w(-p --password), 'PASSWORD', 'Password to access server' |
|
24 |
option '--dir', 'DIRECTORY', 'directory to import from' |
|
25 |
|
|
26 |
RESOURCES = %w( organizations locations puppet_environments operating_systems |
|
27 |
domains architectures partition_tables lifecycle_environments host_collections |
|
28 |
provisioning_templates |
|
29 |
subscriptions activation_keys hosts content_hosts reports roles users ) |
|
30 |
RESOURCES.each do |resource| |
|
31 |
dashed = resource.sub('_', '-') |
|
32 |
option "--#{dashed}", 'FILE', "csv file for #{dashed}" |
|
33 |
end |
|
34 |
|
|
35 |
def execute |
|
36 |
@api = ApipieBindings::API.new({ |
|
37 |
:uri => option_server || HammerCLI::Settings.get(:csv, :host), |
|
38 |
:username => option_username || HammerCLI::Settings.get(:csv, :username), |
|
39 |
:password => option_password || HammerCLI::Settings.get(:csv, :password), |
|
40 |
:api_version => 2 |
|
41 |
}) |
|
42 |
|
|
43 |
# Swing the hammers |
|
44 |
RESOURCES.each do |resource| |
|
45 |
hammer_resource(resource) |
|
46 |
end |
|
47 |
|
|
48 |
HammerCLI::EX_OK |
|
49 |
end |
|
50 |
|
|
51 |
def hammer(context = nil) |
|
52 |
context ||= { |
|
53 |
:interactive => false, |
|
54 |
:username => 'admin', # TODO: this needs to come from config/settings |
|
55 |
:password => 'changeme' # TODO: this needs to come from config/settings |
|
56 |
} |
|
57 |
|
|
58 |
HammerCLI::MainCommand.new('', context) |
|
59 |
end |
|
60 |
|
|
61 |
def hammer_resource(resource) |
|
62 |
return if !self.send("option_#{resource}") && !option_dir |
|
63 |
options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv" |
|
64 |
args = %W( csv #{resource.sub('_', '-')} --csv-export --csv-file #{options_file} ) |
|
65 |
args << '-v' if option_verbose? |
|
66 |
args += %W( --threads #{option_threads} ) |
|
67 |
args += %W( --server #{option_server} ) if option_server |
|
68 |
hammer.run(args) |
|
69 |
end |
|
70 |
end |
|
71 |
end |
|
72 |
end |
lib/hammer_cli_csv/locations.rb | ||
---|---|---|
39 | 39 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
40 | 40 |
csv << [NAME, COUNT, PARENT] |
41 | 41 |
@api.resource(:locations).call(:index, {:per_page => 999999})['results'].each do |location| |
42 |
csv << [location['name'], 1] |
|
42 |
csv << [location['name'], 1, '']
|
|
43 | 43 |
end |
44 | 44 |
end |
45 | 45 |
end |
lib/hammer_cli_csv/organizations.rb | ||
---|---|---|
37 | 37 |
command_name 'organizations' |
38 | 38 |
desc 'import or export organizations' |
39 | 39 |
|
40 |
option %w(--sam), :flag, 'export from SAM-1.3 or SAM-1.4' |
|
41 |
|
|
42 | 40 |
LABEL = 'Label' |
43 | 41 |
DESCRIPTION = 'Description' |
44 | 42 |
|
45 | 43 |
def export |
46 | 44 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
47 | 45 |
csv << [NAME, COUNT, LABEL, DESCRIPTION] |
48 |
if option_sam? |
|
46 |
|
|
47 |
if @server_status['release'] == 'Headpin' |
|
49 | 48 |
server = option_server || HammerCLI::Settings.get(:csv, :host) |
50 | 49 |
username = option_username || HammerCLI::Settings.get(:csv, :username) |
51 | 50 |
password = option_password || HammerCLI::Settings.get(:csv, :password) |
lib/hammer_cli_csv/roles.rb | ||
---|---|---|
90 | 90 |
filter_id = foreman_filter(name, line[RESOURCE], search) |
91 | 91 |
if !filter_id |
92 | 92 |
print " creating filter #{line[RESOURCE]}..." if option_verbose? |
93 |
@api.resource(:filters).call(:create, { 'filter' => { |
|
94 |
'role_id' => @existing_roles[name], |
|
95 |
'search' => search, |
|
96 |
'organization_ids' => organizations, |
|
97 |
'location_ids' => locations, |
|
98 |
'permission_ids' => permissions |
|
99 |
}}) |
|
93 |
@api.resource(:filters) |
|
94 |
.call(:create, { 'filter' => { |
|
95 |
'role_id' => @existing_roles[name], |
|
96 |
'search' => search, |
|
97 |
'unlimited' => search.empty?, |
|
98 |
'organization_ids' => organizations, |
|
99 |
'location_ids' => locations, |
|
100 |
'permission_ids' => permissions |
|
101 |
}}) |
|
100 | 102 |
else |
101 | 103 |
print " updating filter #{line[RESOURCE]}..." |
102 |
@api.resource(:filters).call(:update, { |
|
103 |
'id' => filter_id, |
|
104 |
'search' => search, |
|
105 |
'organization_ids' => organizations, |
|
106 |
'location_ids' => locations, |
|
107 |
'permission_ids' => permissions |
|
108 |
}) |
|
104 |
@api.resource(:filters) |
|
105 |
.call(:update, { |
|
106 |
'id' => filter_id, |
|
107 |
'search' => search, |
|
108 |
'unlimited' => search.empty?, |
|
109 |
'organization_ids' => organizations, |
|
110 |
'location_ids' => locations, |
|
111 |
'permission_ids' => permissions |
|
112 |
}) |
|
109 | 113 |
end |
110 | 114 |
|
111 | 115 |
puts 'done' if option_verbose? |
test/data/roles.csv | ||
---|---|---|
46 | 46 |
jacquesjoaquin,1,ActivationKey,name = jacques.joaquin@megacorp.com,"view_activation_keys",Mega Corporation, |
47 | 47 |
janayjohnson,1,ActivationKey,name = janay.johnson@megacorp.com,"view_activation_keys",Mega Corporation, |
48 | 48 |
jeffersonjock,1,ActivationKey,name = jefferson.jock@megacorp.com,"view_activation_keys",Mega Corporation, |
49 |
jeneejahns,1,ActivationKey,,"view_activation_keys,create_activation_keys,edit_activation_keys,destroy_activation_keys",Mega Corporation, |
|
49 |
jeneejahns,1,ActivationKey,name = jenee.jahns@megacorp.com,"view_activation_keys,create_activation_keys,edit_activation_keys,destroy_activation_keys",Mega Corporation,
|
|
50 | 50 |
jeremyjong,1,ActivationKey,name = jeremy.jong@megacorp.com,"view_activation_keys",Mega Corporation, |
51 | 51 |
judithjoachim,1,ActivationKey,name = judith.joachim@megacorp.com,"view_activation_keys",Mega Corporation, |
52 | 52 |
juliojerabek,1,ActivationKey,name = julio.jerabek@megacorp.com,"view_activation_keys",Mega Corporation, |
Also available in: Unified diff
Checks /api/status for SAM (Headpin)