Subversion Repositories php-qbpwcf

Rev

Rev 145 | Rev 148 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
#!/usr/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
/*
26
 
27
說明:
28
註冊服務.
29
 
30
範例:
31
 
32
#每次執行 ps auxwf | cat 後,下一秒就刷新畫面重新執行.
33
registerService.php --cmds clear --cmds 'ps auxwf | cat'  --cmds 'sleep 1'
34
 
35
備註:
36
目前不支援在 ostree 環境下運行.
37
 
38
*/
39
 
40
#指派命名空間
41
namespace qbpwcf;
42
 
43
#取得 lib path
44
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
45
 
46
#如果執行失敗
47
if($status!==0){
48
 
49
	#debug
50
	var_dump(__LINE__,$output);
51
 
52
	#結束執行,回傳shell 1.
53
	exit(1);
54
 
55
	}#if end
56
 
57
#儲存lib path
58
$folderOfUsrLib=$output[0];
59
 
60
#以該檔案的實際位置的 lib path 為 include path 首位
61
$output=array();
62
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
63
 
64
#如果執行失敗
65
if($status!==0){
66
 
67
	#debug
68
	var_dump(__LINE__,$output);
69
 
70
	#結束執行,回傳shell 1.
71
	exit(1);
72
 
73
	}#if end
74
 
75
#設置 include path 
76
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
77
 
78
#匯入外部套件
79
include("allInOne.php");
80
 
81
#說明函式
130 liveuser 82
function help(){
83
 
3 liveuser 84
	#印出指令說明
85
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
86
	echo "--cmd [command to run] 代表要執行的指令,若有多個則會按照順序執行.".PHP_EOL;
130 liveuser 87
	echo "--name [servie name] 服務的名稱".PHP_EOL;
88
	echo "--runOnBoot [yes/no] 開機好就要啟動該服務,預設為yes.".PHP_EOL;
89
	echo "--runNow [yes/no] 馬上運行服務,預設為yes.".PHP_EOL;
90
	echo "--keepServiceUp [yes/no] 是否要加增確保該服務在線的設定,預設為no.".PHP_EOL;
91
	echo "--interval [秒數] 要多少時間就檢查一次服務是否在線.".PHP_EOL;
92
	echo "--checkListenIp [ipv4/ipv6]:[port]/[tcp/udp] 要檢查哪個ip位置的port與protocol有被使用,才代表服務有在線.".PHP_EOL;
93
	echo "\tip可用*代表所有網路界面取得的ip.".PHP_EOL;
94
	echo "--checkListenAddr [path to socket] 要檢查哪個路徑的unix domain socket有被使用,才代表服務有在線".PHP_EOL;
146 liveuser 95
	echo "--checkCmd [command to check service] 用於確認服務是否有在線的指令,timeout為10秒.".PHP_EOL;
96
 
3 liveuser 97
	#結束執行
98
	exit;
99
 
130 liveuser 100
	}#function help end
101
 
3 liveuser 102
#函式說明:
103
#解析參數.
104
#回傳結果:
105
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
106
#$reuslt["error"],執行不正常結束的錯訊息陣列.
107
#$result["function"],當前執行的函式名稱.
108
#$result["content"],解析好的參數陣列.
109
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
110
#$result["program"],字串,執行的程式名稱.
111
#必填參數:
112
#無.
113
#可省略參數:
114
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
115
$conf["helpFunc"]="help";
116
#$conf["parseFormVar"],解析表單變數(get\post),預設為"false"不使用;反之設置為"true".
117
$conf["parseFormVar"]="false";
118
#參考資料:
119
#無.
120
#備註:
121
#僅能在命令列底下執行.
122
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
123
$parseArgu=cmd::parseArgu($conf);
124
unset($conf);
125
 
126
#如果取得參數失敗
127
if($parseArgu["status"]==="false"){
128
 
129
	#設置執行失敗
130
	$result["status"]="false";
131
 
132
	#設置執行錯誤訊息
133
	$result["error"]=$parseArgu;
134
 
135
	#印出訊息
136
	var_dump($result);
137
 
138
	#結束執行
139
	exit;
140
 
141
	}#if end
