Class: Dumpman::Fetchers::Docker

Inherits:
Object
  • Object
show all
Defined in:
lib/dumpman/fetchers/docker.rb

Overview

it runs docker task to make dump copies dump into tmp dir and retrieves dump to local machine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssh_opts:, ssh_cmd:, docker_image:, app_env:) ⇒ Docker

Returns a new instance of Docker.



9
10
11
12
13
14
# File 'lib/dumpman/fetchers/docker.rb', line 9

def initialize(ssh_opts:, ssh_cmd:, docker_image:, app_env:, **)
  @ssh_opts = ssh_opts
  @ssh_cmd = ssh_cmd
  @docker_image = docker_image
  @app_env = app_env
end

Instance Attribute Details

#app_envObject (readonly)

Returns the value of attribute app_env.



7
8
9
# File 'lib/dumpman/fetchers/docker.rb', line 7

def app_env
  @app_env
end

#docker_imageObject (readonly)

Returns the value of attribute docker_image.



7
8
9
# File 'lib/dumpman/fetchers/docker.rb', line 7

def docker_image
  @docker_image
end

#ssh_cmdObject (readonly)

Returns the value of attribute ssh_cmd.



7
8
9
# File 'lib/dumpman/fetchers/docker.rb', line 7

def ssh_cmd
  @ssh_cmd
end

#ssh_optsObject (readonly)

Returns the value of attribute ssh_opts.



7
8
9
# File 'lib/dumpman/fetchers/docker.rb', line 7

def ssh_opts
  @ssh_opts
end

Instance Method Details

#fetch_dump_to_local(dump_location) ⇒ Object



44
45
46
47
48
49
# File 'lib/dumpman/fetchers/docker.rb', line 44

def fetch_dump_to_local(dump_location)
  <<~SSH_COMMAND
    scp #{ssh_opts} #{ssh_cmd}:#{dump_location}/#{Dumpman.dump_file_name} \
      #{Dumpman.dump_folder}/
  SSH_COMMAND
end

#get_dumpObject



16
17
18
19
20
# File 'lib/dumpman/fetchers/docker.rb', line 16

def get_dump
  Dumpman::Executor.system(make_dump_remotely) do |dump_location|
    Dumpman::Executor.system(fetch_dump_to_local(dump_location))
  end
end

#make_dump_remotelyObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dumpman/fetchers/docker.rb', line 22

def make_dump_remotely
  <<~SSH_COMMAND
    ssh #{ssh_opts} #{ssh_cmd} '
      export TEMP_DIR=$(mktemp -d)
      export DOCKER_IMAGE=$(docker images #{docker_image} --format "{{.ID}}" | head -1)

      docker run -d \
        --name pgdmp \
        --rm \
        -e RAILS_ENV=#{app_env} \
        -v ${TEMP_DIR}:/opt \
        -u root \
        ${DOCKER_IMAGE} /bin/bash -c \
          "bundle exec rake db:dump && cp #{Dumpman.dump_file_name} /opt/ && exit" > /dev/null

      docker wait pgdmp >/dev/null

      echo $TEMP_DIR
    '
  SSH_COMMAND
end