Class: CollinsShell::Asset

Inherits:
Thor
  • Object
show all
Includes:
ThorHelper, Util
Defined in:
lib/collins_shell/asset.rb

Constant Summary

Constants included from Util

Util::SIZABLE_ATTRIBUTES

Constants included from ThorHelper

ThorHelper::COLLINS_OPTIONS, ThorHelper::PAGE_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#asset_exec, #asset_get, #call_collins, #finalize_exec, #format_asset_tags, #format_asset_value, #get_selector, #is_array?, #print_find_results, #run_command_in_thread, #say_status, #thread_mgr

Methods included from ThorHelper

#appropriate_answer?, #batch_selector_operation, #collins_config_from_file, #ensure_password, #get_collins_client, #get_collins_config, #get_password, included, #require_valid_collins_config, #require_yes, #say_error, #say_success, #selector_or_tag, #try_config_merge, #use_collins_options, #use_nuke_option, #use_page_options, #use_selector_option, #use_tag_option

Class Method Details



13
14
15
# File 'lib/collins_shell/asset.rb', line 13

def self.banner task, namespace = true, subcommand = false
  "#{basename} #{task.formatted_usage(self, true, subcommand).gsub(':',' ')}"
end

Instance Method Details

#createObject



23
24
25
26
27
28
# File 'lib/collins_shell/asset.rb', line 23

def create
  call_collins get_collins_client, "create asset" do |client|
    asset = client.create! options.tag, :generate_ipmi => options.ipmi, :status => options.status, :type => options.type
    print_find_results asset, nil
  end
end

#deleteObject



36
37
38
39
40
41
42
43
44
# File 'lib/collins_shell/asset.rb', line 36

def delete
  call_collins get_collins_client, "delete asset" do |client|
    if client.delete!(options.tag, :reason => options.reason, :nuke => options.nuke) then
      say_success "deleted asset #{options.tag}"
    else
      say_error "deleting asset #{options.tag}", :exit => true
    end
  end
end

#delete_attribute(key) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/collins_shell/asset.rb', line 194

def delete_attribute key
  batch_selector_operation Hash[
    :remote => options.remote,
    :operation => "delete_attribute",
    :size => options["size"],
    :success_message => proc {|asset| "Delete attribute on #{asset.tag}"},
    :error_message => proc{|asset| "Delete attribute on #{asset.tag}"},
    :confirmation_message => proc do |assets|
      "You are about to delete #{key} on #{assets.length} hosts. ARE YOU SURE?"
    end
  ] do |client,asset|
    client.delete_attribute!(asset, key)
  end
end

#findObject



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

def find
  client = get_collins_client
  tags = options.tags || [:stuff] 
  selector = get_selector options.selector, tags, options["size"], options.remote
  assets = client.find selector
  if options.details then
    assets.each do |asset|
      if not options.quiet then
        logs = []
        if options.logs and not options.exec? then
          logs = client.logs(asset, :size => 5000, :SORT => "DESC").reverse
        end
        printer = CollinsShell::AssetPrinter.new asset, self, :separator => '*',
                                                              :logs => logs,
                                                              :detailed => !options.exec?
        puts printer
      end
      asset_exec asset, options.exec, options.confirm, options.threads
    end
  else
    if not options.quiet then
      print_find_results assets, options.tags, :header => options.header, :url => options.url
    end
    assets.each {|asset| asset_exec(asset, options.exec, options.confirm, options.threads)}
  end
  finalize_exec
end

#find_similar(tag) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/collins_shell/asset.rb', line 92

def find_similar tag
  call_collins get_collins_client, "similar asset" do |client|
    as_asset = Collins::Asset.new(tag)
    assets = client.find_similar as_asset, options["size"], options["sort"], options.sort_type, options.only_unallocated
    if options.details then
      assets.each do |asset|
        printer = CollinsShell::AssetPrinter.new asset, self, :separator => '*'
        puts printer
      end
    else
      print_find_results assets, options.tags
    end
  end
end

#get(tag) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/collins_shell/asset.rb', line 113

def get tag
  asset = asset_get tag, options
  if asset then
    if not options.quiet then
      logs = []
      if options.logs and not options.exec? then
        logs = call_collins(get_collins_client, "logs") do |client|
          client.logs(asset, :size => 5000, :sort => "DESC").reverse
        end
      end
      printer = CollinsShell::AssetPrinter.new asset, self, :logs => logs, :detailed => !options.exec?
      puts printer
    end
    asset_exec asset, options.exec, options.confirm
  else
    say_error "No such asset #{tag}"
  end
end

#set_attribute(key, value) ⇒ Object



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

def set_attribute key, value
  batch_selector_operation Hash[
    :remote => options.remote,
    :operation => "set_attribute",
    :size => options["size"],
    :success_message => proc {|asset| "Set attribute on #{asset.tag}"},
    :error_message => proc{|asset| "Setting attribute on #{asset.tag}"},
    :confirmation_message => proc do |assets|
      "You are about to set #{key}=#{value} on #{assets.length} hosts. ARE YOU SURE?"
    end
  ] do |client,asset|
    if options.json then
      value = JSON.dump(eval(value))
    end
    dimension = nil
    if options.dimension >= 0 then
      dimension = options.dimension
    end
    client.set_attribute!(asset, key, value, dimension)
  end
end

#set_attributes(*hash) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/collins_shell/asset.rb', line 164

def set_attributes *hash
  attributes = hash.inject({}) do |ret, e|
    k, v = e.split('=', 2)
    if k.nil? or v.nil? then
      k, v = e.split(':', 2)
      if k.nil? or v.nil? then
        raise ::CollinsShell::RequirementFailedError.new "Expected key=value (or key:value) format"
      end
    end
    ret[k] = v
    ret
  end
  batch_selector_operation Hash[
    :remote => options.remote,
    :operation => "set_attributes",
    :size => options["size"],
    :success_message => proc {|asset| "Set attributes on #{asset.tag}"},
    :error_message => proc{|asset| "Setting attributes on #{asset.tag}"},
    :confirmation_message => proc do |assets|
      "You are about to set #{attributes.inspect} on #{assets.length} hosts. ARE YOU SURE?"
    end
  ] do |client,asset|
    client.set_multi_attribute!(asset, attributes)
  end
end

#set_statusObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/collins_shell/asset.rb', line 216

def set_status
  status = options.status
  state = options.state
  reason = options.reason
  if status.nil? && state.nil? then
    raise ::Collins::ExpectationFailedError.new("set_status requires either a status or a state")
  end
  batch_selector_operation Hash[
    :remote => options.remote,
    :operation => "set_status",
    :size => options["size"],
    :success_message => proc {|asset| "Set status to #{status} on #{asset.tag}"},
    :error_message => proc{|asset| "Setting status on #{asset.tag}"},
    :confirmation_message => proc do |assets|
      "You are about to set status to #{status} on #{assets.length} hosts. ARE YOU SURE?"
    end
  ] do |client,asset|
    client.set_status!(asset, :reason => reason, :status => status, :state => state)
  end
end