Class: Kitsune::Kit::Operations::ServiceFiles
- Inherits:
-
Object
- Object
- Kitsune::Kit::Operations::ServiceFiles
- Defined in:
- lib/kitsune/kit/operations/service_files.rb
Instance Method Summary collapse
- #backup_directory ⇒ Object
- #compose_path ⇒ Object
- #destroy(entry, marker_path:) ⇒ Object
- #directory ⇒ Object
- #ensure_managed_or_absent!(actual_fingerprint) ⇒ Object
- #env_path ⇒ Object
-
#initialize(config:, type:, transport:, state_store:, resource:, fingerprint:) ⇒ ServiceFiles
constructor
A new instance of ServiceFiles.
- #prepare_backup ⇒ Object
- #restore_after_failure(backed_up_files) ⇒ Object
- #restore_previous(previous, marker_path:) ⇒ Object
Constructor Details
#initialize(config:, type:, transport:, state_store:, resource:, fingerprint:) ⇒ ServiceFiles
Returns a new instance of ServiceFiles.
9 10 11 12 13 14 15 16 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 9 def initialize(config:, type:, transport:, state_store:, resource:, fingerprint:) @config = config @type = type @transport = transport @state_store = state_store @resource = resource @fingerprint = fingerprint end |
Instance Method Details
#backup_directory ⇒ Object
21 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 21 def backup_directory = "/var/lib/kitsune/backups/service-#{type}-#{fingerprint.call[0, 12]}" |
#compose_path ⇒ Object
19 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 19 def compose_path = "#{directory}/compose.yml" |
#destroy(entry, marker_path:) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 68 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 |
#directory ⇒ Object
18 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 18 def directory = "/home/#{config.ssh.user}/.local/share/kitsune/services/#{type}" |
#ensure_managed_or_absent!(actual_fingerprint) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 23 def ensure_managed_or_absent!(actual_fingerprint) return if managed? return if actual_fingerprint.empty? && !exists?(compose_path) && !exists?(env_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_path ⇒ Object
20 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 20 def env_path = "#{directory}/.env" |
#prepare_backup ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 34 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") [compose_path, env_path].select { |path| exists?(path) }.tap do |paths| paths.each { |path| copy(path, backup_directory) } end end |
#restore_after_failure(backed_up_files) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 47 def restore_after_failure(backed_up_files) if backed_up_files.empty? transport.execute("rm", arguments: ["-f", compose_path, env_path], timeout: 20) else backed_up_files.each { |path| copy("#{backup_directory}/#{File.basename(path)}", File.dirname(path)) } end end |
#restore_previous(previous, marker_path:) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/kitsune/kit/operations/service_files.rb', line 55 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 |