processwanker 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.
@@ -0,0 +1,152 @@
1
+ ############################################################################
2
+ #
3
+ # config_client_host.rb
4
+ #
5
+ # host("name") {
6
+ # auth {
7
+ # ...
8
+ # }
9
+ # hostname "hostname"
10
+ # port port
11
+ # tag "tag"
12
+ # }
13
+ #
14
+ #
15
+ ############################################################################
16
+
17
+ require 'config_auth'
18
+ require 'net_util'
19
+ require 'config'
20
+
21
+ module ProcessWanker
22
+
23
+ ############################################################################
24
+ #
25
+ #
26
+ #
27
+ ############################################################################
28
+
29
+ class ConfigClientHost < ConfigNode
30
+
31
+ attr_accessor :name
32
+ attr_accessor :hostname
33
+ attr_accessor :port
34
+ attr_accessor :tags
35
+ attr_accessor :auth
36
+
37
+ def initialize(name)
38
+ @name=name
39
+ @hostname=name
40
+ @tags={}
41
+ @port=NetUtil::DEFAULT_PORT
42
+ end
43
+
44
+ ############################################################################
45
+ #
46
+ #
47
+ #
48
+ ############################################################################
49
+
50
+ def matches_spec(spec)
51
+
52
+ # ensure it's in array form
53
+ spec=spec.split(",")
54
+
55
+ # check for inversion on first item
56
+ if(spec.first[0..0]=="~")
57
+ # insert implicit "all" at front
58
+ spec=["all"] + spec
59
+ end
60
+
61
+ matches=false
62
+ spec.each do |p|
63
+ matches = matches_single(p,matches)
64
+ end
65
+ matches
66
+
67
+ end
68
+
69
+ ############################################################################
70
+ #
71
+ #
72
+ #
73
+ ############################################################################
74
+
75
+ def matches_single(p,prev)
76
+
77
+ if(p == "all")
78
+ return(true)
79
+ elsif(p[0..0] == "/")
80
+ r=Regexp.new(p.split("/")[1])
81
+ return(true) if(hostname =~ r)
82
+ return(true) if(name =~ r)
83
+ elsif(p[0..0] == "~")
84
+ return(prev && !matches_single(p[1..-1],false))
85
+ elsif(p[0..3] == "tag:")
86
+ tag=p[4..-1]
87
+ return(true) if(tags.include?(tag))
88
+ elsif(p == name)
89
+ return(true)
90
+ elsif(p == hostname)
91
+ return(true)
92
+ end
93
+
94
+ prev
95
+ end
96
+
97
+ end
98
+
99
+ ############################################################################
100
+ #
101
+ #
102
+ #
103
+ ############################################################################
104
+
105
+ class ConfigClientHostBuilder < Builder
106
+
107
+ def build(container,args,block)
108
+ super(container,args,block)
109
+ @config.tags=@config.tags.keys.sort
110
+ @config
111
+ end
112
+
113
+ def auth(&block)
114
+ @config.auth=Deferred.new(@config,[],block,ConfigAuthBuilder)
115
+ end
116
+
117
+ def hostname(v)
118
+ @config.hostname=v
119
+ end
120
+
121
+ def port(v)
122
+ @config.port=v.to_i
123
+ end
124
+
125
+ def tags(*t)
126
+ t.each do |v|
127
+ @config.tags[v.to_s]=true
128
+ end
129
+ end
130
+
131
+ end
132
+
133
+ ############################################################################
134
+ #
135
+ #
136
+ #
137
+ ############################################################################
138
+
139
+ ############################################################################
140
+ #
141
+ #
142
+ #
143
+ ############################################################################
144
+
145
+ ############################################################################
146
+ #
147
+ #
148
+ #
149
+ ############################################################################
150
+
151
+
152
+ end
@@ -0,0 +1,94 @@
1
+ ############################################################################
2
+ #
3
+ # config_daemon.rb
4
+ #
5
+ # daemon {
6
+ # listen_hostname "0.0.0.0"
7
+ # listen_port 45231
8
+ # log_file "filename"
9
+ # auth {
10
+ # ...
11
+ # }
12
+ # services("group-name") {
13
+ # ...
14
+ # }
15
+ # }
16
+ #
17
+ ############################################################################
18
+
19
+ require 'config_auth'
20
+ require 'config_smtp'
21
+ require 'config_daemon_services'
22
+ require 'config'
23
+ require 'config_hook'
24
+
25
+ module ProcessWanker
26
+
27
+ ############################################################################
28
+ #
29
+ #
30
+ #
31
+ ############################################################################
32
+
33
+ class ConfigDaemon < ConfigNode
34
+
35
+ DEFAULT_LISTEN_HOSTNAME="127.0.0.1"
36
+
37
+ attr_accessor :services
38
+ attr_accessor :auth
39
+ attr_accessor :smtp
40
+ attr_accessor :hooks
41
+ attr_accessor :listen_hostname
42
+ attr_accessor :listen_port
43
+ attr_accessor :log_file
44
+
45
+ def initialize()
46
+ @hooks=[]
47
+ @services={}
48
+ @listen_hostname=DEFAULT_LISTEN_HOSTNAME
49
+ @listen_port=NetUtil::DEFAULT_PORT
50
+ end
51
+
52
+
53
+ end
54
+
55
+ ############################################################################
56
+ #
57
+ #
58
+ #
59
+ ############################################################################
60
+
61
+ class ConfigDaemonBuilder < Builder
62
+
63
+ def auth(&block)
64
+ @config.auth=Deferred.new(@config,[],block,ConfigAuthBuilder)
65
+ end
66
+
67
+ def hook(pattern,&block)
68
+ @config.hooks << ConfigHook.new(@config,pattern,block)
69
+ end
70
+
71
+ def smtp(&block)
72
+ @config.smtp=Deferred.new(@config,[],block,ConfigSMTPBuilder)
73
+ end
74
+
75
+ def listen_hostname(v)
76
+ @config.listen_hostname=v
77
+ end
78
+
79
+ def listen_port(v)
80
+ @config.listen_port=v.to_i
81
+ end
82
+
83
+ def log_file(v)
84
+ @config.log_file=v
85
+ end
86
+
87
+ def services(name="default",&block)
88
+ @config.services[name]=Deferred.new(@config,[name],block,ConfigDaemonServicesBuilder)
89
+ end
90
+
91
+ end
92
+
93
+
94
+ end
@@ -0,0 +1,84 @@
1
+ ############################################################################
2
+ #
3
+ # config_daemon_service.rb
4
+ #
5
+ # *_service {
6
+ # ...
7
+ # }
8
+ #
9
+ ############################################################################
10
+
11
+ require 'net_api'
12
+ require 'config_daemon_service_dependency'
13
+ require 'config'
14
+ require 'config_hook'
15
+
16
+ module ProcessWanker
17
+
18
+ ############################################################################
19
+ #
20
+ #
21
+ #
22
+ ############################################################################
23
+
24
+ class ConfigDaemonService < ConfigNode
25
+
26
+ attr_accessor :klass
27
+ attr_accessor :params
28
+ attr_accessor :name
29
+ attr_accessor :hooks
30
+
31
+ def initialize(name,klass)
32
+ @hooks=[]
33
+ @name=name
34
+ @klass=klass
35
+ @params={}
36
+ end
37
+
38
+ end
39
+
40
+ ############################################################################
41
+ #
42
+ #
43
+ #
44
+ ############################################################################
45
+
46
+ class ConfigDaemonServiceBuilder < Builder
47
+
48
+ def hook(pattern,&block)
49
+ @config.hooks << ConfigHook.new(@config,pattern,block)
50
+ end
51
+
52
+ def tags(*t)
53
+ @config.params[:tags] ||= []
54
+ @config.params[:tags] += t
55
+ end
56
+
57
+ def depends(&block)
58
+ @config.params[:dependencies] ||= []
59
+ @config.params[:dependencies] << ConfigDaemonServiceDependencyBuilder.new.build(@config,[],block)
60
+ end
61
+
62
+ def method_missing(method,*args,&block)
63
+ if(block)
64
+ @config.params[method] = block
65
+ elsif(args.length == 0)
66
+ @config.params[method] = true
67
+ elsif(args.length == 1)
68
+ @config.params[method] = args[0]
69
+ else
70
+ @config.params[method] = args
71
+ end
72
+ end
73
+
74
+
75
+ end
76
+
77
+ ############################################################################
78
+ #
79
+ #
80
+ #
81
+ ############################################################################
82
+
83
+
84
+ end
@@ -0,0 +1,59 @@
1
+ ############################################################################
2
+ #
3
+ # config_daemon_service_dependency.rb
4
+ #
5
+ # depends {
6
+ # service "spec"
7
+ # up_for 5
8
+ # }
9
+ #
10
+ ############################################################################
11
+
12
+ require 'net_api'
13
+ require 'config'
14
+
15
+ module ProcessWanker
16
+
17
+ ############################################################################
18
+ #
19
+ #
20
+ #
21
+ ############################################################################
22
+
23
+ class ConfigDaemonServiceDependency < ConfigNode
24
+
25
+ attr_accessor :service
26
+ attr_accessor :up_for
27
+
28
+ def initialize()
29
+ @up_for=3
30
+ end
31
+
32
+ end
33
+
34
+ ############################################################################
35
+ #
36
+ #
37
+ #
38
+ ############################################################################
39
+
40
+ class ConfigDaemonServiceDependencyBuilder < Builder
41
+
42
+ def service(v)
43
+ @config.service=v
44
+ end
45
+
46
+ def up_for(v)
47
+ @config.up_for=v.to_i
48
+ end
49
+
50
+ end
51
+
52
+ ############################################################################
53
+ #
54
+ #
55
+ #
56
+ ############################################################################
57
+
58
+
59
+ end
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env ruby
2
+ ############################################################################
3
+ #
4
+ # config_client_services.rb
5
+ #
6
+ # services("name") {
7
+ # *_service {
8
+ # ...
9
+ # }
10
+ # }
11
+ #
12
+ #
13
+ #
14
+ ############################################################################
15
+
16
+ require 'config_auth'
17
+ require 'config_daemon_service'
18
+ require 'config'
19
+ require 'config_hook'
20
+
21
+ module ProcessWanker
22
+
23
+ ############################################################################
24
+ #
25
+ #
26
+ #
27
+ ############################################################################
28
+
29
+ class ConfigDaemonServices < ConfigNode
30
+
31
+ attr_accessor :services
32
+ attr_accessor :group_name
33
+ attr_accessor :hooks
34
+
35
+ def initialize(group_name)
36
+ @hooks=[]
37
+ @group_name=group_name
38
+ @services={}
39
+ end
40
+
41
+ end
42
+
43
+ ############################################################################
44
+ #
45
+ #
46
+ #
47
+ ############################################################################
48
+
49
+ class ConfigDaemonServicesBuilder < Builder
50
+
51
+ def build(container,args,block)
52
+
53
+ # create construction methods for various service classes
54
+ ServiceMgr.instance.service_classes.each do |k,v|
55
+ str="def #{k}(name,&block) ; "
56
+ str << "@config.services[name] = ConfigDaemonServiceBuilder.new.build(@config,[name,#{v.name}],block) ; "
57
+ str << "end"
58
+ instance_eval(str)
59
+ end
60
+
61
+ super(container,args,block)
62
+ end
63
+
64
+ def hook(pattern,&block)
65
+ @config.hooks << ConfigHook.new(@config,pattern,block)
66
+ end
67
+
68
+
69
+ # def build(args,block)
70
+ #
71
+ # # create construction methods for various service classes
72
+ # ServiceMgr.instance.service_classes.each do |k,v|
73
+ # str="def #{k}(name,&block) ; "
74
+ # str << "@config.services << ConfigDaemonServiceBuilder.new.build(@config,[name],@default_group_name,#{v.name},block) ; "
75
+ # str << "end"
76
+ # instance_eval(str)
77
+ # end
78
+
79
+ # @config=ConfigDaemonServices.new(*args)
80
+ # instance_eval(&block)
81
+ # @config
82
+ # end
83
+
84
+
85
+ end
86
+
87
+
88
+ end
89
+
@@ -0,0 +1,40 @@
1
+ ############################################################################
2
+ #
3
+ # config_hook.rb
4
+ #
5
+ # custom hooks
6
+ #
7
+ # hook("pattern") {
8
+ # ...
9
+ # }
10
+ #
11
+ ############################################################################
12
+
13
+ require 'ipaddr'
14
+ require 'openssl'
15
+ require 'config'
16
+
17
+ module ProcessWanker
18
+
19
+ ############################################################################
20
+ #
21
+ #
22
+ #
23
+ ############################################################################
24
+
25
+ class ConfigHook < ConfigNode
26
+
27
+ attr_accessor :pattern
28
+ attr_accessor :block
29
+
30
+ def initialize(container,pattern,block)
31
+ @container=container
32
+ @pattern=pattern
33
+ @block=block
34
+ end
35
+
36
+ end
37
+
38
+
39
+ end
40
+
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/env ruby
2
+ ############################################################################
3
+ #
4
+ # config_node.rb
5
+ #
6
+ # config helper stuff
7
+ #
8
+ ############################################################################
9
+
10
+ module ProcessWanker
11
+
12
+ ############################################################################
13
+ #
14
+ #
15
+ #
16
+ ############################################################################
17
+
18
+ class ConfigNode
19
+ attr_accessor :container
20
+
21
+ ############################################################################
22
+ #
23
+ # find_attributes
24
+ #
25
+ # walk from the current object up to the root, looking for the named attribute
26
+ #
27
+ ############################################################################
28
+
29
+ def find_attributes(name)
30
+ r=[]
31
+ p=self
32
+ while(p)
33
+ if(p.respond_to?(name))
34
+ v=p.send(name)
35
+ r << v if(v)
36
+ end
37
+ break if(!p.respond_to?("container"))
38
+ p=p.container
39
+ end
40
+ r
41
+ end
42
+
43
+ # find the innermost auth block
44
+ def get_auth()
45
+ find_attributes("auth").first
46
+ end
47
+
48
+ end
49
+
50
+ ############################################################################
51
+ #
52
+ # Deferred
53
+ #
54
+ # there are situations where we don't wish to evaluate all of the
55
+ # configuration unless necessary - for instance, we don't want the daemon
56
+ # to evaluate the list of clusters in the client configuration (particularly
57
+ # if it involves querying the cloud).
58
+ #
59
+ # so, we wrap blocks in Deferred objects which are transparently evaluated
60
+ # on-demand.
61
+ #
62
+ ############################################################################
63
+
64
+ class Deferred
65
+
66
+ def initialize(deferred_container,deferred_args,deferred_block,deferred_builder)
67
+ @deferred_container=deferred_container
68
+ @deferred_args=deferred_args
69
+ @deferred_block=deferred_block
70
+ @deferred_builder=deferred_builder
71
+ @deferred_object=nil
72
+ end
73
+
74
+ def method_missing(name,*args)
75
+ if(!@deferred_object)
76
+ @deferred_object=@deferred_builder.new.build(
77
+ @deferred_container,
78
+ @deferred_args,
79
+ @deferred_block)
80
+ end
81
+ @deferred_object.send(name,*args)
82
+ end
83
+
84
+ end
85
+
86
+ ############################################################################
87
+ #
88
+ #
89
+ #
90
+ ############################################################################
91
+
92
+ class Builder
93
+
94
+ def build(container,args,block)
95
+ @config=klass.new(*args)
96
+ @config.container=container
97
+ instance_eval(&block)
98
+ @config
99
+ end
100
+
101
+ def klass
102
+ cn=self.class.name
103
+ cn.gsub!("Builder","")
104
+ cn=cn.split("::")[-1]
105
+ ProcessWanker::const_get(cn)
106
+ end
107
+
108
+ end
109
+
110
+ ############################################################################
111
+ #
112
+ #
113
+ #
114
+ ############################################################################
115
+
116
+ module Config
117
+
118
+ require 'erb'
119
+
120
+ @@config_path=ENV["PW_CFG"] || "/etc/pw/pw.cfg"
121
+
122
+ def get_config_path()
123
+ @@config_path
124
+ end
125
+ module_function :get_config_path
126
+
127
+ def set_config_path(path)
128
+ @@config_path=path
129
+ end
130
+ module_function :set_config_path
131
+
132
+ def load_config(path)
133
+ ProcessWanker::loaded_config=nil
134
+ # load(path)
135
+ config = ERB.new(File.read(path)).result
136
+ eval(config)
137
+ ProcessWanker::loaded_config
138
+ end
139
+ module_function :load_config
140
+
141
+ end
142
+
143
+
144
+ ############################################################################
145
+ #
146
+ #
147
+ #
148
+ ############################################################################
149
+
150
+ attr_accessor :loaded_config
151
+ module_function :loaded_config
152
+ module_function :loaded_config=
153
+
154
+ def config(&block)
155
+ @loaded_config=ConfigurationBuilder.new.build(nil,[],block)
156
+ end
157
+ module_function :config
158
+
159
+
160
+ end