class Bosh::Director::Jobs::Ssh

Constants

DEFAULT_SSH_DATA_LIFETIME
SSH_TAG

Public Class Methods

job_type() click to toggle source
# File lib/bosh/director/jobs/ssh.rb, line 9
def self.job_type
  :ssh
end
new(deployment_id, options = {}) click to toggle source
# File lib/bosh/director/jobs/ssh.rb, line 13
def initialize(deployment_id, options = {})
  @deployment_id = deployment_id
  @target_payload = options["target"]
  @command = options["command"]
  @params = options["params"]
  @blobstore = options.fetch(:blobstore) { App.instance.blobstores.blobstore }
  @instance_manager = Api::InstanceManager.new
end

Public Instance Methods

perform() click to toggle source
# File lib/bosh/director/jobs/ssh.rb, line 22
def perform
  target = Target.new(@target_payload)

  filter = {}
  filter[:job] = target.job if target.job
  filter.merge!(target.id_filter)

  deployment = Models::Deployment[@deployment_id]
  instances = @instance_manager.filter_by(deployment, filter)

  ssh_info = instances.map do |instance|
    begin
      agent = @instance_manager.agent_client_for(instance)

      logger.info("ssh #{@command} '#{instance.job}/#{instance.uuid}'")
      result = agent.ssh(@command, @params)
      if target.ids_provided?
        result["id"] = instance.uuid
      else
        result["index"] = instance.index
      end

      if Config.default_ssh_options
        result["gateway_host"] = Config.default_ssh_options["gateway_host"]
        result["gateway_user"] = Config.default_ssh_options["gateway_user"]
      end

      result
    rescue Exception => e
      raise e
    ensure
      add_event(deployment.name, instance.name, e)
    end
  end

  result_file.write(JSON.generate(ssh_info))
  result_file.write("\n")

  # task result
  nil
end

Private Instance Methods

add_event(deployment_name, instance_name, error = nil) click to toggle source
# File lib/bosh/director/jobs/ssh.rb, line 66
def add_event(deployment_name, instance_name, error = nil)
  user =  @params['user_regex'] || @params['user']
  event_manager.create_event(
      {
          user:        username,
          action:      "#{@command} ssh",
          object_type: 'instance',
          object_name: instance_name,
          task:        task_id,
          error:       error,
          deployment:  deployment_name,
          instance:    instance_name,
          context:     {user: user}
      })
end