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::BLOBS_DIR, Base::BLOBS_INDEX_FILE

Instance Attribute Summary

Attributes inherited from Base

#cache, #config, #options, #out, #usage, #work_dir

Instance Method Summary collapse

Methods inherited from Base

#blob_manager, #blobstore, command, #confirmed?, #director, #dry_run?, #exit_code, #full_target_name, #initialize, #interactive?, #logged_in?, #non_interactive?, #redirect, #release, #run, #show_usage, #target_name, #target_version, #task_report, #verbose?

Constructor Details

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

Instance Method Details

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



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

def (username = nil, password = nil)
  target_required

  unless options[:non_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

  if options[:director_checks]
    director = Bosh::Cli::Director.new(target, username, password)

    if director.authenticated?
      say("Logged in as '#{username}'")
      logged_in = true
    else
      say("Cannot log in as '#{username}', please try again")
      unless options[:non_interactive]
        redirect(:misc, :login, username, nil)
      end
    end
  end

  if logged_in || !options[:director_checks]
    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}'")
end

#purge_cacheObject



122
123
124
125
126
127
128
129
130
# File 'lib/cli/commands/misc.rb', line 122

def purge_cache
  if cache.cache_dir != Bosh::Cli::DEFAULT_CACHE_DIR
    say("Cache directory '#{@cache.cache_dir}' differs from default, " +
        "please remove manually")
  else
    FileUtils.rm_rf(cache.cache_dir)
    say("Purged cache")
  end
end

#set_alias(name, value) ⇒ Object



190
191
192
193
194
# File 'lib/cli/commands/misc.rb', line 190

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

#set_target(director_url, name = nil) ⇒ Object



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
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/cli/commands/misc.rb', line 138

def set_target(director_url, name = nil)
  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 director_url == target
    say("Target already set to '#{full_target_name.green}'")
    return
  end

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

  if options[:director_checks]
    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
  else
    status = { "name" => "Unknown Director", "version" => "n/a" }
  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 '#{full_target_name.green}'")

  if interactive? && (config.username.blank? || config.password.blank?)
    redirect(:misc, :login)
  end
end

#show_targetObject



132
133
134
135
136
# File 'lib/cli/commands/misc.rb', line 132

def show_target
  say(target ?
          "Current target is '#{full_target_name.green}'" :
          "Target not set")
end

#statusObject



11
12
13
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
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cli/commands/misc.rb', line 11

def status
  if config.target && options[:director_checks]
    say("Updating director data...", " ")

    begin
      timeout(config.status_timeout || DEFAULT_STATUS_TIMEOUT) do
        director = Bosh::Cli::Director.new(config.target)
        status = director.get_status

        config.target_name = status["name"]
        config.target_version = status["version"]
        config.target_uuid = status["uuid"]
        config.save
        say("done".green)
      end
    rescue TimeoutError
      say("timed out".red)
    rescue => e
      say("error".red)
    end
    nl
  end

  target_name = full_target_name ? full_target_name.green : "not set".red
  target_uuid = config.target_uuid ? config.target_uuid.green : "n/a".red
  user = logged_in? ? username.green : "not set".red
  deployment = config.deployment ? config.deployment.green : "not set".red

  say("Target".ljust(15) + target_name)
  say("UUID".ljust(15) + target_uuid)
  say("User".ljust(15) + user)
  say("Deployment".ljust(15) + deployment)

  if in_release_dir?
    header("You are in release directory")

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

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

    say("Dev name:      %s" % [dev_name ? dev_name.green : "not set".red])
    say("Dev version:   %s" % [dev_version ?
                                 dev_version.to_s.green :
                                 "no versions yet".red])
    say("\n")
    say("Final name:    %s" % [final_name ?
                                   final_name.green :
                                   "not set".red])
    say("Final version: %s" % [final_version ?
                                 final_version.to_s.green :
                                 "no versions yet".red])

    say("\n")
    say("Packages")
    print_specs("package", "packages")

    say("\n")
    say("Jobs")
    print_specs("job", "jobs")
  end
end

#versionObject



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

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