Sha256: ec228ad9030a0269c272cd471512b1f989d0eb920a7053cac82bc0796df3d09c

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'base64'

module MCollective
  module Agent
    class Dev_manager < RPC::Agent

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

      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
            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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dtk-node-agent-0.5.10 mcollective_additions/plugins/v2.2/agent/dev_manager.rb