Class: Bosh::Cli::Command::Misc

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

Constant Summary collapse

DEFAULT_STATUS_TIMEOUT =

seconds

3

Constants inherited from Base

Base::DEFAULT_DIRECTOR_PORT

Instance Attribute Summary

Attributes inherited from Base

#args, #exit_code, #options, #out, #runner, #work_dir

Instance Method Summary collapse

Methods inherited from Base

#add_option, #blob_manager, #blobstore, #config, #confirmed?, #deployment, #director, #initialize, #interactive?, #logged_in?, #non_interactive?, #password, #redirect, #release, #remove_option, #target, #target_name, #username, #verbose?

Methods included from Bosh::Cli::CommandDiscovery

#desc, #method_added, #option, #register_command, #usage

Methods included from DeploymentHelper

#cancel_deployment, #deployment_changed?, #inspect_deployment_changes, #job_exists_in_deployment?, #job_must_exist_in_deployment, #job_unique_in_deployment?, #jobs_and_indexes, #latest_release_versions, #prepare_deployment_manifest, #prompt_for_job_and_index, #resolve_release_aliases

Methods included from VersionCalc

#major_version, #minor_version, #version_cmp, #version_greater, #version_less, #version_same

Constructor Details

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

Instance Method Details

#list_aliasesObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/cli/commands/misc.rb', line 234

def list_aliases
  aliases = config.aliases(:cli) || {}
  err("No aliases found") if aliases.empty?

  sorted = aliases.sort_by { |name, _| name }
  aliases_table = table do |t|
    t.headings = %w(Alias Command)
    sorted.each { |row| t << [row[0], row[1]] }
  end

  nl
  say(aliases_table)
  nl
  say("Aliases total: %d" % aliases.size)
end

#list_targetsObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/cli/commands/misc.rb', line 206

def list_targets
  targets = config.aliases(:target) || {}

  err("No targets found") if targets.empty?

  targets_table = table do |t|
    t.headings = [ "Name", "Director URL" ]
    targets.each { |row| t << [row[0], row[1]] }
  end

  nl
  say(targets_table)
  nl
  say("Targets total: %d" % targets.size)
end

#login(username = nil, password = nil) ⇒ Object



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

def (username = nil, password = nil)
  target_required

  if interactive?
    username = ask("Your username: ").to_s if username.blank?

    password_retries = 0
    while password.blank? && password_retries < 3
      password = ask("Enter password: ") { |q| q.echo = "*" }.to_s
      password_retries += 1
    end
  end

  if username.blank? || password.blank?
    err("Please provide username and password")
  end
  logged_in = false

  #Converts HighLine::String to String
  username = username.to_s
  password = password.to_s

  director.user = username
  director.password = password

  if director.authenticated?
    say("Logged in as `#{username}'".make_green)
    logged_in = true
  elsif non_interactive?
    err("Cannot log in as `#{username}'".make_red)
  else
    say("Cannot log in as `#{username}', please try again".make_red)
    (username)
  end

  if logged_in
    config.set_credentials(target, username, password)
    config.save
  end
end

#logoutObject



139
140
141
142
143
144
# File 'lib/cli/commands/misc.rb', line 139

def logout
  target_required
  config.set_credentials(target, nil, nil)
  config.save
  say("You are no longer logged in to `#{target}'".make_yellow)
end

#set_alias(name, command) ⇒ Object



225
226
227
228
229
# File 'lib/cli/commands/misc.rb', line 225

def set_alias(name, command)
  config.set_alias(:cli, name, command.to_s.strip)
  config.save
  say("Alias `#{name.make_green}' created for command `#{command.make_green}'")
end

#set_target(director_url = nil, name = nil) ⇒ Object



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

def set_target(director_url = nil, name = nil)
  if director_url.nil?
    show_target
    return
  end

  if name.nil?
    director_url =
      config.resolve_alias(:target, director_url) || director_url
  end

  if director_url.blank?
    err("Target name cannot be blank")
  end

  director_url = normalize_url(director_url)
  if target && director_url == normalize_url(target)
    say("Target already set to `#{target_name.make_green}'")
    return
  end

  director = Bosh::Cli::Client::Director.new(director_url)

  begin
    status = director.get_status
  rescue Bosh::Cli::AuthError
    status = {}
  rescue Bosh::Cli::DirectorError
    err("Cannot talk to director at `#{director_url}', " +
        "please set correct target")
  end

  config.target = director_url
  config.target_name = status["name"]
  config.target_version = status["version"]
  config.target_uuid = status["uuid"]

  unless name.blank?
    config.set_alias(:target, name, director_url)
  end

  unless status["uuid"].blank?
    config.set_alias(:target, status["uuid"], director_url)
  end

  config.save
  say("Target set to `#{target_name.make_green}'")

  if interactive? && !logged_in?
    redirect("login")
  end
end

#statusObject



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

def status
  if options[:uuid]
    begin
      say(get_director_status["uuid"])
    rescue => e
      err("Error fetching director status: #{e.message}")
    end
  else
    say("Config".make_green)
    print_value("", config.filename)

    nl
    say("Director".make_green)
    if target.nil?
      say("  not set".make_yellow)
    else
      begin
        status = get_director_status

        print_value("Name", status["name"])
        print_value("URL", target_url)
        print_value("Version", status["version"])
        print_value("User", username, "not logged in")
        print_value("UUID", status["uuid"])
        print_value("CPI", status["cpi"], "n/a")
        print_feature_list(status["features"]) if status["features"]

        unless options[:target]
          config.target_name = status["name"]
          config.target_version = status["version"]
          config.target_uuid = status["uuid"]
          config.save
        end
      rescue TimeoutError
        say("  timed out fetching director status".make_red)
      rescue => e
        say("  error fetching director status: #{e.message}".make_red)
      end
    end

    nl
    say("Deployment".make_green)

    if deployment
      print_value("Manifest", deployment)
    else
      say("  not set".make_yellow)
    end

    if in_release_dir?
      nl
      say("Release".make_green)

      dev_version = Bosh::Cli::VersionsIndex.new(
        File.join(work_dir, "dev_releases")).latest_version

      final_version = Bosh::Cli::VersionsIndex.new(
        File.join(work_dir, "releases")).latest_version

      dev = release.dev_name
      dev += "/#{dev_version}" if dev && dev_version

      final = release.final_name
      final += "/#{final_version}" if final && final_version

      print_value("dev", dev)
      print_value("final", final)
    end
  end
end

#versionObject



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

def version
  say("BOSH %s" % [Bosh::Cli::VERSION])
end