Class: KamalBackup::CLI

Inherits:
Thor
  • Object
show all
Includes:
Helpers
Defined in:
lib/kamal_backup/cli.rb

Defined Under Namespace

Modules: Helpers Classes: CommandBase, DrillCLI, RestoreCLI

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#accessory_name, #accessory_reboot_command, #bridge, #command_env, #confirm!, #default_deploy_config?, #deploy_snippet, #deployment_mode?, #direct_app, #ensure_remote_version_match!, #exec_remote, #init_config_root, #local_app, #local_command_config, #local_preferences, #print_remote_version_status, #production_source_defaults, #prompt_required, #redactor, #remote_version, #shared_config_path, #shared_config_source_defaults, #shared_config_template, #validate_deploy_config, #validate_deploy_mode?, #version_remote_mode?, #write_init_file

Class Attribute Details

.command_envObject

Returns the value of attribute command_env.



306
307
308
# File 'lib/kamal_backup/cli.rb', line 306

def command_env
  @command_env
end

Class Method Details

.basenameObject



344
345
346
# File 'lib/kamal_backup/cli.rb', line 344

def self.basename
  "kamal-backup"
end

.normalize_global_options(argv) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/kamal_backup/cli.rb', line 308

def normalize_global_options(argv)
  tokens = Array(argv).dup
  leading = []

  while tokens.any?
    token = tokens.first

    case token
    when "-d", "--destination", "-c", "--config-file"
      leading << tokens.shift
      leading << tokens.shift if tokens.any?
    when /\A--destination=.+\z/, /\A--config-file=.+\z/
      leading << tokens.shift
    else
      break
    end
  end

  if leading.empty? || tokens.empty?
    Array(argv)
  else
    [tokens.shift, *leading, *tokens]
  end
end

.start(argv = ARGV, env: ENV) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/kamal_backup/cli.rb', line 348

def self.start(argv = ARGV, env: ENV)
  self.command_env = env
  super(normalize_global_options(argv))
rescue Error => e
  warn("kamal-backup: #{Redactor.new(env: env).redact_string(e.message)}")
  exit(1)
rescue Interrupt
  warn("kamal-backup: interrupted")
  exit(130)
ensure
  self.command_env = nil
end

Instance Method Details

#backupObject



364
365
366
367
368
369
370
# File 'lib/kamal_backup/cli.rb', line 364

def backup
  if deployment_mode?
    exec_remote(["kamal-backup", "backup"])
  else
    direct_app.backup
  end
end

#checkObject



382
383
384
385
386
387
388
# File 'lib/kamal_backup/cli.rb', line 382

def check
  if deployment_mode?
    exec_remote(["kamal-backup", "check"])
  else
    puts(direct_app.check)
  end
end

#evidenceObject



391
392
393
394
395
396
397
# File 'lib/kamal_backup/cli.rb', line 391

def evidence
  if deployment_mode?
    exec_remote(["kamal-backup", "evidence"])
  else
    puts(direct_app.evidence)
  end
end

#initObject



411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/kamal_backup/cli.rb', line 411

def init
  write_init_file(shared_config_path, shared_config_template)

  puts
  puts "Add this accessory block to your Kamal deploy config:"
  puts
  puts deploy_snippet
  puts
  puts "The accessory runs scheduled database and Active Storage backups with backup_schedule_seconds."
  puts "For most Rails apps, restore local and drill local can infer the development database, Active Storage path, and tmp state directory."
  puts "Local restore and drill also require the restic binary on your machine."
  puts "Create config/kamal-backup.local.yml only if you need to override those local defaults."
end

#listObject



373
374
375
376
377
378
379
# File 'lib/kamal_backup/cli.rb', line 373

def list
  if deployment_mode?
    exec_remote(["kamal-backup", "list"])
  else
    puts(direct_app.snapshots)
  end
end

#scheduleObject



426
427
428
429
430
431
432
# File 'lib/kamal_backup/cli.rb', line 426

def schedule
  if deployment_mode?
    exec_remote(["kamal-backup", "schedule"])
  else
    direct_app.schedule
  end
end

#validateObject



400
401
402
403
404
405
406
407
408
# File 'lib/kamal_backup/cli.rb', line 400

def validate
  if validate_deploy_mode?
    validate_deploy_config
  else
    direct_app.validate
  end

  puts("ok")
end

#versionObject



435
436
437
438
439
440
441
# File 'lib/kamal_backup/cli.rb', line 435

def version
  if version_remote_mode?
    print_remote_version_status
  else
    puts(VERSION)
  end
end