Class: VMC::Cli::Command::Misc

Inherits:
Base show all
Defined in:
lib/cli/commands/misc.rb

Constant Summary

Constants inherited from Base

Base::MANIFEST

Instance Attribute Summary

Attributes inherited from Base

#no_prompt, #prompt_ok

Instance Method Summary collapse

Methods inherited from Base

#auth_token, #client, #client_info, #find_in_hash, #find_symbol, #frameworks_info, #initialize, #load_manifest, #load_manifest_structure, #manifest, #manifest_file, #merge_manifest, #merge_parent, #resolve_in, #resolve_lexically, #resolve_manifest, #resolve_symbol, #runtimes_info, #target_base, #target_url

Constructor Details

This class inherits a constructor from VMC::Cli::Command::Base

Instance Method Details

#alias(k, v = nil) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/cli/commands/misc.rb', line 269

def alias(k, v=nil)
  k,v = k.split('=') unless v
  aliases = VMC::Cli::Config.aliases
  aliases[k] = v
  VMC::Cli::Config.store_aliases(aliases)
  display "Successfully aliased '#{k}' to '#{v}'".green
end

#aliasesObject



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/cli/commands/misc.rb', line 257

def aliases
  aliases = VMC::Cli::Config.aliases
  return display JSON.pretty_generate(aliases) if @options[:json]
  return display "No Aliases" if aliases.empty?
  atable = table do |t|
    t.headings = 'Alias', 'Command'
    aliases.each { |k,v| t << [k, v] }
  end
  display "\n"
  display atable
end

#frameworksObject



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/cli/commands/misc.rb', line 245

def frameworks
  raise VMC::Client::AuthError unless client.logged_in?
  return display JSON.pretty_generate(frameworks_info) if @options[:json]
  return display "No Frameworks" if frameworks_info.empty?
  rtable = table do |t|
    t.headings = ['Name']
    frameworks_info.each { |f| t << f }
  end
  display "\n"
  display rtable
end

#import_from_registryObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
189
190
191
192
193
194
195
196
197
198
# File 'lib/cli/commands/misc.rb', line 149

def import_from_registry
  reg_targets = []
  begin
    require 'win32/registry'
    reg_targets = load_registry_targets
  rescue LoadError
    err "Unable to import data from Windows Registry"
    exit 1
  rescue
    err "Unable to import data from Windows Registry"
    exit 1
  end

  if reg_targets.length == 0
    display "No targets to import"
    exit
  end

  selected_target_desc = ask(
      "Which target to import?",
      {
          :indexed => true,
          :choices => reg_targets.collect{|t| "#{t[:desc]}" }
      }
  )

  selected_target = reg_targets.select{|t| selected_target_desc.index(t[:desc]) }.first

  if selected_target[:uhuruApi]
    VMC::Cli::Config.store_uhuru_target(
        selected_target[:target_url],
        {
            :token => selected_target[:auth_token],
            :cloud_team => selected_target[:cloud_team],
            :cloud_domain => selected_target[:cloud_domain]
        }
    )

    VMC::Cli::Config.store_target(selected_target[:target_url])
  else
    VMC::Cli::Config.store_target(selected_target[:target_url])
    VMC::Cli::Config.store_uhuru_target(
        selected_target[:target_url],
        nil
    )
    VMC::Cli::Config.store_token(selected_target[:auth_token], @options[:token_file])
  end

  display "Targeted to #{selected_target[:desc]}"
end

#infoObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/cli/commands/misc.rb', line 205

def info
  display "\nUsing Uhuru AppCloud [#{VMC::Cli::Config.target_url}]" if VMC::Cli::Config.uhuru_target

  info = client_info
  return display JSON.pretty_generate(info) if @options[:json]

  display "\n#{info[:description]}"
  display "For support visit #{info[:support]}"
  display ""
  display "Target:   #{target_url} (v#{info[:version]})"
  display "Client:   v#{VMC::Cli::VERSION}"
  if info[:user]
    display ''
    display "User:     #{info[:user]}"
  end
  if usage = info[:usage] and limits = info[:limits]
    tmem  = pretty_size(limits[:memory]*1024*1024)
    mem   = pretty_size(usage[:memory]*1024*1024)
    tser  = limits[:services]
    ser   = usage[:services]
    tapps = limits[:apps] || 0
    apps  = usage[:apps]  || 0
    display "Usage:    Memory   (#{mem} of #{tmem} total)"
    display "          Services (#{ser} of #{tser} total)"
    display "          Apps     (#{apps} of #{tapps} total)" if limits[:apps]
  end
