forj 0.0.39 → 0.0.40
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.
- checksums.yaml +4 -4
- data/Gemfile +11 -2
- data/README.md +30 -16
- data/Rakefile +6 -1
- data/bin/forj +113 -54
- data/lib/boot.rb +37 -36
- data/lib/connection.rb +3 -3
- data/lib/defaults.yaml +5 -3
- data/lib/forj-account.rb +454 -0
- data/lib/forj-config.rb +181 -132
- data/lib/log.rb +6 -5
- data/lib/network.rb +4 -4
- data/lib/repositories.rb +1 -1
- data/lib/security.rb +55 -26
- data/lib/setup.rb +7 -291
- data/spec/connection_spec.rb +5 -1
- data/spec/forj-config_spec.rb +121 -19
- metadata +3 -2
data/lib/forj-config.rb
CHANGED
@@ -29,8 +29,6 @@ class ForjDefault
|
|
29
29
|
# @sDefaultsName='defaults.yaml'
|
30
30
|
# @yDefaults = defaults.yaml file data hash
|
31
31
|
|
32
|
-
attr_reader :yDefaults
|
33
|
-
|
34
32
|
def initialize()
|
35
33
|
# Load yaml documents (defaults)
|
36
34
|
# If config doesn't exist, it will be created, empty with 'defaults:' only
|
@@ -46,23 +44,71 @@ class ForjDefault
|
|
46
44
|
@yDefaults=YAML.load_file(@sDefaultsName)
|
47
45
|
end
|
48
46
|
|
47
|
+
def exist?(key, section = 'default')
|
48
|
+
(rhExist?(@yDefaults, section, key) == 2)
|
49
|
+
end
|
50
|
+
|
51
|
+
def get(key, section = 'default')
|
52
|
+
rhGet(@yDefaults, section, key)
|
53
|
+
end
|
54
|
+
|
55
|
+
def dump()
|
56
|
+
@yDefaults
|
57
|
+
end
|
49
58
|
end
|
50
59
|
|
51
60
|
class ForjConfig
|
52
61
|
|
53
|
-
# Internal variables:
|
54
|
-
# @sConfigName='config.yaml'
|
55
|
-
# @yRuntime
|
56
|
-
# @yLocal
|
57
|
-
# @
|
58
|
-
|
59
|
-
|
60
|
-
attr_reader
|
61
|
-
|
62
|
+
# Internal Object variables:
|
63
|
+
# @sConfigName= 'config.yaml'
|
64
|
+
# @yRuntime = data in memory.
|
65
|
+
# @yLocal = config.yaml file data hash.
|
66
|
+
# @yObjConfig = Extra loaded data
|
67
|
+
# @oDefaults = Application defaults object
|
68
|
+
|
69
|
+
attr_reader :sConfigName
|
70
|
+
|
71
|
+
# Load yaml documents (defaults + config)
|
72
|
+
# If config doesn't exist, it will be created, empty with 'defaults:' only
|
73
|
+
|
74
|
+
def default_dump(interms = nil)
|
75
|
+
# Build a config hash.
|
76
|
+
res = {}
|
77
|
+
@oDefaults.dump['default'].each_key { |key|
|
78
|
+
dump_key = exist?(key)
|
79
|
+
rhSet(res, get(key), dump_key, key)
|
80
|
+
}
|
81
|
+
if rhExist?(@yLocal, 'default') == 1
|
82
|
+
@yLocal['default'].each_key { |key|
|
83
|
+
dump_key = exist?(key)
|
84
|
+
rhSet(res, get(key), dump_key, key) if rhExist?(res, dump_key, key) != 2
|
85
|
+
}
|
86
|
+
end
|
87
|
+
if interms
|
88
|
+
if interms.instance_of? Hash
|
89
|
+
@interms.each_key { | key|
|
90
|
+
dump_key = exist?(key)
|
91
|
+
rhSet(res, get(key), dump_key, key) if rhExist?(res, dump_key, key) != 2
|
92
|
+
}
|
93
|
+
elsif interms.instance_of? Array # Array of hash
|
94
|
+
iCount=0
|
95
|
+
interms.each { | elem |
|
96
|
+
elem.each_key { | key|
|
97
|
+
dump_key = exist?(key)
|
98
|
+
rhSet(res, get(key), dump_key, key) if rhExist?(res, dump_key, key) != 2
|
99
|
+
}
|
100
|
+
}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
@yRuntime.each_key { |key|
|
104
|
+
dump_key = exist?(key)
|
105
|
+
rhSet(res, get(key), dump_key, key) if rhExist?(res, dump_key, key) != 2
|
106
|
+
}
|
107
|
+
|
108
|
+
res
|
109
|
+
end
|
62
110
|
|
63
111
|
def initialize(sConfigName=nil)
|
64
|
-
# Load yaml documents (defaults + config)
|
65
|
-
# If config doesn't exist, it will be created, empty with 'defaults:' only
|
66
112
|
|
67
113
|
if not $FORJ_DATA_PATH
|
68
114
|
Logging.fatal(1, 'Internal $FORJ_DATA_PATH was not set.')
|
@@ -72,33 +118,32 @@ class ForjConfig
|
|
72
118
|
|
73
119
|
if sConfigName
|
74
120
|
if File.dirname(sConfigName) == '.'
|
75
|
-
sConfigName= File.join($FORJ_DATA_PATH,sConfigName)
|
121
|
+
sConfigName = File.join($FORJ_DATA_PATH,sConfigName)
|
76
122
|
end
|
77
123
|
sConfigName = File.expand_path(sConfigName)
|
78
124
|
if not File.exists?(sConfigName)
|
79
125
|
Logging.warning("Config file '%s' doesn't exists. Using default one." % [sConfigName] )
|
80
|
-
@sConfigName=File.join($FORJ_DATA_PATH,sConfigDefaultName)
|
126
|
+
@sConfigName = File.join($FORJ_DATA_PATH,sConfigDefaultName)
|
81
127
|
else
|
82
|
-
@sConfigName=sConfigName
|
128
|
+
@sConfigName = sConfigName
|
83
129
|
end
|
84
130
|
else
|
85
|
-
@sConfigName=File.join($FORJ_DATA_PATH,sConfigDefaultName)
|
131
|
+
@sConfigName = File.join($FORJ_DATA_PATH,sConfigDefaultName)
|
86
132
|
end
|
87
133
|
|
88
|
-
@
|
134
|
+
@oDefaults = ForjDefault.new
|
89
135
|
|
90
136
|
if File.exists?(@sConfigName)
|
91
|
-
@yLocal=YAML.load_file(@sConfigName)
|
137
|
+
@yLocal = YAML.load_file(@sConfigName)
|
92
138
|
else
|
93
|
-
@yLocal={ 'default' => nil }
|
139
|
+
@yLocal = { 'default' => nil }
|
94
140
|
# Write the empty file
|
95
|
-
Logging.info
|
141
|
+
Logging.info('Creating your default configuration file ...')
|
96
142
|
self.SaveConfig()
|
97
143
|
end
|
98
144
|
|
99
|
-
|
100
|
-
|
101
|
-
@yRuntime={}
|
145
|
+
@yRuntime = {}
|
146
|
+
@yObjConfig = {}
|
102
147
|
end
|
103
148
|
|
104
149
|
def SaveConfig()
|
@@ -115,7 +160,7 @@ class ForjConfig
|
|
115
160
|
end
|
116
161
|
|
117
162
|
def ExtraSave(sFile, section, name)
|
118
|
-
hVal = rhGet(@
|
163
|
+
hVal = rhGet(@yObjConfig, section, name)
|
119
164
|
if hVal
|
120
165
|
begin
|
121
166
|
File.open(sFile, 'w') do |out|
|
@@ -133,148 +178,152 @@ class ForjConfig
|
|
133
178
|
def ExtraLoad(sFile, section, name)
|
134
179
|
if File.exists?(sFile)
|
135
180
|
hVal = YAML.load_file(sFile)
|
136
|
-
rhSet(@
|
181
|
+
rhSet(@yObjConfig, hVal, section, name)
|
182
|
+
hVal
|
137
183
|
end
|
138
|
-
BuildConfig()
|
139
|
-
end
|
140
|
-
|
141
|
-
def LocalSet(key, value, section = 'default')
|
142
|
-
if not key or not value
|
143
|
-
return false
|
144
|
-
end
|
145
|
-
if @yLocal[section] == nil
|
146
|
-
@yLocal[section]={}
|
147
|
-
end
|
148
|
-
if @yLocal.has_key?(section)
|
149
|
-
@yLocal[section].merge!({key => value})
|
150
|
-
else
|
151
|
-
@yLocal.merge!(section => {key => value})
|
152
|
-
end
|
153
|
-
BuildConfig()
|
154
|
-
return true
|
155
184
|
end
|
156
185
|
|
157
|
-
def
|
158
|
-
if
|
159
|
-
return @yLocal[key]
|
160
|
-
end
|
161
|
-
default
|
162
|
-
end
|
186
|
+
def ExtraExist?(section, name, key = nil)
|
187
|
+
return nil if not section or not name
|
163
188
|
|
164
|
-
|
165
|
-
|
166
|
-
return false
|
167
|
-
end
|
168
|
-
if not @yLocal.has_key?(section)
|
169
|
-
return false
|
170
|
-
end
|
171
|
-
@yLocal[section].delete(key)
|
172
|
-
BuildConfig()
|
173
|
-
return true
|
189
|
+
return(rhExist?(@yObjConfig, section, name) == 2) if not key
|
190
|
+
return(rhExist?(@yObjConfig, section, name, key) == 3)
|
174
191
|
end
|
175
192
|
|
176
|
-
def
|
193
|
+
def ExtraGet(section, name, key = nil, default = nil)
|
177
194
|
return nil if not section or not name
|
178
|
-
|
179
|
-
return
|
180
|
-
return(
|
195
|
+
|
196
|
+
return default unless ExtraExist?(section, name, key)
|
197
|
+
return rhGet(@yObjConfig, section, name, key) if key
|
198
|
+
rhGet(@yObjConfig, section, name)
|
181
199
|
end
|
182
200
|
|
183
|
-
def
|
184
|
-
return nil if not section or not name
|
185
|
-
|
201
|
+
def ExtraSet(section, name, key = nil, value)
|
186
202
|
if key
|
187
|
-
|
188
|
-
rhGet(@yConfig, :extra_loaded, section, name, key)
|
203
|
+
rhSet(@yObjConfig, value, section, name, key)
|
189
204
|
else
|
190
|
-
|
191
|
-
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
def ExtraSet(section, name, key, value)
|
196
|
-
rhSet(@yConfig, value, :extra_loaded, section, name, key)
|
205
|
+
rhSet(@yObjConfig, value, section, name)
|
206
|
+
end
|
197
207
|
end
|
198
208
|
|
199
|
-
def set(key, value
|
200
|
-
# Function to set a
|
209
|
+
def set(key, value)
|
210
|
+
# Function to set a runtime key/value, but remove it if value is nil.
|
211
|
+
# To set in config.yaml, use LocalSet
|
212
|
+
# To set on extra data, like account information, use ExtraSet
|
201
213
|
if not key
|
202
214
|
return false
|
203
215
|
end
|
204
|
-
if
|
205
|
-
|
206
|
-
ExtraSet(par[:section], par[:name], key, value)
|
207
|
-
elsif par[:section]
|
208
|
-
# To set key=value on config.yaml, use LocalSet
|
209
|
-
if value
|
210
|
-
rhSet(@yRuntime, value, par[:section], key)
|
211
|
-
else
|
212
|
-
hVal = rhGet(@yRuntime, par[:section])
|
213
|
-
hVal.delete(key)
|
214
|
-
end
|
216
|
+
if value
|
217
|
+
rhSet(@yRuntime, value, key)
|
215
218
|
else
|
216
|
-
|
217
|
-
rhSet(@yRuntime, value, key)
|
218
|
-
else
|
219
|
-
@yRuntime.delete(key)
|
220
|
-
end
|
219
|
+
@yRuntime.delete(key)
|
221
220
|
end
|
222
221
|
true
|
223
222
|
end
|
224
223
|
|
225
|
-
def get(key,
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
224
|
+
def get(key, interms = nil, default = nil)
|
225
|
+
# If key is in runtime
|
226
|
+
return rhGet(@yRuntime, key) if rhExist?(@yRuntime, key) == 1
|
227
|
+
# Check data in intermediate hashes or array of hash. (like account data - key have to be identical)
|
228
|
+
if interms
|
229
|
+
if interms.instance_of? Hash
|
230
|
+
return rhGet(interms, key) if rhExist?(interms, key) == 1
|
231
|
+
elsif interms.instance_of? Array # Array of hash
|
232
|
+
iCount=0
|
233
|
+
interms.each { | elem |
|
234
|
+
if elem.class == Hash
|
235
|
+
oVal = nil
|
236
|
+
elem.each { | hashkey, value |
|
237
|
+
if value.class == Hash and rhExist?(elem, hashkey, key) == 2
|
238
|
+
oVal = rhGet?(elem, hashkey, key)
|
239
|
+
break
|
240
|
+
end
|
241
|
+
}
|
242
|
+
break if oVal
|
243
|
+
end
|
244
|
+
iCount += 1
|
245
|
+
}
|
246
|
+
return oVal
|
247
|
+
end
|
237
248
|
end
|
238
|
-
# else key in default config of default section.
|
239
|
-
return
|
249
|
+
# else key in local default config of default section.
|
250
|
+
return LocalGet(key) if LocalDefaultExist?(key)
|
251
|
+
# else key in application defaults
|
252
|
+
return @oDefaults.get(key) if @oDefaults.exist?(key)
|
240
253
|
# else default
|
241
|
-
|
254
|
+
default
|
242
255
|
end
|
243
256
|
|
244
|
-
def
|
245
|
-
|
246
|
-
# section/name/key exist in extra_loaded ?
|
247
|
-
return ExtraExist?(par[:section], par[:name], key)
|
248
|
-
elsif par[:section]
|
249
|
-
# section/key exist in runtime?
|
250
|
-
return "runtime" if rhExist?(@yRuntime, par[:section], key) == 2
|
251
|
-
# section/key exist in default config ?
|
252
|
-
return "default" if rhExist?(@yConfig, par[:section], key) == 2
|
253
|
-
else
|
254
|
-
return "runtime" if rhExist?(@yRuntime, key) == 1
|
255
|
-
return "default" if rhExist?(@yRuntime, 'default', key) == 2
|
256
|
-
end
|
257
|
-
false
|
257
|
+
def getAppDefault(section, key)
|
258
|
+
@oDefaults.get(key, section)
|
258
259
|
end
|
259
260
|
|
260
|
-
def
|
261
|
-
#
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
261
|
+
def exist?(key, interms = nil)
|
262
|
+
# Check data in intermediate hashes or array of hash. (like account data - key have to be identical)
|
263
|
+
return "runtime" if rhExist?(@yRuntime, key) == 1
|
264
|
+
if interms
|
265
|
+
if interms.instance_of? Hash
|
266
|
+
return 'hash' if rhExist?(interms, key) == 1
|
267
|
+
elsif interms.instance_of? Array # Array of hash
|
268
|
+
iCount = 0
|
269
|
+
Array.each { | elem |
|
270
|
+
if elem.class == Hash
|
271
|
+
elem.each { | hashkey, value |
|
272
|
+
return ("%s" % hashkey) if value.class == Hash and rhExist?(elem, hashkey, key) == 2
|
273
|
+
}
|
274
|
+
end
|
275
|
+
iCount += 1
|
276
|
+
}
|
277
|
+
end
|
267
278
|
end
|
268
|
-
|
279
|
+
return 'local' if LocalDefaultExist?(key)
|
280
|
+
# else key in application defaults
|
281
|
+
return 'default' if @oDefaults.exist?(key)
|
282
|
+
false
|
269
283
|
end
|
270
284
|
|
271
285
|
def LocalDefaultExist?(key)
|
272
|
-
|
286
|
+
LocalExist?(key)
|
287
|
+
end
|
288
|
+
|
289
|
+
def LocalExist?(key, section = 'default')
|
290
|
+
return true if rhExist?(@yLocal, section, key) == 2
|
273
291
|
false
|
274
292
|
end
|
275
293
|
|
294
|
+
def LocalSet(key, value, section = 'default')
|
295
|
+
if not key or not value
|
296
|
+
return false
|
297
|
+
end
|
298
|
+
if @yLocal[section] == nil
|
299
|
+
@yLocal[section]={}
|
300
|
+
end
|
301
|
+
if @yLocal.has_key?(section)
|
302
|
+
@yLocal[section].merge!({key => value})
|
303
|
+
else
|
304
|
+
@yLocal.merge!(section => {key => value})
|
305
|
+
end
|
306
|
+
return true
|
307
|
+
end
|
308
|
+
|
309
|
+
def LocalGet(key, section = 'default', default = nil)
|
310
|
+
return default if rhExist?(@yLocal, section, key) != 2
|
311
|
+
rhGet(@yLocal, section, key)
|
312
|
+
end
|
313
|
+
|
314
|
+
def LocalDel(key, section = 'default')
|
315
|
+
if not key
|
316
|
+
return false
|
317
|
+
end
|
318
|
+
if not @yLocal.has_key?(section)
|
319
|
+
return false
|
320
|
+
end
|
321
|
+
@yLocal[section].delete(key)
|
322
|
+
return true
|
323
|
+
end
|
324
|
+
|
325
|
+
# Function to return in fatal error if a config data is nil. Help to control function requirement.
|
276
326
|
def fatal_if_inexistent(key)
|
277
|
-
# Function to return in fatal error if a config data is nil. Help to control function requirement.
|
278
327
|
Logging.fatal(1, "Internal error - %s: '%s' is missing" % [caller(), key]) if not self.get(key)
|
279
328
|
end
|
280
329
|
end
|
data/lib/log.rb
CHANGED
@@ -111,9 +111,10 @@ module Logging
|
|
111
111
|
@oOutLogger.error(message + ANSI.clear_line)
|
112
112
|
@oFileLogger.error(message)
|
113
113
|
end
|
114
|
-
def fatal(message)
|
114
|
+
def fatal(message, e)
|
115
115
|
@oOutLogger.fatal(message + ANSI.clear_line)
|
116
|
-
@oFileLogger.fatal(message)
|
116
|
+
@oFileLogger.fatal("%s\n%s\n%s" % [message, e.message, e.backtrace.join("\n")]) if e
|
117
|
+
@oFileLogger.fatal(message)
|
117
118
|
end
|
118
119
|
|
119
120
|
def warn(message)
|
@@ -152,8 +153,8 @@ module Logging
|
|
152
153
|
$FORJ_LOGGER.error(message)
|
153
154
|
end
|
154
155
|
|
155
|
-
def fatal(rc, message)
|
156
|
-
$FORJ_LOGGER.fatal(message)
|
156
|
+
def fatal(rc, message, e = nil)
|
157
|
+
$FORJ_LOGGER.fatal(message, e)
|
157
158
|
puts 'Issues found. Please fix it and retry. Process aborted.'
|
158
159
|
exit rc
|
159
160
|
end
|
@@ -163,7 +164,7 @@ module Logging
|
|
163
164
|
end
|
164
165
|
|
165
166
|
def state(message)
|
166
|
-
print("%s
|
167
|
+
print("%s ...%s\r" % [message, ANSI.clear_line]) if $FORJ_LOGGER.level == Logger::INFO
|
167
168
|
end
|
168
169
|
|
169
170
|
def high_level_msg(message)
|
data/lib/network.rb
CHANGED
@@ -60,7 +60,7 @@ module Network
|
|
60
60
|
Logging.debug('creating network %s' % [name])
|
61
61
|
oFC.oNetwork.networks.create(:name => name)
|
62
62
|
rescue => e
|
63
|
-
Logging.fatal(1, "%s
|
63
|
+
Logging.fatal(1, "Unable to create '%s'network" % name, e)
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -116,7 +116,7 @@ module Network
|
|
116
116
|
:ip_version => '4'
|
117
117
|
)
|
118
118
|
rescue => e
|
119
|
-
Logging.fatal(1, "%s
|
119
|
+
Logging.fatal(1, "Unable to create '%s' subnet." % name, e)
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
@@ -211,7 +211,7 @@ module Network
|
|
211
211
|
)
|
212
212
|
end
|
213
213
|
rescue => e
|
214
|
-
Logging.fatal(1, "%s
|
214
|
+
Logging.fatal(1, "Unable to create '%s' router" % name, e)
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
@@ -220,7 +220,7 @@ module Network
|
|
220
220
|
begin
|
221
221
|
oFC.oNetwork.routers.get(router.id).destroy
|
222
222
|
rescue => e
|
223
|
-
Logging.error("%s
|
223
|
+
Logging.error("Unable to delete '%s' router ID" % router_id, e)
|
224
224
|
end
|
225
225
|
end
|
226
226
|
|