Module: Indocker

Defined in:
lib/indocker.rb,
lib/indocker/version.rb

Defined Under Namespace

Modules: Artifacts, Concerns, Configurations, Containers, EnvFiles, Images, Launchers, Networks, Registries, Repositories, ServerPools, Volumes Classes: BuildContext, BuildContextHelper, BuildServer, ContainerDeployer, ContainerHelper, ContextArgs, CrontabRedeployRulesBuilder, DeployContext, DeploymentChecker, DeploymentPolicy, DeploymentProgress, Docker, DockerRunArgs, EnvFileHelper, HashMerger, IndockerHelper, LoggerFactory, Rsync, Server, Shell, SshResultLogger, SshSession

Constant Summary collapse

VERSION =
"0.3.9"

Class Method Summary collapse

Class Method Details

.add_artifact(artifact) ⇒ Object



158
159
160
# File 'lib/indocker.rb', line 158

def add_artifact(artifact)
  artifacts.push(artifact)
end

.add_build_server(build_server) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/indocker.rb', line 241

def add_build_server(build_server)
  if !build_server.is_a?(Indocker::BuildServer)
    raise ArgumentError.new("should be an instance of Indocker::BuildServer, got: #{build_server.inspect}")
  end

  existing = build_servers.detect {|s| s == build_server}

  if existing
    raise ArgumentError.new("build server with name #{build_server.name} was already defined")
  end

  build_servers.push(build_server)
end

.add_registry(registry) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/indocker.rb', line 170

def add_registry(registry)
  if !registry.is_a?(Indocker::Registries::Abstract)
    raise ArgumentError.new("should be an instance of Indocker::Registries::Abstract, got: #{registry.inspect}")
  end

  registries.push(registry)
end

.add_repository(repository) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/indocker.rb', line 162

def add_repository(repository)
  if !repository.is_a?(Indocker::Repositories::Abstract)
    raise ArgumentError.new("should be an instance of Indocker::Repositories::Abstract, got: #{repository.inspect}")
  end

  repositories.push(repository)
end

.add_server(server) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/indocker.rb', line 178

def add_server(server)
  if !server.is_a?(Indocker::Server)
    raise ArgumentError.new("should be an instance of Indocker::Server, got: #{server.inspect}")
  end

  existing = servers.detect {|s| s == server}

  if existing
    raise ArgumentError.new("server with name #{server.name} was already defined")
  end

  servers.push(server)
end

.artifactsObject



275
276
277
# File 'lib/indocker.rb', line 275

def artifacts
  @artifacts ||= []
end

.build_configuration(name) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/indocker.rb', line 299

def build_configuration(name)
  builder = Indocker::Configurations::ConfigurationBuilder.new(
    name: name,
    repositories: repositories,
    registries: registries,
    servers: servers,
    build_servers: build_servers,
    volumes: volumes,
    networks: networks,
    env_files: env_files,
    containers: containers,
  )

  @configuration = builder.configuration
  builder
end

.build_helper(&proc) ⇒ Object



471
472
473
# File 'lib/indocker.rb', line 471

def build_helper(&proc)
  Indocker::BuildContextHelper.class_exec(&proc)
end

.build_serversObject



279
280
281
# File 'lib/indocker.rb', line 279

def build_servers
  @build_servers ||= []
end

.check(servers: []) ⇒ Object



381
382
383
384
385
386
387
388
# File 'lib/indocker.rb', line 381

def check(servers: [])
  Indocker::DeploymentChecker
    .new(Indocker.logger)
    .run(
      configuration: configuration,
      servers: servers,
    )
end

.compile(images:, skip_dependent:, compile_args: {}) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/indocker.rb', line 401

def compile(images:, skip_dependent:, compile_args: {})
  if compile_args
    configuration.set_global_build_args(
      Indocker::HashMerger.deep_merge(compile_args, configuration.global_build_args)
    )
  end

  Indocker::Launchers::ImagesCompiler
    .new(Indocker.logger)
    .compile(
      configuration: configuration,
      image_list: images,
      skip_dependent: skip_dependent,
    )
end

.configurationObject



287
288
289
# File 'lib/indocker.rb', line 287

def configuration
  @configuration || (raise ArgumentError.new("no configuration provided"))
end

.configuration_nameObject



479
480
481
# File 'lib/indocker.rb', line 479

def configuration_name
  @configuration_name || (raise ArgumentError.new("configuration was not specified"))
end

.container_filesObject



