Class: OsUp::PoolMigrations

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::File, OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/osup/pool_migrations.rb

Constant Summary collapse

FILE =
'/.migrations'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ PoolMigrations

Returns a new instance of PoolMigrations.



29
30
31
32
33
34
35
36
37
# File 'lib/osup/pool_migrations.rb', line 29

def initialize(pool)
  @pool = pool
  @applied = []
  @all = []

  load_pool
  load_applied
  build_list
end

Instance Attribute Details

#allArray<Array> (readonly)

Returns:

  • (Array<Array>)


27
28
29
# File 'lib/osup/pool_migrations.rb', line 27

def all
  @all
end

#appliedArray<Integer> (readonly)

Returns:

  • (Array<Integer>)


24
25
26
# File 'lib/osup/pool_migrations.rb', line 24

def applied
  @applied
end

#datasetString (readonly)

Returns osctl root dataset on pool.

Returns:

  • (String)

    osctl root dataset on pool



15
16
17
# File 'lib/osup/pool_migrations.rb', line 15

def dataset
  @dataset
end

#mountpointString (readonly)

Returns mountpoint of osctl root dataset.

Returns:

  • (String)

    mountpoint of osctl root dataset



18
19
20
# File 'lib/osup/pool_migrations.rb', line 18

def mountpoint
  @mountpoint
end

#poolString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/osup/pool_migrations.rb', line 12

def pool
  @pool
end

#version_fileString (readonly)

Returns path to file with a list of applied migrations.

Returns:

  • (String)

    path to file with a list of applied migrations



21
22
23
# File 'lib/osup/pool_migrations.rb', line 21

def version_file
  @version_file
end

Instance Method Details

#applied?(m) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


40
41
42
# File 'lib/osup/pool_migrations.rb', line 40

def applied?(m)
  @applied.include?(m.id)
end

#build_listObject (protected)

Build a list of all migrations, applied and unapplied and even those that aren’t recognized by ‘osup`



125
126
127
128
129
# File 'lib/osup/pool_migrations.rb', line 125

def build_list
  @all = (MigrationList.get.map(&:id) + applied).sort.uniq.map do |id|
    [id, MigrationList[id]]
  end
end

#each {|m| ... } ⇒ Object

Yield Parameters:



45
46
47
# File 'lib/osup/pool_migrations.rb', line 45

def each(&)
  applied.lazy.map { |id| MigrationList[id] }.each(&)
end

#load_appliedObject (protected)



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/osup/pool_migrations.rb', line 104

def load_applied
  File.open(version_file, 'r') do |f|
    f.each_line do |line|
      id = line.strip.to_i

      if id <= 0
        warn "invalid migration id '#{line}'"
        next
      end

      applied << id
    end
  end

  applied.uniq!
rescue Errno::ENOENT
  # no migrations applied
end

#load_poolObject (protected)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/osup/pool_migrations.rb', line 84

def load_pool
  mnt, active, ds = zfs(
    :get,
    '-Hp -ovalue mountpoint,org.vpsadminos.osctl:active,org.vpsadminos.osctl:dataset',
    pool
  ).output.strip.split

  raise "pool #{pool} is not used by osctld" if active != 'yes'

  if ds == '-'
    @dataset = pool
    @mountpoint = mnt
  else
    @dataset = ds
    @mountpoint = zfs(:get, '-Hp -ovalue mountpoint', ds).output.strip
  end

  @version_file = File.join(@mountpoint, FILE)
end

#log_typeObject



78
79
80
# File 'lib/osup/pool_migrations.rb', line 78

def log_type
  "pool=#{pool}"
end

#saveObject (protected)



131
132
133
134
135
# File 'lib/osup/pool_migrations.rb', line 131

def save
  regenerate_file(version_file, 0o600) do |new|
    applied.each { |id| new.puts(id) }
  end
end

#set_all_upObject

Mark all migrations as applied



72
73
74
75
76
# File 'lib/osup/pool_migrations.rb', line 72

def set_all_up
  @applied = MigrationList.get.map(&:id)
  save
  build_list
end

#set_down(m) ⇒ Object

Parameters:



65
66
67
68
69
# File 'lib/osup/pool_migrations.rb', line 65

def set_down(m)
  applied.delete(m.id)
  save
  build_list
end

#set_up(m) ⇒ Object

Parameters:



58
59
60
61
62
# File 'lib/osup/pool_migrations.rb', line 58

def set_up(m)
  applied << m.id
  save
  build_list
end

#upgradable?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/osup/pool_migrations.rb', line 53

def upgradable?
  all.detect { |_id, m| m.nil? } ? false : true
end

#uptodate?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/osup/pool_migrations.rb', line 49

def uptodate?
  all.detect { |_id, m| m && !applied?(m) } ? false : true
end