Sha256: a53abc938eadd7f8f09655294572a4f6b48ac2cd393b535419f78adc1c329f32

Contents?: true

Size: 1.98 KB

Versions: 6

Compression:

Stored size: 1.98 KB

Contents

require 'base64'

module MCollective
  module Agent
    class Dev_manager < RPC::Agent

      AGENT_MCOLLECTIVE_LOCATION = "#{::MCollective::Config.instance.libdir.join}/mcollective/agent/"
 
      action "inject_agent" do
        begin

          ret ||= Response.new()

          # make sure default `service` command paths are set
          ENV['PATH'] += ":/usr/sbin:/sbin"

          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)
            Log.instance.info "Initiating mcollective restart..."
            system("#{service_command} restart")
          end

          def self.service_command
            cmd = `which service`.chomp
            cmd.empty? ? "/etc/init.d/mcollective" : "#{cmd} mcollective"
          end

          return ret

        rescue Exception => e
          Log.instance.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

6 entries across 6 versions & 1 rubygems

Version Path
dtk-node-agent-0.7.4.1 mcollective_additions/plugins/v2.2/agent/dev_manager.rb
dtk-node-agent-0.7.4 mcollective_additions/plugins/v2.2/agent/dev_manager.rb
dtk-node-agent-0.7.3 mcollective_additions/plugins/v2.2/agent/dev_manager.rb
dtk-node-agent-0.7.2 mcollective_additions/plugins/v2.2/agent/dev_manager.rb
dtk-node-agent-0.7.1 mcollective_additions/plugins/v2.2/agent/dev_manager.rb
dtk-node-agent-0.7.0 mcollective_additions/plugins/v2.2/agent/dev_manager.rb