Subversion Repositories php-qbpwcf

Rev

Rev 239 | 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.
239 liveuser 6
    Copyright (C) 2014~2026 MIN ZHI, CHEN
3 liveuser 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/>.
226 liveuser 22
 
3 liveuser 23
*/
24
 
25
/*
26
 
27
說明:
28
註冊服務.
29
 
30
範例:
31
 
148 liveuser 32
#註冊 每次執行 ps auxwf | cat 後,下一秒就刷新畫面重新執行 的服務
33
registerService.php --cmd clear --cmds 'ps auxwf | cat'  --cmds 'sleep 1'
3 liveuser 34
 
148 liveuser 35
#註冊 執行 ssh 安全連線,並透過定期檢查 port 跟 指定指令 來確保服務在線 的服務
36
registerService.php --cmd "ssh -i /home/liveuser/.ssh/id_ed25519 -N -L 5956:169.254.1.2:22 liveuser@domain" --name secure2liveuser@domain --keepServiceUp yes --checkCmd "echo 'env' | ssh liveuser@domain -p 5956 -i /home/liveuser/.ssh/id_ed25519" --runOnBoot yes --runNow yes --checkListenIp 127.0.0.1:5956/tcp
37
 
3 liveuser 38
備註:
148 liveuser 39
會被SELINUX預設設定阻擋
3 liveuser 40
 
41
*/
42
 
43
#指派命名空間
44
namespace qbpwcf;
45
 
46
#取得 lib path
47
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
48
 
49
#如果執行失敗
50
if($status!==0){
51
 
52
	#debug
53
	var_dump(__LINE__,$output);
54
 
55
	#結束執行,回傳shell 1.
56
	exit(1);
57
 
58
	}#if end
59
 
60
#儲存lib path
61
$folderOfUsrLib=$output[0];
62
 
63
#以該檔案的實際位置的 lib path 為 include path 首位
64
$output=array();
65
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
66
 
67
#如果執行失敗
68
if($status!==0){
69
 
70
	#debug
71
	var_dump(__LINE__,$output);
72
 
73
	#結束執行,回傳shell 1.
74
	exit(1);
75
 
76
	}#if end
77
 
226 liveuser 78
#設置 include path
3 liveuser 79
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
80
 
81
#匯入外部套件
82
include("allInOne.php");
83
 
84
#說明函式
130 liveuser 85
function help(){
86
 
3 liveuser 87
	#印出指令說明
226 liveuser 88
	echo "Usage of ".basename(__FILE__).":".PHP_EOL;
3 liveuser 89
	echo "--cmd [command to run] 代表要執行的指令,若有多個則會按照順序執行.".PHP_EOL;
130 liveuser 90
	echo "--name [servie name] 服務的名稱".PHP_EOL;
91
	echo "--runOnBoot [yes/no] 開機好就要啟動該服務,預設為yes.".PHP_EOL;
92
	echo "--runNow [yes/no] 馬上運行服務,預設為yes.".PHP_EOL;
93
	echo "--keepServiceUp [yes/no] 是否要加增確保該服務在線的設定,預設為no.".PHP_EOL;
94
	echo "--interval [秒數] 要多少時間就檢查一次服務是否在線.".PHP_EOL;
95
	echo "--checkListenIp [ipv4/ipv6]:[port]/[tcp/udp] 要檢查哪個ip位置的port與protocol有被使用,才代表服務有在線.".PHP_EOL;
96
	echo "\tip可用*代表所有網路界面取得的ip.".PHP_EOL;
97
	echo "--checkListenAddr [path to socket] 要檢查哪個路徑的unix domain socket有被使用,才代表服務有在線".PHP_EOL;
146 liveuser 98
	echo "--checkCmd [command to check service] 用於確認服務是否有在線的指令,timeout為10秒.".PHP_EOL;
287 liveuser 99
	echo "--runAfertService [run after specify service] 要於哪個服務啟動後執行".PHP_EOL;
226 liveuser 100
 
3 liveuser 101
	#結束執行
102
	exit;
103
 
130 liveuser 104
	}#function help end
105
 
