Module: DockerCore::Swarm

Defined in:
lib/docker_core.rb

Class Method Summary collapse

Class Method Details

.capture_command(*arguments, environment: {}, bind: {}, throw: false, wait: 0, strip: true, echo: false) ⇒ String

noinspection RubyUnusedLocalVariable

Parameters:

  • arguments (Array)
  • environment (Hash) (defaults to: {})
  • bind (Hash) (defaults to: {})
  • throw (Boolean) (defaults to: false)
  • wait (Numeric) (defaults to: 0)
  • strip (Boolean) (defaults to: true)
  • echo (Boolean) (defaults to: false)

Returns:

  • (String)


238
239
240
# File 'lib/docker_core.rb', line 238

def self.capture_command(*arguments, environment: {}, bind: {}, throw: false, wait: 0, strip: true, echo: false)
  Error.no_method(__method__)
end

.check_name(name, echo: false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/docker_core.rb', line 128

def self.check_name(name, echo: false)
  color = Color::YELLOW
  name = "#{name}"
  items = name.chars

  if items.empty? || 63 < items.length
    if echo
      Color.echo("'#{name}' should be between 1 and 63 characters long", color)
    end

    return false
  end

  if [items.first, items.last].include?('-')
    if echo
      Color.echo("'#{name}' should not start or end with a hyphen(-)", color)
    end

    return false
  end

  test = name.match?(/^[a-zA-Z0-9-]+$/)

  if false == test && echo
    Color.echo("'#{name}' should be a-z or A-Z or 0-9 and hyphen (-)", color)
  end

  return test
end

.deploy_pathObject



108
109
110
# File 'lib/docker_core.rb', line 108

def self.deploy_path
  return File.expand_path('~/deploy')
end

.detect_orchestratorObject



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/docker_core.rb', line 172

def self.detect_orchestrator
  swarm = self.read_swarm.to_sym
  detect = self.detect_services

  if detect.has_key?(swarm) && detect[swarm]
    return "#{swarm}"
  end

  index = detect.key(true)
  return "#{index}"
end

.detect_servicesObject



168
169
170
# File 'lib/docker_core.rb', line 168

def self.detect_services
  return { docker: Shell.is_active_unit('docker') }
end

.from_image(image, registry: REGISTRY, namespace: NAMESPACE, tag: 'latest') ⇒ Object

Parameters:

  • image (String)
  • registry (String) (defaults to: REGISTRY)
  • namespace (String) (defaults to: NAMESPACE)
  • tag (String) (defaults to: 'latest')


104
105
106
# File 'lib/docker_core.rb', line 104

def self.from_image(image, registry: REGISTRY, namespace: NAMESPACE, tag: 'latest')
  return "#{registry}/#{namespace}/#{image}:#{tag}"
end

.orchestratorObject

noinspection RubyClassVariableUsageInspection



255
256
257
258
# File 'lib/docker_core.rb', line 255

def self.orchestrator
  @@orchestrator ||= self.detect_orchestrator
  return "#{@@orchestrator}"
end

.pair_paths(base, relative = '') ⇒ Object

Parameters:

  • base (String)
  • relative (String) (defaults to: '')


208
209
210
211
212
213
214
215
216
# File 'lib/docker_core.rb', line 208

def self.pair_paths(base, relative = '')
  items = {}

  { inside: base, outside: Dir.pwd }.each do |key, value|
    items[key] = File.join(value, relative)
  end

  return items
end

.prepare_folders(*services, folders: %w[stack volume]) ⇒ Object

Parameters:

  • arguments (Array<String>)
  • folders (Array<String>) (defaults to: %w[stack volume])


220
221
222
223
224
225
226
227
# File 'lib/docker_core.rb', line 220

def self.prepare_folders(*services, folders: %w[stack volume])
  deploy = self.deploy_path
  folders.each do |folder|
    services.each do |service|
      FileUtils.mkdir_p(File.join(deploy, folder, service))
    end
  end
end

.profile_pathObject



116
117
118
# File 'lib/docker_core.rb', line 116

def self.profile_path
  return File.realpath('profile.sh', self.deploy_path)
end

.read_swarmObject



163
164
165
166
# File 'lib/docker_core.rb', line 163

def self.read_swarm
  file = self.swarm_path
  return File.exists?(file) ? File.read(file).strip : ''
end

.run_command(*arguments, environment: {}, bind: {}, throw: true, wait: 0, echo: true) ⇒ Boolean

noinspection RubyUnusedLocalVariable

Parameters:

  • arguments (Array)
  • environment (Hash) (defaults to: {})
  • bind (Hash) (defaults to: {})
  • throw (Boolean) (defaults to: true)
  • wait (Numeric) (defaults to: 0)
  • echo (Boolean) (defaults to: true)

Returns:

  • (Boolean)


250
251
252
# File 'lib/docker_core.rb', line 250

def self.run_command(*arguments, environment: {}, bind: {}, throw: true, wait: 0, echo: true)
  Error.no_method(__method__)
end

.runner_pathObject



120
121
122
# File 'lib/docker_core.rb', line 120

def self.runner_path
  return File.realpath('runner.rb', self.deploy_path)
end

.swarm_pathObject



112
113
114
# File 'lib/docker_core.rb', line 112

def self.swarm_path
  return File.expand_path('~/.swarm')
end

.swarm_statusObject



184
185
186
187
188
# File 'lib/docker_core.rb', line 184

def self.swarm_status
  color = Color::GREEN
  Color.echo("Swarm: #{self.detect_orchestrator}", color)
  Color.echo(self.detect_services.to_yaml, color)
end

.update_swarm(orchestrator = '') ⇒ Object

Parameters:

  • orchestrator (String) (defaults to: '')


191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/docker_core.rb', line 191

def self.update_swarm(orchestrator = '')
  if false == orchestrator.empty?
    self.write_swarm(orchestrator)
  end

  orchestrator = self.detect_orchestrator
  self.write_swarm(orchestrator)

  if orchestrator.empty?
    orchestrator = '<none>'
  end

  Color.echo("Swarm: #{orchestrator}", Color::GREEN)
end

.write_swarm(swarm = '') ⇒ Object

Parameters:

  • swarm (String) (defaults to: '')


159
160
161
# File 'lib/docker_core.rb', line 159

def self.write_swarm(swarm = '')
  return File.write(self.swarm_path, "#{swarm}\n")
end