end

#load_registry_targetsObject



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
# File 'lib/cli/commands/misc.rb', line 123

def load_registry_targets
  keyname = 'Software\Uhuru\CloudTargets'
  reg_targets = []

  Win32::Registry::HKEY_CURRENT_USER.open(keyname).each_value do |subkey, type, data|
    if data[0] == 'isUhuruApi'
      reg_targets << {:uhuruApi => true,
                      :target_url => data[2],
                      :auth_token => data[3],
                      :cloud_team => data[4],
                      # todo: get the cloud_domain
                      :cloud_domain => data[6],
                      :desc => "Uhuru AppCloud [#{data[2]}] / #{data[5]}"
      }
    end
    if data[0] == 'notUhuruApi'
      reg_targets << {:uhuruApi => false,
                      :target_url => data[2],
                      :auth_token => data[4],
                      :desc => "Cloud Foundry  [#{data[2]}] / #{data[3]}"
      }
    end
  end
  reg_targets
end

#runtimesObject



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cli/commands/misc.rb', line 233

def runtimes
  raise VMC::Client::AuthError unless client.logged_in?
  return display JSON.pretty_generate(runtimes_info) if @options[:json]
  return display "No Runtimes" if runtimes_info.empty?
  rtable = table do |t|
    t.headings = 'Name', 'Description', 'Version'
    runtimes_info.each_value { |rt| t << [rt[:name], rt[:description], rt[:version]] }
  end
  display "\n"
  display rtable
end

#set_cloud_team(cloud_team = nil) ⇒ Object



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
# File 'lib/cli/commands/misc.rb', line 63

def set_cloud_team(cloud_team = nil)
  err "Must be targeted to a Uhuru App Cloud to use cloud teams" unless client.via_uhuru_cloud

  client.proxy_realm = @options[:realm]

  detailed_cloud_teams = nil
  unless no_prompt || cloud_team

    err "No cloud team found for your UhuruApp account" if (detailed_cloud_teams ||= client.get_detailed_cloud_teams).length == 0

    detailed_cloud_teams.each{|cloud_team|
      cloud_team[:CloudEntity] ||= client.get_cloud_entity((cloud_team[:Cloud] || cloud_team[:CloudTeamCloud])[:Id])
    }

    cloud_team_chosen = ask(
      "\nWhich cloud team would you like to target to?",
      { :indexed => true,
        #:choices => (detailed_cloud_teams ||= client.get_detailed_cloud_teams).collect { |cloud_team| {:Name => cloud_team[:Name], :Id => cloud_team[:Id]}}
        :choices => (detailed_cloud_teams ||= client.get_detailed_cloud_teams).collect { |cloud_team|
          "#{cloud_team[:Name]} \t[Cloud: #{cloud_team[:CloudEntity][:Name]}] \t[Cloud Team Id: #{cloud_team[:Id]}]"
        }
      }
    )

    cloud_team_desc = detailed_cloud_teams.select{|i| cloud_team_chosen.index i[:Id]}.first
    cloud_team_id = cloud_team = cloud_team_desc[:Id]
  end
  err "Cloud team Id or Name required." unless cloud_team

  # if it matches a GUID format
  unless cloud_team =~ /\A\{?\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\}?\z/
    found_cloud_team = (detailed_cloud_teams ||= client.get_detailed_cloud_teams).select { |i|
      i[:Name] == cloud_team
    }

    err "Could not find a cloud team with name \"#{cloud_team}\"" if found_cloud_team.length == 0
    cloud_team_desc = found_cloud_team.first
    cloud_team_id = cloud_team_desc[:Id]
  else
    cloud_team_id = cloud_team
  end

  client.cloud_team = cloud_team_id
  cloud_team_desc ||= client.get_cloud_team(cloud_team_id)
  cloud_team_desc[:CloudEntity] ||= client.get_cloud_entity((cloud_team_desc[:Cloud] || cloud_team_desc[:CloudTeamCloud])[:Id])

  new_uhuru_target = VMC::Cli::Config.all_uhuru_targets[target_url.to_sym] || {}
  new_uhuru_target[:cloud_team] = cloud_team_id
  new_uhuru_target[:cloud_domain] = cloud_team_desc[:CloudEntity][:Domain]
  new_uhuru_target[:realm] = @options[:realm]
  VMC::Cli::Config.store_uhuru_target(
      target_url,
      new_uhuru_target
  )


  display  "\nTargeted to UhuruApp Cloud:\t[#{target_url}]\n Cloud Team:\t\t\t[#{cloud_team_desc[:Name]}]\n Cloud Team Id:\t\t\t[#{new_uhuru_target[:cloud_team]}]\n Cloud Foundry domain:\t\t[#{new_uhuru_target[:cloud_domain]}]"

