sp-job 0.2.3 → 0.3.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/bin/configure +40 -0
- data/lib/sp-job.rb +21 -2
- data/lib/sp/job/back_burner.rb +350 -68
- data/lib/sp/job/broker.rb +18 -16
- data/lib/sp/job/broker_http_client.rb +80 -20
- data/lib/sp/job/broker_oauth2_client.rb +12 -4
- data/lib/sp/job/common.rb +876 -62
- data/lib/sp/job/configure/configure.rb +640 -0
- data/lib/sp/job/curl_http_client.rb +100 -0
- data/lib/sp/job/easy_http_client.rb +94 -0
- data/lib/sp/job/http_client.rb +51 -0
- data/lib/sp/job/job_db_adapter.rb +38 -36
- data/lib/sp/job/jsonapi_error.rb +31 -74
- data/lib/sp/job/jwt.rb +55 -5
- data/lib/sp/job/mail_queue.rb +9 -2
- data/lib/sp/job/manticore_http_client.rb +94 -0
- data/lib/sp/job/pg_connection.rb +90 -10
- data/lib/sp/job/query_params.rb +45 -0
- data/lib/sp/job/rfc822.rb +13 -0
- data/lib/sp/job/session.rb +239 -0
- data/lib/sp/job/unique_file.rb +37 -1
- data/lib/sp/job/uploaded_image_converter.rb +27 -19
- data/lib/sp/job/worker.rb +51 -1
- data/lib/sp/job/worker_thread.rb +22 -7
- data/lib/sp/jsonapi.rb +24 -0
- data/lib/sp/jsonapi/adapters/base.rb +177 -0
- data/lib/sp/jsonapi/adapters/db.rb +26 -0
- data/lib/sp/jsonapi/adapters/raw_db.rb +96 -0
- data/lib/sp/jsonapi/exceptions.rb +54 -0
- data/lib/sp/jsonapi/model/base.rb +31 -0
- data/lib/sp/jsonapi/model/concerns/attributes.rb +91 -0
- data/lib/sp/jsonapi/model/concerns/model.rb +39 -0
- data/lib/sp/jsonapi/model/concerns/persistence.rb +212 -0
- data/lib/sp/jsonapi/model/concerns/serialization.rb +57 -0
- data/lib/sp/jsonapi/parameters.rb +54 -0
- data/lib/sp/jsonapi/service.rb +96 -0
- data/lib/tasks/configure.rake +2 -496
- data/sp-job.gemspec +3 -2
- metadata +24 -2
@@ -0,0 +1,57 @@
|
|
1
|
+
module SP
|
2
|
+
module JSONAPI
|
3
|
+
module Model
|
4
|
+
module Concerns
|
5
|
+
module Serialization
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
attr_accessor :include_root_in_json
|
9
|
+
end
|
10
|
+
|
11
|
+
def as_json(options = {})
|
12
|
+
root = include_root_in_json
|
13
|
+
root = options[:root] if options.try(:key?, :root)
|
14
|
+
if root
|
15
|
+
root = self.class.name.underscore.gsub('/','_').to_sym
|
16
|
+
{ root => serializable_hash(options) }
|
17
|
+
else
|
18
|
+
serializable_hash(options)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def from_json(json)
|
23
|
+
root = include_root_in_json
|
24
|
+
hash = JSON.parse(json)
|
25
|
+
hash = hash.values.first if root
|
26
|
+
self.attributes = hash
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
alias :read_attribute_for_serialization :send
|
33
|
+
|
34
|
+
def serializable_hash(options = {})
|
35
|
+
|
36
|
+
attribute_names = self.class.attributes.sort
|
37
|
+
if only = options[:only]
|
38
|
+
attribute_names &= Array.wrap(only).map(&:to_s)
|
39
|
+
elsif except = options[:except]
|
40
|
+
attribute_names -= Array.wrap(except).map(&:to_s)
|
41
|
+
end
|
42
|
+
|
43
|
+
hash = {}
|
44
|
+
attribute_names.each { |n| hash[n] = read_attribute_for_serialization(n) }
|
45
|
+
|
46
|
+
method_names = Array.wrap(options[:methods]).select { |n| respond_to?(n) }
|
47
|
+
method_names.each { |n| hash[n] = send(n) }
|
48
|
+
|
49
|
+
hash
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module SP
|
2
|
+
module JSONAPI
|
3
|
+
class Parameters
|
4
|
+
attr_reader :user_id, :entity_id, :entity_schema, :sharded_schema, :subentity_schema, :subentity_prefix
|
5
|
+
|
6
|
+
def initialize(parameters = {})
|
7
|
+
check_jsonapi_args(parameters)
|
8
|
+
|
9
|
+
@user_id = parameters[:user_id].to_s unless parameters[:user_id].nil?
|
10
|
+
@entity_id = parameters[:entity_id].to_s unless parameters[:entity_id].nil?
|
11
|
+
@entity_schema = parameters[:entity_schema] unless parameters[:entity_schema].nil?
|
12
|
+
@sharded_schema = parameters[:sharded_schema] unless parameters[:sharded_schema].nil?
|
13
|
+
@subentity_schema = parameters[:subentity_schema] unless parameters[:subentity_schema].nil?
|
14
|
+
@subentity_prefix = parameters[:subentity_prefix] unless parameters[:subentity_prefix].nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_json(options = {})
|
18
|
+
{
|
19
|
+
user_id: self.user_id,
|
20
|
+
entity_id: self.entity_id,
|
21
|
+
company_schema: self.entity_schema,
|
22
|
+
sharded_schema: self.sharded_schema,
|
23
|
+
subentity_schema: self.subentity_schema,
|
24
|
+
subentity_prefix: self.subentity_prefix
|
25
|
+
}.to_json
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def check_jsonapi_args(parameters)
|
30
|
+
if parameters.keys.any? && !(parameters.keys - valid_keys).empty?
|
31
|
+
raise SP::JSONAPI::Exceptions::InvalidJSONAPIKeyError.new(key: (parameters.keys - valid_keys).join(', '))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def valid_keys
|
36
|
+
[ :user_id, :entity_id, :entity_schema, :sharded_schema, :subentity_schema, :subentity_prefix ]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class ParametersNotPicky < Parameters
|
41
|
+
|
42
|
+
def initialize (parameters)
|
43
|
+
@user_id = parameters[:user_id].to_s
|
44
|
+
@entity_id = parameters[:entity_id].to_s
|
45
|
+
@entity_schema = parameters[:entity_schema]
|
46
|
+
@sharded_schema = parameters[:sharded_schema]
|
47
|
+
@subentity_schema = parameters[:subentity_schema]
|
48
|
+
@subentity_prefix = parameters[:subentity_prefix]
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module SP
|
2
|
+
module JSONAPI
|
3
|
+
|
4
|
+
VERSION = '1.0'
|
5
|
+
|
6
|
+
class Status
|
7
|
+
OK = 200
|
8
|
+
ERROR = 500
|
9
|
+
end
|
10
|
+
|
11
|
+
class Service
|
12
|
+
|
13
|
+
def self.protocols ; [ :db, :http ] ; end
|
14
|
+
def connection ; @pg_connection ; end
|
15
|
+
def url ; @url ; end
|
16
|
+
def set_url(value) ; @url = value ; end
|
17
|
+
def adapter
|
18
|
+
raise Exceptions::ServiceSetupError.new('JSONAPI prefix not specified', nil) if (url.nil? || url.empty?)
|
19
|
+
@adapter_instance ||= @adapter.new(self)
|
20
|
+
SP::JSONAPI::Model::Base.adapter ||= @adapter_instance
|
21
|
+
@adapter_instance
|
22
|
+
end
|
23
|
+
|
24
|
+
def protocol ; @protocol ; end
|
25
|
+
def protocol=(value)
|
26
|
+
if !value.to_sym.in?(Service.protocols)
|
27
|
+
raise Exceptions::ServiceProtocolError.new(protocol: value.to_sym, protocols: Service.protocols.join(', '))
|
28
|
+
end
|
29
|
+
@protocol = value.to_sym
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(pg_connection, url, default_adapter = SP::JSONAPI::Adapters::Db)
|
33
|
+
@pg_connection = pg_connection
|
34
|
+
@url = url
|
35
|
+
protocol = :db
|
36
|
+
@adapter = default_adapter
|
37
|
+
adapter unless url.nil?
|
38
|
+
end
|
39
|
+
|
40
|
+
def close
|
41
|
+
@pg_connection.close if !@pg_connection.nil? && !@pg_connection.finished?
|
42
|
+
@adapter_instance = nil
|
43
|
+
@adapter = nil
|
44
|
+
@url = nil
|
45
|
+
end
|
46
|
+
|
47
|
+
def set_jsonapi_parameters(parameters = nil) ; @parameters = parameters ; end
|
48
|
+
def clear_jsonapi_args ; @parameters = nil ; end
|
49
|
+
def parameters ; @parameters ; end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def create_jsonapi_function
|
54
|
+
connection.exec %Q[
|
55
|
+
|
56
|
+
CREATE OR REPLACE FUNCTION public.jsonapi (
|
57
|
+
IN method text,
|
58
|
+
IN uri text,
|
59
|
+
IN body text,
|
60
|
+
IN user_id text,
|
61
|
+
IN company_id text,
|
62
|
+
IN company_schema text,
|
63
|
+
IN sharded_schema text,
|
64
|
+
IN accounting_schema text,
|
65
|
+
IN accounting_prefix text,
|
66
|
+
OUT http_status integer,
|
67
|
+
OUT response text
|
68
|
+
) RETURNS record AS '$libdir/pg-jsonapi.so', 'jsonapi' LANGUAGE C;
|
69
|
+
|
70
|
+
CREATE OR REPLACE FUNCTION public.inside_jsonapi (
|
71
|
+
) RETURNS boolean AS '$libdir/pg-jsonapi.so', 'inside_jsonapi' LANGUAGE C;
|
72
|
+
|
73
|
+
CREATE OR REPLACE FUNCTION public.get_jsonapi_user (
|
74
|
+
) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_user' LANGUAGE C;
|
75
|
+
|
76
|
+
CREATE OR REPLACE FUNCTION public.get_jsonapi_company (
|
77
|
+
) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_company' LANGUAGE C;
|
78
|
+
|
79
|
+
CREATE OR REPLACE FUNCTION public.get_jsonapi_company_schema (
|
80
|
+
) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_company_schema' LANGUAGE C;
|
81
|
+
|
82
|
+
CREATE OR REPLACE FUNCTION public.get_jsonapi_sharded_schema (
|
83
|
+
) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_sharded_schema' LANGUAGE C;
|
84
|
+
|
85
|
+
CREATE OR REPLACE FUNCTION public.get_jsonapi_accounting_schema (
|
86
|
+
) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_accounting_schema' LANGUAGE C;
|
87
|
+
|
88
|
+
CREATE OR REPLACE FUNCTION public.get_jsonapi_accounting_prefix (
|
89
|
+
) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_accounting_prefix' LANGUAGE C;
|
90
|
+
|
91
|
+
]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
data/lib/tasks/configure.rake
CHANGED
@@ -1,502 +1,8 @@
|
|
1
|
-
# require 'byebug'
|
2
|
-
require 'json'
|
3
|
-
require 'erb'
|
4
|
-
require 'ostruct'
|
5
|
-
require 'awesome_print'
|
6
|
-
require 'os'
|
7
|
-
require 'fileutils'
|
8
|
-
require 'tempfile'
|
9
|
-
require 'etc'
|
10
1
|
|
11
|
-
class SpDataStruct < OpenStruct
|
12
2
|
|
13
|
-
def self.to_hash_sp (object)
|
14
|
-
hash = {}
|
15
|
-
object.each_pair do |key, value|
|
16
|
-
if value.is_a?(SpDataStruct)
|
17
|
-
hash[key] = SpDataStruct::to_hash_sp(value)
|
18
|
-
elsif value.is_a?(Array)
|
19
|
-
hash[key] = []
|
20
|
-
value.each do |member|
|
21
|
-
if member.is_a?(SpDataStruct)
|
22
|
-
hash[key] << SpDataStruct::to_hash_sp(member)
|
23
|
-
else
|
24
|
-
hash[key] << member
|
25
|
-
end
|
26
|
-
end
|
27
|
-
else
|
28
|
-
hash[key] = value
|
29
|
-
end
|
30
|
-
end
|
31
|
-
hash
|
32
|
-
end
|
33
|
-
|
34
|
-
def to_json
|
35
|
-
SpDataStruct::to_hash_sp(self).to_json
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def safesudo(cmd)
|
41
|
-
unless true == system(cmd)
|
42
|
-
system("sudo #{cmd}")
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def create_directory (path)
|
47
|
-
|
48
|
-
if ! Dir.exist?(path)
|
49
|
-
if OS.mac?
|
50
|
-
if path.match("^/usr/local/")
|
51
|
-
info = Etc.getpwnam(Etc.getlogin)
|
52
|
-
puts "\t* Creating '#{path}'...".yellow
|
53
|
-
safesudo("mkdir -p #{path}")
|
54
|
-
if 0 != $?.exitstatus
|
55
|
-
puts "\t* Failed to create #{path}".red
|
56
|
-
end
|
57
|
-
next_parent_path = File.join("/usr/local", path.split(File::SEPARATOR).map {|x| x=="" ? File::SEPARATOR : x}[1..-1][2])
|
58
|
-
if ! next_parent_path
|
59
|
-
throw "Unable to create path #{path} - parent not found!"
|
60
|
-
end
|
61
|
-
safesudo("chown -R #{info.name}:#{Etc.getgrgid(info.gid).name} #{next_parent_path}")
|
62
|
-
if 0 != $?.exitstatus
|
63
|
-
puts "\t* Failed to change ownership to #{path}".red
|
64
|
-
end
|
65
|
-
else
|
66
|
-
safesudo("mkdir -p #{path}")
|
67
|
-
if 0 != $?.exitstatus
|
68
|
-
puts "\t* Failed to create #{path}".red
|
69
|
-
end
|
70
|
-
end
|
71
|
-
else
|
72
|
-
if path.match("^/home/")
|
73
|
-
safesudo("mkdir -p #{path}")
|
74
|
-
else
|
75
|
-
safesudo("mkdir -p #{path}")
|
76
|
-
end
|
77
|
-
if 0 != $?.exitstatus
|
78
|
-
puts "\t* Failed to create #{path}".red
|
79
|
-
end
|
80
|
-
end
|
81
|
-
if ! OS.mac? && !path.match("^/home/")
|
82
|
-
safesudo("chown #{$user}:#{$group} #{path}")
|
83
|
-
else
|
84
|
-
safesudo("chown #{$user}:#{$group} #{path}")
|
85
|
-
end
|
86
|
-
if 0 != $?.exitstatus
|
87
|
-
puts "\t* Failed to change ownership to #{path}".red
|
88
|
-
end
|
89
|
-
if ! OS.mac? && !path.match("^/home/")
|
90
|
-
safesudo("chmod 755 #{path}")
|
91
|
-
else
|
92
|
-
safesudo("chmod 755 #{path}")
|
93
|
-
end
|
94
|
-
if 0 != $?.exitstatus
|
95
|
-
puts "\t* Failed to change permissions to #{path}".red
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
def diff_and_write (contents:, path:, diff: true, dry_run: false)
|
102
|
-
|
103
|
-
if OS.mac?
|
104
|
-
create_directory File.dirname path
|
105
|
-
end
|
106
|
-
|
107
|
-
if ! File.exists?(path)
|
108
|
-
if contents.length == 0
|
109
|
-
puts "\t* #{path} does not exist and it's empty, ignored".green
|
110
|
-
return
|
111
|
-
else
|
112
|
-
safesudo("touch #{path}")
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
if true == diff
|
117
|
-
tmp_file = Tempfile.new File.basename path
|
118
|
-
FileUtils::mkdir_p File.dirname tmp_file
|
119
|
-
File.write(tmp_file,contents)
|
120
|
-
diff_contents = %x[diff -u #{path} #{tmp_file.path} 2>/dev/null]
|
121
|
-
if 0 == $?.exitstatus
|
122
|
-
puts "\t* #{path} not changed".green
|
123
|
-
return
|
124
|
-
end
|
125
|
-
if File.exists?(path)
|
126
|
-
puts "\t* #{path} changed:".red
|
127
|
-
puts diff_contents
|
128
|
-
else
|
129
|
-
puts "\t* #{path} does not exist. Will be created".blue
|
130
|
-
end
|
131
|
-
|
132
|
-
end
|
133
|
-
puts "\t* Writing #{path}".green
|
134
|
-
unless dry_run
|
135
|
-
if OS.mac? || File.writable?(path) || path.match("^/home/")
|
136
|
-
File.write(path, contents)
|
137
|
-
else
|
138
|
-
safesudo("chown #{$user}:#{$group} #{path}")
|
139
|
-
File.write(path, contents)
|
140
|
-
safesudo("chown root:root #{path}")
|
141
|
-
end
|
142
|
-
end
|
143
|
-
FileUtils.rm(tmp_file)
|
144
|
-
end
|
145
|
-
|
146
|
-
def pg_conn_string (db)
|
147
|
-
"host=#{db.host} port=#{db.port} dbname=#{db.dbname} user=#{db.user}#{db.password.size != 0 ? ' password='+ db.password : '' }"
|
148
|
-
end
|
149
|
-
|
150
|
-
def expand_template (template, pretty_json: false)
|
151
|
-
begin
|
152
|
-
contents = ERB.new(File.read(template), nil, '-').result()
|
153
|
-
if pretty_json
|
154
|
-
JSON.pretty_generate(JSON.parse(contents))
|
155
|
-
else
|
156
|
-
contents
|
157
|
-
end
|
158
|
-
rescue Exception => e
|
159
|
-
puts "Expansion of #{template} failed".yellow
|
160
|
-
puts e.message.red
|
161
|
-
exit
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
def get_config
|
166
|
-
hostname = %x[hostname -s].strip
|
167
|
-
@project = Dir.pwd
|
168
|
-
@user_home = File.expand_path('~')
|
169
|
-
|
170
|
-
#
|
171
|
-
# Pick file named 'hostname', or use 'developer' as basefile
|
172
|
-
#
|
173
|
-
if File.exists?("#{@project}/configure/#{hostname}.yml")
|
174
|
-
conf = YAML.load_file("#{@project}/configure/#{hostname}.yml")
|
175
|
-
conf['file_name'] = hostname
|
176
|
-
else
|
177
|
-
conf = YAML.load_file("#{@project}/configure/developer.yml")
|
178
|
-
conf['file_name'] = 'developer'
|
179
|
-
end
|
180
|
-
|
181
|
-
#
|
182
|
-
# Follow configuration dependencies and merge the configurations
|
183
|
-
#
|
184
|
-
configs = [ conf ]
|
185
|
-
loop do
|
186
|
-
break if conf['extends'].nil?
|
187
|
-
ancestor = conf['extends']
|
188
|
-
conf = YAML.load_file("#{@project}/configure/#{ancestor}.yml")
|
189
|
-
conf['file_name'] = ancestor || 'developer'
|
190
|
-
configs << conf
|
191
|
-
end
|
192
|
-
|
193
|
-
(configs.size - 2).downto(0).each do |i|
|
194
|
-
puts "Step #{i}: merging '#{configs[i]['file_name']}' with '#{configs[i+1]['file_name']}'"
|
195
|
-
configs[i].config_merge(configs[i+1])
|
196
|
-
end
|
197
|
-
|
198
|
-
conf = configs[0]
|
199
|
-
|
200
|
-
#
|
201
|
-
# Allow overide of project directory
|
202
|
-
#
|
203
|
-
conf['paths']['project'] ||= @project
|
204
|
-
|
205
|
-
#
|
206
|
-
# Resolve user and group if needed
|
207
|
-
#
|
208
|
-
if conf['user'].nil?
|
209
|
-
conf['user'] = %x[id -u -nr].strip
|
210
|
-
end
|
211
|
-
if conf['group'].nil?
|
212
|
-
conf['group'] = %x[id -g -nr].strip
|
213
|
-
end
|
214
|
-
|
215
|
-
#
|
216
|
-
# Pre-cook the connection string # TODO remove this after Hydra goes live
|
217
|
-
#
|
218
|
-
if conf['db']
|
219
|
-
dbname = conf['db']['dbname']
|
220
|
-
dbuser = conf['db']['user']
|
221
|
-
dbhost = conf['db']['host']
|
222
|
-
dbport = conf['db']['port'] || 5432
|
223
|
-
dbpass = conf['db']['password'] || ''
|
224
|
-
conf['db']['connection_string'] = "host=#{dbhost} port=#{dbport} dbname=#{dbname} user=#{dbuser}#{dbpass.size != 0 ? ' password='+ dbpass : '' }"
|
225
|
-
end
|
226
|
-
|
227
|
-
#
|
228
|
-
# Resolve project and user relative paths
|
229
|
-
#
|
230
|
-
conf['paths'].each do |name, path|
|
231
|
-
if path.start_with? '$project'
|
232
|
-
conf['paths'][name] = path.sub('$project', conf['paths']['project'] || @project)
|
233
|
-
elsif path.start_with? '$user_home'
|
234
|
-
conf['paths'][name] = path.sub('$user_home', @user_home)
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
conf.clean_keys!
|
239
|
-
|
240
|
-
ap conf
|
241
|
-
return JSON.parse(conf.to_json, object_class: SpDataStruct), conf
|
242
|
-
end
|
243
3
|
|
244
4
|
desc 'Update project configuration: action=overwrite => update system,user,project; action => hotfix update project only; other no change (dryrun)'
|
245
5
|
task :configure, [ :action ] do |task, args|
|
246
|
-
|
247
|
-
|
248
|
-
class ::Hash
|
249
|
-
|
250
|
-
def config_merge (second)
|
251
|
-
|
252
|
-
second.each do |skey, sval|
|
253
|
-
if self.has_key?(skey+'!')
|
254
|
-
self[skey] = self[skey+'!']
|
255
|
-
self.delete(skey+'!')
|
256
|
-
next
|
257
|
-
elsif skey[-1] == '!'
|
258
|
-
tkey = skey[0..-2]
|
259
|
-
if self.has_key?(tkey)
|
260
|
-
if Array === self[tkey] && Array === sval
|
261
|
-
self[tkey] = self[tkey] | sval
|
262
|
-
elsif Hash === self[tkey] && Hash === sval
|
263
|
-
self[tkey].config_merge(sval)
|
264
|
-
else
|
265
|
-
raise "Error can't merge #{skey} with different types"
|
266
|
-
end
|
267
|
-
end
|
268
|
-
end
|
269
|
-
|
270
|
-
if ! self.has_key?(skey)
|
271
|
-
self[skey] = sval
|
272
|
-
else
|
273
|
-
if Array === self[skey] && Array === sval
|
274
|
-
self[skey] = self[skey] | sval
|
275
|
-
elsif Hash === self[skey] && Hash === sval
|
276
|
-
self[skey].config_merge(sval)
|
277
|
-
end
|
278
|
-
end
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
def clean_keys!
|
283
|
-
tmp = Hash.new
|
284
|
-
|
285
|
-
self.each do |key, val|
|
286
|
-
if Hash === val
|
287
|
-
val.clean_keys!
|
288
|
-
end
|
289
|
-
|
290
|
-
if key[-1] == '!'
|
291
|
-
tmp[key[0..-2]] = val
|
292
|
-
self.delete(key)
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
self.merge! tmp
|
297
|
-
end
|
298
|
-
|
299
|
-
end
|
300
|
-
|
301
|
-
if args[:action] == 'overwrite'
|
302
|
-
dry_run = false
|
303
|
-
action = 'overwrite'
|
304
|
-
elsif args[:action] == 'hotfix'
|
305
|
-
dry_run = false
|
306
|
-
action = 'hotfix'
|
307
|
-
else
|
308
|
-
dry_run = true
|
309
|
-
action = 'dry-run'
|
310
|
-
end
|
311
|
-
|
312
|
-
#
|
313
|
-
# Read the configuration into ostruct @config will be accessible to the ERB templates
|
314
|
-
#
|
315
|
-
@config, conf = get_config()
|
316
|
-
|
317
|
-
#
|
318
|
-
# Resolve project and user again to create the relative paths
|
319
|
-
#
|
320
|
-
conf['paths'].each do |name, path|
|
321
|
-
if path.start_with? '$project'
|
322
|
-
conf['paths'][name] = path.sub('$project', conf['paths']['project'] || @project)
|
323
|
-
FileUtils.mkdir_p conf['paths'][name]
|
324
|
-
elsif path.start_with? '$user_home'
|
325
|
-
conf['paths'][name] = path.sub('$user_home', @user_home)
|
326
|
-
FileUtils.mkdir_p conf['paths'][name]
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
|
331
|
-
# Set helper variables on the task context
|
332
|
-
$user = @config.user
|
333
|
-
$group = @config.group
|
334
|
-
@project = Dir.pwd
|
335
|
-
@user_home = File.expand_path('~')
|
336
|
-
diff_before_copy = true
|
337
|
-
|
338
|
-
#
|
339
|
-
# Create required paths
|
340
|
-
#
|
341
|
-
if @config.nginx_broker && @config.nginx_broker.nginx && @config.nginx_broker.nginx.paths
|
342
|
-
@config.nginx_broker.nginx.paths.each do |path|
|
343
|
-
if OS.mac? && @config.nginx_broker.nginx.suffix
|
344
|
-
path = path.sub('nginx-broker', "nginx-broker#{@config.nginx_broker.nginx.suffix}")
|
345
|
-
end
|
346
|
-
create_directory "#{@config.prefix}#{path}"
|
347
|
-
end
|
348
|
-
end
|
349
|
-
if @config.nginx_epaper && @config.nginx_epaper.nginx && @config.nginx_epaper.nginx.paths
|
350
|
-
@config.nginx_epaper.nginx.paths.each do |path|
|
351
|
-
create_directory "#{@config.prefix}#{path}"
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
|
-
#
|
356
|
-
# Configure system, projects and user files
|
357
|
-
#
|
358
|
-
locations = {}
|
359
|
-
used_locations = []
|
360
|
-
if action == 'dry-run' || action == 'overwrite'
|
361
|
-
paths = { 'system' => @config.prefix, 'project' => @project, 'user' => @user_home}
|
362
|
-
else
|
363
|
-
paths = { 'project' => @project }
|
364
|
-
end
|
365
|
-
paths.each do |src, dest|
|
366
|
-
puts "Configuring #{src.upcase}"
|
367
|
-
|
368
|
-
# List all .erb files in hidden and visible folders
|
369
|
-
erblist = Dir.glob("#{@project}/configure/#{src}/.**/*.erb") +
|
370
|
-
Dir.glob("#{@project}/configure/#{src}/**/*.erb")
|
371
|
-
|
372
|
-
erblist.each do |template|
|
373
|
-
dst_file = template.sub("#{@project}/configure/#{src}", "#{dest}").sub(/\.erb$/, '')
|
374
|
-
|
375
|
-
# developer exception
|
376
|
-
if OS.mac? && @config.nginx_broker && @config.nginx_broker.nginx.suffix
|
377
|
-
dst_file = dst_file.sub('nginx-broker', "nginx-broker#{@config.nginx_broker.nginx.suffix}")
|
378
|
-
end
|
379
|
-
|
380
|
-
# Nginx Locations must be filtered, only handle locations that are used
|
381
|
-
m = /.*\.location$/.match(dst_file)
|
382
|
-
if m
|
383
|
-
locations[dst_file] = template
|
384
|
-
next
|
385
|
-
end
|
386
|
-
|
387
|
-
# Filter nginx vhosts that do not have and entry, only install the vhosts that have an entry in nginx-xxxxx
|
388
|
-
m = /.*(nginx-broker|nginx-epaper)\/conf\.d\/(.*)\.conf$/.match(dst_file)
|
389
|
-
if m && m.size == 3
|
390
|
-
key_l1 = m[1].gsub('-', '_')
|
391
|
-
if conf[key_l1].nil? or conf[key_l1][m[2]].nil?
|
392
|
-
puts "Filtered #{m[1]} - #{m[2]} - #{dst_file}"
|
393
|
-
next
|
394
|
-
end
|
395
|
-
end
|
396
|
-
# do not touch config files on top folder if that nginx is not requested
|
397
|
-
m = /.*(nginx-broker|nginx-epaper)\/(.*)$/.match(dst_file)
|
398
|
-
if m && m.size == 3
|
399
|
-
key_l1 = m[1].gsub('-', '_')
|
400
|
-
if conf[key_l1].nil?
|
401
|
-
puts "Filtered #{m[1]} - #{m[2]} - #{dst_file}"
|
402
|
-
next
|
403
|
-
end
|
404
|
-
end
|
405
|
-
|
406
|
-
# 2nd filtered
|
407
|
-
if @config.erb_exclusion_list
|
408
|
-
base_filename = File.basename(dst_file)
|
409
|
-
if @config.erb_exclusion_list.include?(base_filename)
|
410
|
-
puts "Filtered #{base_filename}".yellow
|
411
|
-
next
|
412
|
-
end
|
413
|
-
end
|
414
|
-
|
415
|
-
# Now expand the template
|
416
|
-
file_contents = expand_template(template)
|
417
|
-
|
418
|
-
if /.*(nginx-broker|nginx-epaper)\/conf\.d\/(.*)\.conf$/.match(dst_file)
|
419
|
-
includes = file_contents.scan(/^\s*include\s+conf\.d\/(.*)\.location\;/)
|
420
|
-
includes.each do |loc|
|
421
|
-
used_locations << loc[0]
|
422
|
-
end
|
423
|
-
end
|
424
|
-
|
425
|
-
# Write text expanded configuration file
|
426
|
-
create_directory(File.dirname dst_file)
|
427
|
-
diff_and_write(contents: file_contents,
|
428
|
-
path: dst_file,
|
429
|
-
diff: diff_before_copy,
|
430
|
-
dry_run: dry_run
|
431
|
-
)
|
432
|
-
end
|
433
|
-
end
|
434
|
-
|
435
|
-
#
|
436
|
-
# configure the nginx locations that are used
|
437
|
-
#
|
438
|
-
if action == 'dry-run' || action == 'overwrite'
|
439
|
-
if used_locations.size
|
440
|
-
puts "Configuring NGINX LOCATIONS"
|
441
|
-
locations.each do |dst_file, template|
|
442
|
-
m = /.*\/(.*).location$/.match dst_file
|
443
|
-
if used_locations.include? m[1]
|
444
|
-
# Write text expanded configuration file
|
445
|
-
create_directory(File.dirname dst_file)
|
446
|
-
diff_and_write(contents: expand_template(template),
|
447
|
-
path: dst_file,
|
448
|
-
diff: diff_before_copy,
|
449
|
-
dry_run: dry_run
|
450
|
-
)
|
451
|
-
end
|
452
|
-
end
|
453
|
-
end
|
454
|
-
end
|
455
|
-
|
456
|
-
#
|
457
|
-
# Configure JOBS
|
458
|
-
#
|
459
|
-
if action == 'dry-run' || action == 'overwrite'
|
460
|
-
puts "Configuring JOBS"
|
461
|
-
@config.jobs.to_h.each do |name, job|
|
462
|
-
@job_name = name
|
463
|
-
@job_description = "TODO Description"
|
464
|
-
@job_dir = "#{@config.paths.working_directory}/jobs/#{@job_name}"
|
465
|
-
|
466
|
-
puts " #{name}:"
|
467
|
-
if File.exists? "#{@job_dir}/conf.json.erb"
|
468
|
-
template = "#{@job_dir}/conf.json.erb"
|
469
|
-
else
|
470
|
-
template = "#{@config.paths.working_directory}/jobs/default_conf.json.erb"
|
471
|
-
end
|
472
|
-
unless File.exists? template
|
473
|
-
throw "Missing #{template} => configuration file for #{@job_name}"
|
474
|
-
end
|
475
|
-
if OS.mac?
|
476
|
-
create_directory("/usr/local/var/lock/#{@job_name}/")
|
477
|
-
end
|
478
|
-
create_directory "#{@config.prefix}/etc/#{@job_name}"
|
479
|
-
create_directory "#{@config.prefix}/var/log/#{@job_name}"
|
480
|
-
diff_and_write(contents: expand_template(template, pretty_json: true),
|
481
|
-
path: "#{@config.prefix}/etc/#{@job_name}/conf.json",
|
482
|
-
diff: diff_before_copy,
|
483
|
-
dry_run: dry_run
|
484
|
-
)
|
485
|
-
|
486
|
-
if File.exists? "#{@job_dir}/service.erb"
|
487
|
-
template = "#{@job_dir}/service.erb"
|
488
|
-
else
|
489
|
-
template = "#{@config.paths.working_directory}/jobs/default.service.erb"
|
490
|
-
end
|
491
|
-
unless File.exists? template
|
492
|
-
throw "Missing service file for #{@job_name}"
|
493
|
-
end
|
494
|
-
|
495
|
-
diff_and_write(contents: expand_template(template),
|
496
|
-
path: "#{@config.prefix}/lib/systemd/system/#{@job_name}@.service",
|
497
|
-
diff: diff_before_copy,
|
498
|
-
dry_run: dry_run
|
499
|
-
)
|
500
|
-
end
|
501
|
-
end
|
6
|
+
require_relative '../sp/job/configure/configure'
|
7
|
+
run_configure(args)
|
502
8
|
end
|