Class: Emissary::Agent::Emissary

Inherits:
Emissary::Agent show all
Defined in:
lib/emissary/agent/emissary.rb

Constant Summary collapse

INIT_DATA =
[
  ::Emissary.identity.name,
  ::Emissary.identity.public_ip,
  ::Emissary.identity.local_ip,
  ::Emissary.identity.instance_id,
  ::Emissary.identity.server_id,
  ::Emissary.identity.cluster_id,
  ::Emissary.identity.,
  ::Emissary.identity.queue_name,
  ::Emissary.version
]

Instance Attribute Summary

Attributes inherited from Emissary::Agent

#args, #config, #message, #method, #name, #operator

Instance Method Summary collapse

Methods inherited from Emissary::Agent

#activate, #initialize, #post_init, #send

Constructor Details

This class inherits a constructor from Emissary::Agent

Instance Method Details

#initdataObject



123
124
125
126
127
# File 'lib/emissary/agent/emissary.rb', line 123

def initdata
  response = message.response
  response.args = INIT_DATA
  response
end

#reconfig(new_config) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/emissary/agent/emissary.rb', line 37

def reconfig new_config
  throw :skip_implicit_response if new_config.strip.empty?
  
  if (test(?w, config[:agents][:emissary][:config_path]))
    begin
      ((tmp = Tempfile.new('new_config')) << new_config).flush
      Emissary::Daemon.get_config(tmp.path)
    rescue Exception => e
      resonse = message.response
      response.status_type = :error
      response.status_note = e.message
      return response
    else
      FileUtils.mv tmp.path, config[:agents][:emissary][:config_path]
      # signal a USR1 to our parent, which will cause it to kill the
      # children and restart them after rereading it's configuration
      Process.kill('HUP', config[:parent_pid])
    ensure
      tmp.close
    end
  end      

end

#selfupdate(version = :latest) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/emissary/agent/emissary.rb', line 61

def selfupdate version = :latest
  begin
    emissary_gem = ::Emissary::GemHelper.new('emissary')
    request_version, source_uri = emissary_gem.versions(version).flatten
    current_version, _ = emissary_gem.versions(:current).flatten

    unless not emissary_gem.installable? request_version
      ::Emissary.logger.debug "Emissary SelfUpdate to version '#{request_version.to_s}' requested."
      new_version = emissary_gem.update(request_version, source_uri)
      ::Emissary.logger.debug "Emissary gem updated from '#{::Emissary.version}' to '#{new_version}'"
    else
      ::Emissary.logger.debug " -- SELFUPDATE -- [         VERSION: #{version}]"
      ::Emissary.logger.debug " -- SELFUPDATE -- [ CURRENT_VERSION: #{current_version}]"
      ::Emissary.logger.debug " -- SELFUPDATE -- [ REQUEST_VERSION: #{request_version}]"
      
      notice = 'Emissary selfupdate skipped - ' + case true
        when request_version.nil?
          if version == :latest
            "already at latest version."
          else
            "non-existent version '#{version}'"
          end
        when current_version == request_version
          "already at specified version #{version}."
        when current_version > request_version
          "downgrade to version #{request_version} not allowed."
        else
          "unable to update from #{::Emissary.version} to requested version #{request_version}."
      end
      ::Emissary.logger.warn notice
      response = message.response
      response.status_note = notice
      return response
    end
  rescue ::Gem::InstallError, ::Gem::GemNotFoundException => e
    ::Emissary.logger.error "Emissary selfupdate failed with reason: #{e.message}"
    return message.error(e)
  else
    ::Emissary.logger.debug "SelfUpdate: About to detach and run commands"
    with_detached_process('emissary-selfupdate') do
      %x{
        emissary stop;
        sleep 2; 
        ps uxa | grep -v grep | grep '(emissary|emop_)' | awk '{ print $2 }' | xargs kill -9;
        sleep 1;
        source /etc/cloudrc;
        emissary start -d;
      }
    end
    ::Emissary.logger.debug "SelfUpdate: Child detached"
    throw :skip_implicit_response
  end
end

#shutdownObject



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/emissary/agent/emissary.rb', line 129

def shutdown
  message.recipient = config[:shutdown]
  message.args = [
    ::Emissary.identity.server_id,
    ::Emissary.identity.cluster_id,
    ::Emissary.identity.,
    ::Emissary.identity.instance_id
  ]
  ::Emissary.logger.notice "Sending Shutdown message with args: #{message.args.inspect}"
  message
end

#startupObject Also known as: reinit



115
116
117
118
119
120
# File 'lib/emissary/agent/emissary.rb', line 115

def startup
  message = initdata
  message.recipient = config[:startup]
  ::Emissary.logger.notice "Sending Startup message with args: #{message.args.inspect}"
  message
end

#valid_methodsObject



33
34
35
# File 'lib/emissary/agent/emissary.rb', line 33

def valid_methods
  [ :reconfig, :selfupdate, :startup, :shutdown, :initdata, :reinit ]
end