Class: KamalBackup::KamalBridge

Inherits:
Object
  • Object
show all
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.



12
13
14
15
16
17
18
19
20
21
# File 'lib/kamal_backup/kamal_bridge.rb', line 12

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



55
56
57
# File 'lib/kamal_backup/kamal_bridge.rb', line 55

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

#accessory_name(preferred: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kamal_backup/kamal_bridge.rb', line 23

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



59
60
61
62
63
64
65
# File 'lib/kamal_backup/kamal_bridge.rb', line 59

def execute_on_accessory(accessory_name:, command:, stream: false)
  if stream && (target = live_accessory_target(accessory_name))
    execute_on_accessory_live(accessory_name: accessory_name, command: command, target: target)
  else
    capture_kamal(kamal_exec_argv(accessory_name, command), stream: stream)
  end
end

#local_restore_defaults(accessory_name:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/kamal_backup/kamal_bridge.rb', line 44

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:



67
68
69
70
71
72
73
74
# File 'lib/kamal_backup/kamal_bridge.rb', line 67

def remote_version(accessory_name:)
  result = execute_on_accessory(accessory_name: accessory_name, command: '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