Sha256: 0d510fb8aa0baabcb47656d613a7452215755fd12599b89155ae272406001e07

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Libvirt
  class Domain
    def initialize(dom_ptr)
      @dom_ptr = dom_ptr

      free = ->(obj_id) do
        STDOUT.puts("finalized Libvirt::Domain obj_id=0x#{obj_id.to_s(16)}, @dom_ptr=#{@dom_ptr},")
      end
      ObjectSpace.define_finalizer(self, free)
    end

    def get_state
      state = ::FFI::MemoryPointer.new(:int)
      reason = ::FFI::MemoryPointer.new(:int)
      result = FFI::Domain.virDomainGetState(@dom_ptr, state, reason, 0)
      raise Error, "Couldn't get domain state" if result < 0
      [state.read_int, reason.read_int]
    end

    def to_ptr
      @dom_ptr
    end

    def uuid
      buff = ::FFI::MemoryPointer.new(:char, FFI::Domain::UUID_STRING_BUFLEN)
      result = FFI::Domain.virDomainGetUUIDString(@dom_ptr, buff)
      raise Error, "Couldn't get domain uuid" if result < 0
      buff.read_string
    end

    def name
      FFI::Domain.virDomainGetName(@dom_ptr)
    end

    def max_vcpus
      FFI::Domain.virDomainGetMaxVcpus(@dom_ptr)
    end

    # def vcpus
    #   # https://github.com/libvirt/ruby-libvirt/blob/9f71ff5add1f57ffef7cf513b72638d92d9fd84f/ext/libvirt/domain.c#L787
    #   # dominfo = virDomainGetInfo
    #   # dominfo.nrVirtCpu
    #   # maxcpus = ruby_libvirt_get_maxcpus(ruby_libvirt_connect_get(d));
    #   # vcpu_infos_ptr
    #   FFI::Domain.virDomainGetVcpus(@dom_ptr, vcpu_infos_ptr, maxinfo, cpumaps, maplen)
    # end
    def vcpus
      OpenStruct.new(count: max_vcpus)
    end

    def max_memory
      FFI::Domain.virDomainGetMaxMemory(@dom_ptr)
    end

    def xml_desc(flags = 0)
      FFI::Domain.virDomainGetXMLDesc(@dom_ptr, flags)
    end

    def screenshot(stream, display = 0)
      mime_type, pointer = FFI::Domain.virDomainScreenshot(@dom_ptr, stream.to_ptr, display, 0)
      raise Error, "Couldn't attach domain screenshot" if pointer.null?
      # free pointer required
      mime_type
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
libvirt_ffi-0.3.0 lib/libvirt/domain.rb