Class: Dockdev::Context::DockerCompose

Inherits:
Object
  • Object
show all
Defined in:
lib/dockdev/context/docker-compose.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DockerCompose

Returns a new instance of DockerCompose.



14
15
16
17
18
19
20
# File 'lib/dockdev/context/docker-compose.rb', line 14

def initialize(path)
  @path = path
  @parsed = false
  @mounts = {}
  @ports = {}
  @pmt = TTY::Prompt.new
end

Class Method Details

.init_path(path) ⇒ Object



10
11
12
# File 'lib/dockdev/context/docker-compose.rb', line 10

def self.init_path(path)
  DockerCompose.new(path)
end

Instance Method Details

#apply_context(dockdev_config) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dockdev/context/docker-compose.rb', line 91

def apply_context(dockdev_config)
  ddConf = dockdev_config
  
  if not ddConf.nil?
    mounts, ports = parse_docker_compose
    mounts.each do |host, docker|
      ddConf.add_mount(host, docker)
    end
    ports.each do |host, docker|
      ddConf.add_port(host, docker)
    end
  end

  ddConf
end

#find_docker_composeObject



26
27
28
# File 'lib/dockdev/context/docker-compose.rb', line 26

def find_docker_compose
  Dir.glob(File.join(@path,"docker-compose.*")).grep(/\.(yml|yaml)$/)
end

#is_context?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/dockdev/context/docker-compose.rb', line 22

def is_context?
  not find_docker_compose.empty?
end

#parse_docker_composeObject



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
86
87
88
89
# File 'lib/dockdev/context/docker-compose.rb', line 30

def parse_docker_compose
  if not @parsed 
    fdc = find_docker_compose

    begin
      if not fdc.empty?
        load_dc = @pmt.yes?("\n docker-compose file found. Load config? ".magenta)  

        if not load_dc
          @parsed = true
          return [@mounts, @ports]
        end
      end

      if fdc.length > 1
        @selected = @pmt.multi_select(" Please select docker-compose file(s) to parse for running config : ".magenta) do |mn|
          fdc.each do |f|
            mn.choice File.basename(f), f
          end
        end
      else
        @selected = fdc
      end

    rescue TTY::Reader::InputInterrupt
      # Ctrl-C is pressed... exit
      exit(-1)
    end

    @selected.each do |dcf|
      logger.debug " Processing docker-compose : #{dcf}"
      dcc = YAML.load(File.read(dcf))
      dcc.each do |k,v|
        next if k == "version"
        v.each do |kk,vv|
          vv.each do |kkk, vvv|
            if kkk == "volumes"
              vvv.each do |vs|
                logger.debug "Extracting mount point from #{dcf} : #{vs}"
                vvs = vs.split(":")
                @mounts[File.expand_path(vvs[0])] = vvs[1]
              end
            elsif kkk == "ports"
              vvv.each do |vs|
                logger.debug "Extracting ports from #{dcf} : #{vs}"
                ps = vs.split(":")
                @ports[ps[0]] = ps[1]
              end
            end
          end
        end
      end
 
    end

    @parsed = true
  end

  [@mounts, @ports]
end