142
 
143
#檢查參數
144
#函式說明:
145
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
146
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
147
#$reuslt["error"],執行不正常結束的錯訊息陣列.
148
#$result["function"],當前執行的函式名稱.
149
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
150
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
151
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
152
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
153
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
154
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
155
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
156
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
157
#必填寫的參數:
158
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
159
$conf["varInput"]=&$parseArgu["content"];
160
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
161
$conf["referenceVarKey"]="variableCheck::checkArguments";
162
#可以省略的參數:
163
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
164
$conf["mustBeFilledVariableName"]=array("cmd","name");
165
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
166
$conf["mustBeFilledVariableType"]=array("array","array");
167
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
168
#$conf["canBeEmptyString"]="false";
169
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
170
#$conf["canNotBeEmpty"]=array();
171
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
172
#$conf["canBeEmpty"]=array();
173
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
174
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
175
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
146 liveuser 176
$conf["skipableVariableName"]=array("runOnBoot","runNow","keepServiceUp","interval","checkListenIp","checkListenAddr","checkCmd");
3 liveuser 177
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
146 liveuser 178
$conf["skipableVariableType"]=array("array","array","array","array","array","array");
3 liveuser 179
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
146 liveuser 180
$conf["skipableVarDefaultValue"]=array(array("yes"),array("yes"),null,null,null,null,null);
3 liveuser 181
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
182
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
183
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
184
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
185
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
186
#$conf["arrayCountEqualCheck"][]=array();
187
#參考資料來源:
188
#array_keys=>http://php.net/manual/en/function.array-keys.php
189
$checkArguments=variableCheck::checkArguments($conf);
190
unset($conf);
191
 
192
#若執行失敗
193
if($checkArguments["status"]==="false"){
194
 
195
	#印出結果
196
	var_dump($checkArguments);
197
 
198
	#結束執行
199
	exit;
200
 
201
	}#if end
202
 
203
#若檢查不通過
204
if($checkArguments["passed"]==="false"){
205
 
206
	#提示用法
207
	help();
208
 
209
	#結束執行
210
	exit;
211
 
212
	}#if end
213
 
214
#取得指令陣列
215
$cmds=$parseArgu["content"]["cmd"];
216
 
217
#取得目標參數
218
$serviceName=$parseArgu["content"]["name"][0];
219
 
220
#取得runOnBoot參數
221
$runOnBoot=$parseArgu["content"]["runOnBoot"][0];
222
 
223
#預設為 false
224
$runOnBoot="false";
225
 
226
#如果是 yes
227
if($runOnBoot==="yes"){
228
 
229
	#轉換為 "true"
230
	$runOnBoot="true";
231
 
232
	}#if end
233
 
234
#取得runNow參數
235
$runNow=$parseArgu["content"]["runNow"][0];
236
 
237
#預設為 false
238
$runNow="false";
239
 
240
#如果是 yes
241
if($runNow==="yes"){
242
 
243
	#轉換為 "true"
244
	$runNow="true";
130 liveuser 245
 
246
	}#if end
3 liveuser 247
 
130 liveuser 248
#如果有指定 keepServiceUp
249
if(isset($parseArgu["content"]["keepServiceUp"][0])){
250
 
251
	#設定keepServiceUp
252
	$keepServiceUp=$parseArgu["content"]["keepServiceUp"][0];
253
 
3 liveuser 254
	}#if end
255
 
130 liveuser 256
#如果有指定 interval
257
if(isset($parseArgu["content"]["interval"][0])){
258
 
259
	#設定interval
260
	$interval=$parseArgu["content"]["interval"][0];
261
 
262
	}#if end
263
 
264
#初始化要檢查的服務名稱
265
$checkListen=array("name"=>$conf["name"]);
266
 
