Class: Kamal::Cli::Proxy

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

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

#bootObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kamal/cli/proxy.rb', line 3

def boot
  with_lock do
    on(KAMAL.hosts) do |host|
      execute *KAMAL.docker.create_network
    rescue SSHKit::Command::Failed => e
      raise unless e.message.include?("already exists")
    end

    on(KAMAL.proxy_hosts) do |host|
      execute *KAMAL.registry.

      version = capture_with_info(*KAMAL.proxy.version).strip.presence

      if version && Kamal::Utils.older_version?(version, Kamal::Configuration::PROXY_MINIMUM_VERSION)
        raise "kamal-proxy version #{version} is too old, run `kamal proxy reboot` in order to update to at least #{Kamal::Configuration::PROXY_MINIMUM_VERSION}"
      end
      execute *KAMAL.proxy.start_or_run
    end
  end
end

#boot_config(subcommand) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kamal/cli/proxy.rb', line 30

def boot_config(subcommand)
  case subcommand
  when "set"
    boot_options = [
      *(KAMAL.config.proxy_publish_args(options[:http_port], options[:https_port]) if options[:publish]),
      *(KAMAL.config.proxy_logging_args(options[:log_max_size])),
      *options[:docker_options].map { |option| "--#{option}" }
    ]

    on(KAMAL.proxy_hosts) do |host|
      execute(*KAMAL.proxy.ensure_proxy_directory)
      upload! StringIO.new(boot_options.join(" ")), KAMAL.config.proxy_options_file
    end
  when "get"
    on(KAMAL.proxy_hosts) do |host|
      puts "Host #{host}: #{capture_with_info(*KAMAL.proxy.get_boot_options)}"
    end
  when "reset"
    on(KAMAL.proxy_hosts) do |host|
      execute *KAMAL.proxy.reset_boot_options
    end
  else
    raise ArgumentError, "Unknown boot_config subcommand #{subcommand}"
  end
end

#detailsObject



166
167
168
# File 'lib/kamal/cli/proxy.rb', line 166

def details
  on(KAMAL.proxy_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.proxy.info), type: "Proxy" }
end

#logsObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/kamal/cli/proxy.rb', line 176

def logs
  grep = options[:grep]
  timestamps = !options[:skip_timestamps]

  if options[:follow]
    run_locally do
      info "Following logs on #{KAMAL.primary_host}..."
      info KAMAL.proxy.follow_logs(host: KAMAL.primary_host, timestamps: timestamps, grep: grep)
      exec KAMAL.proxy.follow_logs(host: KAMAL.primary_host, timestamps: timestamps, grep: grep)
    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(KAMAL.proxy_hosts) do |host|
      puts_by_host host, capture(*KAMAL.proxy.logs(timestamps: timestamps, since: since, lines: lines, grep: grep)), type: "Proxy"
    end
  end
end

#rebootObject



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
91
92
93
94
95
# File 'lib/kamal/cli/proxy.rb', line 59

def reboot
  confirming "This will cause a brief outage on each host. Are you sure?" do
    with_lock do
      host_groups = options[:rolling] ? KAMAL.proxy_hosts : [ KAMAL.proxy_hosts ]
      host_groups.each do |hosts|
        host_list = Array(hosts).join(",")
        run_hook "pre-proxy-reboot", hosts: host_list
        on(hosts) do |host|
          execute *KAMAL.auditor.record("Rebooted proxy"), verbosity: :debug
          execute *KAMAL.registry.

          "Stopping and removing Traefik on #{host}, if running..."
          execute *KAMAL.proxy.cleanup_traefik

          "Stopping and removing kamal-proxy on #{host}, if running..."
          execute *KAMAL.proxy.stop, raise_on_non_zero_exit: false
          execute *KAMAL.proxy.remove_container

          execute *KAMAL.proxy.run

          KAMAL.roles_on(host).select(&:running_proxy?).each do |role|
            app = KAMAL.app(role: role, host: host)

            version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
            endpoint = capture_with_info(*app.container_id_for_version(version)).strip

            if endpoint.present?
              info "Deploying #{endpoint} for role `#{role}` on #{host}..."
              execute *app.deploy(target: endpoint)
            end
          end
        end
        run_hook "post-proxy-reboot", hosts: host_list
      end
    end
  end