3 liveuser 106
#函式說明:
107
#解析參數.
108
#回傳結果:
109
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
110
#$reuslt["error"],執行不正常結束的錯訊息陣列.
111
#$result["function"],當前執行的函式名稱.
112
#$result["content"],解析好的參數陣列.
113
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
114
#$result["program"],字串,執行的程式名稱.
115
#必填參數:
116
#無.
117
#可省略參數:
118
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
119
$conf["helpFunc"]="help";
120
#$conf["parseFormVar"],解析表單變數(get\post),預設為"false"不使用;反之設置為"true".
121
$conf["parseFormVar"]="false";
122
#參考資料:
123
#無.
124
#備註:
125
#僅能在命令列底下執行.
126
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
127
$parseArgu=cmd::parseArgu($conf);
128
unset($conf);
129
 
130
#如果取得參數失敗
131
if($parseArgu["status"]==="false"){
132
 
133
	#設置執行失敗
134
	$result["status"]="false";
135
 
136
	#設置執行錯誤訊息
137
	$result["error"]=$parseArgu;
138
 
139
	#印出訊息
140
	var_dump($result);
141
 
142
	#結束執行
143
	exit;
144
 
145
	}#if end
226 liveuser 146
 
3 liveuser 147
#檢查參數
148
#函式說明:
149
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
150
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
151
#$reuslt["error"],執行不正常結束的錯訊息陣列.
152
#$result["function"],當前執行的函式名稱.
153
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
154
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
155
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
156
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
157
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
158
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
159
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
160
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
161
#必填寫的參數:
162
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
163
$conf["varInput"]=&$parseArgu["content"];
164
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
165
$conf["referenceVarKey"]="variableCheck::checkArguments";
166
#可以省略的參數:
167
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
168
$conf["mustBeFilledVariableName"]=array("cmd","name");
169
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
170
$conf["mustBeFilledVariableType"]=array("array","array");
171
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
172
#$conf["canBeEmptyString"]="false";
173
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
174
#$conf["canNotBeEmpty"]=array();
175
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
176
#$conf["canBeEmpty"]=array();
177
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
178
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
179
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
287 liveuser 180
$conf["skipableVariableName"]=array("runOnBoot","runNow","keepServiceUp","interval","checkListenIp","checkListenAddr","checkCmd","runAfertService");
3 liveuser 181
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
287 liveuser 182
$conf["skipableVariableType"]=array("array","array","array","array","array","array","array","array");
3 liveuser 183
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
287 liveuser 184
$conf["skipableVarDefaultValue"]=array(array("yes"),array("yes"),null,null,null,null,null,null);
3 liveuser 185
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
186
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
187
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
188
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
189
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
190
#$conf["arrayCountEqualCheck"][]=array();
191
#參考資料來源:
192
#array_keys=>http://php.net/manual/en/function.array-keys.php
193
$checkArguments=variableCheck::checkArguments($conf);
194
unset($conf);
195
 
196
#若執行失敗
197
if($checkArguments["status"]==="false"){
198
 
199
	#印出結果
200
	var_dump($checkArguments);
226 liveuser 201
 
3 liveuser 202
	#結束執行
203
	exit;
204
 
205
	}#if end
206
 
207
#若檢查不通過
208
if($checkArguments["passed"]==="false"){
209
 
210
	#提示用法
211
	help();
226 liveuser 212
 
3 liveuser 213
	#結束執行
214
	exit;
215
 
216
	}#if end
148 liveuser 217
 
