Sha256: 2cb72c864d637bbd8c62ac08d4e8b416453cfb1d90bd0a501949b39581e7b163

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require "log4r"

module VagrantPlugins
  module HYPERKIT
    module Action
      # This action reads the state of the machine and puts it in the
      # `:machine_state_id` key in the environment.
      class ReadState
        def initialize(app, env)
          @app = app
          @env = env
          @logger = Log4r::Logger.new("vagrant_hyperkit::action::read_state")
        end

        def call(env)

          env[:machine_state_id] = read_state(env)
          @app.call(env)
        end

        private

        def read_state(env)
          xhyve_status = read_xhyve_status_file(env)
          return :not_created if env[:machine].id.nil?
          env[:xhyve_status] = xhyve_status
          if process_alive(xhyve_status[:pid])
            return :running
          else
            return :stopped
          end
        end

        def read_xhyve_status_file(env)
          xhyve_status_file_path = File.join(env[:machine].data_dir, "xhyve.json")
          return {} unless File.exist?(xhyve_status_file_path)
          machine_json = File.read(xhyve_status_file_path)
          JSON.parse(machine_json, :symbolize_names => true)
        end

        def process_alive(pid)
          return false if pid.nil?
          begin
            Process.getpgid(pid.to_i)
            true
          rescue Errno::ESRCH
            false
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-hyperkit-0.4.3 lib/vagrant-hyperkit/action/read_state.rb