Class: Kitsune::Kit::Operations::ServiceFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/kitsune/kit/operations/service_files.rb

Instance Method Summary collapse

Constructor Details

#initialize(config:, type:, transport:, state_store:, resource:, fingerprint:, compose_filenames:) ⇒ ServiceFiles

Returns a new instance of ServiceFiles.



9
10
11
12
13
14
15
16
17
# File 'lib/kitsune/kit/operations/service_files.rb', line 9

def initialize(config:, type:, transport:, state_store:, resource:, fingerprint:, compose_filenames:)
  @config = config
  @type = type
  @transport = transport
  @state_store = state_store
  @resource = resource
  @fingerprint = fingerprint
  @compose_filenames = compose_filenames
end

Instance Method Details

#backup_directoryObject



23
# File 'lib/kitsune/kit/operations/service_files.rb', line 23

def backup_directory = "/var/lib/kitsune/backups/service-#{type}-#{fingerprint.call[0, 12]}"

#compose_path(filename = "compose.yml") ⇒ Object



20
# File 'lib/kitsune/kit/operations/service_files.rb', line 20

def compose_path(filename = "compose.yml") = "#{directory}/#{filename}"

#compose_pathsObject



21
# File 'lib/kitsune/kit/operations/service_files.rb', line 21

def compose_paths = compose_filenames.call.map { |filename| compose_path(filename) }

#destroy(entry, marker_path:) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/kitsune/kit/operations/service_files.rb', line 67

def destroy(entry, marker_path:)
  result = transport.execute("rm", arguments: ["-rf", directory], timeout: 30)
  raise_remote!(result, "remove service files")
  result = transport.execute("sudo", arguments: ["rm", "-f", marker_path], timeout: 10)
  raise_remote!(result, "remove service marker")
  backup_directories(entry).each { |backup| remove_backup(backup) }
end

#directoryObject



19
# File 'lib/kitsune/kit/operations/service_files.rb', line 19

def directory = "/home/#{config.ssh.user}/.local/share/kitsune/services/#{type}"

#ensure_managed_or_absent!(actual_fingerprint) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/kitsune/kit/operations/service_files.rb', line 25

def ensure_managed_or_absent!(actual_fingerprint)
  return if managed?
  return if actual_fingerprint.empty? && known_paths.none? { |path| exists?(path) }

  raise Errors::UnsafeOperationError.new(
    "refusing to replace unmanaged #{type} service files",
    hint: "Restore the local state backup or move the existing files before applying again.",
    context: { directory: directory }
  )
end

#env_pathObject



22
# File 'lib/kitsune/kit/operations/service_files.rb', line 22

def env_path = "#{directory}/.env"

#prepare_backupObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kitsune/kit/operations/service_files.rb', line 36

def prepare_backup
  result = transport.execute(
    "sudo",
    arguments: ["install", "-d", "-m", "0700", "-o", config.ssh.user, "-g", config.ssh.user,
                backup_directory],
    timeout: 20
  )
  raise_remote!(result, "create service backup directory")
  known_paths.select { |path| exists?(path) }.tap do |paths|
    paths.each { |path| copy(path, backup_directory) }
  end
end

#restore_after_failure(backed_up_files) ⇒ Object



49
50
51
52
# File 'lib/kitsune/kit/operations/service_files.rb', line 49

def restore_after_failure(backed_up_files)
  transport.execute("rm", arguments: ["-f", *known_paths], timeout: 20)
  backed_up_files.each { |path| copy("#{backup_directory}/#{File.basename(path)}", File.dirname(path)) }
end

#restore_previous(previous, marker_path:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kitsune/kit/operations/service_files.rb', line 54

def restore_previous(previous, marker_path:)
  if previous["backup_directory"]
    restore_files(previous["backup_directory"], previous.fetch("backed_up_files", []))
    restore_marker(marker_path, previous["previous_fingerprint"])
    remove_backup(previous["backup_directory"])
    true
  else
    transport.execute("rm", arguments: ["-rf", directory], timeout: 30)
    transport.execute("sudo", arguments: ["rm", "-f", marker_path], timeout: 10)
    false
  end
end