File: vagrant_wrapper.rb

package info (click to toggle)
ruby-sshkit 1.21.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 700 kB
  • sloc: ruby: 3,522; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,254 bytes parent folder | download | duplicates (3)
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
class VagrantWrapper
  class << self
    def hosts
      @vm_hosts ||= begin
        result = {}

        boxes = boxes_list

        unless running?
          boxes.map! do |box|
            box['user'] = ENV['USER']
            box['port'] = '22'
            box
          end
        end

        boxes.each do |vm|
          result[vm['name']] = vm_host(vm)
        end

        result
      end
    end

    def running?
      @running ||= begin
        status = `#{vagrant_binary} status`
        status.include?('running')
      end
    end

    def boxes_list
      json_config_path = File.join('test', 'boxes.json')
      boxes = File.open(json_config_path).read
      JSON.parse(boxes)
    end

    def vagrant_binary
      'vagrant'
    end

    private

    def vm_host(vm)
      host_options = {
          user: vm['user'] || 'vagrant',
          hostname: vm['hostname'] || 'localhost',
          port: vm['port'] || '22',
          password: vm['password'] || 'vagrant',
          ssh_options: host_verify_options
      }

      SSHKit::Host.new(host_options)
    end

    def host_verify_options
      if Net::SSH::Version::MAJOR >= 5
        { verify_host_key: :never }
      else
        { paranoid: false }
      end
    end
  end
end