267
#如果有指定 checkListenIp
268
if(isset($parseArgu["content"]["checkListenIp"])){
269
 
270
	#針對每個 checkListenIp
271
	foreach($parseArgu["content"]["checkListenIp"] as $checkListenIp){
272
 
273
		#記錄要檢查的ip & port & protocol
274
		$checkListen[]=array("ip"=>$checkListenIp);
275
 
276
		}#foreach end
277
 
278
	}#if end
279
 
280
#如果有指定 checkListenAddr
281
if(isset($parseArgu["content"]["checkListenAddr"])){
282
 
283
	#針對每個 checkListenAddr
284
	foreach($parseArgu["content"]["checkListenAddr"] as $checkListenAddr){
285
 
286
		#記錄要檢查的 unix domain socket 路徑
287
		$checkListen[]=array("addr"=>$checkListenAddr);
288
 
289
		}#foreach end
290
 
291
	}#if end
292
 
146 liveuser 293
#如果有指定 checkCmd
294
if(isset($parseArgu["content"]["checkCmd"])){
295
 
296
	#針對每個 checkCmd
297
	foreach($parseArgu["content"]["checkCmd"] as $checkCmd){
298
 
299
		#記錄用於檢查服務是否在線的指令
300
		$checkListen[]=array("cmd"=>$checkCmd);
301
 
302
		}#foreach end
303
 
304
	}#if end
305
 
3 liveuser 306
#建立暫存檔案
307
#函式說明:
308
#於本套件位置底下的tmp資料夾下建立與回傳暫存檔案名稱路徑 
309
#回傳結果:
310
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
311
#$result["error"],錯誤訊息.
312
#$result["function"],當前執行的函數名稱.
313
#$result["content"],暫存檔案的路徑與名稱.
314
#$result["fileName"],暫存檔案的名稱.
315
#$result["path"],暫存檔案的路徑.
316
#必填參數:
317
#無.
318
#可省略參數:
319
#無.
320
#參考資料:
321
#無.
322
#備註:
323
#無.
38 liveuser 324
$conf=array();
325
$createTempFile=fileAccess::createTempFile($conf);
326
unset($conf);
3 liveuser 327
 
328
#如果建立暫存檔案失敗
329
if($createTempFile["status"]==="false"){
330
 
331
	#debug
332
	var_dump($createTempFile);
333
 
334
	#結束執行,回傳 shell 1
335
	exit(1);
336
 
337
	}#if end
338
 
339
#撰寫要執行的程式內容
340
#函式說明:
341
#將多行字串寫入到檔案
342
#回傳結果:
343
#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
344
#$result["error"],錯誤訊息陣列.
345
#$result["function"],當前執行函數的名稱.
346
#必填參數:
347
#$conf["fileName"],字串,爲要編輯的檔案名稱
348
$conf["fileName"]=$createTempFile["content"];
349
#$conf["inputString"],字串陣列,爲要寫入到 $conf["fileName"] 裏面的內容. $conf["inputString"][$i] 代表第 $i+1 行。
350
$conf["inputString"]=$cmds;
351
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
352
$conf["fileArgu"]=__FILE__;
353
#可省略參數:
354
#$conf["writeMethod"]="a";#爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入。
355
#參考資料:
356
#無.
357
#備註:
358
#無.
359
$writeMultiLine=fileAccess::writeMultiLine($conf);
360
unset($conf);
361
 
362
#如果建立檔案失敗
363
if($writeMultiLine["status"]==="false"){
364
 
365
	#debug
366
	var_dump($writeMultiLine);
367
 
368
	#結束執行,回傳 shell 1
369
	exit(1);
370
 
371
	}#if end
131 liveuser 372
 
373
#path of bin for systemctl 
374
$pathOfBinForSystemctl="/var/qbpwcf/bin_for_systemctl/";
375
 
376
#service path
377
$servicePath=$pathOfBinForSystemctl.$serviceName.".sh";
378
 
