Class: Capistrano::DockerCluster::Scripts

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/docker_cluster/scripts.rb

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Scripts

Returns a new instance of Scripts.



8
9
10
# File 'lib/capistrano/docker_cluster/scripts.rb', line 8

def initialize(context)
  @context = context
end

Instance Method Details

#docker_config_map(host) ⇒ Object

Returns a list of all local configuration file paths that need to be uploaded to the host.



126
127
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/capistrano/docker_cluster/scripts.rb', line 126

def docker_config_map(host)
  configs = {}
  Array(fetch(:docker_configs)).each do |path|
    configs[File.basename(path)] = path
  end

  apps = Array(fetch_for_host(host, :docker_apps))
  app_configs = app_configuration(fetch(:docker_app_configs, nil))
  host_app_configs = app_configuration(host.properties.send(:docker_app_configs))
  apps += app_configs.keys if app_configs
  apps += host_app_configs.keys if host_app_configs
  apps = apps.collect(&:to_s).uniq

  if app_configs
    app_configs.values.each do |paths|
      Array(paths).each do |path|
        configs[File.basename(path)] = path
      end
    end
  end

  apps.each do |app|
    Array(fetch(:"docker_app_configs_#{app}")).each do |path|
      configs[File.basename(path)] = path
    end
  end

  Array(host.properties.docker_configs).each do |path|
    configs[File.basename(path)] = path
  end

  if host_app_configs
    host_app_configs.values.each do |paths|
      Array(paths).each do |path|
        configs[File.basename(path)] = path
      end
    end
  end

  apps.each do |app|
    Array(host.properties.send(:"docker_app_configs_#{app}")).each do |path|
      configs[File.basename(path)] = path
    end
  end

  configs
end

#fetch_for_host(host, property, default = nil) ⇒ Object

Fetch a host specific property. If a the value is not defined as host specific, then fallback to the globally defined property.



176
177
178
# File 'lib/capistrano/docker_cluster/scripts.rb', line 176

def fetch_for_host(host, property, default = nil)
  host.properties.send(property) || fetch(property, default)
end

#run_script(host) ⇒ Object

Build a custom command line run script with the configuration arguments for each docker application on the host to run one off containers.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/capistrano/docker_cluster/scripts.rb', line 51

def run_script(host)
  # For the run script, all configured apps are included, not just the deployed ones.
  # This allows configuring, for example, a "console" app to open up a console in a container.
  apps = Array(fetch_for_host(host, :docker_apps))
  app_configs = fetch_for_host(host, :docker_app_configs)
  apps.concat(app_configs.keys) if app_configs.is_a?(Hash)
  app_args = fetch_for_host(host, :docker_app_args)
  apps.concat(app_args.keys) if app_args.is_a?(Hash)
  apps = apps.collect(&:to_s).uniq

  cmd = "exec bin/docker-cluster"
  image_id = "#{fetch(:docker_repository)}:#{fetch(:docker_tag)}"

  cases = []
  apps.each do |app|
    args = app_host_args(app, host)
    cases << "  '#{app}')\n    #{cmd} #{args.join(' ')} --image '#{image_id}' --one-off \"$@\"\n    ;;"
  end

  <<~BASH
    #!/usr/bin/env bash

    # Generated: #{Time.now.utc.iso8601}
    # Docker image tag: #{fetch(:docker_repository)}:#{fetch(:docker_tag)}

    set -o errexit

    cd $(dirname $0)/..

    typeset app=$1
    shift

    case $app in
    #{cases.join("\n")}
      *)
        >&2 echo "Usage: $0 #{apps.join('|')}"
        exit 1
    esac
  BASH
end

#start_script(host) ⇒ Object

Build a custom command line start script with the configuration arguments for each docker application on the host. This allows each app to be started with the predefined configuration by calling ‘bin/start app`.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/capistrano/docker_cluster/scripts.rb', line 15

def start_script(host)
  apps = Array(fetch_for_host(host, :docker_apps))
  cmd = "exec bin/docker-cluster"
  image_id = "#{fetch(:docker_repository)}:#{fetch(:docker_tag)}"
  prefix = fetch(:docker_prefix)

  cases = []
  apps.each do |app|
    args = app_host_args(app, host)
    cases << "  '#{app}')\n    #{cmd} #{args.join(' ')} --name '#{prefix}#{app}' --image '#{image_id}' \"$@\"\n    ;;"
  end

  <<~BASH
    #!/usr/bin/env bash

    # Generated: #{Time.now.utc.iso8601}
    # Docker image tag: #{fetch(:docker_repository)}:#{fetch(:docker_tag)}

    set -o errexit

    cd $(dirname $0)/..

    typeset app=$1
    shift

    case $app in
    #{cases.join("\n")}
      *)
        >&2 echo "Usage: $0 #{apps.join('|')}"
        exit 1
    esac
  BASH
end

#stop_script(host) ⇒ Object

Build a custom command line stop script for each docker application on the host.



93
94
95
96
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
122
# File 'lib/capistrano/docker_cluster/scripts.rb', line 93

def stop_script(host)
  apps = Array(fetch_for_host(host, :docker_apps))
  prefix = fetch(:docker_prefix)

  cases = []
  all = []
  apps.each do |app|
    cases << "  '#{app}')\n    exec bin/docker-cluster --name '#{prefix}#{app}' --count 0\n    ;;"
  end

  <<~BASH
    #!/usr/bin/env bash

    # Generated: #{Time.now.utc.iso8601}
    # Docker image tag: #{fetch(:docker_repository)}:#{fetch(:docker_tag)}

    set -o errexit

    cd $(dirname $0)/..

    typeset app=$1

    case $app in
    #{cases.join("\n")}
      *)
        >&2 echo "Usage: $0 #{apps.join('|')}"
        exit 1
    esac
  BASH
end