Class: MofaCmd

Inherits:
Object
  • Object
show all
Defined in:
lib/mofa/mofa_cmd.rb

Direct Known Subclasses

ProvisionCmd, UploadCmd

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, cookbook) ⇒ MofaCmd

Returns a new instance of MofaCmd.



13
14
15
16
# File 'lib/mofa/mofa_cmd.rb', line 13

def initialize(token, cookbook)
  @token = token
  @cookbook = cookbook
end

Instance Attribute Details

#cookbookObject

Returns the value of attribute cookbook.



6
7
8
# File 'lib/mofa/mofa_cmd.rb', line 6

def cookbook
  @cookbook
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/mofa/mofa_cmd.rb', line 7

def options
  @options
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/mofa/mofa_cmd.rb', line 5

def token
  @token
end

Class Method Details

.generate_tokenObject



9
10
11
# File 'lib/mofa/mofa_cmd.rb', line 9

def self.generate_token
  Digest::SHA1.hexdigest([Time.now, rand].join)[0..10]
end

Instance Method Details

#cleanupObject

This method is abstract.

Raises:

  • (RuntimeError)


29
30
31
# File 'lib/mofa/mofa_cmd.rb', line 29

def cleanup
  raise RuntimeError, 'must be implemented'
end

#execute(_ssh_port) ⇒ Object

This method is abstract.

Raises:

  • (RuntimeError)


24
25
26
# File 'lib/mofa/mofa_cmd.rb', line 24

def execute(_ssh_port)
  raise RuntimeError, 'must be implemented'
end

#prepareObject

This method is abstract.

Raises:

  • (RuntimeError)


19
20
21
# File 'lib/mofa/mofa_cmd.rb', line 19

def prepare
  raise RuntimeError, 'must be implemented'
end

#ssh_exec!(ssh, command) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mofa/mofa_cmd.rb', line 33

def ssh_exec!(ssh, command)
  stdout_data = ''
  stderr_data = ''
  exit_code = nil
  exit_signal = nil
  ssh.open_channel do |channel|
    channel.exec(command) do |_ch, success|
      unless success
        abort "FAILED: couldn't execute command (ssh.channel.exec)"
      end
      channel.on_data do |_ch, data|
        stdout_data += data
      end

      channel.on_extended_data do |_ch, _type, data|
        stderr_data += data
      end

      channel.on_request('exit-status') do |_ch, data|
        exit_code = data.read_long
      end

      channel.on_request('exit-signal') do |_ch, data|
        exit_signal = data.read_long
      end
    end
  end
  ssh.loop
  [exit_code, stdout_data, stderr_data, exit_signal]
end