File: vagrant.rb

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (85 lines) | stat: -rw-r--r-- 2,438 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require "vagrant/util/shell_quote"

module VagrantPlugins
  module DockerProvider
    module Executor
      # The Vagrant executor runs Docker over SSH against the given
      # Vagrant-managed machine.
      class Vagrant
        def initialize(host_machine)
          @host_machine = host_machine
        end

        def execute(*cmd, **opts, &block)
          quote = '"'
          cmd   = cmd.map do |a|
            "#{quote}#{::Vagrant::Util::ShellQuote.escape(a, quote)}#{quote}"
          end.join(" ")

          # If we want stdin, we just run in a full subprocess
          return ssh_run(cmd) if opts[:stdin]

          # Add a start fence so we know when to start reading output.
          # We have to do this because boot2docker outputs a login shell
          # boot2docker version that we get otherwise and messes up output.
          start_fence = "========== VAGRANT DOCKER BEGIN =========="
          ssh_cmd     = "echo -n \"#{start_fence}\"; #{cmd}"

          stderr = ""
          stdout = ""
          fenced = false
          comm   = @host_machine.communicate
          code   = comm.execute(ssh_cmd, error_check: false) do |type, data|
            next if ![:stdout, :stderr].include?(type)
            stderr << data if type == :stderr
            stdout << data if type == :stdout

            if !fenced
              index = stdout.index(start_fence)
              if index
                fenced = true

                index += start_fence.length
                stdout = stdout[index..-1]
                stdout.chomp!

                # We're now fenced, send all the data through
                if block
                  block.call(:stdout, stdout) if stdout != ""
                  block.call(:stderr, stderr) if stderr != ""
                end
              end
            else
              # If we're already fenced, just send the data through.
              block.call(type, data) if block && fenced
            end
          end

          if code != 0
            raise Errors::ExecuteError,
              command: cmd,
              stderr: stderr.chomp,
              stdout: stdout.chomp
          end

          stdout.chomp
        end

        def windows?
          false
        end

        protected

        def ssh_run(cmd)
          @host_machine.action(
            :ssh_run,
            ssh_run_command: cmd,
          )

          ""
        end
      end
    end
  end
end