Class: Dockerun::DockerCli

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/dockerun/docker_cli.rb

Overview

Generate docker cli string

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, dfile) ⇒ DockerCli

Returns a new instance of DockerCli.



18
19
20
21
# File 'lib/dockerun/docker_cli.rb', line 18

def initialize(config, dfile)
  @_config = config
  @_dfile = dfile
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mtd, *args, &block) ⇒ Object (private)



109
110
111
112
113
# File 'lib/dockerun/docker_cli.rb', line 109

def method_missing(mtd, *args, &block)
  if @_config.respond_to?(mtd)
    @_config.send(mtd, *args, &block)
  end
end

Class Method Details

.docker_exeObject

Raises:



12
13
14
15
16
# File 'lib/dockerun/docker_cli.rb', line 12

def self.docker_exe
  exe = TTY::Which.which("docker")      
  raise Error, "Docker executable cannot be found. Please make sure it is installed and in the PATH" if is_empty?(exe)
  exe
end

Instance Method Details

#command_to_be_runObject



97
98
99
# File 'lib/dockerun/docker_cli.rb', line 97

def command_to_be_run
  @cmdToBeRun 
end

#container_nameObject



26
27
28
# File 'lib/dockerun/docker_cli.rb', line 26

def container_name
  @name.nil? ? "" : @name
end

#expose_portsObject



80
81
82
83
84
85
# File 'lib/dockerun/docker_cli.rb', line 80

def expose_ports
  if @_ports.nil?
    @_ports = {}
  end
  @_ports
end

#generated_container_nameObject



32
33
34
35
36
37
# File 'lib/dockerun/docker_cli.rb', line 32

def generated_container_name
  if @_gen_cont_name.nil?
    @_gen_cont_name = "#{File.basename(Dir.getwd)}_cont_name"
  end
  @_gen_cont_name
end

#has_exposed_ports?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/dockerun/docker_cli.rb', line 86

def has_exposed_ports?
  expose_ports.size > 0
end

#interactive(bool = true) ⇒ Object



46
47
48
# File 'lib/dockerun/docker_cli.rb', line 46

def interactive(bool = true)
  @interactive = bool
end

#is_container_name_given?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/dockerun/docker_cli.rb', line 29

def is_container_name_given?
  not_empty?(container_name)
end

#is_interactive?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/dockerun/docker_cli.rb', line 49

def is_interactive?
  @interactive.nil? ? false : true
end

#keep(bool = true) ⇒ Object



39
40
41
# File 'lib/dockerun/docker_cli.rb', line 39

def keep(bool = true)
  @keep = bool
end

#keep_container?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dockerun/docker_cli.rb', line 42

def keep_container?
  @keep.nil? ? true : @keep
end

#listener(&block) ⇒ Object



90
91
92
# File 'lib/dockerun/docker_cli.rb', line 90

def listener(&block)
  @listener = block
end

#mount(*mnt) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dockerun/docker_cli.rb', line 53

def mount(*mnt)
  mnt.each do |mm|
    mm.each do |on_host, on_docker|
      case on_host
      when :current_dir 
        hostPath = Dir.getwd 
      else
        hostPath = File.expand_path(on_host) 
      end

      req_mounts[hostPath] = on_docker

    end
  end
end

#mountsObject



68
69
70
# File 'lib/dockerun/docker_cli.rb', line 68

def mounts
  req_mounts
end

#port(*ports) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/dockerun/docker_cli.rb', line 72

def port(*ports)
  ports.each do |port|
    raise Error, "Hash of hostPort => dockerPort is required" if not port.is_a?(Hash)
    port.each do |hostPort, dockerPort|
      expose_ports[hostPort] = dockerPort
    end
  end
end

#run_in_docker(cmd) ⇒ Object



94
95
96
# File 'lib/dockerun/docker_cli.rb', line 94

def run_in_docker(cmd)
  @cmdToBeRun = cmd
end

#set_container_name(val) ⇒ Object



23
24
25
# File 'lib/dockerun/docker_cli.rb', line 23

def set_container_name(val)
  @name = val 
end

#trigger_listener(evt, *args, &block) ⇒ Object



101
102
103
104
105
106
# File 'lib/dockerun/docker_cli.rb', line 101

def trigger_listener(evt, *args, &block)
  if not @listener.nil?
    args << { docker_cli: self }
    @listener.call(evt, *args, &block)
  end
end