218
#針對每個要執行的指令字串
219
foreach($parseArgu["content"]["cmd"] as $index => $cmdStr){
220
 
221
	#解析成程式與參數
222
	#函式說明:
226 liveuser 223
	#將指令字串解析成陣列,方便給予 external::callShell 使用
148 liveuser 224
	#回傳結果:
225
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
226
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
227
	#$result["function"],當前執行的函式名稱.
228
	#$result["content"],解析好的指令陣列.
229
	#$result["cmd"],解析好的指令名稱.
230
	#$result["argus"],解析好的參數陣列.
231
	#$result["argu"],所使用的參數.
232
	#必填參數
233
	#$conf["cmdStr"],字串,要解析的指令字串
234
	$conf["cmdStr"]=$cmdStr;
235
	#可省略參數:
236
	#無.
237
	#參考資料:
238
	#無.
239
	#備註:
240
	#無.
241
	$parseCmdString=cmd::parseCmdString($conf);
242
	unset($conf);
243
 
244
	#若執行失敗
245
	if($parseCmdString["status"]==="false"){
246
 
247
		#印出結果
248
		var_dump($parseCmdString);
226 liveuser 249
 
148 liveuser 250
		#結束執行
251
		exit;
252
 
253
		}#if end
226 liveuser 254
 
148 liveuser 255
	#取得要執行的程式
256
	$cmds[]=$parseCmdString["cmd"];
226 liveuser 257
 
148 liveuser 258
	#若存在程式對應的參數
259
	if(isset($parseCmdString["argus"])){
226 liveuser 260
 
148 liveuser 261
		#取得程式對應的參數
262
		$argus[]=$parseCmdString["argus"];
226 liveuser 263
 
148 liveuser 264
		}#if end
226 liveuser 265
 
148 liveuser 266
	#反之
267
	else{
226 liveuser 268
 
148 liveuser 269
		#設置無該程式的參數
270
		$argus[]=null;
226 liveuser 271
 
148 liveuser 272
		}#else end
3 liveuser 273
 
148 liveuser 274
	}#foreache end
275
 
276
#取得要執行服務的指令
277
#$cmds=;
278
 
3 liveuser 279
#取得目標參數
280
$serviceName=$parseArgu["content"]["name"][0];
281
 
282
#預設為 false
283
$runOnBoot="false";
284
 
148 liveuser 285
#如果 runOnBoot 參數是 yes
286
if($parseArgu["content"]["runOnBoot"][0]==="yes"){
3 liveuser 287
 
288
	#轉換為 "true"
289
	$runOnBoot="true";
290
 
291
	}#if end
292
 
293
#預設為 false
294
$runNow="false";
295
 
148 liveuser 296
#如果 runNow參數 是 yes
297
if($parseArgu["content"]["runNow"][0]==="yes"){
3 liveuser 298
 
299
	#轉換為 "true"
300
	$runNow="true";
226 liveuser 301
 
130 liveuser 302
	}#if end
3 liveuser 303
 
148 liveuser 304
#預設不用 cmd::keepServiceUp 來確保服務
305
$keepServiceUp="false";
306
 
130 liveuser 307
#如果有指定 keepServiceUp
308
if(isset($parseArgu["content"]["keepServiceUp"][0])){
309
 
148 liveuser 310
	#如果參數 keepServiceUp 為 "true"
311
	if($parseArgu["content"]["keepServiceUp"][0]==="yes"){
226 liveuser 312
 
148 liveuser 313
		#設置 keepServiceUp 為 "true"
314
		$keepServiceUp="true";
226 liveuser 315
 
148 liveuser 316
		}#if end
130 liveuser 317
 
3 liveuser 318
	}#if end
319
 
130 liveuser 320
#如果有指定 interval
321
if(isset($parseArgu["content"]["interval"][0])){
322
 
323
	#設定interval
324
	$interval=$parseArgu["content"]["interval"][0];
325
 
326
	}#if end
226 liveuser 327
 
152 liveuser 328
#初始化儲存判斷的方式
329
$checkListen=array();
226 liveuser 330
 
130 liveuser 331
#如果有指定 checkListenIp
332
if(isset($parseArgu["content"]["checkListenIp"])){
333
 
334
	#針對每個 checkListenIp
335
	foreach($parseArgu["content"]["checkListenIp"] as $checkListenIp){
226 liveuser 336
 
130 liveuser 337
		#記錄要檢查的ip & port & protocol
338
		$checkListen[]=array("ip"=>$checkListenIp);
226 liveuser 339
 
130 liveuser 340
		}#foreach end
226 liveuser 341
 
130 liveuser 342
	}#if end
226 liveuser 343
 
130 liveuser 344
#如果有指定 checkListenAddr
345
if(isset($parseArgu["content"]["checkListenAddr"])){
346
 
347
	#針對每個 checkListenAddr
348
	foreach($parseArgu["content"]["checkListenAddr"] as $checkListenAddr){
226 liveuser 349
 
130 liveuser 350
		#記錄要檢查的 unix domain socket 路徑
351
		$checkListen[]=array("addr"=>$checkListenAddr);
226 liveuser 352
 
130 liveuser 353
		}#foreach end
354
 
355
	}#if end
