cewebs-aman 2.0.1

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/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # CEWebS AMan
2
+
3
+ The administration manager: websites subscribe to services here, and the configuration of the services and the
4
+ engine is changed here.
5
+
6
+ ```sh
7
+ cewebs-aman new aman # scaffold a server
8
+ aman/aman start # port 9021
9
+ cewebs-aman service aman didaktiki # offer the service in the directory didaktiki
10
+ cewebs-aman addkey aman # partner key for a new website
11
+ ```
12
+
13
+ See the README of `cewebs-transformationengine` for how all parts of CEWebS work together.
14
+
15
+ ## License
16
+
17
+ GPL-3.0-or-later, see LICENSE.GPL.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'rake'
2
+ require 'rubygems/package_task'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.test_files = FileList['./test/tc_*.rb']
7
+ t.warning = false
8
+ end
9
+
10
+ spec = eval(File.read('cewebs-aman.gemspec'))
11
+ Gem::PackageTask.new(spec) do |pkg|
12
+ FileUtils.mkdir 'pkg' rescue nil
13
+ FileUtils.rm_rf Dir.glob('pkg/*')
14
+ FileUtils.ln_sf "#{pkg.name}.gem", "pkg/#{spec.name}.gem"
15
+ end
16
+
17
+ task :default => :gem
18
+
19
+ task :push => :gem do |r|
20
+ sh "gem push pkg/#{spec.name}.gem"
21
+ end
22
+
23
+ task :install => :gem do |r|
24
+ sh "gem install pkg/#{spec.name}.gem"
25
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cewebs-aman"
3
+ s.version = "2.0.1"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.license = "GPL-3.0-or-later"
6
+ s.summary = "Administration manager of CEWebS"
7
+
8
+ s.description = "Subscribing to services, and configuration of services and the engine."
9
+
10
+ s.files = Dir['{server/**/*,tools/**/*,lib/**/*}'] + %w(LICENSE.GPL Rakefile cewebs-aman.gemspec README.md AUTHORS)
11
+ s.require_path = 'lib'
12
+ s.extra_rdoc_files = ['README.md']
13
+ s.bindir = 'tools'
14
+ s.executables = ['cewebs-aman']
15
+ s.test_files = Dir['test/tc_*.rb']
16
+
17
+ s.required_ruby_version = '>=3.2'
18
+
19
+ s.authors = ['Juergen eTM Mangler']
20
+ s.email = 'juergen.mangler@gmail.com'
21
+ s.homepage = 'http://www.cewebs.org'
22
+
23
+ s.add_runtime_dependency 'cewebs-servicesbase', '~> 3.0', '>= 3.0.1'
24
+ end
@@ -0,0 +1,261 @@
1
+ # Please see the LICENSE.GPL file for copyright and distribution information
2
+ require 'digest/md5'
3
+ require 'open3'
4
+
5
+ module AMan
6
+ # Everything below is called by the engine on behalf of an administrator. The key
7
+ # is the key of the engine.
8
+ class Delivery < CEWebS::Delivery
9
+ def authorized?(r); AMan::engine?(r.key); end
10
+ def changes; %w{add delete sourcesave edit new sort}; end
11
+
12
+ def prepare(r) #{{{
13
+ Place::catch_up
14
+ case r.command
15
+ when 'show'
16
+ pars = r.parameters.map { |p| "_#{p.name}=#{p.raw}" }.join
17
+ CEWebS::Prepare.new(:cachekey => Digest::MD5.hexdigest("show#{pars}_#{r.language.join(',')}"), :authenticate => true)
18
+ when 'gui', 'delete', 'add', 'more', 'source', 'sourcesave'
19
+ CEWebS::Prepare.new(:cachekey => '', :authenticate => true)
20
+ else
21
+ unknown(r)
22
+ end
23
+ end #}}}
24
+
25
+ def document(r) #{{{
26
+ c = Context.new(r)
27
+ case r.command
28
+ when 'show'; show(r,c)
29
+ when 'gui'; gui(r,c)
30
+ when 'more'; more(r,c)
31
+ when 'add'; add(r,c)
32
+ when 'delete'
33
+ c.place.change(c.part,'treeDelete',r.decode('del').to_s)
34
+ redirect(c.url('gui'))
35
+ when 'source'; source(r,c)
36
+ when 'sourcesave'; sourcesave(r,c)
37
+ else unknown(r)
38
+ end
39
+ end #}}}
40
+
41
+ def chunk(r) #{{{
42
+ c = Context.new(r)
43
+ set = r.decode('set').to_s
44
+ value = r.raw('value')&.delete("\r")
45
+ ret = case r.command
46
+ when 'delete'
47
+ c.place.change(c.part,'valueDelete',c.path + c.id + set)
48
+ ''
49
+ when 'edit', 'new'
50
+ operation = r.command.to_sym
51
+ if value.nil?
52
+ c.gui.render([:set_form,operation,set])
53
+ else
54
+ gui = c.gui
55
+ c.place.change(c.part,operation == :new ? 'valueAdd' : 'valueReplace',c.path + c.id + set,value)
56
+ gui.set_value(operation,set,value)
57
+ end
58
+ when 'sort'
59
+ order = r.raw('set').to_s.split(';').map { |e| CGI.unescape(e).unpack1('m').force_encoding('UTF-8') }
60
+ xml = CEWebS::Configuration::sort(c.place.file(c.part),c.path + c.id,order)
61
+ c.place.change(c.part,'treeReplace',c.path + c.id,xml) if xml
62
+ ''
63
+ else unknown(r)
64
+ end
65
+ CEWebS::File_.new(:content => ret, :mime => 'text/plain; charset=utf-8')
66
+ end #}}}
67
+
68
+ private
69
+
70
+ def redirect(url)
71
+ CEWebS::Document.new(:headers => [{ :name => 'Location', :value => url.gsub('&amp;','&') }])
72
+ end
73
+
74
+ def show(r,c) #{{{
75
+ modified = r.newer(CEWebS::data('strings.xml'),CEWebS::data('services.xml'),CEWebS::data(r.key,'Services.xml'),c.place.index) or return nil
76
+ CEWebS::Document.new(
77
+ :content => ShowView.new(r,c).render(:document),
78
+ :modified => modified,
79
+ :breadcrumbs => c.breadcrumbs
80
+ )
81
+ end #}}}
82
+
83
+ def gui(r,c) #{{{
84
+ access = [{ :href => c.url('source'), :text => c.strings.sfind('source') }]
85
+ if c.path =~ /.\//
86
+ back = CEWebS::Configuration::parent(c.path)
87
+ backid = back[/#{CEWebS::Configuration::PREDICATE}\z/].to_s
88
+ back = back.delete_suffix(backid)
89
+ access.unshift({ :href => c.url('gui',:path => back,:id => backid), :text => c.strings.sfind('back') })
90
+ access.unshift({ :href => c.url('add'), :text => c.strings.sfind('newentry') })
91
+ end
92
+ CEWebS::Document.new(
93
+ :content => c.gui.render(:to_ui,:script),
94
+ :js => 'jquery',
95
+ :breadcrumbs => c.breadcrumbs,
96
+ :access => access
97
+ )
98
+ end #}}}
99
+
100
+ def more(r,c) #{{{
101
+ criteria = nil
102
+ if r.parameter('search')
103
+ criteria = r.parameters.filter_map { |p| [$1.to_i,p.raw] if p.name =~ /\Aspath_(\d+)\z/ }.sort.map do |i,path|
104
+ [path,r.raw("svalue_#{i}").to_s]
105
+ end
106
+ end
107
+ CEWebS::Document.new(
108
+ :content => c.gui.render([:to_more,r.user.delete("'"),c.place.file(c.part,'search.xml'),criteria],:script),
109
+ :js => 'jquery',
110
+ :breadcrumbs => c.breadcrumbs,
111
+ :access => [{ :href => c.url('gui',:path => r.decode('rpath').to_s,:id => r.decode('rid').to_s), :text => c.strings.sfind('back') }]
112
+ )
113
+ end #}}}
114
+
115
+ def add(r,c) #{{{
116
+ gui = c.gui
117
+ xml = c.place.change(c.part,'treeAddDynamic',gui.path_pos)
118
+ if r.parameter('optional')
119
+ # don't show the new element, but where it belongs to
120
+ path = CEWebS::Configuration::parent(c.path)
121
+ redirect(c.url('gui',:path => path.sub(/#{CEWebS::Configuration::PREDICATE}\z/,''),:id => c.id))
122
+ else
123
+ gui.new_part(xml) unless xml == ''
124
+ redirect(c.url('gui',:id => gui.id))
125
+ end
126
+ end #}}}
127
+
128
+ # after an error the text of the user is shown again
129
+ def source(r,c,error=nil) #{{{
130
+ CEWebS::Document.new(
131
+ :content => SourceView.new(r,c,error).render(:document),
132
+ :breadcrumbs => c.breadcrumbs,
133
+ :access => [{ :href => c.url('gui'), :text => c.strings.sfind('back') }]
134
+ )
135
+ end #}}}
136
+
137
+ def sourcesave(r,c) #{{{
138
+ path = (c.path == '/' ? '/*' : c.path) + c.id
139
+ source = r.raw('source').to_s.delete("\r")
140
+ doc = XML::Smart.open(c.place.file(c.part))
141
+ src = doc.find(path).first
142
+ error = nil
143
+ begin
144
+ new = XML::Smart.string(source).root
145
+ rescue
146
+ new = nil
147
+ end
148
+ if src.nil? || src.dump != r.decode('original')
149
+ error = 'meanwhile'
150
+ elsif new.nil?
151
+ error = 'wellformed'
152
+ else
153
+ src.replace_by(new)
154
+ _, _, status = Open3.capture3('xmllint','--noout','--relaxng',c.place.file(c.part,'rng'),'-',:stdin_data => doc.root.dump)
155
+ error = 'schema' unless status.success?
156
+ end
157
+ return source(r,c,error) if error
158
+ c.place.change(c.part,'treeReplace',path,source)
159
+ redirect(c.url('gui'))
160
+ end #}}}
161
+ end
162
+
163
+ # what a request is about
164
+ class Context
165
+ attr_reader :place, :part, :path, :id
166
+
167
+ def initialize(r) #{{{
168
+ @r = r
169
+ @place = Place.new(r.key,r.raw('t'),r.raw('s') == 'Services')
170
+ @part = r.raw('part').to_s
171
+ @path = r.decode('path').to_s
172
+ @path = '/' if @path == '' || @path == '/*'
173
+ @id = r.decode('id').to_s
174
+ end #}}}
175
+
176
+ def strings #{{{
177
+ @strings ||= XML::Smart.open(CEWebS::data('strings.xml')).lfind('/strings/strings',@r.language)
178
+ end #}}}
179
+
180
+ def base(mode,command,part=true) #{{{
181
+ CEWebS::View::link(mode,command,{ :t => @place.target, :s => @place.services ? 'Services' : nil, :part => part ? @part : nil })
182
+ end #}}}
183
+ def url(command,path: @path,id: @id) #{{{
184
+ base('document',command) + "&amp;CEWebS_path=#{RelaxNGUI::path_to_send(path)}&amp;CEWebS_id=#{RelaxNGUI::path_to_send(id)}"
185
+ end #}}}
186
+
187
+ def gui #{{{
188
+ RelaxNGUI.new(
189
+ @place.file(@part),
190
+ @place.file(@part,'rng'),
191
+ @place.helper_files,
192
+ strings,
193
+ @id,
194
+ {
195
+ :more_list => base('document','more'),
196
+ :edit_child => base('document','gui'),
197
+ :add_child => base('document','add'),
198
+ :delete_child => base('document','delete'),
199
+ :edit_value => base('chunk','edit'),
200
+ :new_value => base('chunk','new'),
201
+ :delete_value => base('chunk','delete'),
202
+ :sort_values => base('chunk','sort')
203
+ },
204
+ @path,
205
+ @r.language
206
+ )
207
+ end #}}}
208
+
209
+ def breadcrumbs #{{{
210
+ ret = [{ :href => base('document','show',false), :text => @place.title }]
211
+ node = @part == '' ? nil : @place.part(@part)
212
+ ret << { :href => base('document','gui'), :text => node.lfind('description',@r.language).sfind('short') } if node
213
+ ret
214
+ end #}}}
215
+ end
216
+
217
+ class ShowView < CEWebS::View
218
+ def initialize(r,c); @r = r; @c = c; end
219
+
220
+ def document #{{{
221
+ ul_ do
222
+ @c.place.parts.each do |e|
223
+ next unless e.attributes['share'].to_s == '' || @c.place.target == 'Shared'
224
+ s = e.lfind('description',@r.language)
225
+ li_ do
226
+ div_ do
227
+ a_ s.sfind('short'), :href => @c.base('document','gui') + "&amp;CEWebS_part=#{e.attributes['id']}"
228
+ end
229
+ div_ s.sfind('long')
230
+ end
231
+ end
232
+ nil
233
+ end
234
+ end #}}}
235
+ end
236
+
237
+ class SourceView < CEWebS::View
238
+ def initialize(r,c,error); @r = r; @c = c; @error = error; end
239
+
240
+ def document #{{{
241
+ if @error
242
+ src, original = @r.raw('source').to_s, @r.decode('original').to_s
243
+ else
244
+ src = XML::Smart.open(@c.place.file(@c.part)).find((@c.path == '/' ? '/*' : @c.path) + @c.id).first
245
+ src = original = src ? src.dump : ''
246
+ end
247
+ div_ :id => 'icontent' do
248
+ div_ @c.strings.sfind(@error ? "source_error_#{@error}" : 'source_edit'), :class => 'border margin-bottom-big'
249
+ form_ :method => 'post', :action => @c.url('sourcesave') do
250
+ input_ :type => 'hidden', :name => 'CEWebS_original', :value => [original].pack('m0')
251
+ textarea_ src, :id => 'CEWebS_source', :name => 'CEWebS_source', :rows => '30', :cols => '40', :class => 'size-max'
252
+ div_ :class => 'margin-top-big' do
253
+ input_ :type => 'submit', :value => @c.strings.sfind('set')
254
+ space_
255
+ input_ :type => 'reset', :value => @c.strings.sfind('reset')
256
+ end
257
+ end
258
+ end
259
+ end #}}}
260
+ end
261
+ end
@@ -0,0 +1,185 @@
1
+ # Please see the LICENSE.GPL file for copyright and distribution information
2
+ module AMan
3
+ # Layout of the data directory
4
+ # services.xml available services
5
+ # partnerkeys.xml who is allowed to register an engine
6
+ # Default/ template for new engines
7
+ # {engine}/Services.xml services of an engine
8
+ # {engine}/{Engine|Shared}.xml parts, configuration in the directory with the same name
9
+ # {engine}/Services/{service}.xml
10
+
11
+ def self::engine?(key)
12
+ CEWebS::key?(key) && File.directory?(CEWebS::data(key))
13
+ end
14
+
15
+ # parts of the configuration that belong to an engine, a service, or are shared
16
+ class Place
17
+ attr_reader :engine, :target, :services
18
+
19
+ def initialize(engine,target,services=nil) #{{{
20
+ @engine = engine
21
+ @target = target.to_s
22
+ @services = services.nil? ? CEWebS::key?(@target) : services
23
+ raise CEWebS::Error.new('not known',404) unless AMan::engine?(engine) && (CEWebS::key?(@target) || %w{Engine Shared}.include?(@target))
24
+ raise CEWebS::Error.new('not known',404) unless File.exist?(index)
25
+ end #}}}
26
+
27
+ def directory; CEWebS::data(@engine,@services ? 'Services' : '',@target); end
28
+ def index; directory + '.xml'; end
29
+
30
+ def file(part,type='xml') #{{{
31
+ raise CEWebS::Error.new('part not known',404) unless part.to_s =~ /\A[\w:][\w:.\-]*\z/ && File.exist?(File.join(directory,"#{part}.xml"))
32
+ File.join(directory,"#{part}.#{type}")
33
+ end #}}}
34
+
35
+ def parts; XML::Smart.open(index).find('/parts/part'); end
36
+ def part(id); parts.find { |e| e.attributes['id'] == id }; end
37
+
38
+ def title #{{{
39
+ return @target unless @services
40
+ XML::Smart.open(CEWebS::data(@engine,'Services.xml')).sfind("/services/service[@key='#{@target}']")
41
+ end #}}}
42
+
43
+ # files that can be used by references in schemas
44
+ def helper_files #{{{
45
+ ret = parts.to_h { |e| [e.attributes['id'],file(e.attributes['id'])] }
46
+ if @target != 'Shared' && File.exist?(CEWebS::data(@engine,'Shared.xml'))
47
+ shared = Place.new(@engine,'Shared')
48
+ shared.parts.each do |e|
49
+ ret[e.attributes['id']] ||= shared.file(e.attributes['id']) if e.find('who').any? { |w| w.to_s == @target }
50
+ end
51
+ end
52
+ ret
53
+ end #}}}
54
+
55
+ # [url,key] of everybody who uses a part
56
+ def remotes(part) #{{{
57
+ whos = @target == 'Shared' ? part(part).find('who').map(&:to_s) : [@target]
58
+ whos.filter_map { |who| Place::remote(@engine,who) }
59
+ end #}}}
60
+ def self::remote(engine,who) #{{{
61
+ tdoc = XML::Smart.open(CEWebS::data(engine,'Services.xml'))
62
+ return [tdoc.find('string(/services/@engine)'),engine] if who == 'Engine'
63
+ ser = tdoc.find("/services/service[@key='#{who}']").first or return nil
64
+ url = XML::Smart.open(CEWebS::data('services.xml')).sfind("/services/service[@id='#{ser.attributes['id']}']/administration")
65
+ ["#{url.sub(/\/+\z/,'')}/#{ser.attributes['instance']}",who]
66
+ end #}}}
67
+
68
+ # Changes a part everywhere, returns the xml that has been used.
69
+ # except is the key of somebody who already changed it.
70
+ def change(part,operation,path,xml='',except=nil) #{{{
71
+ fname = file(part)
72
+ xml = CEWebS::Password::protect(operation,path,xml) if part == 'persons'
73
+ Place::synchronize(fname) do
74
+ all = remotes(part).reject { |_,key| key == except }
75
+ failed = {}
76
+ all.each do |url,key|
77
+ begin
78
+ res = CEWebS::Remote.new(url,key).call(:put,"parts/#{part}",[['operation',operation],['path',path],['xml',xml]])[1]
79
+ if operation == 'treeAddDynamic'
80
+ xml = CEWebS::Messages::parse_list(res)[0][:xml]
81
+ operation = 'treeAdd'
82
+ end
83
+ rescue => e
84
+ puts "#{url}: #{e.message}"
85
+ failed[key] = e
86
+ end
87
+ end
88
+ # the copy must not differ from what everybody else uses
89
+ raise CEWebS::Error.new("Configuration not changed: #{failed.values.first.message}",502) if except.nil? && failed.any? && failed.length == all.length
90
+ CEWebS::Configuration::apply(fname,operation,path,xml)
91
+ store_pending(part,pending(part) | failed.keys) if failed.any?
92
+ end
93
+ Place::catch_up
94
+ xml
95
+ end #}}}
96
+
97
+ ### Who missed a change gets the whole part later. The keys are in {part}.pending
98
+
99
+ PENDING = File.join('**','*.pending')
100
+ GUARD = Mutex.new
101
+ LOCKS = {}
102
+
103
+ # changes of a part have to arrive in the same order everywhere
104
+ def self::synchronize(fname,&blk) #{{{
105
+ GUARD.synchronize { LOCKS[fname] ||= Mutex.new }.synchronize(&blk)
106
+ end #}}}
107
+
108
+ def pending(part) #{{{
109
+ fname = file(part,'pending')
110
+ File.exist?(fname) ? File.readlines(fname,:chomp => true) : []
111
+ end #}}}
112
+ def store_pending(part,keys) #{{{
113
+ fname = file(part,'pending')
114
+ keys.empty? ? FileUtils.rm_f(fname) : File.write(fname,keys.join("\n"))
115
+ end #}}}
116
+
117
+ def resend(part) #{{{
118
+ fname = file(part)
119
+ Place::synchronize(fname) do
120
+ left = pending(part).select do |key|
121
+ url, = remotes(part).find { |_,k| k == key }
122
+ next if url.nil? # not subscribed any longer
123
+ begin
124
+ CEWebS::Remote.new(url,key).call(:put,"parts/#{part}",[['operation','treeReplace'],['path','/*'],['xml',XML::Smart.open(fname).root.dump]])
125
+ false
126
+ rescue
127
+ true
128
+ end
129
+ end
130
+ store_pending(part,left)
131
+ end
132
+ end #}}}
133
+ def self::resend #{{{
134
+ Dir.glob(PENDING,:base => CEWebS::data_dir).each do |f|
135
+ engine, *, target = File.dirname(f).split('/')
136
+ begin
137
+ Place.new(engine,target).resend(File.basename(f,'.pending'))
138
+ rescue => e
139
+ puts "#{f}: #{e.message}"
140
+ end
141
+ end
142
+ end #}}}
143
+
144
+ # tries until everybody has the changes, also called by requests because the AMan could have been restarted
145
+ def self::catch_up(interval=60) #{{{
146
+ GUARD.synchronize do
147
+ next if @catch_up&.alive? || Dir.glob(PENDING,:base => CEWebS::data_dir).empty?
148
+ @catch_up = Thread.new do
149
+ loop do
150
+ resend
151
+ break if Dir.glob(PENDING,:base => CEWebS::data_dir).empty?
152
+ sleep interval
153
+ end
154
+ end
155
+ end
156
+ end #}}}
157
+
158
+ # copy the configuration of somebody. Shared parts that already exist are sent instead.
159
+ def self::sync(engine,who) #{{{
160
+ url, key = remote(engine,who)
161
+ remote = CEWebS::Remote.new(url,key)
162
+ own = who == 'Engine' ? 'Engine' : File.join('Services',who)
163
+ remote.list('parts').each do |p|
164
+ raise CEWebS::Error.new('invalid part',502) unless p[:identifier] =~ /\A[\w:][\w:.\-]*\z/
165
+ target = CEWebS::data(engine,p[:share].to_s == '' ? own : 'Shared')
166
+ fname = File.join(target,p[:identifier])
167
+ FileUtils.mkdir_p(target)
168
+ if File.exist?(fname + '.xml')
169
+ remote.call(:put,"parts/#{p[:identifier]}",[['operation','treeReplace'],['path','/*'],['xml',XML::Smart.open(fname + '.xml').root.dump]])
170
+ else
171
+ File.write(fname + '.xml',p[:xml])
172
+ File.write(fname + '.rng',p[:schema])
173
+ end
174
+ XML::Smart.modify(target + '.xml','<parts/>') do |doc|
175
+ part = doc.find("/parts/part[@id='#{p[:identifier]}']").first
176
+ if part.nil?
177
+ part = doc.root.add('part',{ 'id' => p[:identifier], 'share' => p[:share].to_s })
178
+ XML::Smart.string("<d>#{p[:description]}</d>").find('/d/*').each { |d| part.add(d) }
179
+ end
180
+ part.add('who',who) if p[:share].to_s != '' && part.find('who').none? { |w| w.to_s == who }
181
+ end
182
+ end
183
+ end #}}}
184
+ end
185
+ end