|
| 1 | +require "ceph-ruby/rados/lib" |
| 2 | +require "ceph-ruby/rados/pool" |
| 3 | + |
| 4 | +module CephRuby |
| 5 | + class Rados |
| 6 | + attr_accessor :state, :cluster |
| 7 | + |
| 8 | + def self.version |
| 9 | + major = FFI::MemoryPointer.new(:int) |
| 10 | + minor= FFI::MemoryPointer.new(:int) |
| 11 | + extra = FFI::MemoryPointer.new(:int) |
| 12 | + Lib.rados_version(major, minor, extra) |
| 13 | + { |
| 14 | + :major => major.get_int(0), |
| 15 | + :minor => minor.get_int(0), |
| 16 | + :extra => extra.get_int(0), |
| 17 | + } |
| 18 | + end |
| 19 | + |
| 20 | + def initialize(configuration_path = "/etc/ceph/ceph.conf") |
| 21 | + cluster_p = FFI::MemoryPointer.new(:pointer) |
| 22 | + ret = Lib.rados_create(cluster_p, nil) |
| 23 | + raise "could not initialize rados cluster: #{ret}" if ret < 0 |
| 24 | + self.cluster = cluster_p.get_pointer(0) |
| 25 | + self.state = :initialized |
| 26 | + read_configuration_file(configuration_path) |
| 27 | + |
| 28 | + if block_given? |
| 29 | + connect |
| 30 | + yield self |
| 31 | + shutdown |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + def read_configuration_file(path = "/etc/ceph/ceph.conf") |
| 36 | + require_state(:initialized, :connected) |
| 37 | + raise ArgumentError, "path must be a string" unless path.is_a?(String) |
| 38 | + ret = Lib.rados_conf_read_file(cluster, path) |
| 39 | + raise "error reading configuration file '#{path}': #{ret}" if ret < 0 |
| 40 | + end |
| 41 | + |
| 42 | + def shutdown |
| 43 | + return unless cluster |
| 44 | + Lib.rados_shutdown(cluster) |
| 45 | + self.cluster = nil |
| 46 | + self.state = :shutdown |
| 47 | + end |
| 48 | + |
| 49 | + def connect |
| 50 | + require_state(:initialized) |
| 51 | + ret = Lib.rados_connect(cluster) |
| 52 | + raise "could not connect to cluster: #{ret}" if ret < 0 |
| 53 | + self.state = :connected |
| 54 | + end |
| 55 | + |
| 56 | + def exists?(name) |
| 57 | + require_state(:connected) |
| 58 | + raise ArgumentError, "name must be a string" unless name.is_a?(String) |
| 59 | + ret = Lib.rados_pool_lookup(cluster, name) |
| 60 | + return true if ret >= 0 |
| 61 | + return false if ret == -Errno::ENOENT::Errno |
| 62 | + raise "error looking up pool '#{name}': #{ret}" |
| 63 | + end |
| 64 | + |
| 65 | + def pool(name, &block) |
| 66 | + require_state(:connected) |
| 67 | + raise ArgumentError, "name must be a string" unless name.is_a?(String) |
| 68 | + ioctx_p = FFI::MemoryPointer.new(:pointer) |
| 69 | + ret = Lib.rados_ioctx_create(cluster, name, ioctx_p) |
| 70 | + raise "error creating io context for '#{name}': #{ret}" if ret < 0 |
| 71 | + Pool.new(self, name, ioctx_p.get_pointer(0), &block) |
| 72 | + end |
| 73 | + |
| 74 | + def rbd(name, &block) |
| 75 | + pool(name) do |pool| |
| 76 | + Rbd.new(pool, &block) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + private |
| 81 | + |
| 82 | + def require_state(*states) |
| 83 | + return if states.include?(state) |
| 84 | + raise "current state must be one of: #{states*", "}" |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments