Class: KamalBackup::KamalBridge

Inherits:
Object
  • Object
show all
Includes:
YamlAccess
Defined in:
lib/kamal_backup/kamal_bridge.rb

Defined Under Namespace

Classes: FilteringIO

Constant Summary collapse

DEFAULT_CONFIG_FILE =
'config/deploy.yml'
VERSION_LINE_PATTERN =
/\A\d+(?:\.\d+)+(?:[-.][A-Za-z0-9]+)*\z/

Instance Method Summary collapse

Constructor Details

#initialize(redactor:, config_file: nil, destination: nil, env: ENV, cwd: Dir.pwd, stdout: $stdout, stderr: $stderr) ⇒ KamalBridge

Returns a new instance of KamalBridge.



30
31
32
33
34
35
36
37
38
39
# File 'lib/kamal_backup/kamal_bridge.rb', line 30

def initialize(redactor:, config_file: nil, destination: nil, env: ENV, cwd: Dir.pwd, stdout: $stdout,
               stderr: $stderr)
  @redactor = redactor
  @config_file = config_file
  @destination = destination
  @env = env
  @cwd = cwd
  @stdout = stdout
  @stderr = stderr
end

Instance Method Details

#accessory_environment(accessory_name:) ⇒ Object



73
74
75
# File 'lib/kamal_backup/kamal_bridge.rb', line 73

def accessory_environment(accessory_name:)
  accessory_secret_placeholders(accessory_name).merge(accessory_clear_env(accessory_name))
end

#accessory_name(preferred: nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kamal_backup/kamal_bridge.rb', line 41

def accessory_name(preferred: nil)
  if preferred && !preferred.to_s.strip.empty?
    accessory_clear_env(preferred)
    return preferred.to_s
  end

  matching = accessories.select do |_name, accessory|
    fetch(accessory, :image).to_s.include?('kamal-backup')
  end

  if matching.size == 1
    matching.keys.first.to_s
  elsif accessories.key?('backup') || accessories.key?(:backup)
    'backup'
  else
    names = accessories.keys.map(&:to_s).sort
    raise ConfigurationError,
          "could not infer the backup accessory from #{names.join(', ')}; set accessory in config/kamal-backup.yml"
  end
end

#execute_on_accessory(accessory_name:, command:, stream: false) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/kamal_backup/kamal_bridge.rb', line 77

def execute_on_accessory(accessory_name:, command:, stream: false)
  command_argv = remote_command_argv(command)

  if stream && (target = live_accessory_target(accessory_name))
    execute_on_accessory_live(accessory_name: accessory_name, command_argv: command_argv, target: target)
  else
    capture_kamal(kamal_exec_argv(accessory_name, command_argv), stream: stream)
  end
end

#local_restore_defaults(accessory_name:) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/kamal_backup/kamal_bridge.rb', line 62

def local_restore_defaults(accessory_name:)
  clear_env = accessory_clear_env(accessory_name)

  {}.tap do |defaults|
    defaults['APP_NAME'] = clear_env['APP_NAME'] if clear_env['APP_NAME']
    defaults['DATABASE_ADAPTER'] = clear_env['DATABASE_ADAPTER'] if clear_env['DATABASE_ADAPTER']
    defaults['RESTIC_REPOSITORY'] = clear_env['RESTIC_REPOSITORY'] if clear_env['RESTIC_REPOSITORY']
    defaults['LOCAL_RESTORE_SOURCE_PATHS'] = clear_env['BACKUP_PATHS'] if clear_env['BACKUP_PATHS']
  end
end

#remote_version(accessory_name:) ⇒ Object

Raises:



87
88
89
90
91
92
93
94
# File 'lib/kamal_backup/kamal_bridge.rb', line 87

def remote_version(accessory_name:)
  result = execute_on_accessory(accessory_name: accessory_name, command: %w[kamal-backup version])
  version = parse_version_line(result.stdout)

  raise ConfigurationError, "could not determine remote kamal-backup version from accessory #{accessory_name}" if version.empty?

  version
end