k2hr3-api 1.0.41 → 2.0.0

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.
Files changed (149) hide show
  1. package/config/k2hr3-init.sh.templ +2 -2
  2. package/dist/.gitkeep +0 -0
  3. package/dist/src/app.js +262 -0
  4. package/{bin → dist/src/bin}/run.sh +1 -1
  5. package/dist/src/bin/watcher.js +113 -0
  6. package/dist/src/bin/www.js +217 -0
  7. package/dist/src/lib/basicipcheck.js +392 -0
  8. package/dist/src/lib/cacerts.js +106 -0
  9. package/dist/src/lib/dbglogging.js +190 -0
  10. package/dist/src/lib/dummyuserapi.js +719 -0
  11. package/dist/src/lib/ipwatch.js +354 -0
  12. package/dist/src/lib/k2hr3acrutil.js +532 -0
  13. package/dist/src/lib/k2hr3apiutil.js +1444 -0
  14. package/dist/src/lib/k2hr3cliutil.js +183 -0
  15. package/dist/src/lib/k2hr3config.js +832 -0
  16. package/dist/src/lib/k2hr3cryptutil.js +258 -0
  17. package/dist/src/lib/k2hr3dkc.js +12121 -0
  18. package/dist/src/lib/k2hr3extdata.js +198 -0
  19. package/dist/src/lib/k2hr3keys.js +207 -0
  20. package/dist/src/lib/k2hr3resutil.js +111 -0
  21. package/dist/src/lib/k2hr3template.js +6546 -0
  22. package/dist/src/lib/k2hr3tokens.js +2643 -0
  23. package/dist/src/lib/k2hr3userdata.js +296 -0
  24. package/dist/src/lib/k8soidc.js +1000 -0
  25. package/dist/src/lib/openstackapiv2.js +695 -0
  26. package/dist/src/lib/openstackapiv3.js +932 -0
  27. package/dist/src/lib/openstackep.js +667 -0
  28. package/{tests/auto_common.js → dist/src/lib/types.js} +4 -38
  29. package/dist/src/routes/acr.js +704 -0
  30. package/dist/src/routes/debugVerify.js +294 -0
  31. package/dist/src/routes/extdata.js +219 -0
  32. package/dist/src/routes/list.js +264 -0
  33. package/dist/src/routes/policy.js +840 -0
  34. package/dist/src/routes/resource.js +1489 -0
  35. package/dist/src/routes/role.js +2627 -0
  36. package/dist/src/routes/service.js +908 -0
  37. package/dist/src/routes/tenant.js +1141 -0
  38. package/dist/src/routes/userTokens.js +482 -0
  39. package/dist/src/routes/userdata.js +212 -0
  40. package/dist/src/routes/version.js +103 -0
  41. package/package.json +152 -121
  42. package/ChangeLog +0 -372
  43. package/app.js +0 -292
  44. package/bin/watcher +0 -122
  45. package/bin/www +0 -180
  46. package/eslint.config.mjs +0 -68
  47. package/lib/basicipcheck.js +0 -376
  48. package/lib/cacerts.js +0 -71
  49. package/lib/dbglogging.js +0 -151
  50. package/lib/dummyuserapi.js +0 -766
  51. package/lib/ipwatch.js +0 -379
  52. package/lib/k2hr3acrutil.js +0 -516
  53. package/lib/k2hr3apiutil.js +0 -1494
  54. package/lib/k2hr3cliutil.js +0 -191
  55. package/lib/k2hr3config.js +0 -826
  56. package/lib/k2hr3cryptutil.js +0 -254
  57. package/lib/k2hr3dkc.js +0 -12632
  58. package/lib/k2hr3extdata.js +0 -198
  59. package/lib/k2hr3keys.js +0 -234
  60. package/lib/k2hr3resutil.js +0 -100
  61. package/lib/k2hr3template.js +0 -6925
  62. package/lib/k2hr3tokens.js +0 -2799
  63. package/lib/k2hr3userdata.js +0 -312
  64. package/lib/k8soidc.js +0 -1012
  65. package/lib/openstackapiv2.js +0 -764
  66. package/lib/openstackapiv3.js +0 -1032
  67. package/lib/openstackep.js +0 -553
  68. package/routes/acr.js +0 -738
  69. package/routes/debugVerify.js +0 -263
  70. package/routes/extdata.js +0 -232
  71. package/routes/list.js +0 -270
  72. package/routes/policy.js +0 -869
  73. package/routes/resource.js +0 -1441
  74. package/routes/role.js +0 -2664
  75. package/routes/service.js +0 -894
  76. package/routes/tenant.js +0 -1095
  77. package/routes/userTokens.js +0 -511
  78. package/routes/userdata.js +0 -218
  79. package/routes/version.js +0 -108
  80. package/templ/Dockerfile.templ +0 -71
  81. package/tests/auto_acr.js +0 -1101
  82. package/tests/auto_acr_spec.js +0 -79
  83. package/tests/auto_all_spec.js +0 -142
  84. package/tests/auto_control_subprocess.sh +0 -243
  85. package/tests/auto_extdata.js +0 -220
  86. package/tests/auto_extdata_spec.js +0 -79
  87. package/tests/auto_init_config_json.sh +0 -275
  88. package/tests/auto_k2hdkc_server.ini +0 -109
  89. package/tests/auto_k2hdkc_slave.ini +0 -83
  90. package/tests/auto_list.js +0 -439
  91. package/tests/auto_list_spec.js +0 -79
  92. package/tests/auto_policy.js +0 -1579
  93. package/tests/auto_policy_spec.js +0 -79
  94. package/tests/auto_resource.js +0 -10956
  95. package/tests/auto_resource_spec.js +0 -79
  96. package/tests/auto_role.js +0 -6150
  97. package/tests/auto_role_spec.js +0 -79
  98. package/tests/auto_service.js +0 -770
  99. package/tests/auto_service_spec.js +0 -79
  100. package/tests/auto_subprocesses.js +0 -114
  101. package/tests/auto_template.sh +0 -126
  102. package/tests/auto_tenant.js +0 -1100
  103. package/tests/auto_tenant_spec.js +0 -79
  104. package/tests/auto_token_util.js +0 -219
  105. package/tests/auto_userdata.js +0 -292
  106. package/tests/auto_userdata_spec.js +0 -79
  107. package/tests/auto_usertokens.js +0 -565
  108. package/tests/auto_usertokens_spec.js +0 -79
  109. package/tests/auto_version.js +0 -127
  110. package/tests/auto_version_spec.js +0 -79
  111. package/tests/auto_watcher.js +0 -157
  112. package/tests/auto_watcher_spec.js +0 -79
  113. package/tests/k2hdkc_test.data +0 -986
  114. package/tests/k2hdkc_test_load.sh +0 -255
  115. package/tests/k2hr3template_test.js +0 -187
  116. package/tests/k2hr3template_test.sh +0 -339
  117. package/tests/k2hr3template_test_async.js +0 -216
  118. package/tests/k2hr3template_test_template.result +0 -7117
  119. package/tests/k2hr3template_test_template.txt +0 -3608
  120. package/tests/k2hr3template_test_vars.js +0 -194
  121. package/tests/manual_acr_delete.js +0 -143
  122. package/tests/manual_acr_get.js +0 -297
  123. package/tests/manual_acr_postput.js +0 -215
  124. package/tests/manual_allusertenant_get.js +0 -113
  125. package/tests/manual_extdata_get.js +0 -191
  126. package/tests/manual_k2hr3keys_get.js +0 -84
  127. package/tests/manual_list_gethead.js +0 -230
  128. package/tests/manual_policy_delete.js +0 -132
  129. package/tests/manual_policy_gethead.js +0 -275
  130. package/tests/manual_policy_postput.js +0 -297
  131. package/tests/manual_resource_delete.js +0 -433
  132. package/tests/manual_resource_gethead.js +0 -423
  133. package/tests/manual_resource_postput.js +0 -487
  134. package/tests/manual_role_delete.js +0 -404
  135. package/tests/manual_role_gethead.js +0 -547
  136. package/tests/manual_role_postput.js +0 -544
  137. package/tests/manual_service_delete.js +0 -153
  138. package/tests/manual_service_gethead.js +0 -178
  139. package/tests/manual_service_postput.js +0 -348
  140. package/tests/manual_tenant_delete.js +0 -186
  141. package/tests/manual_tenant_gethead.js +0 -268
  142. package/tests/manual_tenant_postput.js +0 -293
  143. package/tests/manual_test.sh +0 -352
  144. package/tests/manual_userdata_get.js +0 -173
  145. package/tests/manual_usertoken_gethead.js +0 -136
  146. package/tests/manual_usertoken_postput.js +0 -310
  147. package/tests/manual_version_get.js +0 -127
  148. package/tests/run_local_test_k2hdkc.sh +0 -174
  149. package/tests/test.sh +0 -333
