Class: VpsAdminOS::Converter::Vz6::Migrator::State

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#ctidString (readonly)

Returns:

  • (String)


52
53
54
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 52

def ctid
  @ctid
end

#optsHash (readonly)

Migration options

Returns:

  • (Hash)


65
66
67
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 65

def opts
  @opts
end

#snapshotsArray<String> (readonly)

List of created snapshots

Returns:

  • (Array<String>)


69
70
71
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 69

def snapshots
  @snapshots
end

#stepSymbol (readonly)

Returns:

  • (Symbol)


55
56
57
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 55

def step
  @step
end

#target_ctContainer (readonly)

Returns:



61
62
63
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 61

def target_ct
  @target_ct
end

#vz_ctVz6::Container (readonly)

Returns:



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

Parameters:

Options Hash (opts):

  • :dst (String)
  • :port (Integer)
  • :zfs (Boolean)
  • :zfs_dataset (String)
  • :zfs_subdir (String)
  • :zfs_compressed_send (Boolean)

Returns:

  • (Cli::Vz6::State)


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

Parameters:

  • ctid (String)

Returns:

  • (Cli::Vz6::State)


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_dirObject



43
44
45
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 43

def self.state_dir
  @state_dir ||= File.expand_path(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

Parameters:

  • new_step (Symbol)

Returns:

  • (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

#destroyObject

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

#saveObject

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

Parameters:

  • new_step (Symbol)


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_dirObject (protected)



127
128
129
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 127

def state_dir
  self.class.state_dir
end

#state_fileObject (protected)



131
132
133
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 131

def state_file
  self.class.state_file(ctid)
end