marionette 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/marionette/connect.rb +3 -10
- data/lib/marionette/master.rb +95 -34
- data/lib/marionette/version.rb +1 -1
- metadata +4 -4
data/.gitignore
CHANGED
data/lib/marionette/connect.rb
CHANGED
@@ -38,17 +38,10 @@ module HeadStartApp
|
|
38
38
|
end
|
39
39
|
|
40
40
|
# Connect master
|
41
|
-
# require 'ffi-rzmq'
|
42
41
|
def master
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
|
data/lib/marionette/master.rb
CHANGED
@@ -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, :
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
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
|
-
|
37
|
-
|
65
|
+
end
|
66
|
+
|
67
|
+
# Unserialize if necessary
|
68
|
+
def process_message(response)
|
38
69
|
|
39
|
-
|
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
|
-
|
44
|
-
|
83
|
+
class Poller < ZMQ::Poller
|
84
|
+
attr_accessor :pull, :reconnect
|
45
85
|
|
46
|
-
|
47
|
-
|
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
|
-
|
97
|
+
def pull?
|
98
|
+
@pull
|
99
|
+
end
|
50
100
|
|
51
|
-
|
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
|
data/lib/marionette/version.rb
CHANGED
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:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
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-
|
18
|
+
date: 2011-01-12 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|