200
201
202
# File 'lib/indocker.rb', line 200

def container_files
  @container_files || (raise ArgumentError.new("container files were not found. Set bounded contexts dir"))
end

.containersObject



295
296
297
# File 'lib/indocker.rb', line 295

def containers
  @containers ||= []
end

.define_container(name) ⇒ Object



335
336
337
338
339
340
341
342
343
# File 'lib/indocker.rb', line 335

def define_container(name)
  builder = Indocker::Containers::ContainerBuilder.new(
    name: name,
    configuration: configuration,
  )

  containers.push(builder.container)
  builder
end

.define_env_file(env_file) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/indocker.rb', line 224

def define_env_file(env_file)
  if env_files.detect {|ef| ef.name == env_file.name}
    Indocker.logger.error("env file :#{env_file.name} was already defined")
    exit 1
  end

  env_files.push(env_file)
end

.define_image(name) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/indocker.rb', line 316

def define_image(name)
  path = caller[0].split(':').first

  if !(path =~ /\/image.rb$/)
    Indocker.logger.error("image :#{name} should be defined in image.rb file")
    exit 1
  end

  builder = Indocker::Images::ImageBuilder.new(
    name: name,
    configuration: configuration,
    dir: path.split('image.rb').first
  )

  images.push(builder.image)

  builder
end

.define_network(name) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/indocker.rb', line 192

def define_network(name)
  if networks.detect {|n| n.name == name}
    raise ArgumentError.new("network :#{name} was already defined")
  end

  networks.push(Indocker::Networks::Network.new(name))
end

.define_volume(volume) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/indocker.rb', line 233

def define_volume(volume)
  if volumes.detect { |v| v.name == volume.name}
    raise ArgumentError.new("volume :#{volume.name} was already defined")
  end

  volumes.push(volume)
end

.deploy(containers: [], skip_tags: [], tags: [], skip_dependent: false, skip_containers: [], servers: [], skip_build: false, skip_deploy: false, force_restart: false, skip_force_restart: [], auto_confirm: false, require_confirmation: false, compile_args: {}, deploy_args: {}) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/indocker.rb', line 345

def deploy(containers: [], skip_tags: [], tags: [], skip_dependent: false,
  skip_containers: [], servers: [], skip_build: false, skip_deploy: false,
  force_restart: false, skip_force_restart: [], auto_confirm: false,
  require_confirmation: false, compile_args: {}, deploy_args: {})

  deployment_policy = Indocker::DeploymentPolicy.new(
    deploy_containers:    containers,
    deploy_tags:          tags,
    servers:              servers,
    skip_dependent:       skip_dependent,
    skip_containers:      skip_containers,
    skip_build:           skip_build,
    skip_deploy:          skip_deploy,
    skip_tags:            skip_tags,
    force_restart:        force_restart,
    skip_force_restart:   skip_force_restart,
    auto_confirm:         auto_confirm,
    require_confirmation: require_confirmation,
  )

  if compile_args
    configuration.set_compile_args(compile_args)
  end

  if deploy_args
    configuration.set_deploy_args(deploy_args)
  end

  Indocker::Launchers::ConfigurationDeployer
    .new(logger: Indocker.logger, global_logger: Indocker.global_logger)
    .run(
      configuration:     configuration,
      deployment_policy: deployment_policy
    )
end

.deploy_dirObject



142
143
144
145
146
147
148
# File 'lib/indocker.rb', line 142

def deploy_dir
  if @deploy_dir
    @deploy_dir
  else
    raise ArgumentError.new("deploy dir was not specified")
  end
end

.dockerignoreObject



467
468
469
# File 'lib/indocker.rb', line 467

def dockerignore
  @dockerignore || []
end

.env_filesObject



283
284
285
# File 'lib/indocker.rb', line 283

def env_files
  @env_files ||= []
end

.export_commandObject



122
123
124
# File 'lib/indocker.rb', line 122

def export_command
  @export_command
end

.global_build_argsObject



431
432
433
# File 'lib/indocker.rb', line 431

def global_build_args
  Indocker::ContextArgs.new(nil, configuration.global_build_args, nil)
end

.global_loggerObject

Global logger would output data without dependency on how we deploy the progress Currently it will always output data to stdout



455
456
457
# File 'lib/indocker.rb', line 455

def global_logger
  @global_logger ||= Indocker::LoggerFactory.create(STDOUT, @log_level)
end

.helperObject



