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.
71 72 73 74 75 76 77 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 71 def initialize(ctid, data) @ctid = ctid data.each do |k, v| instance_variable_set("@#{k}", v) end end |
Instance Attribute Details
#ctid ⇒ String (readonly)
52 53 54 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 52 def ctid @ctid end |
#opts ⇒ Hash (readonly)
Migration options
65 66 67 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 65 def opts @opts end |
#snapshots ⇒ Array<String> (readonly)
List of created snapshots
69 70 71 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 69 def snapshots @snapshots end |
#step ⇒ Symbol (readonly)
55 56 57 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 55 def step @step end |
#target_ct ⇒ Container (readonly)
61 62 63 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 61 def target_ct @target_ct end |
#vz_ct ⇒ Vz6::Container (readonly)
58 59 60 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 58 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
20 21 22 23 24 25 26 27 28 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 20 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
33 34 35 36 37 38 39 40 41 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 33 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
43 44 45 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 43 def self.state_dir @state_dir ||= File.(DIR) end |
.state_file(ctid) ⇒ Object
47 48 49 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 47 def self.state_file(ctid) File.join(state_dir, "#{ctid}.state") end |
Instance Method Details
#can_proceed?(new_step) ⇒ Boolean
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 80 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
121 122 123 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 121 def destroy File.unlink(state_file) end |
#save ⇒ Object
Persist the state to disk
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 101 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
94 95 96 97 98 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 94 def set_step(new_step) raise 'invalid migration sequence' unless can_proceed?(new_step) @step = new_step end |
#state_dir ⇒ Object (protected)
127 128 129 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 127 def state_dir self.class.state_dir end |
#state_file ⇒ Object (protected)
131 132 133 |
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 131 def state_file self.class.state_file(ctid) end |