379
#移動程式到 /var/qbpwcf/bin_for_systemctl/ 底下
3 liveuser 380
#函式說明:
381
#移動檔案
382
#回傳結果:
383
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
384
#$result["function"],當前執行的函數名稱
385
#$result["error"],錯誤訊息.
386
#$result["content"],檔案輸出後的位置與名稱.
387
#必填參數:
388
#$conf["from"],字串,要移動的檔案名稱與位置.
389
$conf["from"]=$createTempFile["content"];
390
#$conf["to"],字串,要移動到的位置與名稱
131 liveuser 391
$conf["to"]=$servicePath;
3 liveuser 392
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
393
$conf["fileArgu"]=__FILE__;
394
#可省略參數:
395
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
396
#$conf["commentsArray"]=array("");
397
#參考資料:
398
#無.
399
#備註:
400
#無.
401
$mv=fileAccess::mv($conf);
402
unset($conf);
403
 
404
#如果移動檔案失敗
405
if($mv["status"]==="false"){
406
 
407
	#debug
408
	var_dump($mv);
409
 
410
	#結束執行,回傳 shell 1
411
	exit(1);
412
 
413
	}#if end
414
 
415
#函式說明:
416
#將要執行的程式變成透過 systemd 來運行.
417
#回傳結果:
418
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
419
#$result["error"],錯誤訊息.
420
#$result["function"],當前執行的函式名稱.
421
#$result["argu"],所使用的參數.
422
#$result["content"],執行的結果.
423
#必填參數:
424
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
425
$conf["fileArgu"]=__FILE__;
426
#$conf["name"],字串,服務名稱,實際產生的system名稱會加上".service".
427
$conf["name"]=$serviceName;
428
#$conf["cmd"],字串,要執行的指令.
131 liveuser 429
$conf["cmd"]=$servicePath;
3 liveuser 430
#可省略參數:
431
#$conf["params"],字串陣列,指令要使用的參數.
432
#$conf["params"]=array("");
433
#$conf["enable"],字串,預設為"true",代表該服務為enable.
434
$conf["enable"]=$runOnBoot;
435
#$conf["startNow"],字串,預設為"true",代表該服務要立刻執行.
436
$conf["startNow"]=$runNow;
130 liveuser 437
 
438
#如果存在 $keepServiceUp
439
if(isset($keepServiceUp)){
440
 
441
	#$conf["keepServiceUp"],字串,是否要另外增加確保服務有保持啟動的服務,預設為"false"代表否;反之為"true"代表是.
442
	$conf["keepServiceUp"]=$keepServiceUp;
443
 
444
	}#if end
445
 
446
#如果存在 $checkListen
447
if(isset($checkListen)){
448
 
146 liveuser 449
	#$conf["cmd::keepServiceUp"]["checkListen"],#$conf["checkListen"],陣列,用來判斷服務是否有正確啟動的條件,一個元素代表,其中要有一個socket info符合之,若元素的key為"ip",則其數值為[ipv4/ipv6]:port/protocol(tcp/udp);若元素的key為"addr",則其數值為unix domain socket 的位置與名稱;若元素的key為"name",則其數值為程序名稱;若元素的key為"cmd",則其數值為一陣列,該陣列的key有cmd,其value為要執行的指令,另外一個key為timeout,其value為秒數,預設為10秒,代表指令於10秒內結束為正常,反之為異常.
145 liveuser 450
	$conf["cmd::keepServiceUp"]["checkListen"]=$checkListen;
130 liveuser 451
 
452
	}#if end
453
 
454
#如果存在 $interval
455
if(isset($interval)){
456
 
457
	#$conf["cmd::keepServiceUp"]["interval"],字串,檢查沒問題後,要多少秒後再檢查一次,預設為30秒,亦即"30".
458
	$conf["cmd::keepServiceUp"]["interval"]=$interval;
459
 
460
	}#if end
461
 
3 liveuser 462
#參考資料:
463
#無.
464
#備註:
465
#無.
466
$registerService=cmd::registerService($conf);
467
unset($conf);
468
 
469
#如果執行失敗
470
if($registerService["status"]==="false"){
471
 
472
	#debug
473
	var_dump($registerService);
474
 
475
	#結束執行,回傳 shell 1
476
	exit(1);
477
 
478
	}#if end