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!, #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)



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/osctld/commands/pool/import.rb', line 83

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

  log(:info, pool, 'Importing pool')
  Hook.run(pool, :pre_import)

  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

    if Daemon.get.shutdown?
      log(:info, 'Shutdown in progress, disabling pool')
      pool.disable
    elsif opts[:autostart]
      begin
        pool.autostart
      rescue HookFailed => e
        log(:warn, "pre-autostart hook failed: #{e.message}")
      end
    end
  end

  Hook.run(pool, :post_import)

  log(:info, pool, "Pool imported in #{(Time.now - t1).round(2)} seconds")
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)



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
51
52
53
54
# File 'lib/osctld/commands/pool/import.rb', line 21

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 HookFailed => e
      log(:warn, "Pool pre-import hook failed: #{e.message}")
      next
    rescue PoolUpgradeError => e
      log(:warn, "Pool upgrade failed: #{e.message}")
      next
    end
  end

  ok
end

#import_one(name) ⇒ Object (protected)



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

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, HookFailed => e
    error(e.message)
  end
end

#pool_ready?(name) ⇒ Boolean (protected)

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/osctld/commands/pool/import.rb', line 75

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)



126
127
128
129
130
131
132
# File 'lib/osctld/commands/pool/import.rb', line 126

def upgrade(name)
  OsUp.upgrade(name)
rescue OsUp::PoolUpToDate
  # pass
rescue StandardError => e
  raise PoolUpgradeError.new(name, e)
end