Class: Kamal::Cli::Accessory

Inherits:
Base
  • Object
show all
Defined in:
lib/kamal/cli/accessory.rb

Constant Summary

Constants inherited from Base

Base::AUTOMATIC_DEPLOY_LOCK_MESSAGE, Base::VERBOSITY

Instance Method Summary collapse

Methods inherited from Base

dynamic_command_class, exit_on_failure?, #initialize

Constructor Details

This class inherits a constructor from Kamal::Cli::Base

Instance Method Details

#boot(name, prepare: true) ⇒ Object



6
7
8
9
10
11
12
13
14
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
# File 'lib/kamal/cli/accessory.rb', line 6

def boot(name, prepare: true)
  modify(lock: true) do
    if name == "all"
      KAMAL.accessory_names.each { |accessory_name| boot(accessory_name) }
    else
      prepare(name) if prepare

      with_accessory(name) do |accessory, hosts|
        booted_hosts = Concurrent::Array.new
        on(hosts) do |host|
          booted_hosts << host.to_s if capture_with_info(*accessory.info(all: true, quiet: true)).strip.presence
        end

        if booted_hosts.any?
          say "Skipping booting `#{name}` on #{booted_hosts.sort.join(", ")}, a container already exists", :yellow
          hosts -= booted_hosts
        end

        directories(name)
        upload(name)

        on(hosts) do |host|
          execute *KAMAL.auditor.record("Booted #{name} accessory"), verbosity: :debug
          execute *accessory.ensure_env_directory
          upload! accessory.secrets_io, accessory.secrets_path, mode: "0600"
          execute *accessory.run(host: host)

          if accessory.running_proxy?
            target = capture_with_info(*accessory.container_id_for(container_name: accessory.service_name, only_running: true)).strip
            execute *accessory.deploy(target: target)
          end
        end
      end
    end
  end
end

#details(name) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/kamal/cli/accessory.rb', line 134

def details(name)
  quiet = options[:quiet]
  if name == "all"
    KAMAL.accessory_names.each { |accessory_name| details(accessory_name) }
  else
    type = "Accessory #{name}"
    with_accessory(name) do |accessory, hosts|
      on(hosts) { puts_by_host host, capture_with_info(*accessory.info), type: type, quiet: quiet }
    end
  end
end

#directories(name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kamal/cli/accessory.rb', line 63

def directories(name)
  modify(lock: true) do
    with_accessory(name) do |accessory, hosts|
      on(hosts) do
        accessory.directories.each do |(local, config)|
          execute *accessory.make_directory(local)
          execute :chmod, config[:mode], local if config[:mode]
          execute :chown, config[:owner], local if config[:owner]
        end
      end
    end
  end
end

#exec(name, *cmd) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/kamal/cli/accessory.rb', line 150

def exec(name, *cmd)
  raw = options[:raw]

  if raw && options[:interactive]
    raise ArgumentError, "Raw is not compatible with interactive"
  end

  with_raw_output(raw) do
    pre_connect_if_required

    cmd = Kamal::Utils.join_commands(cmd)
    quiet = options[:quiet]

    with_accessory(name) do |accessory, hosts|
      case
      when options[:interactive] && options[:reuse]
        say "Launching interactive command via SSH from existing container...", :magenta
        run_locally { exec accessory.execute_in_existing_container_over_ssh(cmd) }

      when options[:interactive]
        say "Launching interactive command via SSH from new container...", :magenta
        on(accessory.hosts.first) { execute *KAMAL.registry. }
        run_locally { exec accessory.execute_in_new_container_over_ssh(cmd) }

      when options[:reuse]
        say "Launching command from existing container...", :magenta
        on(hosts) do |host|
          execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug
          puts_by_host host, capture_with_info(*accessory.execute_in_existing_container(cmd), strip: !raw), quiet: quiet, raw: raw
        end

      else
        say "Launching command from new container...", :magenta
        on(hosts) do |host|
          execute *KAMAL.registry.
          execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug
          puts_by_host host, capture_with_info(*accessory.execute_in_new_container(cmd), strip: !raw), quiet: quiet, raw: raw
        end
      end
    end
  end
end

#logs(name) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/kamal/cli/accessory.rb', line 200

def logs(name)
  with_accessory(name) do |accessory, hosts|
    grep = options[:grep]
    grep_options = options[:grep_options]
    timestamps = !options[:skip_timestamps]

    if options[:follow]
      run_locally do
        info "Following logs on #{hosts}..."
        info accessory.follow_logs(timestamps: timestamps, grep: grep, grep_options: grep_options)
        exec accessory.follow_logs(timestamps: timestamps, grep: grep, grep_options: grep_options)
      end
    else
      since = options[:since]
      lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set

      on(hosts) do
        puts capture_with_info(*accessory.logs(timestamps: timestamps, since: since, lines: lines, grep: grep, grep_options: grep_options))
      end
    end
  end
