Class: Vagrant::Smartos::Zones::Communicator::Smartos

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/smartos/zones/communicator/smartos.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Smartos

Returns a new instance of Smartos.



8
9
10
11
# File 'lib/vagrant/smartos/zones/communicator/smartos.rb', line 8

def initialize(machine)
  @machine = machine
  super
end

Instance Method Details

#gz_execute(command, opts = {}, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
63
64
65
66
67
68
69
70
71
# File 'lib/vagrant/smartos/zones/communicator/smartos.rb', line 14

def gz_execute(command, opts = {}, &block)
  opts = {
    error_check: true,
    error_class: Vagrant::Errors::VagrantError,
    error_key:   :ssh_bad_exit_status,
    good_exit:   0,
    command:     command,
    shell:       nil,
    sudo:        false
  }.merge(opts)

  opts[:good_exit] = Array(opts[:good_exit])

  # Connect via SSH and execute the command in the shell.
  stdout = ''
  stderr = ''
  global_zone_connector.connect

  begin
    generic_ssh_info = @connection_ssh_info
    @connection_ssh_info = global_zone_connector.ssh_info

    exit_status = global_zone_connector.with_connection do |connection|
      shell_opts = {
        sudo: opts[:sudo],
        shell: opts[:shell]
      }

      shell_execute(connection, command, **shell_opts) do |type, data|
        if type == :stdout
          stdout += data
        elsif type == :stderr
          stderr += data
        end

        block.call(type, data) if block
      end
    end
  ensure
    @connection_ssh_info = generic_ssh_info
  end

  # Check for any errors
  if opts[:error_check] && !opts[:good_exit].include?(exit_status)
    # The error classes expect the translation key to be _key,
    # but that makes for an ugly configuration parameter, so we
    # set it here from `error_key`
    error_opts = opts.merge(
      _key: opts[:error_key],
      stdout: stdout,
      stderr: stderr
    )
    raise opts[:error_class], error_opts
  end

  # Return the exit status
  exit_status
end

#gz_test(command, opts = nil) ⇒ Object



73
74
75
76
# File 'lib/vagrant/smartos/zones/communicator/smartos.rb', line 73

def gz_test(command, opts = nil)
  opts = { error_check: false }.merge(opts || {})
  gz_execute(command, opts) == 0
end