hammer-cli-csv / lib / hammer_cli_csv / base.rb @ 104144fb
1 |
# Copyright (c) 2013 Red Hat
|
---|---|
2 |
#
|
3 |
# MIT License
|
4 |
#
|
5 |
# Permission is hereby granted, free of charge, to any person obtaining
|
6 |
# a copy of this software and associated documentation files (the
|
7 |
# "Software"), to deal in the Software without restriction, including
|
8 |
# without limitation the rights to use, copy, modify, merge, publish,
|
9 |
# distribute, sublicense, and/or sell copies of the Software, and to
|
10 |
# permit persons to whom the Software is furnished to do so, subject to
|
11 |
# the following conditions:
|
12 |
#
|
13 |
# The above copyright notice and this permission notice shall be
|
14 |
# included in all copies or substantial portions of the Software.
|
15 |
#
|
16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20 |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21 |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22 |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23 |
#
|
24 |
#
|
25 |
|
26 |
require 'hammer_cli'
|
27 |
require 'katello_api'
|
28 |
require 'foreman_api'
|
29 |
require 'json'
|
30 |
require 'csv'
|
31 |
|
32 |
module HammerCLICsv |
33 |
class BaseCommand < HammerCLI::AbstractCommand |
34 |
|
35 |
HEADERS = {'Accept' => 'version=2,application/json'} |
36 |
|
37 |
option ["-v", "--verbose"], :flag, "be verbose" |
38 |
option ['--threads'], 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1 |
39 |
option ['--csv-file'], 'FILE_NAME', 'CSV file to name', :required => true |
40 |
option ['--csv-export'], :flag, 'Export current data instead of importing' |
41 |
|
42 |
def initialize(*args) |
43 |
@init_options = {
|
44 |
:katello => {
|
45 |
:base_url => HammerCLI::Settings.get(:katello, :host), |
46 |
:username => HammerCLI::Settings.get(:katello, :username), |
47 |
:password => HammerCLI::Settings.get(:katello, :password) |
48 |
}, |
49 |
:foreman => {
|
50 |
:base_url => HammerCLI::Settings.get(:foreman, :host), |
51 |
:username => HammerCLI::Settings.get(:foreman, :username), |
52 |
:password => HammerCLI::Settings.get(:foreman, :password) |
53 |
} |
54 |
} |
55 |
end
|
56 |
|
57 |
def get_lines(filename) |
58 |
file = File.open(filename ,'r') |
59 |
contents = file.readlines |
60 |
file.close |
61 |
contents |
62 |
end
|
63 |
|
64 |
def namify(name_format, number) |
65 |
if name_format.index('%') |
66 |
name_format % number |
67 |
else
|
68 |
name_format |
69 |
end
|
70 |
end
|
71 |
|
72 |
def thread_import |
73 |
csv = get_lines(csv_file)[1..-1] |
74 |
lines_per_thread = csv.length/threads.to_i + 1
|
75 |
splits = [] |
76 |
|
77 |
threads.to_i.times do |current_thread|
|
78 |
start_index = ((current_thread) * lines_per_thread).to_i |
79 |
finish_index = ((current_thread + 1) * lines_per_thread).to_i
|
80 |
lines = csv[start_index...finish_index].clone |
81 |
splits << Thread.new do |
82 |
lines.each do |line|
|
83 |
if line.index('#') != 0 |
84 |
yield line
|
85 |
end
|
86 |
end
|
87 |
end
|
88 |
end
|
89 |
|
90 |
splits.each do |thread|
|
91 |
thread.join |
92 |
end
|
93 |
|
94 |
end
|
95 |
end
|
96 |
end
|