Class: Kamaze::Project::Tools::Vagrant::Remote

Inherits:
Shell show all
Defined in:
lib/kamaze/project/tools/vagrant/remote.rb,
lib/kamaze/project/tools/vagrant/remote.rb

Overview

Provide remote command execution (ssh)

Commands can use aliases through configuration: box_id.ssh.aliases; where box_id is the box identifier.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boxes, options = {}) ⇒ Remote

Initialize remote shell with given boxes and options

Parameters:

  • boxes (Hash)
  • options (Hash) (defaults to: {})


29
30
31
32
33
# File 'lib/kamaze/project/tools/vagrant/remote.rb', line 29

def initialize(boxes, options = {})
  @boxes = boxes

  super(options)
end

Instance Attribute Details

#boxesHash (readonly)

Returns:

  • (Hash)


23
24
25
# File 'lib/kamaze/project/tools/vagrant/remote.rb', line 23

def boxes
  @boxes
end

Instance Method Details

#apply_alias(box_id, command) ⇒ String|nil (protected)

Apply alias on command for given box_id

Parameters:

  • box_id (String)
  • command (String|nil)

Returns:

  • (String|nil)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kamaze/project/tools/vagrant/remote.rb', line 65

def apply_alias(box_id, command)
  return unless command

  conf = boxes.fetch(box_id.to_s) # fetch related config
  args = Shellwords.split(command) # command split into words
  exeb = conf['ssh']['aliases'][args[0]] # executable
  if exeb
    command = Shellwords.split(exeb)
                        .concat(args.drop(1))
                        .yield_self { |c| Shellwords.shelljoin(c) }
  end

  command
end

#execute(*params, &block) ⇒ Object

Run a command remotely on box identified by box_id

Sample of use:

remote.execute(:freebsd)
remote.execute(:freebsd, 'rake clobber')

At least, one argument is required.



50
51
52
53
54
55
56
# File 'lib/kamaze/project/tools/vagrant/remote.rb', line 50

def execute(*params, &block)
  box_id  = params.fetch(0)
  command = apply_alias(box_id, params[1]) # remote command
  params  = command ? [box_id, '-c', command] : [box_id]

  super(*params, &block)
end

#to_aArray

Returns:

  • (Array)


36
37
38
# File 'lib/kamaze/project/tools/vagrant/remote.rb', line 36

def to_a
  super.push('ssh')
end