Module: CollinsShell::Util

Included in:
Asset, AssetType, Cli, IpAddress, Ipmi, Provision, State, Tag
Defined in:
lib/collins_shell/util.rb,
lib/collins_shell/util/asset_stache.rb

Defined Under Namespace

Classes: AssetStache

Constant Summary collapse

SIZABLE_ATTRIBUTES =
[:memory_size_total, :disk_storage_total]

Instance Method Summary collapse

Instance Method Details

#asset_exec(asset, execs, confirm = true, threads = 1) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/collins_shell/util.rb', line 54

def asset_exec asset, execs, confirm = true, threads = 1
  return unless execs
  mustache = CollinsShell::Util::AssetStache.new asset
  rendered = mustache.render "/bin/bash -c '#{execs}'"
  say_status("exec", rendered, :red)
  require_yes("Running on #{asset.tag}. ARE YOU SURE?", :red) if confirm
  if threads < 2 then
    system(rendered)
  else
    run_command_in_thread(rendered, threads)
  end
end

#asset_get(tag, options) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/collins_shell/util.rb', line 115

def asset_get tag, options
  call_collins get_collins_client, "get asset" do |client|
    as_asset = Collins::Asset.new(tag)
    as_asset.location = options.remote
    asset = client.get as_asset
  end
end

#call_collins(client, operation, &block) ⇒ Object



10
11
12
13
14
15
# File 'lib/collins_shell/util.rb', line 10

def call_collins client, operation, &block
  begin
    block.call(client)
  rescue SystemExit => e
  end
end

#finalize_execObject

Should be called after asset_exec



68
69
70
# File 'lib/collins_shell/util.rb', line 68

def finalize_exec
  thread_mgr.threads.each(&:join)
end

#format_asset_tags(asset, tags) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/collins_shell/util.rb', line 136

def format_asset_tags asset, tags
  tags.map do |tag|
    if tag.to_s =~ /^ipmi_(.*)/ then
      asset.ipmi.send($1.to_sym)
    elsif tag.to_s =~ /^addresses_(.*)/ then
      result = asset.addresses.select do |a|
        a.pool.upcase == $1.upcase
      end
      format_asset_value result
    elsif tag.to_s.split('.').length == 2 then
      o, m = tag.to_s.split('.')
      result = asset.send(o.to_sym)
      if result.nil? then
        "nil"
      else
        format_asset_value result.send(m.to_sym)
      end
    elsif tag == :url then
      config = get_collins_config
      " #{config[:host]}/asset/#{asset.tag}"
    else
      result = asset.send(tag.to_sym)
      format_asset_value result
    end
  end.join(',')
end

#format_asset_value(value) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/collins_shell/util.rb', line 163

def format_asset_value value
  if is_array? value then
    value.map {|v| format_asset_value v}
  elsif value.is_a?(Collins::Address) then
    [:address,:gateway,:netmask].map {|s| value.send(s)}.join('|')
  else
    value.to_s
  end
end

#get_selector(selector, tags, size, remote = false) ⇒ Object



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
# File 'lib/collins_shell/util.rb', line 17

def get_selector selector, tags, size, remote = false
  asset_params = ::Collins::Asset::Find.to_a.inject({}) {|res,el| res.update(el.to_s.upcase => el)}
  selector = symbolize_hash(selector).inject({}) do |result, (k,v)|
    upcase_key = k.to_s.upcase
    if asset_params.key?(upcase_key) then # normalized reserved params
      corrected_key = asset_params[upcase_key]
      if ::Collins::Asset::Find::DATE_PARAMS.include?(corrected_key) then
        result[corrected_key.to_sym] = ::Collins::Asset.format_date_string(v)
      else
        result[corrected_key.to_sym] = v
      end
    elsif upcase_key == k.to_s then # respect case
      result[k] = v
    else # otherwise downcase
      result[k.downcase] = v
    end
    result
  end
  if not selector.include?(:operation) then
    selector.update(:operation => 'and')
  end
  if not selector.include?(:remoteLookup) and remote then
    selector.update(:remoteLookup => remote.to_s)
  end
  if not selector.include?(:size) and size then
    selector.update(:size => size)
  end
  selector.update(:details => true) if is_array?(tags)
  CollinsShell::Util::SIZABLE_ATTRIBUTES.each do |attrib|
    attrib_value = selector[attrib]
    if attrib_value && attrib_value.to_s.is_disk_size? then
      selector.update(attrib => attrib_value.to_s.to_bytes)
    end
  end
  selector
end

#is_array?(tags) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/collins_shell/util.rb', line 173

def is_array? tags
  tags && tags.is_a?(Array) && tags.length > 0
end


123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/collins_shell/util.rb', line 123

def print_find_results assets, tags, options = {}
  tags = [:tag,:status,:type,:created,:updated,:location] if not is_array?(tags)
  if options[:url] then
    tags << :url
  end
  if options[:header] then
    puts(tags.join(','))
  end
  [assets].flatten.each do |asset|
    puts format_asset_tags(asset, tags)
  end
end

#run_command_in_thread(syscmd, thread_count) ⇒ Object

Parameters:

  • syscmd (String)

    The system command to execute

  • thread_count (Fixnum)

    The numbers of threads to allow running at one time



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
# File 'lib/collins_shell/util.rb', line 88

def run_command_in_thread syscmd, thread_count
  if thread_mgr.threads.size > thread_count then
    thread_mgr.threads.each(&:join)
    thread_mgr.threads.clear
  else
    thread_mgr.threads << Thread.new(syscmd) do |cmd|
      begin
        stdin, stdout, stderr, thread = Open3.popen3(cmd)
        stdin.close
        o, e = [stdout, stderr].map {|p| begin p.read ensure p.close end}
        unless o.empty? then
          say_status("stdout: #{cmd}", o, :green)
        end
        unless e.empty? then
          say_status("stderr: #{cmd}", e, :red)
        end
        if thread then
          Process.kill('TERM', thread.pid) rescue nil
          thread.kill unless nil
        end
      rescue Exception => e
        say_status("exception: #{cmd}", e.to_s, :red)
      end
    end
  end
end

#say_status(status, message, log_status = true) ⇒ Object



80
81
82
83
84
# File 'lib/collins_shell/util.rb', line 80

def say_status(status, message, log_status=true)
  thread_mgr.lock.synchronize {
    super
  }
end

#thread_mgrObject

Wraps open threads and provides a synchronized output buffer for concurrently writing output



73
74
75
76
77
78
# File 'lib/collins_shell/util.rb', line 73

def thread_mgr
  @_thread_mgr ||= OpenStruct.new({
    :threads => [],
    :lock => Mutex.new
  })
end