Class: Dash::Cli::Main

Inherits:
Base
  • Object
show all
Defined in:
lib/dash/cli/main.rb

Defined Under Namespace

Classes: Migrate

Constant Summary

Constants inherited from Base

Base::AUTOMATIC_DEPLOY_LOCK_MESSAGE, Base::HOOK_MUTEX, Base::VERBOSITY

Instance Method Summary collapse

Methods inherited from Base

dynamic_command_class, exit_on_failure?, #initialize

Constructor Details

This class inherits a constructor from Dash::Cli::Base

Instance Method Details

#audit ⇒ Object



154
155
156
157
158
159
# File 'lib/dash/cli/main.rb', line 154

def audit
  quiet = options[:quiet]
  on(DASH.hosts) do |host|
    puts_by_host host, capture_with_info(*DASH.auditor.reveal), quiet: quiet
  end
end

#config ⇒ Object



162
163
164
165
166
# File 'lib/dash/cli/main.rb', line 162

def config
  run_locally do
    puts Dash::Utils.redacted(DASH.config.to_h).to_yaml
  end
end

#deploy(boot_accessories: false) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dash/cli/main.rb', line 21

def deploy(boot_accessories: false)
  modify do
    runtime = print_runtime do
      invoke_options = deploy_options

      print_config_banner

      say "Validate configuration and secrets...", :magenta
      timed("Validate config and secrets") { DASH.config.validate_secrets!(include_accessories: boot_accessories) }

      if options[:skip_push]
        say "Pull app image...", :magenta
        timed("Pull app image") { invoke "dash:cli:build:pull", [], invoke_options }
      else
        say "Build and push app image...", :magenta
        timed("Build and push app image") do |entry|
          DASH.report.build_entry = entry
          invoke "dash:cli:build:deliver", [], invoke_options
        end
      end

      # Before the boot, so the advice still prints when a boot fails — a slow build is
      # exactly the kind of thing an operator wants to see on a deploy that went wrong.
      analyze_report

      modify(lock: true) do
        run_hook "pre-deploy", secrets: true

        say "Ensure dash-proxy is running...", :magenta
        timed("Ensure dash-proxy") { invoke "dash:cli:proxy:boot", [], invoke_options }

        timed("Boot accessories") { invoke "dash:cli:accessory:boot", [ "all" ], invoke_options } if boot_accessories

        say "Detect stale containers...", :magenta
        timed("Detect stale containers") { invoke "dash:cli:app:stale_containers", [], invoke_options.merge(stop: true) }

        timed("Boot") { invoke "dash:cli:app:boot", [], invoke_options }

        if DASH.config.proxy.load_balancing?
          say "Updating loadbalancer configuration...", :magenta
          timed("Loadbalancer") { invoke "dash:cli:proxy:loadbalancer", [ "deploy" ], invoke_options }
        end

        say "Prune old containers and images...", :magenta
        timed("Prune") { invoke "dash:cli:prune:all", [], invoke_options }
      end
    end

    run_hook "post-deploy", secrets: true, runtime: runtime.round.to_s, **report_hook_details
  end
end

#details ⇒ Object



147
148
149
150
151
# File 'lib/dash/cli/main.rb', line 147

def details
  invoke "dash:cli:proxy:details"
  invoke "dash:cli:app:details"
  invoke "dash:cli:accessory:details", [ "all" ]
end

#docs(section = nil) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/dash/cli/main.rb', line 169

def docs(section = nil)
  case section
  when NilClass
    puts Dash::Configuration.validation_doc
  else
    puts Dash::Configuration.const_get(section.titlecase.to_sym).validation_doc
  end
rescue NameError
  puts "No documentation found for #{section}"
end

#doctor ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dash/cli/main.rb', line 181

def doctor
  say "Running readiness checks...", :magenta
  pre_connect_if_required

  doctor = Dash::Cli::Doctor.new
  doctor.run

  print_doctor_report doctor.results

  if doctor.successful?
    say doctor_summary(doctor), :green
  else
    raise Dash::Cli::DoctorError, doctor_failure_message(doctor)
  end
end

