marionette 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ pkg/*
3
3
  .bundle
4
4
  *.output
5
5
  *.pid
6
+ *.rb
@@ -38,17 +38,10 @@ module HeadStartApp
38
38
  end
39
39
 
40
40
  # Connect master
41
- # require 'ffi-rzmq'
42
41
  def master
43
- require 'ffi-rzmq'
44
-
45
- # Set ZMQ context
46
- context = ZMQ::Context.new(1)
47
-
48
- # Set socket to talk to puppet
49
- socket = context.socket(ZMQ::REQ)
50
- socket.connect(@uri.to_s)
51
- @connection = HeadStartApp::Marionette::Master.new(socket)
42
+
43
+ # socket is created within Master so Master can attempt reconnect if Puppet reboots
44
+ @connection = HeadStartApp::Marionette::Master.new(@uri.to_s)
52
45
 
53
46
  end
54
47
 
@@ -2,61 +2,122 @@ module HeadStartApp
2
2
  module Marionette
3
3
 
4
4
  require 'uri'
5
-
5
+ require 'ffi-rzmq'
6
6
 
7
7
  # Master class is the ZMQ socket connection on the puppet master
8
8
  class Master
9
- attr_accessor :socket, :threads
10
-
11
- def initialize(socket)
12
-
13
- # Set socket
14
- @socket = socket
9
+ attr_accessor :socket, :reply
15
10
 
11
+ def initialize(uri)
12
+
13
+ # Set URI and connect to socket
14
+ @uri = uri
15
+
16
16
  end
17
-
17
+
18
18
  # Sends a msg to puppet node
19
- def send(msg)
20
-
21
- # Send and Receive
22
- @socket.send_string Marshal.dump(msg)
19
+ # and stands by for a reply
20
+ # and processes reply
21
+ def talk(msg)
22
+
23
+ # Initial connection to socket
24
+ @socket = socket_connect
25
+
26
+ # Repeat until talk succeeds.
27
+ while true do
28
+
29
+ # Send the message
30
+ @socket.send_string Marshal.dump(msg)
31
+
32
+ # Poll server until it is receive-able and re-poll if necessary
33
+ poller = Poller.new @socket, {:max => 10, :interval => 500}
34
+ break if poller.pull?
35
+ @socket = socket_reconnect(@socket)
36
+
37
+ end
38
+
39
+ @reply = process_message(@socket.recv_string)
23
40
 
24
41
  end
25
42
 
26
- # Stands by for the next msg from puppet
27
- # Processes and returns response
28
- def receive
29
-
30
- # Poll server until it is receive-able
31
- poller = ZMQ::Poller.new
32
- poller.register_readable @socket
43
+ private
44
+
45
+ # Re-connect master to puppet socket
46
+ def socket_reconnect(socket)
47
+
48
+ socket.close
49
+ socket_connect
50
+
51
+ end
33
52
 
34
- while true do
53
+ # Connect master to puppet socket
54
+ def socket_connect(uri = @uri)
55
+
56
+ # Set ZMQ context
57
+ context = ZMQ::Context.new(1)
58
+
59
+ # Set socket to talk to puppet
60
+ socket = context.socket(ZMQ::REQ)
61
+ socket.connect(uri.to_s)
62
+
63
+ socket
35
64
 
36
- poll_reply = poller.poll 500
37
- key = poll_reply.keys.first # fetch the first and only hash key
65
+ end
66
+
67
+ # Unserialize if necessary
68
+ def process_message(response)
38
69
 
39
- break if poll_reply[key][:revents] == 1 # fetch revents
70
+ begin
71
+
72
+ msg = Marshal.load(response)
73
+
74
+ rescue
75
+
76
+ # Catch non-marshal-able response
77
+ msg = response
78
+
79
+ end
40
80
 
41
81
  end
42
82
 
43
- # Receive message
44
- begin
83
+ class Poller < ZMQ::Poller
84
+ attr_accessor :pull, :reconnect
45
85
 
46
- response = socket.recv_string
47
- @response = Marshal.load(response)
86
+ def initialize(socket, options)
87
+
88
+ super()
89
+ register_readable socket
90
+ @max = options[:max] if options[:max]
91
+ @interval = options[:interval] ? options[:interval] : 10
92
+ @attempt = 0
93
+ start
94
+
95
+ end
48
96
 
49
- rescue
97
+ def pull?
98
+ @pull
99
+ end
50
100
 
51
- # Catch non-marshal-able response
52
- @response = response
101
+ def run!
53
102
 
103
+ @attempt+=1
104
+ poll_reply = poll @interval
105
+ key = poll_reply.keys.first # fetch the first and only hash key
106
+ @pull = poll_reply[key][:revents] == 1 # true if revents==1
107
+ @attempt <= @max # true if allowed attempt
108
+
109
+ end
110
+
111
+ def start
112
+
113
+ while run! do
114
+ break if @pull or @reconnect
115
+ end
116
+
117
+ end
118
+
54
119
  end
55
120
 
56
- @response
57
-
58
- end
59
-
60
121
  end
61
122
 
62
123
  end
@@ -1,5 +1,5 @@
1
1
  module HeadStartApp
2
2
  module Marionette
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marionette
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dan Lee
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-11 00:00:00 -08:00
18
+ date: 2011-01-12 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency