Class: Bosh::Cli::Command::Base

Inherits:
Object
  • Object
show all
Extended by:
Bosh::Cli::CommandDiscovery
Includes:
DeploymentHelper
Defined in:
lib/cli/base_command.rb

Constant Summary collapse

DEFAULT_DIRECTOR_PORT =
25555

Instance Attribute Summary collapse

Instance Method Summary collapse

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, #prepare_deployment_manifest, #prompt_for_errand_name, #prompt_for_job_and_index

Constructor Details

#initialize(runner = nil, director = nil) ⇒ Base

Returns a new instance of Base.

Parameters:



13
14
15
16
17
18
19
20
21
# File 'lib/cli/base_command.rb', line 13

def initialize(runner = nil, director = nil)
  @runner = runner
  @director = director
  @options = {}
  @work_dir = Dir.pwd
  @exit_code = 0
  @out = nil
  @args = []
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



8
9
10
# File 'lib/cli/base_command.rb', line 8

def exit_code
  @exit_code
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#outObject

Returns the value of attribute out.



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

def out
  @out
end

#runnerObject (readonly)

Returns the value of attribute runner.



8
9
10
# File 'lib/cli/base_command.rb', line 8

def runner
  @runner
end

#work_dirObject (readonly)

Returns the value of attribute work_dir.



8
9
10
# File 'lib/cli/base_command.rb', line 8

def work_dir
  @work_dir
end

Instance Method Details

#add_option(name, value) ⇒ Object



33
34
35
# File 'lib/cli/base_command.rb', line 33

def add_option(name, value)
  @options[name] = value
end

#blob_managerObject



56
57
58
# File 'lib/cli/base_command.rb', line 56

def blob_manager
  @blob_manager ||= Bosh::Cli::BlobManager.new(release, config.max_parallel_downloads, progress_renderer)
end

#blobstoreObject



60
61
62
# File 'lib/cli/base_command.rb', line 60

def blobstore
  release.blobstore
end

#cache_dirObject



121
122
123
# File 'lib/cli/base_command.rb', line 121

def cache_dir
  File.join(Dir.home, '.bosh', 'cache')
end

#configBosh::Cli::Config

Returns Current configuration.

Returns:



24
25
26
27
28
29
30
31
# File 'lib/cli/base_command.rb', line 24

def config
  @config ||= begin
    # Handle the environment variable being set to the empty string.
    env_bosh_config = ENV['BOSH_CONFIG'].to_s.empty? ? nil : ENV['BOSH_CONFIG']
    config_file = options[:config] || env_bosh_config || Bosh::Cli::DEFAULT_CONFIG_PATH
    Bosh::Cli::Config.new(config_file)
  end
end

#confirmed?(question = 'Are you sure?') ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/cli/base_command.rb', line 84

def confirmed?(question = 'Are you sure?')
  return true if non_interactive?
  ask("#{question} (type 'yes' to continue): ") == 'yes'
end

#credentialsObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cli/base_command.rb', line 103

def credentials
  return @credentials if @credentials

  if auth_info.uaa?
    token_decoder = Client::Uaa::TokenDecoder.new
    uaa_token_provider = Client::Uaa::TokenProvider.new(auth_info, config, token_decoder, target)
    @credentials = Client::UaaCredentials.new(uaa_token_provider)
  elsif username && password
    @credentials = Client::BasicCredentials.new(username, password)
  end

  @credentials
end

#deploymentString

Returns Deployment manifest path.

Returns:

  • (String)

    Deployment manifest path



99
100
101
# File 'lib/cli/base_command.rb', line 99

def deployment
  options[:deployment] || config.deployment
end

#directorObject



41
42
43
44
# File 'lib/cli/base_command.rb', line 41

def director
  @director ||= Bosh::Cli::Client::Director.new(
    target, credentials, @options.select { |k, _| k == :no_track })
end

#interactive?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/cli/base_command.rb', line 72

def interactive?
  !non_interactive?
end

#logged_in?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/cli/base_command.rb', line 64

def logged_in?
  !!(credentials && credentials.authorization_header)
end

#non_interactive?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/cli/base_command.rb', line 68

def non_interactive?
  options[:non_interactive]
end

#progress_rendererObject



52
53
54
# File 'lib/cli/base_command.rb', line 52

def progress_renderer
  interactive? ? Bosh::Cli::InteractiveProgressRenderer.new : Bosh::Cli::NonInteractiveProgressRenderer.new
end

#redirect(*args) ⇒ Object



80
81
82
# File 'lib/cli/base_command.rb', line 80

def redirect(*args)
  Bosh::Cli::Runner.new(args, @options).run
end

#releaseObject



46
47
48
49
50
# File 'lib/cli/base_command.rb', line 46

def release
  return @release if @release
  check_if_release_dir
  @release = Bosh::Cli::Release.new(release_directory, options[:final])
end

#remove_option(name) ⇒ Object



37
38
39
# File 'lib/cli/base_command.rb', line 37

def remove_option(name)
  @options.delete(name)
end

#show_current_state(deployment_name = nil) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/cli/base_command.rb', line 125

def show_current_state(deployment_name=nil)
  user_desc = auth_info.client_auth? ? 'client' : 'user'
  msg = "Acting as #{user_desc} '#{credentials.username.to_s.make_green}'"
  msg += " on deployment '#{deployment_name.make_green}'" if deployment_name
  msg += " on '#{target_name.make_green}'" if target_name
  warn(msg)
end

#targetString Also known as: target_url

Returns Target director URL.

Returns:

  • (String)

    Target director URL



90
91
92
93
94
# File 'lib/cli/base_command.rb', line 90

def target
  raw_url = options[:target] || config.target
  url = config.resolve_alias(:target, raw_url) || raw_url
  url ? normalize_url(url) : nil
end

#target_nameObject



117
118
119
# File 'lib/cli/base_command.rb', line 117

def target_name
  options[:target] || config.target_name || target_url
end

#verbose?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/cli/base_command.rb', line 76

def verbose?
  @options[:verbose]
end