@@ -1,339 +0,0 @@
1
- #!/bin/sh
2
- #
3
- # K2HR3 REST API
4
- #
5
- # Copyright 2017 Yahoo Japan Corporation.
6
- #
7
- # K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
8
- # common management information for the cloud.
9
- # K2HR3 can dynamically manage information as "who", "what", "operate".
10
- # These are stored as roles, resources, policies in K2hdkc, and the
11
- # client system can dynamically read and modify these information.
12
- #
13
- # For the full copyright and license information, please view
14
- # the license file that was distributed with this source code.
15
- #
16
- # AUTHOR: Takeshi Nakatani
17
- # CREATE: Wed Jul 12 2017
18
- # REVISION:
19
- #
20
-
21
- #==========================================================
22
- # Common Variables
23
- #==========================================================
24
- PRGNAME=$(basename "$0")
25
- SCRIPTDIR=$(dirname "$0")
26
- SCRIPTDIR=$(cd "${SCRIPTDIR}" || exit 1; pwd)
27
- SRCTOP=$(cd "${SCRIPTDIR}/.." || exit 1; pwd)
28
-
29
- #
30
- # Variables
31
- #
32
- LOCAL_HOSTNAME="$(hostname | tr -d '\n')"
33
- DEFAULT_TEST_PROG="tests/k2hr3template_test.js"
34
- ASYNC_TEST_PROG="tests/k2hr3template_test_async.js"
35
-
36
- #==============================================================
37
- # Utility functions
38
- #==============================================================
39
- #
40
- # Usage
41
- #
42
- PrintUsage()
43
- {
44
- echo "Usage: $1 [--inspect(-i)]"
45
- echo " [--debuglevel(-d) DBG/MSG/WARN/ERR/(custom debug level)]"
46
- echo " [--varlist(-v) \"variables JSON file path\"]"
47
- echo " {--templ(-t) \"template file path\" | --string(-s) \"template string\"}"
48
- echo " {--async(-a)}"
49
- echo ""
50
- echo " If you do not specify input file path or template string,"
51
- echo " $1 prompts you to enter a template string from stdin."
52
- echo ""
53
- }
54
-
55
- #==========================================================
56
- # Parse options
57
- #==========================================================
58
- INPUT_TEMPLFILE=""
59
- INPUT_TEMPLSTR=""
60
- INPUT_VARFILE=""
61
- INPUT_ASYNCMODE=0
62
- DEBUG_USE_INSPECT=0
63
- DEBUG_ENV_LEVEL=0
64
- DEBUG_ENV_CUSTOM=""
65
-
66
- while [ $# -ne 0 ]; do
67
- if [ -z "$1" ]; then
68
- break
69
-
70
- elif echo "$1" | grep -q -i -e "^-h$" -e "^--help$"; then
71
- PrintUsage "${PRGNAME}"
72
- exit 0
73
-
74
- elif echo "$1" | grep -q -i -e "^-i$" -e "^--inspect$"; then
75
- if [ "${DEBUG_USE_INSPECT}" -ne 0 ]; then
76
- echo "[ERROR] Already specified --inspect(-i) option"
77
- exit 1
78
- fi
79
- DEBUG_USE_INSPECT=1
80
-
81
- elif echo "$1" | grep -q -i -e "^-d$" -e "^--debuglevel$"; then
82
- #
83
- # DEBUG option
84
- #
85
- shift
86
- if [ $# -eq 0 ]; then
87
- echo "[ERROR] --debuglevel(-dl) option needs parameter(dbg/msg/warn/err/custom debug level)"
88
- exit 1
89
- fi
90
- if echo "$1" | grep -q -i -e "^dbg$" -e "^debug$"; then
91
- if [ "${DEBUG_ENV_LEVEL}" -ne 0 ]; then
92
- echo "[ERROR] --debuglevel(-dl) option already is set"
93
- exit 1
94
- fi
95
- DEBUG_ENV_LEVEL=4
96
- elif echo "$1" | grep -q -i -e "^msg$" -e "^message$" -e "^info$"; then
97
- if [ "${DEBUG_ENV_LEVEL}" -ne 0 ]; then
98
- echo "[ERROR] --debuglevel(-dl) option already is set"
99
- exit 1
100
- fi
101
- DEBUG_ENV_LEVEL=3
102
- elif echo "$1" | grep -q -i -e "^wan$" -e "^warn$" -e "^warning$"; then
103
- if [ "${DEBUG_ENV_LEVEL}" -ne 0 ]; then
104
- echo "[ERROR] --debuglevel(-dl) option already is set"
105
- exit 1
106
- fi
107
- DEBUG_ENV_LEVEL=2
108
- elif echo "$1" | grep -q -i -e "^err$" -e "^error$"; then
109
- if [ "${DEBUG_ENV_LEVEL}" -ne 0 ]; then
110
- echo "[ERROR] --debuglevel(-dl) option already is set"
111
- exit 1
112
- fi
113
- DEBUG_ENV_LEVEL=1
114
- else
115
- #
116
- # Custom debug level value
117
- #
118
- if [ -n "${DEBUG_ENV_CUSTOM}" ]; then
119
- DEBUG_ENV_CUSTOM="${DEBUG_ENV_CUSTOM},"
120
- fi
121
- DEBUG_ENV_CUSTOM="${DEBUG_ENV_CUSTOM}$1"
122
- fi
123
-
124
- elif echo "$1" | grep -q -i -e "^-v$" -e "^--varlist$"; then
125
- #
126
- # input variable list file path
127
- #
128
- if [ -n "${INPUT_VARFILE}" ]; then
129
- echo "[ERROR] Already specified variable list file ${INPUT_VARFILE}"
130
- exit 1
131
- fi
132
- shift
133
- if [ $# -eq 0 ]; then
134
- echo "[ERROR] --varlist(-v) option needs parameter( input variable JSON file path )"
135
- exit 1
136
- fi
137
- if [ ! -f "$1" ]; then
138
- echo "[ERROR] could not find variable list file $1"
139
- exit 1
140
- fi
141
- INPUT_VARFILE="$1"
142
-
143
- elif echo "$1" | grep -q -i -e "^-t$" -e "^--templ$" -e "^--template$"; then
144
- #
145
- # input template file path
146
- #
147
- if [ -n "${INPUT_TEMPLFILE}" ]; then
148
- echo "[ERROR] Already specified template file ${INPUT_TEMPLFILE}"
149
- exit 1
150
- fi
151
- if [ -n "${INPUT_TEMPLSTR}" ]; then
152
- echo "[ERROR] Already specified template string \"${INPUT_TEMPLSTR}\""
153
- exit 1
154
- fi
155
- shift
156
- if [ $# -eq 0 ]; then
157
- echo "[ERROR] --templ(-t) option needs parameter( input template file path )"
158
- exit 1
159
- fi
160
- if [ ! -f "$1" ]; then
161
- echo "[ERROR] could not find template file $1"
162
- exit 1
163
- fi
164
- INPUT_TEMPLFILE="$1"
165
-
166
- elif echo "$1" | grep -q -i -e "^-s$" -e "^--str$" -e "^--string$"; then
167
- #
168
- # input template string
169
- #
170
- if [ -n "${INPUT_TEMPLSTR}" ]; then
171
- echo "[ERROR] Already specified template string ${INPUT_TEMPLSTR}"
172
- exit 1
173
- fi
174
- if [ -n "${INPUT_TEMPLFILE}" ]; then
175
- echo "[ERROR] Already specified template file ${INPUT_TEMPLFILE}"
176
- exit 1
177
- fi
178
- shift
179
- if [ $# -eq 0 ]; then
180
- echo "[ERROR] --string(-s) option needs parameter( input template string )"
181
- exit 1
182
- fi
183
- INPUT_TEMPLSTR="$1"
184
-
185
- elif echo "$1" | grep -q -i -e "^-a$" -e "^--async$"; then
186
- #
187
- # async mode
188
- #
189
- if [ "${INPUT_ASYNCMODE}" -ne 0 ]; then
190
- echo "[ERROR] --async(-a) option is already specified"
191
- exit 1
192
- fi
193
- INPUT_ASYNCMODE=1
194
-
195
- else
196
- echo "[ERROR] unknown option $1"
197
- exit 1
198
- fi
199
- shift
200
- done
201
-
202
- #----------------------------------------------------------
203
- # Set varibales and files
204
- #----------------------------------------------------------
205
- #
206
- # Checking async mode
207
- #
208
- if [ "${INPUT_ASYNCMODE}" -eq 0 ]; then
209
- TEST_PROG="${DEFAULT_TEST_PROG}"
210
- else
211
- TEST_PROG="${ASYNC_TEST_PROG}"
212
- fi
213
-
214
- #
215
- # Make NODE_DEBUG environment
216
- #
217
- DEBUG_ENV_PARAM=""
218
- if [ "${DEBUG_ENV_LEVEL}" -ge 4 ]; then
219
- DEBUG_ENV_PARAM="LOGLEVEL_DBG"
220
- elif [ "${DEBUG_ENV_LEVEL}" -ge 3 ]; then
221
- DEBUG_ENV_PARAM="LOGLEVEL_MSG"
222
- elif [ "${DEBUG_ENV_LEVEL}" -ge 2 ]; then
223
- DEBUG_ENV_PARAM="LOGLEVEL_WAN"
224
- elif [ "${DEBUG_ENV_LEVEL}" -ge 1 ]; then
225
- DEBUG_ENV_PARAM="LOGLEVEL_ERR"
226
- else
227
- DEBUG_ENV_PARAM="LOGLEVEL_SILENT"
228
- fi
229
- if [ -n "${DEBUG_ENV_CUSTOM}" ]; then
230
- if [ -n "${DEBUG_ENV_PARAM}" ]; then
231
- DEBUG_ENV_PARAM="${DEBUG_ENV_PARAM},"
232
- fi
233
- DEBUG_ENV_PARAM="${DEBUG_ENV_PARAM}${DEBUG_ENV_CUSTOM}"
234
- fi
235
-
236
- #
237
- # Create Template file
238
- #
239
- rm -f /tmp/"${PRGNAME}".templ.*
240
-
241
- if [ -z "${INPUT_TEMPLFILE}" ]; then
242
- #
243
- # Temporary template file
244
- #
245
- INPUT_TEMPLFILE="/tmp/${PRGNAME}.templ.$$"
246
-
247
- if [ "${INPUT_TEMPLSTR}" != "" ]; then
248
- #
249
- # Put input template string to temporary file
250
- #
251
- if ! echo "${INPUT_TEMPLSTR}" >"${INPUT_TEMPLFILE}"; then
252
- echo "[ERROR] Could not create ${INPUT_TEMPLFILE} file"
253
- exit 1
254
- fi
255
- else
256
- #
257
- # Get template string from stdin
258
- #
259
- if ! touch "${INPUT_TEMPLFILE}"; then
260
- echo "[ERROR] Could not create ${INPUT_TEMPLFILE} file"
261
- exit 1
262
- fi
263
-
264
- echo "---------------------------------------------------------------"
265
- echo " Please input template string."
266
- echo " You can end template string input by entering \"EOF\"."
267
- echo "---------------------------------------------------------------"
268
-
269
- IS_EOF=0
270
- while [ "${IS_EOF}" -eq 0 ]; do
271
- printf '> '
272
- read -r TEMPLLINE
273
-
274
- if echo "${TEMPLLINE}" | grep -q -i "^eof$"; then
275
- IS_EOF=1
276
- else
277
- echo "${TEMPLLINE}" >> "${INPUT_TEMPLFILE}"
278
- fi
279
- done
280
- echo ""
281
- fi
282
- fi
283
-
284
- #
285
- # Input template string
286
- #
287
- if [ "${DEBUG_ENV_LEVEL}" -ge 4 ]; then
288
- echo "---------------------------------------------------------------"
289
- echo " Template string"
290
- echo "---------------------------------------------------------------"
291
- sed -e 's/^/ /g' "${INPUT_TEMPLFILE}"
292
- echo "---------------------------------------------------------------"
293
- fi
294
-
295
- #==========================================================
296
- # Do work
297
- #==========================================================
298
- cd "${SRCTOP}" || exit 1
299
-
300
- if [ "${DEBUG_USE_INSPECT}" -eq 1 ]; then
301
- DEBUG_INSPECT_OPTION="--inspect"
302
- DEBUG_BREAK_OPTION="--debug-brk"
303
- DEBUG_PRINT_OPTIONS="${DEBUG_INSPECT_OPTION} ${DEBUG_BREAK_OPTION} "
304
- DEBUG_PRINT_PIPELINE=" | sed -e 's/127.0.0.1/${LOCAL_HOSTNAME}/'"
305
- else
306
- DEBUG_PRINT_OPTIONS=""
307
- DEBUG_PRINT_PIPELINE=""
308
- fi
309
-
310
- if [ "${DEBUG_ENV_LEVEL}" -ge 4 ]; then
311
- echo "***** Run *****"
312
- echo "NODE_PATH=${NODE_PATH} NODE_DEBUG=${DEBUG_ENV_PARAM} R3TEMPLFILE=${INPUT_TEMPLFILE} R3VARFILE=${INPUT_VARFILE} node ${DEBUG_PRINT_OPTIONS}${TEST_PROG}${DEBUG_PRINT_PIPELINE}"
313
- echo ""
314
- fi
315
-
316
- if [ "${DEBUG_USE_INSPECT}" -eq 1 ]; then
317
- if ! NODE_PATH="${NODE_PATH}" NODE_DEBUG="${DEBUG_ENV_PARAM}" R3TEMPLFILE="${INPUT_TEMPLFILE}" R3VARFILE="${INPUT_VARFILE}" node "${DEBUG_INSPECT_OPTION}" "${DEBUG_BREAK_OPTION}" "${TEST_PROG}" 2>&1 | sed -e "s/127.0.0.1/${LOCAL_HOSTNAME}/"; then
318
- EXIT_CODE="$?"
319
- echo "[ERROR] Failed to run test with error code : ${EXIT_CODE}"
320
- exit 1
321
- fi
322
- else
323
- if ! NODE_PATH="${NODE_PATH}" NODE_DEBUG="${DEBUG_ENV_PARAM}" R3TEMPLFILE="${INPUT_TEMPLFILE}" R3VARFILE="${INPUT_VARFILE}" node "${TEST_PROG}"; then
324
- EXIT_CODE="$?"
325
- echo "[ERROR] Failed to run test with error code : ${EXIT_CODE}"
326
- exit 1
327
- fi
328
- fi
329
-
330
- exit 0
331
-
332
- #
333
- # Local variables:
334
- # tab-width: 4
335
- # c-basic-offset: 4
336
- # End:
337
- # vim600: noexpandtab sw=4 ts=4 fdm=marker
338
- # vim<600: noexpandtab sw=4 ts=4
339
- #
@@ -1,216 +0,0 @@
1
- /*
2
- * K2HR3 REST API
3
- *
4
- * Copyright 2017 Yahoo Japan Corporation.
5
- *
6
- * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
7
- * common management information for the cloud.
8
- * K2HR3 can dynamically manage information as "who", "what", "operate".
9
- * These are stored as roles, resources, policies in K2hdkc, and the
10
- * client system can dynamically read and modify these information.
11
- *
12
- * For the full copyright and license information, please view
13
- * the license file that was distributed with this source code.
14
- *
15
- * AUTHOR: Takeshi Nakatani
16
- * CREATE: Wed Jul 12 2017
17
- * REVISION:
18
- *
19
- */
20
-
21
- 'use strict';
22
-
23
- var fs = require('fs');
24
-
25
- var r3templeng = require('../lib/k2hr3template');
26
- var apiutil = require('../lib/k2hr3apiutil');
27
-
28
- // Debug logging objects
29
- var r3logger = require('../lib/dbglogging');
30
-
31
- // [NOTE]
32
- // The processing in this file is the same as k2hr3template_test.js.
33
- // Since I don't want to make the code of k2hr3template_test.js complicated,
34
- // we make it a separate file as an asynchronous test.
35
- // Thus please be careful when making corrections.
36
- //
37
-
38
- //
39
- // check environments
40
- //
41
- var templfile = apiutil.getSafeString(process.env.R3TEMPLFILE);
42
- var varfile = apiutil.getSafeString(process.env.R3VARFILE);
43
- if(!apiutil.isSafeString(templfile) || ('' !== varfile && !apiutil.isSafeString(varfile))){
44
- r3logger.elog('Environments are wrong : R3TEMPLFILE=' + JSON.stringify(templfile) + ', R3VARFILE=' + JSON.stringify(varfile));
45
- process.exit(0);
46
- }
47
-
48
- //
49
- // execute main
50
- //
51
- function execTemplateEngine(templString, varmap, callback)
52
- {
53
- if(!apiutil.isSafeEntity(callback) || 'function' !== typeof callback){
54
- r3logger.elog('parameter callback is wrong.');
55
- return;
56
- }
57
- var _callback = callback;
58
- var _errorstr = null;
59
- var r3templ = new r3templeng.r3template;
60
-
61
- // load template string
62
- r3templ.load(templString, function(error, result){
63
- if(null !== error || !result){
64
- _errorstr = 'could not load string to k2hr3 template engine : string=' + JSON.stringify(templString) + ', error = ' + JSON.stringify(error);
65
- r3logger.elog(_errorstr);
66
- _callback(_errorstr);
67
- return;
68
- }
69
-
70
- // print variable names in template
71
- r3templ.getVariableNames(function(error, vars){
72
- if(null !== error){
73
- r3logger.wlog(error);
74
- }
75
-
76
- console.log('----------------------------------------------------------------');
77
- if(apiutil.isEmptyArray(vars)){
78
- console.log('var names = n/a');
79
- }else{
80
- console.log('var names = ' + r3logger.dump(vars));
81
- }
82
-
83
- // execute template engine
84
- r3templ.execute(varmap, function(resobj){
85
-
86
- console.log('----------------------------------------------------------------');
87
- console.log('RESULT');
88
- console.log('----------------------------------------------------------------');
89
- if(resobj.result){
90
- console.log('result = succeed');
91
- console.log('input string = ' + templString);
92
- console.log('output string = ' + resobj.getString());
93
- }else{
94
- console.log('result = failed');
95
- console.log('input string = ' + templString);
96
- }
97
- console.log('----------------------------------------------------------------');
98
- _callback(null);
99
- return;
100
- });
101
- });
102
- });
103
- }
104
-
105
- //
106
- // load template file
107
- //
108
- fs.readFile(templfile, 'utf8', function(err, templString)
109
- {
110
- if(null !== err){
111
- r3logger.elog('could not load template string from file : ' + JSON.stringify(templfile));
112
- r3logger.dump(err);
113
- process.exit(0);
114
- }
115
- if(!apiutil.isSafeString(templString)){
116
- r3logger.elog('loaded template string is empty : ' + JSON.stringify(templString));
117
- process.exit(0);
118
- }
119
- //r3logger.dlog('Template string : ' + JSON.stringify(templString));
120
-
121
- //
122
- // check variable file type(allow varfile is null)
123
- //
124
- var is_varfile_json = false;
125
- if('' === varfile){
126
- r3logger.dlog('variable file(' + varfile + ') is not specified.');
127
- is_varfile_json = false;
128
- }else if(apiutil.compareCaseString('.json5', varfile.substr(-6))){
129
- r3logger.dlog('variable file(' + varfile + ') is JSON5 type.');
130
- is_varfile_json = true;
131
- }else if(apiutil.compareCaseString('.json', varfile.substr(-5))){
132
- r3logger.dlog('variable file(' + varfile + ') is JSON type.');
133
- is_varfile_json = true;
134
- }else if(apiutil.compareCaseString('.js', varfile.substr(-3))){
135
- r3logger.dlog('variable file(' + varfile + ') is JS type.');
136
- is_varfile_json = false;
137
- }else{
138
- r3logger.dlog('variable file(' + varfile + ') is unknown type, then we will load it as JSON type.');
139
- is_varfile_json = true;
140
- }
141
-
142
- //
143
- // load variable file type
144
- //
145
- var varmap = {}; // default for varfile is null
146
- if(is_varfile_json){
147
- // json file
148
- fs.readFile(varfile, 'utf8', function(err, jsonVarString)
149
- {
150
- if(null !== err){
151
- r3logger.elog('could not load variable JSON string fromm file : ' + JSON.stringify(varfile));
152
- r3logger.dump(err);
153
- process.exit(0);
154
- }
155
- if(apiutil.isSafeString(jsonVarString)){
156
- // parse JSON
157
- varmap = jsonVarString;
158
- if(apiutil.checkSimpleJSON(jsonVarString)){
159
- varmap = JSON.parse(jsonVarString);
160
- }
161
- if(!apiutil.isSafeEntity(varmap)){
162
- r3logger.elog('loaded variable file(.json) is something wrong : ' + JSON.stringify(varmap));
163
- process.exit(0);
164
- }
165
- }else{
166
- // allow empty string
167
- r3logger.dlog('loaded variable JSON string is empty.');
168
- }
169
- //r3logger.dump(varmap);
170
-
171
- // execute
172
- execTemplateEngine(templString, varmap, function(error){
173
- if(null !== error){
174
- r3logger.elog('failed to execute k2hr3 template engine.');
175
- }
176
- process.exit(0);
177
- });
178
- });
179
-
180
- }else{
181
- // js file
182
- // (js file must be like following contents)
183
- //
184
- // module.exports = {
185
- // 'key1': 'value1',
186
- // 'key2': 'value2',
187
- // ...
188
- // };
189
- //
190
- if('' !== varfile){
191
- varmap = require(varfile);
192
- if(!apiutil.isSafeEntity(varmap)){
193
- r3logger.elog('loaded variable file(.js) is something wrong : ' + JSON.stringify(varmap));
194
- process.exit(0);
195
- }
196
- }
197
- //r3logger.dump(varmap);
198
-
199
- // execute
200
- execTemplateEngine(templString, varmap, function(error){
201
- if(null !== error){
202
- r3logger.elog('failed to execute k2hr3 template engine.');
203
- }
204
- process.exit(0);
205
- });
206
- }
207
- });
208
-
209
- /*
210
- * Local variables:
211
- * tab-width: 4
212
- * c-basic-offset: 4
213
- * End:
214
- * vim600: noexpandtab sw=4 ts=4 fdm=marker
215
- * vim<600: noexpandtab sw=4 ts=4
216
- */