Class: Dockdev::Config

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/dockdev/dockdev_config.rb

Overview

Content of the config file

which shall configure the to be run docker instance
Hence mostly the accessors/readers are related to docker configuration items

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val = {}) ⇒ Config

Returns a new instance of Config.



70
71
72
73
74
75
76
77
78
# File 'lib/dockdev/dockdev_config.rb', line 70

def initialize(val = {})
  @mounts = val[:mounts] || {}
  @ports = val[:ports] || {}
  @network = val[:network] || nil
  @dockerfile_entries = val[:dockerfile_entries] || []
  @workdir = val[:workdir] || "/opt"
  @skip_context = val[:skip_context] || []
  @activate_context = val[:activate_context] || []
end

Instance Attribute Details

#activate_contextObject (readonly)

since context activation is automated:

  1. skip_context shall skip all found automated activated context

  2. activate_context shall add those context which is not found by automated discovery



69
70
71
# File 'lib/dockdev/dockdev_config.rb', line 69

def activate_context
  @activate_context
end

#dockerfile_entriesObject (readonly)

for image



64
65
66
# File 'lib/dockdev/dockdev_config.rb', line 64

def dockerfile_entries
  @dockerfile_entries
end

#mountsObject (readonly)

for container



60
61
62
# File 'lib/dockdev/dockdev_config.rb', line 60

def mounts
  @mounts
end

#networkObject

Returns the value of attribute network.



61
62
63
# File 'lib/dockdev/dockdev_config.rb', line 61

def network
  @network
end

#portsObject (readonly)

for container



60
61
62
# File 'lib/dockdev/dockdev_config.rb', line 60

def ports
  @ports
end

#skip_contextObject (readonly)

since context activation is automated:

  1. skip_context shall skip all found automated activated context

  2. activate_context shall add those context which is not found by automated discovery



69
70
71
# File 'lib/dockdev/dockdev_config.rb', line 69

def skip_context
  @skip_context
end

#workdirObject

for image



57
58
59
# File 'lib/dockdev/dockdev_config.rb', line 57

def workdir
  @workdir
end

Instance Method Details

#add_mount(on_host, on_docker, opts = { duplicated_entry_policy: :error }) ⇒ Object

Add mount mapping of host => docker follows docker-cli which follows

docker cli -v format

Parameters:

  • on_host (String)

    path on host

  • on_docker (String)

    path on docker

  • opts (Hash) (defaults to: { duplicated_entry_policy: :error })

    options for the mount spec definition. Value keys including:

Options Hash (opts):

  • :duplicated_entry_policy (Symbol)

    :error (default) raise error if the on_host is duplicated/already defined

  • :duplicated_entry_policy (Symbol)

    :warn_replace raise warning if the on_host is duplicated/already defined and new entry shall replace the old entry

  • :duplicated_entry_policy (Symbol)

    :warn_discard raise warning if the on_host is duplicated/already defined and new entry is discarded



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dockdev/dockdev_config.rb', line 97

def add_mount(on_host, on_docker, opts = { duplicated_entry_policy: :error })
  if not_empty?(on_host) and not_empty?(on_docker)
    if @mounts.keys.include?(on_host)
      policy = opts[:duplicated_entry_policy] || :error 
      case policy
      when :warn_replace
        logger.warn "on_host '#{on_host}' was mapped to '#{@mounts[on_host]}'. It shall be replaced with '#{on_docker}'"    
        @mounts[on_host] = on_docker 

      when :warn_discard
        logger.warn "on_host '#{on_host}' already mapped to '#{@mounts[on_host]}'. New value on_docker is ignored."    

      else
        # default policy always raise error
        raise Error, "on_host '#{on_host}' already mapped to '#{@mounts[on_host]}'"
      end
    else
      @mounts[on_host] = on_docker 
    end
  else
    logger.debug "add_mount unsuccessful = on_host : #{on_host} / on_docker : #{on_docker}"
    raise Error, "on_host mount entry cannot be empty" if is_empty?(on_host)
    raise Error, "on_docker mount entry cannot be empty" if is_empty?(on_docker)
  end
end

#add_port(on_host, on_docker, opts = { duplicated_entry_policy: :error }) ⇒ Object

Add port mapping of host => docker follows docker-cli which follows

docker cli -p format

Parameters:

  • on_host (String)

    port on host

  • on_docker (String)

    port on docker

  • opts (Hash) (defaults to: { duplicated_entry_policy: :error })

    options for the port spec definition. Value keys including:

Options Hash (opts):

  • :duplicated_entry_policy (Symbol)

    :error (default) raise error if the on_host is duplicated/already defined

  • :duplicated_entry_policy (Symbol)

    :warn_replace raise warning if the on_host is duplicated/already defined and new entry shall replace the old entry

  • :duplicated_entry_policy (Symbol)

    :warn_discard raise warning if the on_host is duplicated/already defined and new entry is discarded



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dockdev/dockdev_config.rb', line 133

def add_port(on_host, on_docker, opts = { duplicated_entry_policy: :error })
  if not_empty?(on_host) and not_empty?(on_docker)
    if @ports.keys.include?(on_host)
      policy = opts[:duplicated_entry_policy] || :error 
      case policy
      when :warn_replace
        logger.warn "on_host '#{on_host}' was mapped to '#{@mounts[on_host]}'. It shall be replaced with '#{on_docker}'"    
        @ports[on_host] = on_docker 

      when :warn_discard
        logger.warn "on_host '#{on_host}' already mapped to '#{@mounts[on_host]}'. New value on_docker is ignored."    

      else
        # default policy always raise error
        raise Error, "on_host '#{on_host}' already mapped to '#{@mounts[on_host]}'"
      end
    else
      @ports[on_host] = on_docker 
    end
  else
    logger.debug "add_port unsuccessful = on_host : #{on_host} / on_docker : #{on_docker}"
    raise Error, "on_host port entry cannot be empty" if is_empty?(on_host)
    raise Error, "on_docker port entry cannot be empty" if is_empty?(on_docker)
  end

end

#append_Dockerfile(st) ⇒ Object

Any instruction to be appended into Dockerfile.

Note this did not presume the entry is RUN, COPY or anyting. 
A full valid Dockerfile entry has to be provided here

Parameters:

  • st (String)

    Full valid Dockerfile line to be embed into Dockerfile



165
166
167
168
# File 'lib/dockdev/dockdev_config.rb', line 165

def append_Dockerfile(st)
  logger.debug "Appending : #{st}"
  @dockerfile_entries << st
end

#is_context_should_skip?(name) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/dockdev/dockdev_config.rb', line 170

def is_context_should_skip?(name)
  @skip_context.include?(name)
end

#manual_activated_contextObject



175
176
177
# File 'lib/dockdev/dockdev_config.rb', line 175

def manual_activated_context
  @activate_context.freeze
end

#to_storageObject

Convert internal value into hash to be written to file to get rid of the object

encoding in the yaml file


83
84
85
# File 'lib/dockdev/dockdev_config.rb', line 83

def to_storage
  { mounts: @mounts, ports: @ports, dockerfile_entries: @dockerfile_entries, workdir: @workdir, skip_context: @skip_context, activate_context: @activate_context }
end