356
 
146 liveuser 357
#如果有指定 checkCmd
358
if(isset($parseArgu["content"]["checkCmd"])){
359
 
360
	#針對每個 checkCmd
361
	foreach($parseArgu["content"]["checkCmd"] as $checkCmd){
226 liveuser 362
 
148 liveuser 363
		#另外儲存成指定的格式
364
		$checkCmd=array("cmd"=>$checkCmd);
226 liveuser 365
 
146 liveuser 366
		#記錄用於檢查服務是否在線的指令
367
		$checkListen[]=array("cmd"=>$checkCmd);
226 liveuser 368
 
146 liveuser 369
		}#foreach end
370
 
371
	}#if end
372
 
287 liveuser 373
#如果有指定 runAfertService
374
if(isset($parseArgu["content"]["runAfertService"])){
375
 
376
	#設置本服務要在哪些服務啟動後再啟動
377
	$unAfertService=$parseArgu["content"]["runAfertService"];
378
 
379
	}#if end
380
 
3 liveuser 381
#函式說明:
382
#將要執行的程式變成透過 systemd 來運行.
383
#回傳結果:
384
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
385
#$result["error"],錯誤訊息.
386
#$result["function"],當前執行的函式名稱.
387
#$result["argu"],所使用的參數.
388
#$result["content"],執行的結果.
389
#必填參數:
390
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
391
$conf["fileArgu"]=__FILE__;
392
#$conf["name"],字串,服務名稱,實際產生的system名稱會加上".service".
393
$conf["name"]=$serviceName;
148 liveuser 394
#$conf["cmds"],字串陣列,每個要執行的程式.
395
$conf["cmds"]=$cmds;
3 liveuser 396
#可省略參數:
148 liveuser 397
#$conf["params"],字串陣列,每個程式使用的參數.
398
$conf["params"]=$argus;
3 liveuser 399
#$conf["enable"],字串,預設為"true",代表該服務為enable.
400
$conf["enable"]=$runOnBoot;
401
#$conf["startNow"],字串,預設為"true",代表該服務要立刻執行.
402
$conf["startNow"]=$runNow;
130 liveuser 403
 
404
#如果存在 $keepServiceUp
405
if(isset($keepServiceUp)){
406
 
407
	#$conf["keepServiceUp"],字串,是否要另外增加確保服務有保持啟動的服務,預設為"false"代表否;反之為"true"代表是.
408
	$conf["keepServiceUp"]=$keepServiceUp;
409
 
410
	}#if end
411
 
412
#如果存在 $checkListen
413
if(isset($checkListen)){
414
 
146 liveuser 415
	#$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秒內結束為正常,反之為異常.
287 liveuser 416
	$conf["checkListen"]=$checkListen;
130 liveuser 417
 
418
	}#if end
226 liveuser 419
 
130 liveuser 420
#如果存在 $interval
421
if(isset($interval)){
422
 
423
	#$conf["cmd::keepServiceUp"]["interval"],字串,檢查沒問題後,要多少秒後再檢查一次,預設為30秒,亦即"30".
287 liveuser 424
	$conf["interval"]=$interval;
130 liveuser 425
 
426
	}#if end
427
 
287 liveuser 428
#如果有設定 $unAfertService
429
if(isset($unAfertService)){
430
 
431
	#$conf["runAfertService"],陣列,每個元素代表要在哪個服務啟動後再運行,預設不設定.
432
	$conf["runAfertService"]=$unAfertService;
433
 
434
	}#if end
435
 
3 liveuser 436
#參考資料:
437
#無.
438
#備註:
439
#無.
440
$registerService=cmd::registerService($conf);
441
unset($conf);
442
 
443
#如果執行失敗
444
if($registerService["status"]==="false"){
445
 
446
	#debug
447
	var_dump($registerService);
448
 
449
	#結束執行,回傳 shell 1
450
	exit(1);
451
 
452
	}#if end