Class: OsCtld::Commands::Pool::Import

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/commands/pool/import.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, cmd, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

Instance Method Details

#do_import(name, dataset) ⇒ Object (protected)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/osctld/commands/pool/import.rb', line 79

def do_import(name, dataset)
  pool = Pool.new(name, dataset == '-' ? nil : dataset)

  manipulate(pool) do
    DB::Pools.sync do
      if DB::Pools.contains?(name)
        raise PoolExists, "pool #{name} is already imported"
      end

      DB::Pools.add(pool)
    end

    begin
      upgrade(name)
    rescue PoolUpgradeError
      DB::Pools.remove(pool)
      raise
    end

    pool.init
    pool.setup
    pool.autostart if opts[:autostart]
  end
end

#executeObject



11
12
13
14
15
16
17
# File 'lib/osctld/commands/pool/import.rb', line 11

def execute
  if opts[:all]
    import_all
  else
    import_one(opts[:name])
  end
end

#import_allObject (protected)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/osctld/commands/pool/import.rb', line 20

def import_all
  props = [
    'name',
    'mounted',
    Pool::PROPERTY_ACTIVE,
    Pool::PROPERTY_DATASET,
  ]

  zfs(
    :list,
    "-H -d0 -o #{props.join(',')}",
    ''
  ).output.split("\n").each do |line|
    name, mounted, active, dataset = line.split
    next if active != 'yes' \
            || mounted != 'yes' \
            || !pool_ready?(name) \
            || DB::Pools.contains?(name)

    begin
      do_import(name, dataset)
    rescue PoolExists
      next
    rescue PoolUpgradeError => e
      log(:warn, "Pool upgrade failed: #{e.message}")
      next
    end
  end

  ok
end

#import_one(name) ⇒ Object (protected)



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/osctld/commands/pool/import.rb', line 52

def import_one(name)
  error!('pool already imported') if DB::Pools.contains?(name)

  mounted, dataset = zfs(
    :get,
    "-H -o value mounted,#{Pool::PROPERTY_DATASET}",
    name
  ).output.strip.split

  error!('the pool is not mounted') if mounted != 'yes'

  begin
    do_import(name, dataset)
    ok

  rescue PoolExists, PoolUpgradeError => e
    error(e.message)
  end
end

#pool_ready?(name) ⇒ Boolean (protected)

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'lib/osctld/commands/pool/import.rb', line 72

def pool_ready?(name)
  sv = "pool-#{name}"

  return true unless Dir.exist?(File.join('/service', sv))
  File.exist?(File.join('/run/service', sv, 'done'))
end

#upgrade(name) ⇒ Object (protected)



104
105
106
107
108
109
110
111
112
# File 'lib/osctld/commands/pool/import.rb', line 104

def upgrade(name)
  OsUp.upgrade(name)

rescue OsUp::PoolUpToDate
  # pass

rescue => e
  raise PoolUpgradeError.new(name, e)
end