Class: Kitsune::Kit::Operations::RemoteScript

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

Constant Summary collapse

MARKER_DIRECTORY =
"/var/lib/kitsune/state"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, transport_factory:, state_store:, resource:, script_path:, arguments: [], transport_roles: { apply: :deploy }, verification: {}) ⇒ RemoteScript

Returns a new instance of RemoteScript.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kitsune/kit/operations/remote_script.rb', line 15

def initialize(config:, transport_factory:, state_store:, resource:, script_path:, arguments: [],
               transport_roles: { apply: :deploy }, verification: {})
  @config = config
  @transport_factory = transport_factory
  @state_store = state_store
  @resource = resource
  @script_path = script_path
  @arguments = arguments.map(&:to_s).freeze
  @transport_roles = transport_roles
  @post_verify = verification[:callback]
  @finalize_action = verification[:finalize_action]
  @rollback_on_failure = verification.fetch(:rollback_on_failure, false)
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



13
14
15
# File 'lib/kitsune/kit/operations/remote_script.rb', line 13

def resource
  @resource
end

Instance Method Details

#apply(change) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kitsune/kit/operations/remote_script.rb', line 45

def apply(change)
  return true unless change.changed?

  remote = "/tmp/kitsune-#{safe_resource}-#{fingerprint[0, 12]}.sh"
  applied = false
  current_transport.with_session do
    current_transport.upload(content: script, remote_path: remote, mode: "0700")
    execute_script(remote, "apply")
    applied = true
    execute_script(remote, "verify")
    @post_verify&.call
    finalize(remote)
    write_marker
    record_state(change)
    true
  rescue StandardError => e
    recover_verified_transition(remote, e) if applied && @rollback_on_failure
    raise e
  ensure
    cleanup(remote)
  end
end

#planObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kitsune/kit/operations/remote_script.rb', line 29

def plan
  return pending_change unless transport_factory.server&.public_ip

  actual = remote_fingerprint
  return unchanged_change if actual == fingerprint

  Change.new(
    resource: resource,
    action: actual.empty? ? "create" : "update",
    summary: actual.empty? ? "Configure #{resource}" : "Update #{resource}",
    details: { fingerprint: fingerprint, actual_fingerprint: actual.empty? ? nil : actual }
  )
rescue Errors::ConnectionError
  pending_change
end

#rollbackObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kitsune/kit/operations/remote_script.rb', line 68

def rollback
  state = state_store.read(config.environment).dig("resources", resource)
  return false unless state && state["managed"]

  remote = "/tmp/kitsune-#{safe_resource}-rollback.sh"
  rollback_transport.upload(content: script, remote_path: remote, mode: "0700")
  execute_script(remote, "rollback", transport: rollback_transport)
  rollback_transport.execute("sudo", arguments: ["rm", "-f", marker_path], timeout: 10)
  state_store.update(config.environment) do |current|
    current["resources"].delete(resource)
    current["operations"] << { "resource" => resource, "action" => "rollback", "status" => "applied" }
    current
  end
  true
ensure
  cleanup(remote, transport: rollback_transport) if defined?(remote) && remote
end