-
Notifications
You must be signed in to change notification settings - Fork 10
/
onefind
executable file
·155 lines (124 loc) · 3.78 KB
/
onefind
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env ruby
# This script finds all elements that have an specific value in their template
# To run it, simply execute:
# ./find VALUE_TO_FIND PATH_TO_SEARCH
# ./find YES TEMPLATE/ALLOW_ORPHANS
#
# How you should read the output? The script will output the following:
# ELEMENT_NAME
# ------------
# ID_1
# ID_2
#
# If no elements are found, you will see a message like: `No elements found`
#
# WARNING: if you want to check all the elements in your cloud, run the script
# under oneadmin (or oneadmin group) user
ONE_LOCATION = ENV['ONE_LOCATION']
if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems'
else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems'
end
if File.directory?(GEMS_LOCATION)
$LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
require 'rubygems'
Gem.use_paths(File.realpath(GEMS_LOCATION))
end
$LOAD_PATH << RUBY_LIB_LOCATION
require 'opennebula'
################################################################################
# FUNCTIONS
################################################################################
# Search inside path
#
# @param hash [Hash] Hash to seach in
# @param path [String] Path to search on
def dsearch(hash, path)
path.delete_if {|s| s.nil? || s.empty? }
path.each do |p|
if hash.is_a? Hash
if hash[p]
hash = hash[p]
else
hash = nil
break
end
else
hash = nil
break
end
end
hash
end
# Execute main loop to find elements
#
# @param pools [Array] OpenNebula pools names
# @param value [String] Value to find
# @param path [Array] Path to search on
#
# @return [Hash] Hash with each element as key and found IDs as value
def main(pools, value, path)
# Get client object to make calls
client = OpenNebula::Client.new
ret = {}
# Iterate over all pools
pools.each do |p_class|
pool = OpenNebula.const_get(p_class).new(client)
rc = pool.info_all
ids = []
if OpenNebula.is_error?(rc)
STDERR.puts rc.message
exit(-1)
end
# Iterate over each pool element
pool.each do |elem|
elem = elem.to_hash[pool.element_name]
id = elem['ID']
elem = dsearch(elem, path)
next unless elem == value
ids << id
end
next if ids.empty?
ret[pool.element_name] = ids
end
ret
end
################################################################################
################################################################################
if !ARGV[0] && !ARGV[1]
STDERR.puts 'Value to find or path not provided!'
STDERR.puts '`find.rb` <value_to_find> <path>'
exit(-1)
end
# Get path to find value
path = ARGV[1].split('/')
unless path.is_a? Array
STDERR.puts 'Wrong path format! It should be A/B/C way'
end
################################################################################
################################################################################
# OpenNebula pools
pools = OpenNebula.constants.select {|c| OpenNebula.const_get(c).is_a? Class }
# Remove parent classes or pools that are accessed by other services like flow
pools -= [:Pool,
:DocumentPool,
:ServicePool,
:ServiceTemplatePool,
:XMLPool,
:OpenNebulaServicePool]
pools.select! {|p| p =~ /Pool$/ }
ret = main(pools, ARGV[0], path)
if ret.empty?
puts 'No elements found'
else
ret.each do |key, value|
puts "#{key}S"
(key.size + 1).times { print '-' }
puts
value.each {|id| puts id }
puts unless key == ret.keys.last
end
end