end

#removeObject



198
199
200
201
202
203
204
205
206
207
# File 'lib/kamal/cli/proxy.rb', line 198

def remove
  with_lock do
    if removal_allowed?(options[:force])
      stop
      remove_container
      remove_image
      remove_proxy_directory
    end
  end
end

#remove_containerObject



210
211
212
213
214
215
216
217
# File 'lib/kamal/cli/proxy.rb', line 210

def remove_container
  with_lock do
    on(KAMAL.proxy_hosts) do
      execute *KAMAL.auditor.record("Removed proxy container"), verbosity: :debug
      execute *KAMAL.proxy.remove_container
    end
  end
end

#remove_imageObject



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

def remove_image
  with_lock do
    on(KAMAL.proxy_hosts) do
      execute *KAMAL.auditor.record("Removed proxy image"), verbosity: :debug
      execute *KAMAL.proxy.remove_image
    end
  end
end

#remove_proxy_directoryObject



230
231
232
233
234
235
236
# File 'lib/kamal/cli/proxy.rb', line 230

def remove_proxy_directory
  with_lock do
    on(KAMAL.proxy_hosts) do
      execute *KAMAL.proxy.remove_proxy_directory, raise_on_non_zero_exit: false
    end
  end
end

#restartObject



158
159
160
161
162
163
# File 'lib/kamal/cli/proxy.rb', line 158

def restart
  with_lock do
    stop
    start
  end
end

#startObject



138
139
140
141
142
143
144
145
# File 'lib/kamal/cli/proxy.rb', line 138

def start
  with_lock do
    on(KAMAL.proxy_hosts) do |host|
      execute *KAMAL.auditor.record("Started proxy"), verbosity: :debug
      execute *KAMAL.proxy.start
    end
  end
end

#stopObject



148
149
150
151
152
153
154
155
# File 'lib/kamal/cli/proxy.rb', line 148

def stop
  with_lock do
    on(KAMAL.proxy_hosts) do |host|
      execute *KAMAL.auditor.record("Stopped proxy"), verbosity: :debug
      execute *KAMAL.proxy.stop, raise_on_non_zero_exit: false
    end
  end
end

#upgradeObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/kamal/cli/proxy.rb', line 100

def upgrade
  invoke_options = { "version" => KAMAL.config.latest_tag }.merge(options)

  confirming "This will cause a brief outage on each host. Are you sure?" do
    host_groups = options[:rolling] ? KAMAL.hosts : [ KAMAL.hosts ]
    host_groups.each do |hosts|
      host_list = Array(hosts).join(",")
      say "Upgrading proxy on #{host_list}...", :magenta
      run_hook "pre-proxy-reboot", hosts: host_list
      on(hosts) do |host|
        execute *KAMAL.auditor.record("Rebooted proxy"), verbosity: :debug
        execute *KAMAL.registry.

        "Stopping and removing Traefik on #{host}, if running..."
        execute *KAMAL.proxy.cleanup_traefik

        "Stopping and removing kamal-proxy on #{host}, if running..."
        execute *KAMAL.proxy.stop, raise_on_non_zero_exit: false
        execute *KAMAL.proxy.remove_container
        execute *KAMAL.proxy.remove_image
      end

      KAMAL.with_specific_hosts(hosts) do
        invoke "kamal:cli:proxy:boot", [], invoke_options
        reset_invocation(Kamal::Cli::Proxy)
        invoke "kamal:cli:app:boot", [], invoke_options
        reset_invocation(Kamal::Cli::App)
        invoke "kamal:cli:prune:all", [], invoke_options
        reset_invocation(Kamal::Cli::Prune)
      end

      run_hook "post-proxy-reboot", hosts: host_list
      say "Upgraded proxy on #{host_list}", :magenta
    end
  end
end