<commit_before>require 'base64'

module MCollective
  module Agent
    class Dev_manager < RPC::Agent

      AGENT_MCOLLECTIVE_LOCATION = "#{::MCollective::Config.instance.libdir}/mcollective/agent/"

      action "inject_agent" do
        begin

          ret ||= Response.new() 

          request[:agent_files].each do |k,v|
            if v == :deleted
              File.delete("#{AGENT_MCOLLECTIVE_LOCATION}#{k}")
              next
            end
            content = Base64.decode64(v)
            File.open("#{AGENT_MCOLLECTIVE_LOCATION}#{k}",'w') do |file|
              file << content
            end
          end
          ret.set_status_succeeded!()

          t1 = Thread.new do 
            sleep(2)
            system("sudo /etc/init.d/mcollective restart")
          end

          return ret

        rescue Exception => e
          Log.error e
          ret.set_status_failed!()
          error_info = { :error => { :message => "Error syncing agents: #{e}" } }
          ret.merge!(error_info)
        end
        return ret
      end
    end
    #TODO: this should be common accross Agents
    class Response < Hash
      def initialize(hash={})
        super()
        self.merge!(hash)
        self[:status] = :unknown unless hash.has_key?(:status)
      end

      def to_hash()
        Hash.new.merge(self)
      end

      def failed?()
        self[:status] == :failed
      end

      def set_status_failed!()
        self[:status] = :failed
      end
      def set_status_succeeded!()
        self[:status] = :succeeded
      end
      def set_dynamic_attributes!(dynamic_attributes)
        self[:dynamic_attributes] = dynamic_attributes
      end
    end
  end
end<commit_msg>experiment with dev manager that doe snot need to restart<commit_after>require 'base64'

module MCollective
  module Agent
    class Dev_manager < RPC::Agent

      AGENT_MCOLLECTIVE_LOCATION = "#{::MCollective::Config.instance.libdir}/mcollective/agent/"

      action "inject_agent" do
        begin
          #TODO: look at locking out other agents calls during this segment
          ret = Response.new() 

          #first update the agent files and tell mcolelctive about it
          request[:agent_files].each do |agent_path,v|
            agent_name = agent_path.gsub(/\.rb$/,"") 
            full_path = "#{AGENT_MCOLLECTIVE_LOCATION}#{agent_path}"
            if v.kind_of?(Symbol) and v == :deleted
              MCHelper.uninstall(agent_name)
              File.delete(full_path) if File.exists?(full_path)
            else
              content = Base64.decode64(v)
              File.open(full_path,'w'){|file|file << content}
              MCHelper.install_or_reinstall_agent(agent_name)
            end
          end
          ret.set_status_succeeded!()
        rescue Exception => e
          Log.error e
          ret.set_status_failed!()
          error_info = { :error => { :message => "Error syncing agents: #{e}" } }
          ret.merge!(error_info)
        end
        ret
      end

      module MCHelper
        def self.uninstall_agent(agent_name)
          ::MCollective::PluginManager.delete("#{agent_name}_agent")
        end
        def self.install_or_reinstall_agent(agent_name)
          #Modified fragment fRom https://github.com/ripienaar/mcollective-agent-debugger
          classname = "MCollective::Agent::#{agent_name.capitalize}"
          ::MCollective::PluginManager.delete("#{agent_name}_agent")
          ::MCollective::PluginManager.loadclass(classname)
          ::MCollective::PluginManager << {:type => "#{agent_name}_agent", :class => classname}
        end
      end
    end
    #TODO: this should be common accross Agents
    class Response < Hash
      def initialize(hash={})
        super()
        self.merge!(hash)
        self[:status] = :unknown unless hash.has_key?(:status)
      end

      def to_hash()
        Hash.new.merge(self)
      end

      def set_status_failed!()
        self[:status] = :failed
      end
      def set_status_succeeded!()
        self[:status] = :succeeded
      end
    end
  end
end
 
{'fraction_non_alphanumeric': 0.10543052837573386, 'fraction_numerical': 0.003424657534246575, 'mean_word_length': 2.0492170022371363, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 1, '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': '976487', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1197, 'n_words': 259}