module VarnishCookbook
  # Helper methods to be used in multiple Varnish cookbook libraries.
  module Helpers

    def percent_of_total_mem(percent)
      "#{(node['memory']['total'][0..-3].to_i * (percent / 100)).to_i}K"
    end

    #def systemd_daemon_reload
    #  execute 'systemctl-daemon-reload' do
    #    command '/bin/systemctl --system daemon-reload'
    #    action :nothing
    #  end
    #end
  end
end

module VarnishCookbook
  # Helper methods to be used in multiple Varnish cookbook libraries.
  module Helpers
    def self.installed_major_version # Has issues being stubbed without 'self.'
      cmd_str = 'varnishd -V 2>&1'
      cmd = Mixlib::ShellOut.new(cmd_str)
      cmd.environment['HOME'] = ENV.fetch('HOME', '/root')

      begin
        cmd.run_command
        cmd_stdout = cmd.stdout.to_s

        raise "Output of #{cmd_str} was nil; can't determine varnish version" unless cmd_stdout
        Chef::Log.debug "#{cmd_str} ran and detected varnish version: #{cmd_stdout}"

        matches = cmd_stdout.match(/varnish-([0-9]\.[0-9])/)
        version_found = matches && matches[0] && matches[1]
        raise "Cannot parse varnish version from #{cmd_stdout}" unless version_found

        return matches[1].to_f
      rescue => ex
        Chef::Log.warn 'Unable to run varnishd to get version.'
        raise ex
      end

    end

    def self.percent_of_total_mem(total_mem, percent)
      "#{(total_mem[0..-3].to_i * (percent / 100)).to_i}K"
    end

    def systemd_daemon_reload
      execute 'systemctl-daemon-reload' do
        command '/bin/systemctl --system daemon-reload'
        action :nothing
      end
    end
  end unless defined?(VarnishCookbook::Helpers)
end

Add back shell out version detection
Add back shell out version detection