#init ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/dash/cli/main.rb', line 199

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

  # Resolved rather than hardcoded to .dash: a project still on .kamal must
  # keep getting its stubs there, or init would create a second directory that
  # silently wins resolution and orphans the operator's real secrets.
  project_directory = Dash::ProjectDirectory.path

  unless (secrets_file = Pathname.new(File.expand_path("#{project_directory}/secrets"))).exist?
    FileUtils.mkdir_p secrets_file.dirname
    FileUtils.cp_r Pathname.new(File.expand_path("templates/secrets", __dir__)), secrets_file
    puts "Created #{project_directory}/secrets file"
  end

  unless (hooks_dir = Pathname.new(File.expand_path("#{project_directory}/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 #{project_directory}/hooks"
  end

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

#migrate ⇒ Object



258
259
260
# File 'lib/dash/cli/main.rb', line 258

def migrate
  Dash::Cli::Main::Migrate.new(dry_run: options[:dry_run]).run.each { |message, color| say message, color }
end

#redeploy ⇒ Object



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dash/cli/main.rb', line 76

def redeploy
  modify do
    runtime = print_runtime do
      invoke_options = deploy_options

      print_config_banner

      say "Validate configuration and secrets...", :magenta
      timed("Validate config and secrets") { DASH.config.validate_secrets! }

      if options[:skip_push]
        say "Pull app image...", :magenta
        timed("Pull app image") { invoke "dash:cli:build:pull", [], invoke_options }
      else
        say "Build and push app image...", :magenta
        timed("Build and push app image") do |entry|
          DASH.report.build_entry = entry
          invoke "dash:cli:build:deliver", [], invoke_options
        end
      end

      # Before the boot, so the advice still prints when a boot fails — a slow build is
      # exactly the kind of thing an operator wants to see on a deploy that went wrong.
      analyze_report

      modify(lock: true) do
        run_hook "pre-deploy", secrets: true

        say "Detect stale containers...", :magenta
        timed("Detect stale containers") { invoke "dash:cli:app:stale_containers", [], invoke_options.merge(stop: true) }

        timed("Boot") { invoke "dash:cli:app:boot", [], invoke_options }

        if DASH.config.proxy.load_balancing?
          say "Updating loadbalancer configuration...", :magenta
          timed("Loadbalancer") { invoke "dash:cli:proxy:loadbalancer", [ "deploy" ], invoke_options }
        end
      end
    end

    run_hook "post-deploy", secrets: true, runtime: runtime.round.to_s, **report_hook_details
  end
end

#remove ⇒ Object



245
246
247
248
249
250
251
252
253
254
# File 'lib/dash/cli/main.rb', line 245

def remove
  confirming "This will remove all containers and images. Are you sure?" do
    modify(lock: true) do
      invoke "dash:cli:app:remove", [], options.without(:confirmed)
      invoke "dash:cli:proxy:remove", [], options.without(:confirmed)
      invoke "dash:cli:accessory:remove", [ "all" ], options
      invoke "dash:cli:registry:remove", [], options.without(:confirmed).merge(skip_local: true)
    end
  end
end

#rollback(version) ⇒ Object



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

def rollback(version)
  rolled_back = false

  modify do
    runtime = print_runtime do
      modify(lock: true) do
        invoke_options = deploy_options

        DASH.config.version = version

        if container_available?(version)
          run_hook "pre-deploy", secrets: true

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

    run_hook "post-deploy", secrets: true, runtime: runtime.round.to_s, **report_hook_details if rolled_back
  end
end

#setup ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dash/cli/main.rb', line 5

def setup
  print_runtime do
    modify(lock: true) do
      invoke_options = deploy_options

      say "Ensure Docker is installed...", :magenta
      timed("Ensure Docker is installed") { invoke "dash:cli:server:bootstrap", [], invoke_options }

      deploy(boot_accessories: true)
    end
  end
end

#upgrade ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/dash/cli/main.rb', line 265

def upgrade
  confirming "This will replace Traefik with dash-proxy and restart all accessories" do
    modify(lock: true) do
      if options[:rolling]
        DASH.hosts.each do |host|
          DASH.with_specific_hosts(host) do
            say "Upgrading #{host}...", :magenta
            if DASH.app_hosts.include?(host)
              invoke "dash:cli:proxy:upgrade", [], options.merge(confirmed: true, rolling: false)
              reset_invocation(Dash::Cli::Proxy)
            end
            if DASH.accessory_hosts.include?(host)
              invoke "dash:cli:accessory:upgrade", [ "all" ], options.merge(confirmed: true, rolling: false)
              reset_invocation(Dash::Cli::Accessory)
            end
            say "Upgraded #{host}", :magenta
          end
        end
      else
        say "Upgrading all hosts...", :magenta
        invoke "dash:cli:proxy:upgrade", [], options.merge(confirmed: true)
        invoke "dash:cli:accessory:upgrade", [ "all" ], options.merge(confirmed: true)
        say "Upgraded all hosts", :magenta
      end
    end
  end
end

#version ⇒ Object



294
295
296
# File 'lib/dash/cli/main.rb', line 294

def version
  puts Dash::VERSION
end