<commit_before>module MCollective
  module Agent
    class Varnish<RPC::Agent
      action "purge" do
        url_to_purge = request[:url] 
        parsed_url = URI.parse(url_to_purge)
        fail "Invalid Url" unless parsed_url.scheme == "http"
        hostname = parsed_url.host
        uri = parsed_url.request_uri
        Log.warn("URL parsed: #{hostname} ,  #{uri}")
        v_version = discover_varnish_version
        Log.warn("Varnish version discovered: #{v_version}")
        purge_cmd = create_purge_command(v_version, uri, hostname)
        Log.warn("Purge cmd: #{purge_cmd}")
        purge_output = run(purge_cmd)
        Log.warn("purge command run.... #{purge_output}")
        if request.include?(:debug) 
          reply[:purge_cmd] = purge_cmd
        end
        reply[:urlpurged] = url_to_purge
      end
      def run(cmd) 
        Log.warn("Running command: #{cmd}")
        command_output = `#{cmd}`
        command_status = $?
        unless command_status == 0
          fail "Error while running command: #{cmd}"
        end
        File.open("/tmp/teste","w") do |file| 
          file.puts("#{cmd}")
        end
        Log.warn("Command output: #{command_output}")
        return command_output
      end
      def discover_varnish_version
        varnishd_output = run("/usr/sbin/varnishd -V 2>&1")
        Log.warn("Varnishd -V output: #{varnishd_output}")
        if varnishd_output =~ /varnish-(\d)/
          Log.warn("Varnishd detected version #{$1}")
          version=$1.to_i
          return version
        else 
          return "NOTFOUND"
        end
      end
      
      def create_purge_command(varnish_version, uri, hostname)
        Log.warn("Varnish Version: #{varnish_version}, uri: #{uri}, hostname: #{hostname}")
        if varnish_version == 2
          purge_method = "purge.url"<commit_msg>cleanup.<commit_after>module MCollective
  module Agent
    class Varnish<RPC::Agent
      action "purge" do
        url_to_purge = request[:url] 
        parsed_url = URI.parse(url_to_purge)
        fail "Invalid Url" unless parsed_url.scheme == "http"
        hostname = parsed_url.host
        uri = parsed_url.request_uri
        Log.warn("URL parsed: #{hostname} ,  #{uri}")
        v_version = discover_varnish_version
        Log.warn("Varnish version discovered: #{v_version}")
        purge_cmd = create_purge_command(v_version, uri, hostname)
        Log.warn("Purge cmd: #{purge_cmd}")
        purge_output = run(purge_cmd)
        Log.warn("purge command run.... #{purge_output}")
        if request.include?(:debug) 
          reply[:purge_cmd] = purge_cmd
        end
        reply[:urlpurged] = url_to_purge
      end
      def run(cmd) 
        Log.warn("Running command: #{cmd}")
        command_output = `#{cmd}`
        command_status = $?
        unless command_status == 0
          fail "Error while running command: #{cmd}"
        end
        Log.warn("Command output: #{command_output}")
        return command_output
      end
      def discover_varnish_version
        varnishd_output = run("/usr/sbin/varnishd -V 2>&1")
        Log.warn("Varnishd -V output: #{varnishd_output}")
        if varnishd_output =~ /varnish-(\d)/
          Log.warn("Varnishd detected version #{$1}")
          version=$1.to_i
          return version
        else 
          return "NOTFOUND"
        end
      end
      
      def create_purge_command(varnish_version, uri, hostname)
        Log.warn("Varnish Version: #{varnish_version}, uri: #{uri}, hostname: #{hostname}")
        if varnish_version == 2
          purge_method = "purge.url"
{'fraction_non_alphanumeric': 0.11763070077864293, 'fraction_numerical': 0.0033370411568409346, 'mean_word_length': 2.287934186471664, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '180786', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1135, 'n_words': 242}