end

#pull_image(name) ⇒ Object



224
225
226
227
228
229
230
231
232
233
# File 'lib/kamal/cli/accessory.rb', line 224

def pull_image(name)
  modify(lock: true) do
    with_accessory(name) do |accessory, hosts|
      on(hosts) do
        execute *KAMAL.auditor.record("Pull #{name} accessory image"), verbosity: :debug
        execute *accessory.pull_image
      end
    end
  end
end

#reboot(name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kamal/cli/accessory.rb', line 78

def reboot(name)
  modify(lock: true) do
    if name == "all"
      KAMAL.accessory_names.each { |accessory_name| reboot(accessory_name) }
    else
      prepare(name)
      pull_image(name)
      stop(name)
      remove_container(name)
      boot(name, prepare: false)
    end
  end
end

#remove(name) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/kamal/cli/accessory.rb', line 237

def remove(name)
  confirming "This will remove all containers, images and data directories for #{name}. Are you sure?" do
    modify(lock: true) do
      if name == "all"
        KAMAL.accessory_names.each { |accessory_name| remove_accessory(accessory_name) }
      else
        remove_accessory(name)
      end
    end
  end
end

#remove_container(name) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/kamal/cli/accessory.rb', line 250

def remove_container(name)
  modify(lock: true) do
    with_accessory(name) do |accessory, hosts|
      on(hosts) do
        execute *KAMAL.auditor.record("Remove #{name} accessory container"), verbosity: :debug
        execute *accessory.remove_container
      end
    end
  end
end

#remove_image(name) ⇒ Object



262
263
264
265
266
267
268
269
270
271
# File 'lib/kamal/cli/accessory.rb', line 262

def remove_image(name)
  modify(lock: true) do
    with_accessory(name) do |accessory, hosts|
      on(hosts) do
        execute *KAMAL.auditor.record("Removed #{name} accessory image"), verbosity: :debug
        execute *accessory.remove_image
      end
    end
  end
end

#remove_service_directory(name) ⇒ Object



274
275
276
277
278
279
280
281
282
# File 'lib/kamal/cli/accessory.rb', line 274

def remove_service_directory(name)
  modify(lock: true) do
    with_accessory(name) do |accessory, hosts|
      on(hosts) do
        execute *accessory.remove_service_directory
      end
    end
  end
end

#restart(name) ⇒ Object



126
127
128
129
130
131
# File 'lib/kamal/cli/accessory.rb', line 126

def restart(name)
  modify(lock: true) do
    stop(name)
    start(name)
  end
end

#start(name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kamal/cli/accessory.rb', line 93

def start(name)
  modify(lock: true) do
    with_accessory(name) do |accessory, hosts|
      on(hosts) do
        execute *KAMAL.auditor.record("Started #{name} accessory"), verbosity: :debug
        execute *accessory.start
        if accessory.running_proxy?
          target = capture_with_info(*accessory.container_id_for(container_name: accessory.service_name, only_running: true)).strip
          execute *accessory.deploy(target: target)
        end
      end
    end
  end
end

#stop(name) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/kamal/cli/accessory.rb', line 109

def stop(name)
  modify(lock: true) do
    with_accessory(name) do |accessory, hosts|
      on(hosts) do
        execute *KAMAL.auditor.record("Stopped #{name} accessory"), verbosity: :debug
        execute *accessory.stop, raise_on_non_zero_exit: false

        if accessory.running_proxy?
          target = capture_with_info(*accessory.container_id_for(container_name: accessory.service_name, only_running: true)).strip
          execute *accessory.remove if target
        end
      end
    end
  end
end

#upgrade(name) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/kamal/cli/accessory.rb', line 287

def upgrade(name)
  confirming "This will restart all accessories" do
    modify(lock: true) do
      host_groups = options[:rolling] ? KAMAL.accessory_hosts : [ KAMAL.accessory_hosts ]
      host_groups.each do |hosts|
        host_list = Array(hosts).join(",")
        KAMAL.with_specific_hosts(hosts) do
          say "Upgrading #{name} accessories on #{host_list}...", :magenta
          reboot name
          say "Upgraded #{name} accessories on #{host_list}...", :magenta
        end
      end
    end
  end
end

#upload(name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kamal/cli/accessory.rb', line 44

def upload(name)
  modify(lock: true) do
    with_accessory(name) do |accessory, hosts|
      on(hosts) do
        accessory.files.each do |(local, config)|
          remote = config[:host_path]
          accessory.ensure_local_file_present(local)

          execute *accessory.make_directory_for(remote)
          upload! local, remote
          execute :chmod, config[:mode], remote
          execute :chown, config[:owner], remote if config[:owner]
        end
      end
    end
  end
end