Method: Kamal::Cli::App#exec

Defined in:
lib/kamal/cli/app.rb

#exec(*cmd) ⇒ Object



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
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/kamal/cli/app.rb', line 109

def exec(*cmd)
  pre_connect_if_required

  if (incompatible_options = [ :interactive, :reuse ].select { |key| options[:detach] && options[key] }.presence)
    raise ArgumentError, "Detach is not compatible with #{incompatible_options.join(" or ")}"
  end

  if cmd.empty?
    raise ArgumentError, "No command provided. You must specify a command to execute."
  end

  cmd = Kamal::Utils.join_commands(cmd)
  env = options[:env]
  detach = options[:detach]
  case
  when options[:interactive] && options[:reuse]
    say "Get current version of running container...", :magenta unless options[:version]
    using_version(options[:version] || current_running_version) do |version|
      say "Launching interactive command with version #{version} via SSH from existing container on #{KAMAL.primary_host}...", :magenta
      run_locally { exec KAMAL.app(role: KAMAL.primary_role, host: KAMAL.primary_host).execute_in_existing_container_over_ssh(cmd, env: env) }
    end

  when options[:interactive]
    say "Get most recent version available as an image...", :magenta unless options[:version]
    using_version(version_or_latest) do |version|
      say "Launching interactive command with version #{version} via SSH from new container on #{KAMAL.primary_host}...", :magenta
      on(KAMAL.primary_host) { execute *KAMAL.registry. }
      run_locally do
        exec KAMAL.app(role: KAMAL.primary_role, host: KAMAL.primary_host).execute_in_new_container_over_ssh(cmd, env: env)
      end
    end

  when options[:reuse]
    say "Get current version of running container...", :magenta unless options[:version]
    using_version(options[:version] || current_running_version) do |version|
      say "Launching command with version #{version} from existing container...", :magenta

      on(KAMAL.app_hosts) do |host|
        roles = KAMAL.roles_on(host)

        roles.each do |role|
          execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}", role: role), verbosity: :debug
          puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).execute_in_existing_container(cmd, env: env))
        end
      end
    end

  else
    say "Get most recent version available as an image...", :magenta unless options[:version]
    using_version(version_or_latest) do |version|
      say "Launching command with version #{version} from new container...", :magenta
      on(KAMAL.app_hosts) do |host|
        execute *KAMAL.registry.

        roles = KAMAL.roles_on(host)

        roles.each do |role|
          execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
          puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).execute_in_new_container(cmd, env: env, detach: detach))
        end
      end
    end
  end
end