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

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

Constant Summary

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, #warn_about_stemcell_changes

Constructor Details

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

Instance Method Details

#list_aliasesObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/cli/commands/misc.rb', line 210

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cli/commands/misc.rb', line 182

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



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

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



115
116
117
118
119
120
# File 'lib/cli/commands/misc.rb', line 115

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



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

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



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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cli/commands/misc.rb', line 126

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



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

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
  end
end

#versionObject



6
7
8
# File 'lib/cli/commands/misc.rb', line 6

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