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
# File 'lib/mrsk/cli/app.rb', line 3

def boot
  say "Get most recent version available as an image...", :magenta unless options[:version]
  using_version(options[:version] || most_recent_version_available) do |version|
    say "Start container with version #{version} (or reboot if already running)...", :magenta

    cli = self
    
    MRSK.config.roles.each do |role|
      on(role.hosts) do |host|
        execute *MRSK.auditor.record("Booted app version #{version}"), verbosity: :debug

        begin
          old_version = capture_with_info(*MRSK.app.current_running_version).strip
          execute *MRSK.app.run(role: role.name)

          cli.say "Waiting #{MRSK.config.readiness_delay}s for app to boot...", :magenta
          sleep MRSK.config.readiness_delay

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

        rescue SSHKit::Command::Failed => e
          if e.message =~ /already in use/
            error "Rebooting container with same version #{version} already deployed on #{host} (may cause gap in zero-downtime promise!)"
            execute *MRSK.auditor.record("Rebooted app version #{version}"), verbosity: :debug

            execute *MRSK.app.stop(version: version)
            execute *MRSK.app.remove_container(version: version)
            execute *MRSK.app.run(role: role.name)
          else
            raise
          end
        end
      end
    end
  end
end

#containersObject



105
106
107
# File 'lib/mrsk/cli/app.rb', line 105

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

#detailsObject



58
59
60
# File 'lib/mrsk/cli/app.rb', line 58

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

#exec(cmd) ⇒ Object



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
96
97
98
99
100
101
102
# File 'lib/mrsk/cli/app.rb', line 65

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.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(options[:version] || most_recent_version_available) 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|
        execute *MRSK.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
        puts_by_host host, capture_with_info(*MRSK.app.execute_in_existing_container(cmd))
      end
    end

  else
    say "Get most recent version available as an image...", :magenta unless options[:version]
    using_version(options[:version] || most_recent_version_available) 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



110
111
112
# File 'lib/mrsk/cli/app.rb', line 110

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

#logsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/mrsk/cli/app.rb', line 119

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}..."
      info MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep)
      exec MRSK.app.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|
      begin
        puts_by_host host, capture_with_info(*MRSK.app.logs(since: since, lines: lines, grep: grep))
      rescue SSHKit::Command::Failed
        puts_by_host host, "Nothing found"
      end
    end
  end
end

#removeObject



145
146
147
148
149
# File 'lib/mrsk/cli/app.rb', line 145

def remove
  stop
  remove_containers
  remove_images
end

#remove_container(version) ⇒ Object



152
153
154
155
156
157
# File 'lib/mrsk/cli/app.rb', line 152

def remove_container(version)
  on(MRSK.hosts) do
    execute *MRSK.auditor.record("Removed app container with version #{version}"), verbosity: :debug
    execute *MRSK.app.remove_container(version: version)
  end
end

#remove_containersObject



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

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

#remove_imagesObject



168
169
170
171
172
173
# File 'lib/mrsk/cli/app.rb', line 168

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

#startObject



41
42
43
44
45
46
# File 'lib/mrsk/cli/app.rb', line 41

def start
  on(MRSK.hosts) do
    execute *MRSK.auditor.record("Started app version #{MRSK.version}"), verbosity: :debug
    execute *MRSK.app.start, raise_on_non_zero_exit: false
  end
end

#stopObject



49
50
51
52
53
54
# File 'lib/mrsk/cli/app.rb', line 49

def stop
  on(MRSK.hosts) do
    execute *MRSK.auditor.record("Stopped app"), verbosity: :debug
    execute *MRSK.app.stop, raise_on_non_zero_exit: false
  end
end

#versionObject



176
177
178
# File 'lib/mrsk/cli/app.rb', line 176

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