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!, #confirm_production_restore!, #default_deploy_config?, #deploy_snippet, #deployment_mode?, #direct_app, #ensure_remote_version_match!, #exec_remote, #init_config_root, #local_command_config, #local_preferences, #local_restore_app, #print_backup_result, #print_prune_result, #print_remote_version_status, #production_restore_confirmation_config, #production_source_defaults, #prompt_required, #redactor, #remote_command_mode?, #remote_version, #require_typed_confirmation, #shared_config_path, #shared_config_source_defaults, #shared_config_template, #validate_deploy_config, #write_init_file

Class Attribute Details

.command_envObject

Returns the value of attribute command_env.



391
392
393
# File 'lib/kamal_backup/cli.rb', line 391

def command_env
  @command_env
end

Class Method Details

.basenameObject



429
430
431
# File 'lib/kamal_backup/cli.rb', line 429

def self.basename
  "kamal-backup"
end

.normalize_global_options(argv) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/kamal_backup/cli.rb', line 393

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



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/kamal_backup/cli.rb', line 433

def self.start(argv = ARGV, env: ENV)
  self.command_env = env
  output = CommandOutput.new(io: $stderr, env: env)
  Command.with_output(output) do
    super(normalize_global_options(argv))
  end
rescue Error => e
  output ||= CommandOutput.new(io: $stderr, env: env)
  output.error("(#{e.class}): #{e.message}", redactor: Redactor.new(env: env))
  exit(1)
rescue StandardError => e
  output ||= CommandOutput.new(io: $stderr, env: env)
  output.error("(#{e.class}): #{e.message}", redactor: Redactor.new(env: env))
  exit(1)
rescue Interrupt
  output ||= CommandOutput.new(io: $stderr, env: env)
  output.error("(Interrupt): interrupted", redactor: Redactor.new(env: env))
  exit(130)
ensure
  self.command_env = nil
end

Instance Method Details

#backupObject



459
460
461
462
463
464
465
466
467
# File 'lib/kamal_backup/cli.rb', line 459

def backup
  if remote_command_mode?
    argv = ["kamal-backup", "backup"]
    argv << "--force" if options[:force]
    exec_remote(argv)
  else
    print_backup_result(direct_app.backup(force: options[:force]))
  end
end

#checkObject



479
480
481
482
483
484
485
# File 'lib/kamal_backup/cli.rb', line 479

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

#evidenceObject



497
498
499
500
501
502
503
# File 'lib/kamal_backup/cli.rb', line 497

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

#initObject



517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/kamal_backup/cli.rb', line 517

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 file backups with backup.schedule."
  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



470
471
472
473
474
475
476
# File 'lib/kamal_backup/cli.rb', line 470

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

#pruneObject



488
489
490
491
492
493
494
# File 'lib/kamal_backup/cli.rb', line 488

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

#scheduleObject



532
533
534
535
536
537
538
# File 'lib/kamal_backup/cli.rb', line 532

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

#validateObject



506
507
508
509
510
511
512
513
514
# File 'lib/kamal_backup/cli.rb', line 506

def validate
  if remote_command_mode?
    validate_deploy_config
  else
    direct_app.validate
  end

  puts("ok")
end

#versionObject



541
542
543
544
545
546
547
# File 'lib/kamal_backup/cli.rb', line 541

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