belphanior-servant 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +202 -0
- data/lib/belphanior/servant/belphanior_servant_helper.rb +5 -0
- data/lib/belphanior/servant/role_builder.rb +146 -0
- data/lib/belphanior/servant/servant.rb +59 -0
- data/lib/belphanior/servant/servant_config.rb +68 -0
- data/lib/belphanior/servant/servant_config_db.rb +58 -0
- data/lib/belphanior/servant/test/tc_role_builder.rb +170 -0
- data/lib/belphanior/servant/test/tc_servant_config.rb +77 -0
- data/lib/belphanior/servant/test/tc_servant_config_db.rb +41 -0
- metadata +106 -0
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# Copyright 2012 Mark T. Tomczak
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'sinatra/base'
|
5
|
+
require 'belphanior/servant/belphanior_servant_helper'
|
6
|
+
|
7
|
+
module RoleBuilderUtils
|
8
|
+
# converts Belphanior-style "$(arg)" arguments to Sinatra-style
|
9
|
+
# ":arg" specifiers
|
10
|
+
def self.usage_string_to_sinatra_path(usage)
|
11
|
+
output = ""
|
12
|
+
usage.split("$").each { |substring|
|
13
|
+
if substring[0,1]=="("
|
14
|
+
substring[0]=":"
|
15
|
+
remainder_array = substring.split(")")
|
16
|
+
|
17
|
+
output += (substring.split(")").join)
|
18
|
+
else
|
19
|
+
output += substring
|
20
|
+
end
|
21
|
+
}
|
22
|
+
output
|
23
|
+
end
|
24
|
+
def self.arguments_and_path_to_sinatra_path(arguments, path)
|
25
|
+
arguments.each { |arg|
|
26
|
+
path = path.sub("$("+arg+")", ":"+identifier_to_url_component(arg))
|
27
|
+
}
|
28
|
+
path
|
29
|
+
end
|
30
|
+
def self.is_valid_identifier?(identifier)
|
31
|
+
identifier =~ /^[a-zA-Z][a-zA-Z0-9 ]*$/
|
32
|
+
end
|
33
|
+
def self.normalize_identifier(identifier)
|
34
|
+
identifier.downcase
|
35
|
+
end
|
36
|
+
def self.identifier_to_url_component(identifier)
|
37
|
+
identifier.gsub(/ /,"_").downcase
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module Sinatra
|
42
|
+
|
43
|
+
module RoleDescriber
|
44
|
+
class BadParameterException < Exception
|
45
|
+
end
|
46
|
+
|
47
|
+
# Adds a new role description. Note: this is a VERY quick-and-dirty hack;
|
48
|
+
# the added description isn't vetted for format conformance at all.
|
49
|
+
def add_role_description(description)
|
50
|
+
if not RoleBuilderUtils::is_valid_identifier?(description["name"])
|
51
|
+
raise BadParameterException, "Role name was not a valid identifier."
|
52
|
+
end
|
53
|
+
name_as_identifier = RoleBuilderUtils::identifier_to_url_component(description["name"])
|
54
|
+
description_local_url = "/role_descriptions/" + name_as_identifier
|
55
|
+
description_as_json = JSON.generate description
|
56
|
+
get description_local_url do
|
57
|
+
BelphaniorServantHelper.text_out_as_json(description_as_json)
|
58
|
+
end
|
59
|
+
role_index = implementation["roles"].length
|
60
|
+
implementation["roles"] << {
|
61
|
+
"role_url" => "",
|
62
|
+
"handlers" => []
|
63
|
+
}
|
64
|
+
|
65
|
+
if implementation["roles"][role_index]["role_url"] == "" then
|
66
|
+
implementation["roles"][role_index]["role_url"] = (
|
67
|
+
"/role_descriptions/" + name_as_identifier)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
register RoleDescriber
|
73
|
+
|
74
|
+
module RoleBuilder
|
75
|
+
class BadParameterException < Exception
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.empty_handlers
|
79
|
+
{
|
80
|
+
"roles" => [
|
81
|
+
]
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.registered(app)
|
86
|
+
app.set :implementation, Sinatra::RoleBuilder.empty_handlers
|
87
|
+
app.get '/protocol' do
|
88
|
+
BelphaniorServantHelper.text_out_as_json(JSON.dump(app.implementation))
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
# Sets the implementation's URL
|
94
|
+
def set_role_url(url, role_index=0)
|
95
|
+
implementation["roles"][role_index]["role_url"]=url
|
96
|
+
end
|
97
|
+
|
98
|
+
# Adds a handler at the specified URL
|
99
|
+
#
|
100
|
+
# params are
|
101
|
+
# - command_name: The identifier for the command this handler implements.
|
102
|
+
# - argument_names: The identifiers for the arguments. The command block will receive the arguments in the same order.
|
103
|
+
# - http_method: HTTP access method, one of "GET", "POST", etc.
|
104
|
+
# - path: The path for the HTTP request (including arguments specified in $(argument name) format).
|
105
|
+
# - data: If a POST method, the data that should be sent (including arguments specified in $(argument name) format).
|
106
|
+
# - role_index: Which role this handler should be mapped into. Assumes the role already has a description.
|
107
|
+
def add_handler(command_name, argument_names, http_method, path, data, role_index=0, &blk)
|
108
|
+
# validate name, args, and method
|
109
|
+
if not RoleBuilderUtils::is_valid_identifier? command_name
|
110
|
+
raise BadParameterException, (command_name + " is not a valid command name.")
|
111
|
+
end
|
112
|
+
argument_names.each { |i|
|
113
|
+
if not RoleBuilderUtils::is_valid_identifier? i
|
114
|
+
raise BadParameterException, (i + " is not a valid argument name.")
|
115
|
+
end
|
116
|
+
}
|
117
|
+
if not ["GET", "POST", "PUT", "DELETE"].include? http_method
|
118
|
+
raise BadParameterException, (http_method + " is not a valid HTTP method (is it capitalized?)")
|
119
|
+
end
|
120
|
+
|
121
|
+
new_handler = {
|
122
|
+
"name" => RoleBuilderUtils::normalize_identifier(command_name),
|
123
|
+
"method" => http_method,
|
124
|
+
"path" => path,
|
125
|
+
"data" => data
|
126
|
+
}
|
127
|
+
|
128
|
+
implementation["roles"][role_index]["handlers"] << new_handler
|
129
|
+
|
130
|
+
# Add the method that will execute for this handler
|
131
|
+
sinatra_path = RoleBuilderUtils::arguments_and_path_to_sinatra_path(argument_names, path)
|
132
|
+
if http_method == "GET"
|
133
|
+
get(sinatra_path, &blk)
|
134
|
+
elsif http_method == "POST"
|
135
|
+
post(sinatra_path, &blk)
|
136
|
+
else
|
137
|
+
raise BadParameterException, ("Unknown HTTP method '" + http_method + "'.")
|
138
|
+
end
|
139
|
+
def clear_handlers
|
140
|
+
# Resets the handler list.
|
141
|
+
set :implementation, Sinatra::RoleBuilder.empty_handlers
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
register RoleBuilder
|
146
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'ftools'
|
3
|
+
require 'optparse'
|
4
|
+
require 'belphanior/servant/servant_config'
|
5
|
+
require 'belphanior/servant/role_builder'
|
6
|
+
require 'sinatra/base'
|
7
|
+
|
8
|
+
module Sinatra
|
9
|
+
module Servant
|
10
|
+
def self.registered(app)
|
11
|
+
if ENV.has_key? "BELPHANIOR_CONFIG_FILE"
|
12
|
+
app.set :servant_config_file, ENV["BELPHANIOR_CONFIG_FILE"]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def servant_init
|
17
|
+
load_servant_config
|
18
|
+
# readonly because changing them involves rebooting the server, so the
|
19
|
+
# change cannot be honored.
|
20
|
+
servant_config.set_readonly "bind"
|
21
|
+
servant_config.set_readonly "port"
|
22
|
+
|
23
|
+
set :bind, servant_config.get("bind")
|
24
|
+
set :port, servant_config.get("port")
|
25
|
+
|
26
|
+
# To simplify functionality, we make every request handle synchronously.
|
27
|
+
enable :lock
|
28
|
+
|
29
|
+
# default handler for top-level index. A user-defined top-level index
|
30
|
+
# created before servant.init is called would override this.
|
31
|
+
get '/' do
|
32
|
+
server_name = servant_config.get("server_name") || "<TODO: set name>"
|
33
|
+
<<EOF
|
34
|
+
<html>
|
35
|
+
<head>
|
36
|
+
<title>Belphanior Servant: #{server_name}</title>
|
37
|
+
</head>
|
38
|
+
<body>
|
39
|
+
<h1>Belphanior Servant Online</h1>
|
40
|
+
<h2>#{server_name}</h2>
|
41
|
+
<p>Hello! I am #{server_name}, and I am happy to serve you.</p>
|
42
|
+
<p>To learn more about what I can do, check my
|
43
|
+
<a href="/protocol">protocol</a>.</p>
|
44
|
+
<p>Want to know my settings? Check my <a href="/config">config</a>.
|
45
|
+
</body>
|
46
|
+
EOF
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
register Servant
|
51
|
+
end
|
52
|
+
|
53
|
+
# To add commands, use the following style:
|
54
|
+
# add_command(
|
55
|
+
# :name => "test",
|
56
|
+
# :description => "Test command.",
|
57
|
+
# :arguments => [["test arg 1"], ["test arg 2","test description"]],
|
58
|
+
# :return => "None.",
|
59
|
+
# :usage => ["GET", "/hi/everybody", ""])
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'belphanior/servant/servant_config_db'
|
3
|
+
require 'belphanior/servant/belphanior_servant_helper'
|
4
|
+
|
5
|
+
module ServantConfigHelper
|
6
|
+
DEFAULT_CONFIG_PATH = "servant_config"
|
7
|
+
def self.prepare_config_file(file_path, config)
|
8
|
+
if File.exist? file_path
|
9
|
+
config_file = File.open(file_path, 'r')
|
10
|
+
new_config = ServantConfigDb.new(config_file.read)
|
11
|
+
config_file.close
|
12
|
+
return new_config
|
13
|
+
else
|
14
|
+
write_config_file(file_path, config)
|
15
|
+
return config
|
16
|
+
end
|
17
|
+
end
|
18
|
+
# Helper function: Tags a text object as a JSON-type
|
19
|
+
def self.write_config_file(file_path, config)
|
20
|
+
out = File.open(file_path, "w")
|
21
|
+
out.write(config.to_json)
|
22
|
+
out.close
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Sinatra
|
27
|
+
module ServantConfig
|
28
|
+
def self.registered(app)
|
29
|
+
app.set :servant_config_file, ServantConfigHelper::DEFAULT_CONFIG_PATH
|
30
|
+
app.set :servant_config_db, ServantConfigDb.new(
|
31
|
+
<<EOF
|
32
|
+
{
|
33
|
+
"ip":"127.0.0.1",
|
34
|
+
"port": "80"
|
35
|
+
}
|
36
|
+
EOF
|
37
|
+
)
|
38
|
+
|
39
|
+
app.get '/config' do
|
40
|
+
BelphaniorServantHelper.text_out_as_json(settings.servant_config_db.to_json)
|
41
|
+
end
|
42
|
+
|
43
|
+
app.get '/config/:name' do
|
44
|
+
[200, settings.servant_config_db.get(params[:name])]
|
45
|
+
end
|
46
|
+
|
47
|
+
app.post '/config/:name' do
|
48
|
+
old_value = settings.servant_config_db.get(params[:name])
|
49
|
+
begin
|
50
|
+
settings.servant_config_db.set(params[:name], request.body.read)
|
51
|
+
ServantConfigHelper.write_config_file(
|
52
|
+
settings.servant_config_file, settings.servant_config_db)
|
53
|
+
return [200, old_value]
|
54
|
+
rescue ServantConfigException => e
|
55
|
+
return [500, "Could not write config: #{e}"]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
def load_servant_config
|
60
|
+
set(:servant_config_db, ServantConfigHelper.prepare_config_file(
|
61
|
+
servant_config_file, servant_config_db))
|
62
|
+
end
|
63
|
+
def servant_config
|
64
|
+
servant_config_db
|
65
|
+
end
|
66
|
+
end
|
67
|
+
register ServantConfig
|
68
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Configuration management library
|
2
|
+
# Manages key-value pair configurations and their serialization to JSON
|
3
|
+
# keys and values are strings
|
4
|
+
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
class ServantConfigException < RuntimeError
|
8
|
+
end
|
9
|
+
|
10
|
+
class ServantConfigDb
|
11
|
+
def initialize(json_string)
|
12
|
+
@config = JSON.parse(json_string)
|
13
|
+
# validation: @config is a hash, keys are strings, vals are strings
|
14
|
+
if (@config.class != {}.class)
|
15
|
+
raise ServantConfigException,
|
16
|
+
"ServantConfig error: JSON string did not evaluate to an object",
|
17
|
+
caller
|
18
|
+
end
|
19
|
+
@config.each{|key, value|
|
20
|
+
if (value.class != "".class)
|
21
|
+
raise ServantConfigException,
|
22
|
+
("ServantConfig error: JSON initialization string, "+
|
23
|
+
"value for key '#{key}' is type '#{value.class}'"),
|
24
|
+
caller
|
25
|
+
end
|
26
|
+
}
|
27
|
+
@config.default ""
|
28
|
+
@readonly = []
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_json
|
32
|
+
JSON.dump(@config)
|
33
|
+
end
|
34
|
+
|
35
|
+
def get(key)
|
36
|
+
@config[key]
|
37
|
+
end
|
38
|
+
|
39
|
+
def set(key, value)
|
40
|
+
key = key.to_s
|
41
|
+
value = value.to_s
|
42
|
+
if @readonly.include? key
|
43
|
+
raise ServantConfigException,
|
44
|
+
"Attempted to change read-only property '#{key}'",
|
45
|
+
caller
|
46
|
+
end
|
47
|
+
@config[key]=value
|
48
|
+
end
|
49
|
+
|
50
|
+
def is_readonly(key)
|
51
|
+
@readonly.include? key
|
52
|
+
end
|
53
|
+
|
54
|
+
def set_readonly(key)
|
55
|
+
@readonly << key
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'belphanior/servant/role_builder'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rack/test'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
ENV['RACK_ENV'] = 'test'
|
8
|
+
|
9
|
+
# Test helper function
|
10
|
+
# Validates that two JSON-style objects are equivalent
|
11
|
+
# Equivalence is defined as follows:
|
12
|
+
# Array type: each element equivalent
|
13
|
+
# Dict type: For each key |k| in reference, key in value
|
14
|
+
# exists and value for the key is equivalent.
|
15
|
+
# NOTE: This means that the input can contain
|
16
|
+
# additional data, and this is acceptable.
|
17
|
+
# All others: Simple ruby equivalence.
|
18
|
+
def assert_equivalent_json_objects(reference, tested)
|
19
|
+
assert_equal(reference.class(), tested.class())
|
20
|
+
if reference.class() == [].class()
|
21
|
+
assert_equal(reference.length, tested.length)
|
22
|
+
for i in 0..reference.length
|
23
|
+
assert_equivalent_json_objects(
|
24
|
+
reference[i], tested[i])
|
25
|
+
end
|
26
|
+
elsif reference.class() == {}.class()
|
27
|
+
reference.each do |key, value|
|
28
|
+
assert_equal(true, tested.has_key?(key))
|
29
|
+
assert_equivalent_json_objects(value, tested[key])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
# String or number (or other type): value compare
|
33
|
+
assert_equal(reference, tested)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
class TestRoleBuilder < Test::Unit::TestCase
|
39
|
+
include Rack::Test::Methods
|
40
|
+
|
41
|
+
def app
|
42
|
+
Sinatra::Application
|
43
|
+
end
|
44
|
+
|
45
|
+
def setup
|
46
|
+
app.set :roles, []
|
47
|
+
@default_description = {
|
48
|
+
"name" => "test description",
|
49
|
+
"description" => "An example of a role description.",
|
50
|
+
"commands" => [
|
51
|
+
{
|
52
|
+
"name" => "test command",
|
53
|
+
"description" => "An example command.",
|
54
|
+
"arguments" => [
|
55
|
+
{
|
56
|
+
"name" => "test arg",
|
57
|
+
"description" => "An example argument."
|
58
|
+
}
|
59
|
+
]
|
60
|
+
}
|
61
|
+
]
|
62
|
+
}
|
63
|
+
@default_usage = [
|
64
|
+
"GET",
|
65
|
+
"/path",
|
66
|
+
"data"
|
67
|
+
]
|
68
|
+
end
|
69
|
+
|
70
|
+
def teardown
|
71
|
+
app.clear_handlers
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_role_describer_accepts_role_description
|
75
|
+
app.add_role_description @default_description
|
76
|
+
get '/role_descriptions/test_description'
|
77
|
+
assert last_response.ok?
|
78
|
+
assert_equal JSON.generate(@default_description), last_response.body
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_add_handler_adds_handler
|
82
|
+
app.add_role_description @default_description
|
83
|
+
app.add_handler("test command", ["argument 1", "argument 2"],
|
84
|
+
"POST", "/test/$(argument 1)", "$(argument 2)") { |arg1|
|
85
|
+
"arg1 is "+arg1+" arg2 is "+(request.body.read)
|
86
|
+
}
|
87
|
+
post '/test/foo', 'bar'
|
88
|
+
assert last_response.ok?
|
89
|
+
assert_equal("arg1 is foo arg2 is bar", last_response.body)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_add_handler_updates_protocol
|
93
|
+
app.add_role_description @default_description
|
94
|
+
app.set_role_url("/test")
|
95
|
+
app.add_handler("test command", ["argument 1"], "GET", "/test/$(argument 1)", "") {|arg1|}
|
96
|
+
get '/protocol'
|
97
|
+
assert_equal(200, last_response.status)
|
98
|
+
assert_equivalent_json_objects(
|
99
|
+
{
|
100
|
+
"roles" => [{
|
101
|
+
"role_url" => "/test",
|
102
|
+
"handlers" => [{
|
103
|
+
"name" => "test command",
|
104
|
+
"method" => "GET",
|
105
|
+
"path" => "/test/$(argument 1)"}]
|
106
|
+
}]},
|
107
|
+
JSON.parse(last_response.body))
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_role_builder_utils_usage_string_to_sinatra_path
|
111
|
+
assert_equal "/test/:value/test/:value2",
|
112
|
+
RoleBuilderUtils.usage_string_to_sinatra_path(
|
113
|
+
"/test/$(value)/test/$(value2)")
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_identifier_case_insensitivity
|
117
|
+
app.add_role_description @default_description
|
118
|
+
app.set_role_url "/test"
|
119
|
+
app.add_handler("My command", ["Cap"], "GET", "path", "data") do end
|
120
|
+
get '/protocol'
|
121
|
+
assert_equal(200, last_response.status)
|
122
|
+
assert_equivalent_json_objects(
|
123
|
+
{
|
124
|
+
"roles" => [{
|
125
|
+
"role_url" => "/test",
|
126
|
+
"handlers" => [{
|
127
|
+
"name" => "my command",
|
128
|
+
"method" => "GET",
|
129
|
+
"path" => "path",
|
130
|
+
"data" => "data"}]
|
131
|
+
}]},
|
132
|
+
JSON.parse(last_response.body))
|
133
|
+
end
|
134
|
+
def test_multi_role
|
135
|
+
app.add_role_description @default_description
|
136
|
+
app.add_role_description({
|
137
|
+
"name" => "second role",
|
138
|
+
"description" => "An example of a role description.",
|
139
|
+
"commands" => [
|
140
|
+
{
|
141
|
+
"name" => "test command 2",
|
142
|
+
"description" => "An example command.",
|
143
|
+
"arguments" => [
|
144
|
+
{
|
145
|
+
"name" => "test arg b",
|
146
|
+
"description" => "An example argument."
|
147
|
+
}
|
148
|
+
]
|
149
|
+
}
|
150
|
+
]
|
151
|
+
}
|
152
|
+
)
|
153
|
+
app.add_handler("test command", ["test arg"],
|
154
|
+
"POST", "/test2/$(test arg)", "", 0) { |arg1|
|
155
|
+
"arg1 is " + arg1
|
156
|
+
}
|
157
|
+
app.add_handler("test command 2", ["test arg b"],
|
158
|
+
"GET", "/test3/$(test arg b)", "", 1) { |arg1|
|
159
|
+
"arg1b is " + arg1
|
160
|
+
}
|
161
|
+
|
162
|
+
post '/test2/foo', ""
|
163
|
+
assert last_response.ok?
|
164
|
+
assert_equal("arg1 is foo", last_response.body)
|
165
|
+
|
166
|
+
get '/test3/bar'
|
167
|
+
assert last_response.ok?
|
168
|
+
assert_equal("arg1b is bar", last_response.body)
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'belphanior/servant/servant_config'
|
3
|
+
require 'belphanior/servant/servant_config_db'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'rack/test'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
ENV['RACK_ENV'] = 'test'
|
9
|
+
|
10
|
+
class TestServantConfig < Test::Unit::TestCase
|
11
|
+
include Rack::Test::Methods
|
12
|
+
SERVANT_CONFIG_FILE = Dir.tmpdir << "/tc_servant_config_out.json"
|
13
|
+
|
14
|
+
def app
|
15
|
+
Sinatra::Application
|
16
|
+
end
|
17
|
+
|
18
|
+
def setup
|
19
|
+
app.set :servant_config_file, SERVANT_CONFIG_FILE
|
20
|
+
app.set :servant_config_db, ServantConfigDb.new(
|
21
|
+
<<EOF
|
22
|
+
{
|
23
|
+
"ip":"127.0.0.1",
|
24
|
+
"port": "80"
|
25
|
+
}
|
26
|
+
EOF
|
27
|
+
)
|
28
|
+
app.load_servant_config
|
29
|
+
end
|
30
|
+
|
31
|
+
def teardown
|
32
|
+
if File.exist? SERVANT_CONFIG_FILE
|
33
|
+
File.delete SERVANT_CONFIG_FILE
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_config_initialize
|
38
|
+
validate = File.open(SERVANT_CONFIG_FILE, "r")
|
39
|
+
content = JSON.parse(validate.read)
|
40
|
+
validate.close
|
41
|
+
assert_equal "127.0.0.1", content["ip"]
|
42
|
+
assert_equal "80", content["port"]
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_get_all_configs
|
46
|
+
get '/config'
|
47
|
+
assert_equal 200, last_response.status
|
48
|
+
result = JSON.parse(last_response.body)
|
49
|
+
assert_equal("127.0.0.1", result["ip"])
|
50
|
+
assert_equal("80", result["port"])
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_get_specific_config
|
54
|
+
get '/config/port'
|
55
|
+
assert_equal 200, last_response.status
|
56
|
+
assert_equal "80", last_response.body
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_write_config
|
60
|
+
post '/config/test', "hi"
|
61
|
+
assert_equal 200, last_response.status
|
62
|
+
get '/config/test'
|
63
|
+
assert_equal 200, last_response.status
|
64
|
+
assert_equal 'hi', last_response.body
|
65
|
+
assert(File.exist? SERVANT_CONFIG_FILE)
|
66
|
+
settings_file = File.open(app.servant_config_file, 'r')
|
67
|
+
result = JSON.parse(settings_file.read)
|
68
|
+
settings_file.close
|
69
|
+
assert_equal 'hi', result["test"]
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_readonly_block
|
73
|
+
app.servant_config_db.set_readonly('port')
|
74
|
+
post '/config/port', '81'
|
75
|
+
assert_equal 500, last_response.status
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'json'
|
3
|
+
require 'belphanior/servant/servant_config_db'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class TestServantConfigDb < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@config = ServantConfigDb.new(
|
9
|
+
<<EOF
|
10
|
+
{
|
11
|
+
"ip":"127.0.0.1",
|
12
|
+
"port": "80"
|
13
|
+
}
|
14
|
+
EOF
|
15
|
+
)
|
16
|
+
end
|
17
|
+
def test_initialization
|
18
|
+
assert_equal(@config.get("ip"),"127.0.0.1")
|
19
|
+
assert_equal(@config.get("port"), "80")
|
20
|
+
end
|
21
|
+
def test_serialization
|
22
|
+
out = JSON.parse(@config.to_json)
|
23
|
+
assert_equal(out.length, 2)
|
24
|
+
assert_equal(out["ip"],"127.0.0.1")
|
25
|
+
assert_equal(out["port"], "80")
|
26
|
+
end
|
27
|
+
def test_set
|
28
|
+
@config.set("bar","hi")
|
29
|
+
assert_equal(@config.get("bar"),"hi")
|
30
|
+
@config.set("number_of_pigs",3)
|
31
|
+
assert_equal(@config.get("number_of_pigs"),"3")
|
32
|
+
end
|
33
|
+
def test_readonly
|
34
|
+
@config.set_readonly("ip")
|
35
|
+
assert(@config.is_readonly("ip"), true)
|
36
|
+
assert_raises(ServantConfigException) {
|
37
|
+
@config.set("ip","127.0.0.10")
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: belphanior-servant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Mark T. Tomczak
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-30 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 13
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 6
|
32
|
+
- 1
|
33
|
+
version: 1.6.1
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sinatra
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 25
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 3
|
48
|
+
- 1
|
49
|
+
version: 1.3.1
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
description: " Belphanior-servant is a library for creating servants using Sinatra. It is\n a prerequesite for all such servants.\n\n For an example of the basics of a servant, consult the examples/empty.rb file or view\n the servants posted to GitHub (http://github.com/fixermark).\n"
|
53
|
+
email: iam+belphanior-servant@fixermark.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- LICENSE
|
62
|
+
- lib/belphanior/servant/belphanior_servant_helper.rb
|
63
|
+
- lib/belphanior/servant/role_builder.rb
|
64
|
+
- lib/belphanior/servant/servant.rb
|
65
|
+
- lib/belphanior/servant/servant_config_db.rb
|
66
|
+
- lib/belphanior/servant/servant_config.rb
|
67
|
+
- lib/belphanior/servant/test/tc_servant_config.rb
|
68
|
+
- lib/belphanior/servant/test/tc_role_builder.rb
|
69
|
+
- lib/belphanior/servant/test/tc_servant_config_db.rb
|
70
|
+
homepage: http://belphanior.net
|
71
|
+
licenses:
|
72
|
+
- http://www.apache.org/licenses/LICENSE-2.0
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 3
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 1.8.24
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: Support library for Belphanior servants written in Ruby using Sinatra.
|
103
|
+
test_files:
|
104
|
+
- lib/belphanior/servant/test/tc_servant_config.rb
|
105
|
+
- lib/belphanior/servant/test/tc_role_builder.rb
|
106
|
+
- lib/belphanior/servant/test/tc_servant_config_db.rb
|