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.



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

def initialize(machine)
  @machine = machine
  super
end

Instance Method Details

#gz_download(from, to = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant/smartos/zones/communicator/smartos.rb', line 14

def gz_download(from, to = nil)
  @logger.debug("Downloading from global zone: #{from} to #{to}")

  if gz_rsync_connector.available?
    gz_rsync_connector.download(from, to)
  else
    gz_scp_connect do |scp|
      scp.download!(from, to) do |_ch, name, sent, total|
        percent = (sent.to_f / total) * 100
        print "#{name}: #{sent}/#{total} : #{percent.to_i}%\r"
        $stdout.flush
      end
    end
  end
end

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

rubocop:disable Metrics/MethodLength



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vagrant/smartos/zones/communicator/smartos.rb', line 47

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_scp_connectObject



106
107
108
109
110
111
112
113
# File 'lib/vagrant/smartos/zones/communicator/smartos.rb', line 106

def gz_scp_connect
  global_zone_connector.with_connection do |connection|
    yield connection.scp
  end
rescue Net::SCP::Error => e
  raise Vagrant::Errors::SCPUnavailable if e.message =~ /\(127\)/
  raise
end

#gz_test(command, opts = nil) ⇒ Object



115
116
117
118
# File 'lib/vagrant/smartos/zones/communicator/smartos.rb', line 115

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

#gz_upload(from, to) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant/smartos/zones/communicator/smartos.rb', line 30

def gz_upload(from, to)
  @logger.debug("Uploading to global zone: #{from} to #{to}")

  if gz_rsync_connector.available?
    gz_rsync_connector.upload(from, to)
  else
    gz_scp_connect do |scp|
      scp.upload!(from, to) do |_ch, name, sent, total|
        percent = (sent.to_f / total) * 100
        print "#{name}: #{sent}/#{total} : #{percent.to_i}%\r"
        $stdout.flush
      end
    end
  end
end