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'
STEPS =
%i(stage sync cancel transfer cleanup)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctid, data) ⇒ State

Returns a new instance of State



70
71
72
73
74
75
76
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 70

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)


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

def ctid
  @ctid
end

#optsHash (readonly)

Migration options

Returns:

  • (Hash)


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

def opts
  @opts
end

#snapshotsArray<String> (readonly)

List of created snapshots

Returns:

  • (Array<String>)


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

def snapshots
  @snapshots
end

#stepSymbol (readonly)

Returns:

  • (Symbol)


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

def step
  @step
end

#target_ctContainer (readonly)

Returns:



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

def target_ct
  @target_ct
end

#vz_ctVz6::Container (readonly)

Returns:



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

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: vz_ct,
    target_ct: target_ct,
    opts: 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
# 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)
  end

  fail 'invalid state format' unless ret.is_a?(Hash)
  new(ctid, ret)
end

.state_dirObject



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

def self.state_dir
  @dir ||= File.expand_path(DIR)
end

.state_file(ctid) ⇒ Object



46
47
48
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 46

def self.state_file(ctid)
  File.join(state_dir, "#{ctid}.state")
end

Instance Method Details

#can_proceed?(new_step) ⇒ Boolean

Parameters:

  • step (Symbol)

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 79

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



118
119
120
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 118

def destroy
  File.unlink(state_file)
end

#saveObject

Persist the state to disk



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 98

def save
  Dir.mkdir(state_dir, 0700) unless Dir.exist?(state_dir)

  orig = state_file
  tmp = "#{orig}.new"

  File.open(tmp, 'w', 0700) do |f|
    Marshal.dump({
      step: step,
      vz_ct: vz_ct,
      target_ct: target_ct,
      opts: opts,
      snapshots: snapshots,
    }, f)
  end

  File.rename(tmp, orig)
end

#set_step(new_step) ⇒ Object

Parameters:

  • step (Symbol)


92
93
94
95
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 92

def set_step(new_step)
  fail 'invalid migration sequence' unless can_proceed?(new_step)
  @step = new_step
end

#state_dirObject (protected)



123
124
125
# File 'lib/vpsadminos-converter/vz6/migrator/state.rb', line 123

def state_dir
  self.class.state_dir
end

#state_fileObject (protected)



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

def state_file
  self.class.state_file(ctid)
end