end

#set_target(target_url) ⇒ Object



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
# File 'lib/cli/commands/misc.rb', line 28

def set_target(target_url)
  target_url = "http://#{target_url}" unless /^https?/ =~ target_url
  target_url = target_url.gsub(/\/+$/, '')

  uhuru_targets = VMC::Cli::Config.all_uhuru_targets || {}

  client = VMC::Client.new(target_url)
  client.trace = VMC::Cli::Config.trace if VMC::Cli::Config.trace

  unless client.target_valid?
    unless client.uhuru_target_valid?
      if prompt_ok
        display "Host is not available or is not valid: '#{target_url}'".red
        show_response = ask "Would you like see the response?",
                            :default => false
        display "\n<<<\n#{client.raw_info}\n>>>\n" if show_response
      end
      exit(false)
    else
      VMC::Cli::Config.store_target(target_url)

      new_uhuru_target = uhuru_targets[target_url.to_sym] || {}
      VMC::Cli::Config.store_uhuru_target(
          target_url,
          new_uhuru_target
      )
      say "Successfully targeted to Uhuru App Cloud [#{target_url}]".green

    end
  else
    VMC::Cli::Config.store_target(target_url)
    say "Successfully targeted to [#{target_url}]".green
  end
end

#targetObject



8
9
10
11
12
# File 'lib/cli/commands/misc.rb', line 8

def target
  return uhuru_target if VMC::Cli::Config.uhuru_target
  return display JSON.pretty_generate({:target => target_url}) if @options[:json]
  banner "[#{target_url}]"
end

#targetsObject Also known as: tokens



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cli/commands/misc.rb', line 14

def targets
  targets = VMC::Cli::Config.targets
  return display JSON.pretty_generate(targets) if @options[:json]
  return display 'None specified' if targets.empty?
  targets_table = table do |t|
    t.headings = 'Target', 'Authorization'
    targets.each { |target, token| t << [target, token] }
  end
  display "\n"
  display targets_table
end

#uhuru_targetObject



200
201
202
203
# File 'lib/cli/commands/misc.rb', line 200

def uhuru_target
  return display JSON.pretty_generate(VMC::Cli::Config.uhuru_target) if @options[:json]
  banner "Uhuru AppCloud [#{VMC::Cli::Config.target_url}]"
end

#unalias(key) ⇒ Object



277
278
279
280
281
282
283
284
285
286
# File 'lib/cli/commands/misc.rb', line 277

def unalias(key)
  aliases = VMC::Cli::Config.aliases
  if aliases.has_key?(key)
    aliases.delete(key)
    VMC::Cli::Config.store_aliases(aliases)
    display "Successfully unaliased '#{key}'".green
  else
    display "Unknown alias '#{key}'".red
  end
end

#versionObject



4
5
6
# File 'lib/cli/commands/misc.rb', line 4

def version
  say "vmcu #{VMC::Cli::VERSION}"
end