Class: Mrsk::Cli::App

Inherits:
Base
  • Object
show all
Defined in:
lib/mrsk/cli/app.rb

Instance Method Summary collapse

Methods inherited from Base

exit_on_failure?, #initialize

Constructor Details

This class inherits a constructor from Mrsk::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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mrsk/cli/app.rb', line 3

def boot
  mutating do
    hold_lock_on_error do
      say "Get most recent version available as an image...", :magenta unless options[:version]
      using_version(version_or_latest) do |version|
        say "Start container with version #{version} using a #{MRSK.config.readiness_delay}s readiness delay (or reboot if already running)...", :magenta

        on(MRSK.hosts) do
          execute *MRSK.auditor.record("Tagging #{MRSK.config.absolute_image} as the latest image"), verbosity: :debug
          execute *MRSK.app.tag_current_as_latest
        end

        on(MRSK.hosts, **MRSK.boot_strategy) do |host|
          roles = MRSK.roles_on(host)

          roles.each do |role|
            app = MRSK.app(role: role)
            auditor = MRSK.auditor(role: role)

            if capture_with_info(*app.container_id_for_version(version, only_running: true), raise_on_non_zero_exit: false).present?
              tmp_version = "#{version}_replaced_#{SecureRandom.hex(8)}"
              info "Renaming container #{version} to #{tmp_version} as already deployed on #{host}"
              execute *auditor.record("Renaming container #{version} to #{tmp_version}"), verbosity: :debug
              execute *app.rename_container(version: version, new_version: tmp_version)
            end

            execute *auditor.record("Booted app version #{version}"), verbosity: :debug

            old_version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
            execute *app.start_or_run(hostname: "#{host}-#{SecureRandom.hex(6)}")

            Mrsk::Utils::HealthcheckPoller.wait_for_healthy(pause_after_ready: true) { capture_with_info(*app.status(version: version)) }

            execute *app.stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
          end
        end
      end
    end
  end
end

#containersObject



131
132
133
# File 'lib/mrsk/cli/app.rb', line 131

def containers
  on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.list_containers) }
end

#detailsObject



74
75
76
77
78
79
80
81
82
# File 'lib/mrsk/cli/app.rb', line 74

def details
  on(MRSK.hosts) do |host|
    roles = MRSK.roles_on(host)

    roles.each do |role|
      puts_by_host host, capture_with_info(*MRSK.app(role: role).info)
    end
  end
end

#exec(cmd) ⇒ Object



87
88
89
90
91
92
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
123
124
125
126
127
128
# File 'lib/mrsk/cli/app.rb', line 87

def exec(cmd)
  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 #{MRSK.primary_host}...", :magenta
      run_locally { exec MRSK.app(role: "web").execute_in_existing_container_over_ssh(cmd, host: MRSK.primary_host) }
    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 #{MRSK.primary_host}...", :magenta
      run_locally { exec MRSK.app.execute_in_new_container_over_ssh(cmd, host: MRSK.primary_host) }
    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(MRSK.hosts) do |host|
        roles = MRSK.roles_on(host)

        roles.each do |role|
          execute *MRSK.auditor.record("Executed cmd '#{cmd}' on app version #{version}", role: role), verbosity: :debug
          puts_by_host host, capture_with_info(*MRSK.app(role: role).execute_in_existing_container(cmd))
        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(MRSK.hosts) do |host|
        execute *MRSK.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
        puts_by_host host, capture_with_info(*MRSK.app.execute_in_new_container(cmd))
      end
    end
  end
end

#imagesObject



161
162
163
# File 'lib/mrsk/cli/app.rb', line 161

def images
  on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.list_images) }
end

#logsObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/mrsk/cli/app.rb', line 170

def logs
  # FIXME: Catch when app containers aren't running

  grep = options[:grep]

  if options[:follow]
    run_locally do
      info "Following logs on #{MRSK.primary_host}..."

      MRSK.specific_roles ||= ["web"]
      role = MRSK.roles_on(MRSK.primary_host).first

      info MRSK.app(role: role).follow_logs(host: MRSK.primary_host, grep: grep)
      exec MRSK.app(role: role).follow_logs(host: MRSK.primary_host, 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(MRSK.hosts) do |host|
      roles = MRSK.roles_on(host)

      roles.each do |role|
        begin
          puts_by_host host, capture_with_info(*MRSK.app(role: role).logs(since: since, lines: lines, grep: grep))
        rescue SSHKit::Command::Failed
          puts_by_host host, "Nothing found"
        end
      end
    end
  end
end

#removeObject



204
205
206
207
208
209
210
# File 'lib/mrsk/cli/app.rb', line 204

def remove
  mutating do
    stop
    remove_containers
    remove_images
  end
end

#remove_container(version) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/mrsk/cli/app.rb', line 213

def remove_container(version)
  mutating do
    on(MRSK.hosts) do |host|
      roles = MRSK.roles_on(host)

      roles.each do |role|
        execute *MRSK.auditor.record("Removed app container with version #{version}", role: role), verbosity: :debug
        execute *MRSK.app(role: role).remove_container(version: version)
      end
    end
  end
end

#remove_containersObject



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/mrsk/cli/app.rb', line 227

def remove_containers
  mutating do
    on(MRSK.hosts) do |host|
      roles = MRSK.roles_on(host)

      roles.each do |role|
        execute *MRSK.auditor.record("Removed all app containers", role: role), verbosity: :debug
        execute *MRSK.app(role: role).remove_containers
      end
    end
  end
end

#remove_imagesObject



241
242
243
244
245
246
247
248
# File 'lib/mrsk/cli/app.rb', line 241

def remove_images
  mutating do
    on(MRSK.hosts) do
      execute *MRSK.auditor.record("Removed all app images"), verbosity: :debug
      execute *MRSK.app.remove_images
    end
  end
end

#stale_containersObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/mrsk/cli/app.rb', line 137

def stale_containers
  mutating do
    stop = options[:stop]

    cli = self

    on(MRSK.hosts) do |host|
      roles = MRSK.roles_on(host)

      roles.each do |role|
        cli.send(:stale_versions, host: host, role: role).each do |version|
          if stop
            puts_by_host host, "Stopping stale container for role #{role} with version #{version}"
            execute *MRSK.app(role: role).stop(version: version), raise_on_non_zero_exit: false
          else
            puts_by_host host,  "Detected stale container for role #{role} with version #{version} (use `mrsk app stale_containers --stop` to stop)"
          end
        end
      end
    end
  end
end

#startObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mrsk/cli/app.rb', line 45

def start
  mutating do
    on(MRSK.hosts) do |host|
      roles = MRSK.roles_on(host)

      roles.each do |role|
        execute *MRSK.auditor.record("Started app version #{MRSK.config.version}"), verbosity: :debug
        execute *MRSK.app(role: role).start, raise_on_non_zero_exit: false
      end
    end
  end
end

#stopObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mrsk/cli/app.rb', line 59

def stop
  mutating do
    on(MRSK.hosts) do |host|
      roles = MRSK.roles_on(host)

      roles.each do |role|
        execute *MRSK.auditor.record("Stopped app", role: role), verbosity: :debug
        execute *MRSK.app(role: role).stop, raise_on_non_zero_exit: false
      end
    end
  end
end

#versionObject



251
252
253
# File 'lib/mrsk/cli/app.rb', line 251

def version
  on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.current_running_version).strip }
end