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

#build_manifest, #cancel_deployment, #deployment_changed?, #inspect_deployment_changes, #job_exists_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
22
# 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 = []
  @info = {}
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

#infoObject

Returns the value of attribute info.



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

def info
  @info
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



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

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

#blob_managerObject



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

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

#blobstoreObject



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

def blobstore
  release.blobstore
end

#cache_dirObject



131
132
133
# File 'lib/cli/base_command.rb', line 131

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

#configBosh::Cli::Config

Returns Current configuration.

Returns:



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

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)


95
96
97
98
# File 'lib/cli/base_command.rb', line 95

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

#credentialsObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cli/base_command.rb', line 114

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



110
111
112
# File 'lib/cli/base_command.rb', line 110

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

#directorObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/cli/base_command.rb', line 42

def director
  return @director if @director

  director_client_options = [:no_track, :ca_cert]
  @director = Bosh::Cli::Client::Director.new(
    target,
    credentials,
    @options.select { |k, _| director_client_options.include?(k) }
  )
end

#interactive?Boolean

Returns:

  • (Boolean)


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

def interactive?
  !non_interactive?
end

#logged_in?Boolean

Returns:

  • (Boolean)


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

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

#non_interactive?Boolean

Returns:

  • (Boolean)


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

def non_interactive?
  options[:non_interactive]
end

#progress_rendererObject



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

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

#redirect(*args) ⇒ Object



87
88
89
# File 'lib/cli/base_command.rb', line 87

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

#releaseObject



53
54
55
56
57
# File 'lib/cli/base_command.rb', line 53

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

#remove_option(name) ⇒ Object



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

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

#run_nested_command(*args) ⇒ Object



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

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

#show_current_state(deployment_name = nil) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/cli/base_command.rb', line 135

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



101
102
103
104
105
# File 'lib/cli/base_command.rb', line 101

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



127
128
129
# File 'lib/cli/base_command.rb', line 127

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

#verbose?Boolean

Returns:

  • (Boolean)


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

def verbose?
  @options[:verbose]
end