class Bosh::Cli::Command::Base

Constants

DEFAULT_DIRECTOR_PORT

Attributes

args[RW]
exit_code[R]
info[RW]
options[RW]
out[RW]
runner[R]
work_dir[R]

Public Class Methods

new(runner = nil, director = nil) click to toggle source

@param [Bosh::Cli::Runner] runner

# 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

Public Instance Methods

add_option(name, value) click to toggle source
# File lib/cli/base_command.rb, line 34
def add_option(name, value)
  @options[name] = value
end
blob_manager() click to toggle source
# 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
blobstore() click to toggle source
# File lib/cli/base_command.rb, line 67
def blobstore
  release.blobstore
end
cache_dir() click to toggle source
# File lib/cli/base_command.rb, line 131
def cache_dir
  File.join(Dir.home, '.bosh', 'cache')
end
config() click to toggle source

@return [Bosh::Cli::Config] Current configuration

# 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?') click to toggle source
# 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
credentials() click to toggle source
# 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
deployment() click to toggle source

@return [String] Deployment manifest path

# File lib/cli/base_command.rb, line 110
def deployment
  options[:deployment] || config.deployment
end
director() click to toggle source
# 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?() click to toggle source
# File lib/cli/base_command.rb, line 79
def interactive?
  !non_interactive?
end
logged_in?() click to toggle source
# File lib/cli/base_command.rb, line 71
def logged_in?
  !!(credentials && credentials.authorization_header)
end
non_interactive?() click to toggle source
# File lib/cli/base_command.rb, line 75
def non_interactive?
  options[:non_interactive]
end
progress_renderer() click to toggle source
# File lib/cli/base_command.rb, line 59
def progress_renderer
  interactive? ? Bosh::Cli::InteractiveProgressRenderer.new : Bosh::Cli::NonInteractiveProgressRenderer.new
end
redirect(*args) click to toggle source
# File lib/cli/base_command.rb, line 87
def redirect(*args)
  Bosh::Cli::Runner.new(args, @options).run
end
release() click to toggle source
# 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) click to toggle source
# File lib/cli/base_command.rb, line 38
def remove_option(name)
  @options.delete(name)
end
run_nested_command(*args) click to toggle source
# 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) click to toggle source
# 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
target() click to toggle source

@return [String] Target director URL

# 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
Also aliased as: target_url
target_name() click to toggle source
# File lib/cli/base_command.rb, line 127
def target_name
  options[:target] || config.target_name || target_url
end
target_url()
Alias for: target
verbose?() click to toggle source
# File lib/cli/base_command.rb, line 83
def verbose?
  @options[:verbose]
end

Protected Instance Methods

auth_info() click to toggle source
# File lib/cli/base_command.rb, line 145
def auth_info
  @auth_info ||= begin
    ca_cert = options[:ca_cert] || config.ca_cert(target)
    director_client = Client::Director.new(target, nil, ca_cert: ca_cert)
    Client::Uaa::AuthInfo.new(director_client, ENV, ca_cert)
  end
end
auth_required() click to toggle source
# File lib/cli/base_command.rb, line 192
def auth_required
  target_required
  err('Please log in first') unless logged_in?
end
check_if_release_dir() click to toggle source
# File lib/cli/base_command.rb, line 219
def check_if_release_dir
  unless in_release_dir?
    err("Sorry, your current directory doesn't look like release directory")
  end
end
deployment_required() click to toggle source
# File lib/cli/base_command.rb, line 201
def deployment_required
  err('Please choose deployment first') if deployment.nil?
end
dirty_state?() click to toggle source
# File lib/cli/base_command.rb, line 250
def dirty_state?
  git_status = `git status 2>&1`
  case $?.exitstatus
    when 128 # Not in a git repo
      false
    when 127 # git command not found
      false
    else
      !git_status.lines.to_a.last.include?('nothing to commit')
  end
end
in_release_dir?() click to toggle source
# File lib/cli/base_command.rb, line 244
def in_release_dir?
  File.directory?(File.join(release_directory, 'packages')) &&
    File.directory?(File.join(release_directory, 'jobs')) &&
    File.directory?(File.join(release_directory, 'src'))
end
no_track_unsupported() click to toggle source
# File lib/cli/base_command.rb, line 209
def no_track_unsupported
  if @options.delete(:no_track)
    say("Ignoring '" + '--no-track'.make_yellow + "' option")
  end
end
normalize_url(url) click to toggle source
# File lib/cli/base_command.rb, line 262
def normalize_url(url)
  url = url.gsub(/\/$/, '')
  url = "https://#{url}" unless url.match(/^http:?/)
  uri = URI.parse(url)

  if port = url.match(/:(\d+)$/)
    port_number = port.captures[0].to_i
    if port_number == URI::HTTPS::DEFAULT_PORT
      uri.to_s + ":#{URI::HTTPS::DEFAULT_PORT}"
    else
      uri.port = port_number
      uri.to_s
    end
  else
    uri.port = DEFAULT_DIRECTOR_PORT
    uri.to_s
  end
  end
password() click to toggle source

@return [String] Director password

# File lib/cli/base_command.rb, line 159
def password
  options[:password] || ENV['BOSH_PASSWORD'] || config.password(target)
end
raise_dirty_state_error() click to toggle source
# File lib/cli/base_command.rb, line 225
def raise_dirty_state_error
  say("\n%s\n" % [`git status`])
  err('Your current directory has some local modifications, ' +
      "please discard or commit them first.\n\n" +
      'Use the --force option to skip this check.')
end
release_directory() click to toggle source
# File lib/cli/base_command.rb, line 232
def release_directory
  return @release_directory if @release_directory

  if options[:dir]
    @release_directory = File.expand_path(options[:dir])
  else
    @release_directory = @work_dir
  end

  @release_directory
end
show_deployment() click to toggle source
# File lib/cli/base_command.rb, line 205
def show_deployment
  say("Current deployment is #{deployment.make_green}")
end
switch_to_release_dir() click to toggle source
# File lib/cli/base_command.rb, line 215
def switch_to_release_dir
  Dir.chdir(release_directory)
end
target_required() click to toggle source
# File lib/cli/base_command.rb, line 197
def target_required
  err('Please choose target first') if target.nil?
end
task_report(status, task_id, success_msg = nil, error_msg = nil) click to toggle source

Prints director task completion report. Note that event log usually contains pretty detailed error report and other UI niceties, so most of the time this could just do nothing @param [Symbol] status Task status @param [#to_s] task_id Task ID

# File lib/cli/base_command.rb, line 168
def task_report(status, task_id, success_msg = nil, error_msg = nil)
  case status
    when :non_trackable
      report = "Can't track director task".make_red
    when :track_timeout
      report = 'Task tracking timeout'.make_red
    when :running
      report = "Task #{task_id.make_yellow} running"
    when :error
      report = error_msg
    when :done
      report = success_msg
    else
      report = "Task exited with status #{status}"
  end

  unless [:running, :done].include?(status)
    @exit_code = 1
  end

  say("\n#{report}") if report
  say("\nFor a more detailed error report, run: bosh task #{task_id} --debug") if status == :error
end
username() click to toggle source

@return [String] Director username

# File lib/cli/base_command.rb, line 154
def username
  options[:username] || ENV['BOSH_USER'] || config.username(target)
end