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



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

def boot
  cli = self

  say "Ensure no other version of the app is running...", :magenta
  stop

  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

    MRSK.config.roles.each do |role|
      on(role.hosts) do |host|
        begin
          execute *MRSK.app.run(role: role.name)
        rescue SSHKit::Command::Failed => e
          if e.message =~ /already in use/
            error "Rebooting container with same version already deployed on #{host}"

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

#containersObject



85
86
87
# File 'lib/mrsk/cli/app.rb', line 85

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

#currentObject



95
96
97
# File 'lib/mrsk/cli/app.rb', line 95

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

#current_versionObject



151
152
153
# File 'lib/mrsk/cli/app.rb', line 151

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

#detailsObject



45
46
47
# File 'lib/mrsk/cli/app.rb', line 45

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

#exec(cmd) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mrsk/cli/app.rb', line 52

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 on #{MRSK.primary_host}...", :magenta
      on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.execute_in_existing_container(cmd)) }
    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 on #{MRSK.primary_host}...", :magenta
      on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.execute_in_new_container(cmd)) }
    end
  end
end

#imagesObject



90
91
92
# File 'lib/mrsk/cli/app.rb', line 90

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

#logsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mrsk/cli/app.rb', line 104

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



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

def remove
  remove_containers
  remove_images
end

#remove_container(version) ⇒ Object



136
137
138
# File 'lib/mrsk/cli/app.rb', line 136

def remove_container(version)
  on(MRSK.hosts) { execute *MRSK.app.remove_container(version: version) }
end

#remove_containersObject



141
142
143
# File 'lib/mrsk/cli/app.rb', line 141

def remove_containers
  on(MRSK.hosts) { execute *MRSK.app.remove_containers }
end

#remove_imagesObject



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

def remove_images
  on(MRSK.hosts) { execute *MRSK.app.remove_images }
end

#startObject



35
36
37
# File 'lib/mrsk/cli/app.rb', line 35

def start
  on(MRSK.hosts) { execute *MRSK.app.start, raise_on_non_zero_exit: false }
end

#stopObject



40
41
42
# File 'lib/mrsk/cli/app.rb', line 40

def stop
  on(MRSK.hosts) { execute *MRSK.app.stop, raise_on_non_zero_exit: false }
end