435
436
437
# File 'lib/indocker.rb', line 435

def helper
  Indocker::BuildContextHelper.new(Indocker.configuration, nil)
end

.image_filesObject



204
205
206
# File 'lib/indocker.rb', line 204

def image_files
  @image_files || (raise ArgumentError.new("image files were not found. Set bounded contexts dir"))
end

.imagesObject



291
292
293
# File 'lib/indocker.rb', line 291

def images
  @images ||= []
end

.launched?(contaner_name, servers: nil) ⇒ Boolean

Returns:

  • (Boolean)


390
391
392
393
394
395
396
397
398
399
# File 'lib/indocker.rb', line 390

def launched?(contaner_name, servers: nil)
  silent_logger = Logger.new(File.open(File::NULL, "w"))
  Indocker::DeploymentChecker
    .new(silent_logger, silent_logger)
    .launched?(
      contaner_name,
      configuration: configuration,
      servers: servers,
    )
end

.loggerObject

This logger outputs progress of the deployment It will not output anything for deployment without debug option



441
442
443
444
445
446
447
448
449
450
451
# File 'lib/indocker.rb', line 441

def logger
  @logger ||= begin
    logger_stdout = if @log_level == Logger::DEBUG
      STDOUT
    else
      File.open(File::NULL, "w")
    end

    Indocker::LoggerFactory.create(logger_stdout, @log_level)
  end
end

.networksObject



263
264
265
# File 'lib/indocker.rb', line 263

def networks
  @networks ||= []
end

.redeploy_crontab_pathObject



138
139
140
# File 'lib/indocker.rb', line 138

def redeploy_crontab_path
  @redeploy_crontab_path
end

.registriesObject



259
260
261
# File 'lib/indocker.rb', line 259

def registries
  @registries ||= []
end

.repositoriesObject



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

def repositories
  @repositories ||= []
end

.root_dirObject



150
151
152
153
154
155
156
# File 'lib/indocker.rb', line 150

def root_dir
  if @root_dir
    File.expand_path(@root_dir)
  else
    raise ArgumentError.new("root dir was not specified")
  end
end

.run(container_name, force_restart, deploy_args: {}) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/indocker.rb', line 417

def run(container_name, force_restart, deploy_args: {})
  if deploy_args
    configuration.set_deploy_args(deploy_args)
  end

  Indocker::Launchers::ContainerRunner
    .new(Indocker.logger)
    .run(
      configuration: configuration,
      container_name: container_name,
      force_restart: force_restart
    )
end

.serversObject



271
272
273
# File 'lib/indocker.rb', line 271

def servers
  @servers ||= []
end

.set_bounded_contexts_dir(path) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/indocker.rb', line 208

def set_bounded_contexts_dir(path)
  @container_files = {}

  Dir[File.join(path, '**/container.rb')].map do |path|
    name = path.gsub('/container.rb', '').split('/').last.to_sym
    @container_files[name] = path
  end

  @image_files = {}

  Dir[File.join(path, '**/image.rb')].map do |path|
    name = path.gsub('/image.rb', '').split('/').last.to_sym
    @image_files[name] = path
  end
end

.set_configuration_name(name) ⇒ Object



475
476
477
# File 'lib/indocker.rb', line 475

def set_configuration_name(name)
  @configuration_name = name
end

.set_deploy_dir(val) ⇒ Object



126
127
128
# File 'lib/indocker.rb', line 126

def set_deploy_dir(val)
  @deploy_dir = val
end

.set_dockerignore(ignore_list) ⇒ Object



463
464
465
# File 'lib/indocker.rb', line 463

def set_dockerignore(ignore_list)
  @dockerignore = ignore_list
end

.set_export_command(command) ⇒ Object



118
119
120
# File 'lib/indocker.rb', line 118

def set_export_command(command)
  @export_command = command
end

.set_log_level(level) ⇒ Object



459
460
461
# File 'lib/indocker.rb', line 459

def set_log_level(level)
  @log_level = level
end

.set_redeploy_crontab_path(val) ⇒ Object



134
135
136
# File 'lib/indocker.rb', line 134

def set_redeploy_crontab_path(val)
  @redeploy_crontab_path = val
end

.set_root_dir(val) ⇒ Object



130
131
132
# File 'lib/indocker.rb', line 130

def set_root_dir(val)
  @root_dir = val
end

.volumesObject



267
268
269
# File 'lib/indocker.rb', line 267

def volumes
  @volumes ||= []
end