Class: Mrsk::Cli::Main

Inherits:
Base
  • Object
show all
Defined in:
lib/mrsk/cli/main.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

#auditObject



108
109
110
111
112
# File 'lib/mrsk/cli/main.rb', line 108

def audit
  on(MRSK.hosts) do |host|
    puts_by_host host, capture_with_info(*MRSK.auditor.reveal)
  end
end

#configObject



115
116
117
118
119
# File 'lib/mrsk/cli/main.rb', line 115

def config
  run_locally do
    puts Mrsk::Utils.redacted(MRSK.config.to_h).to_yaml
  end
end

#deployObject



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
43
44
45
46
47
48
# File 'lib/mrsk/cli/main.rb', line 15

def deploy
  runtime = print_runtime do
    with_lock do
      invoke_options = deploy_options

      say "Log into image registry...", :magenta
      invoke "mrsk:cli:registry:login", [], invoke_options

      if options[:skip_push]
        say "Pull app image...", :magenta
        invoke "mrsk:cli:build:pull", [], invoke_options
      else
        say "Build and push app image...", :magenta
        invoke "mrsk:cli:build:deliver", [], invoke_options
      end

      say "Ensure Traefik is running...", :magenta
      invoke "mrsk:cli:traefik:boot", [], invoke_options

      say "Ensure app can pass healthcheck...", :magenta
      invoke "mrsk:cli:healthcheck:perform", [], invoke_options

      say "Detect stale containers...", :magenta
      invoke "mrsk:cli:app:stale_containers", [], invoke_options

      invoke "mrsk:cli:app:boot", [], invoke_options

      say "Prune old containers and images...", :magenta
      invoke "mrsk:cli:prune:all", [], invoke_options
    end
  end

  run_hook "post-deploy", runtime: runtime.round
end

#detailsObject



101
102
103
104
105
# File 'lib/mrsk/cli/main.rb', line 101

def details
  invoke "mrsk:cli:traefik:details"
  invoke "mrsk:cli:app:details"
  invoke "mrsk:cli:accessory:details", [ "all" ]
end

#envifyObject



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/mrsk/cli/main.rb', line 162

def envify
  if destination = options[:destination]
    env_template_path = ".env.#{destination}.erb"
    env_path          = ".env.#{destination}"
  else
    env_template_path = ".env.erb"
    env_path          = ".env"
  end

  File.write(env_path, ERB.new(File.read(env_template_path)).result, perm: 0600)
end

#initObject



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

def init
  require "fileutils"

  if (deploy_file = Pathname.new(File.expand_path("config/deploy.yml"))).exist?
    puts "Config file already exists in config/deploy.yml (remove first to create a new one)"
  else
    FileUtils.mkdir_p deploy_file.dirname
    FileUtils.cp_r Pathname.new(File.expand_path("templates/deploy.yml", __dir__)), deploy_file
    puts "Created configuration file in config/deploy.yml"
  end

  unless (deploy_file = Pathname.new(File.expand_path(".env"))).exist?
    FileUtils.cp_r Pathname.new(File.expand_path("templates/template.env", __dir__)), deploy_file
    puts "Created .env file"
  end

  unless (hooks_dir = Pathname.new(File.expand_path(".mrsk/hooks"))).exist?
    hooks_dir.mkpath
    Pathname.new(File.expand_path("templates/sample_hooks", __dir__)).each_child do |sample_hook|
      FileUtils.cp sample_hook, hooks_dir, preserve: true
    end
    puts "Created sample hooks in .mrsk/hooks"
  end

  if options[:bundle]
    if (binstub = Pathname.new(File.expand_path("bin/mrsk"))).exist?
      puts "Binstub already exists in bin/mrsk (remove first to create a new one)"
    else
      puts "Adding MRSK to Gemfile and bundle..."
      run_locally do
        execute :bundle, :add, :mrsk
        execute :bundle, :binstubs, :mrsk
      end
      puts "Created binstub file in bin/mrsk"
    end
  end
end

#redeployObject



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

def redeploy
  runtime = print_runtime do
    with_lock do
      invoke_options = deploy_options

      if options[:skip_push]
        say "Pull app image...", :magenta
        invoke "mrsk:cli:build:pull", [], invoke_options
      else
        say "Build and push app image...", :magenta
        invoke "mrsk:cli:build:deliver", [], invoke_options
      end

      say "Ensure app can pass healthcheck...", :magenta
      invoke "mrsk:cli:healthcheck:perform", [], invoke_options

      say "Detect stale containers...", :magenta
      invoke "mrsk:cli:app:stale_containers", [], invoke_options

      invoke "mrsk:cli:app:boot", [], invoke_options
    end
  end

  run_hook "post-deploy", runtime: runtime.round
end

#removeObject



176
177
178
179
180
181
182
183
184
185
# File 'lib/mrsk/cli/main.rb', line 176

def remove
  with_lock do
    if options[:confirmed] || ask("This will remove all containers and images. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
      invoke "mrsk:cli:traefik:remove", [], options.without(:confirmed)
      invoke "mrsk:cli:app:remove", [], options.without(:confirmed)
      invoke "mrsk:cli:accessory:remove", [ "all" ], options
      invoke "mrsk:cli:registry:logout", [], options.without(:confirmed)
    end
  end
end

#rollback(version) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mrsk/cli/main.rb', line 79

def rollback(version)
  rolled_back = false
  runtime = print_runtime do
    with_lock do
      invoke_options = deploy_options

      MRSK.config.version = version
      old_version = nil

      if container_available?(version)
        invoke "mrsk:cli:app:boot", [], invoke_options.merge(version: version)
        rolled_back = true
      else
        say "The app version '#{version}' is not available as a container (use 'mrsk app containers' for available versions)", :red
      end
    end
  end

  run_hook "post-deploy", runtime: runtime.round if rolled_back
end

#setupObject



3
4
5
6
7
8
9
10
11
# File 'lib/mrsk/cli/main.rb', line 3

def setup
  print_runtime do
    with_lock do
      invoke "mrsk:cli:server:bootstrap"
      invoke "mrsk:cli:accessory:boot", [ "all" ]
      deploy
    end
  end
end

#versionObject



188
189
190
# File 'lib/mrsk/cli/main.rb', line 188

def version
  puts Mrsk::VERSION
end