File: command_builder.rb

package info (click to toggle)
vagrant 2.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,072 kB
  • sloc: ruby: 80,731; sh: 369; makefile: 9; lisp: 1
file content (84 lines) | stat: -rw-r--r-- 2,176 bytes parent folder | download | duplicates (6)
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
module VagrantPlugins
  module Chef
    class CommandBuilder
      def self.command(type, config, options = {})
        new(type, config, options).command
      end

      attr_reader :type
      attr_reader :config
      attr_reader :options

      def initialize(type, config, options = {})
        @type    = type
        @config  = config
        @options = options.dup

        if type != :client && type != :solo
          raise "Unknown type `#{type.inspect}'!"
        end
      end

      def command
        if config.binary_env
          "#{config.binary_env} #{binary_path} #{args}"
        else
          "#{binary_path} #{args}"
        end
      end

      protected

      def binary_path
        binary_path = "chef-#{type}"

        if config.binary_path
          binary_path = File.join(config.binary_path, binary_path)
          if windows?
            binary_path = windows_friendly_path(binary_path)
          end
        end

        binary_path
      end

      def args
        args =  ""
        args << " --config #{provisioning_path("#{type}.rb")}"
        args << " --json-attributes #{provisioning_path("dna.json")}"
        args << " --local-mode" if options[:local_mode]
        args << " --legacy-mode" if options[:legacy_mode]
        args << " --log_level #{config.log_level}" if config.log_level
        args << " --no-color" if !options[:colored]

        if config.install && (config.version == :latest || config.version.to_s >= "11.0")
          args << " --force-formatter"
        end

        args << " #{config.arguments.strip}" if config.arguments

        args.strip
      end

      def provisioning_path(file)
        if windows?
          path = config.provisioning_path || "C:/vagrant-chef"
          return windows_friendly_path(File.join(path, file))
        else
          path = config.provisioning_path || "/tmp/vagrant-chef"
          return File.join(path, file)
        end
      end

      def windows_friendly_path(path)
        path = path.gsub("/", "\\")
        path = "c:#{path}" if path.start_with?("\\")
        return path
      end

      def windows?
        !!options[:windows]
      end
    end
  end
end