Class: VpsAdminOS::Converter::Vz6::Migrator::State
- Inherits:
-
Object
- Object
- VpsAdminOS::Converter::Vz6::Migrator::State
- Defined in:
- lib/vpsadminos-converter/vz6/migrator/state.rb
Overview
Store/load migration state to/from disk
Constant Summary collapse
- DIR =
'~/.vpsadminos-converter'.freeze
- STEPS =
%i[stage sync cancel transfer cleanup].freeze
Instance Attribute Summary collapse
- #ctid ⇒ String readonly
-
#opts ⇒ Hash
readonly
Migration options.
-
#snapshots ⇒ Array<String>
readonly
List of created snapshots.
- #step ⇒ Symbol readonly
- #target_ct ⇒ Container readonly
- #vz_ct ⇒ Vz6::Container readonly
Class Method Summary collapse
-
.create(vz_ct, target_ct, opts) ⇒ Cli::Vz6::State
Create a new migration state, save it to disk and return it.
-
.load(ctid) ⇒ Cli::Vz6::State
Load migration state from disk.
- .state_dir ⇒ Object
- .state_file(ctid) ⇒ Object
Instance Method Summary collapse
- #can_proceed?(new_step) ⇒ Boolean
-
#destroy ⇒ Object
Remove the state from disk.
-
#initialize(ctid, data) ⇒ State
constructor
A new instance of State.
-
#save ⇒ Object
Persist the state to disk.
- #set_step(new_step) ⇒ Object
- #state_dir ⇒ Object protected
- #state_file ⇒ Object protected
Constructor Details
#initialize(ctid, data) ⇒ State
Returns a new instance of State.
72 73 74 75 76 77 78 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 72 def initialize(ctid, data) @ctid = ctid data.each do |k, v| instance_variable_set("@#{k}", v) end end |
Instance Attribute Details
#ctid ⇒ String (readonly)
53 54 55 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 53 def ctid @ctid end |
#opts ⇒ Hash (readonly)
Migration options
66 67 68 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 66 def opts @opts end |
#snapshots ⇒ Array<String> (readonly)
List of created snapshots
70 71 72 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 70 def snapshots @snapshots end |
#step ⇒ Symbol (readonly)
56 57 58 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 56 def step @step end |
#target_ct ⇒ Container (readonly)
62 63 64 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 62 def target_ct @target_ct end |
#vz_ct ⇒ Vz6::Container (readonly)
59 60 61 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 59 def vz_ct @vz_ct end |
Class Method Details
.create(vz_ct, target_ct, opts) ⇒ Cli::Vz6::State
Create a new migration state, save it to disk and return it
21 22 23 24 25 26 27 28 29 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 21 def self.create(vz_ct, target_ct, opts) new(target_ct.id, { step: :stage, vz_ct:, target_ct:, opts:, snapshots: [] }) end |
.load(ctid) ⇒ Cli::Vz6::State
Load migration state from disk
34 35 36 37 38 39 40 41 42 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 34 def self.load(ctid) ret = File.open(state_file(ctid)) do |f| Marshal.load(f) # rubocop:disable Security/MarshalLoad end raise 'invalid state format' unless ret.is_a?(Hash) new(ctid, ret) end |
.state_dir ⇒ Object
44 45 46 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 44 def self.state_dir @state_dir ||= File.(DIR) end |
.state_file(ctid) ⇒ Object
48 49 50 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 48 def self.state_file(ctid) File.join(state_dir, "#{ctid}.state") end |
Instance Method Details
#can_proceed?(new_step) ⇒ Boolean
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 81 def can_proceed?(new_step) return false if new_step == step new_i = STEPS.index(new_step) cur_i = STEPS.index(step) return false if new_i < cur_i return true if new_step == :transfer && step == :sync return false if new_step != :cancel && new_i != (cur_i + 1) true end |
#destroy ⇒ Object
Remove the state from disk
122 123 124 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 122 def destroy File.unlink(state_file) end |
#save ⇒ Object
Persist the state to disk
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 102 def save FileUtils.mkdir_p(state_dir, mode: 0o700) orig = state_file tmp = "#{orig}.new" File.open(tmp, 'w', 0o700) do |f| Marshal.dump({ step:, vz_ct:, target_ct:, opts:, snapshots: }, f) end File.rename(tmp, orig) end |
#set_step(new_step) ⇒ Object
95 96 97 98 99 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 95 def set_step(new_step) raise 'invalid migration sequence' unless can_proceed?(new_step) @step = new_step end |
#state_dir ⇒ Object (protected)
128 129 130 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 128 def state_dir self.class.state_dir end |
#state_file ⇒ Object (protected)
132 133 134 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 132 def state_file self.class.state_file(ctid) end |