Class: OsUp::PoolMigrations
- Inherits:
-
Object
- Object
- OsUp::PoolMigrations
- 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
- #all ⇒ Array<Array> readonly
- #applied ⇒ Array<Integer> readonly
-
#dataset ⇒ String
readonly
Osctl root dataset on pool.
-
#mountpoint ⇒ String
readonly
Mountpoint of osctl root dataset.
- #pool ⇒ String readonly
-
#version_file ⇒ String
readonly
Path to file with a list of applied migrations.
Instance Method Summary collapse
- #applied?(m) ⇒ Boolean
-
#build_list ⇒ Object
protected
Build a list of all migrations, applied and unapplied and even those that aren’t recognized by ‘osup`.
- #each {|m| ... } ⇒ Object
-
#initialize(pool) ⇒ PoolMigrations
constructor
A new instance of PoolMigrations.
- #load_applied ⇒ Object protected
- #load_pool ⇒ Object protected
- #log_type ⇒ Object
- #save ⇒ Object protected
-
#set_all_up ⇒ Object
Mark all migrations as applied.
- #set_down(m) ⇒ Object
- #set_up(m) ⇒ Object
- #upgradable? ⇒ Boolean
- #uptodate? ⇒ Boolean
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
#all ⇒ Array<Array> (readonly)
27 28 29 |
# File 'lib/osup/pool_migrations.rb', line 27 def all @all end |
#applied ⇒ Array<Integer> (readonly)
24 25 26 |
# File 'lib/osup/pool_migrations.rb', line 24 def applied @applied end |
#dataset ⇒ String (readonly)
Returns osctl root dataset on pool.
15 16 17 |
# File 'lib/osup/pool_migrations.rb', line 15 def dataset @dataset end |
#mountpoint ⇒ String (readonly)
Returns mountpoint of osctl root dataset.
18 19 20 |
# File 'lib/osup/pool_migrations.rb', line 18 def mountpoint @mountpoint end |
#pool ⇒ String (readonly)
12 13 14 |
# File 'lib/osup/pool_migrations.rb', line 12 def pool @pool end |
#version_file ⇒ String (readonly)
Returns 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
40 41 42 |
# File 'lib/osup/pool_migrations.rb', line 40 def applied?(m) @applied.include?(m.id) end |
#build_list ⇒ Object (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
45 46 47 |
# File 'lib/osup/pool_migrations.rb', line 45 def each(&) applied.lazy.map { |id| MigrationList[id] }.each(&) end |
#load_applied ⇒ Object (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_pool ⇒ Object (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_type ⇒ Object
78 79 80 |
# File 'lib/osup/pool_migrations.rb', line 78 def log_type "pool=#{pool}" end |
#save ⇒ Object (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_up ⇒ Object
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
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
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
53 54 55 |
# File 'lib/osup/pool_migrations.rb', line 53 def upgradable? all.detect { |_id, m| m.nil? } ? false : true end |
#uptodate? ⇒ Boolean
49 50 51 |
# File 'lib/osup/pool_migrations.rb', line 49 def uptodate? all.detect { |_id, m| m && !applied?(m) } ? false : true end |