Subversion Repositories php-qbpwcf

Rev

Rev 387 | 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;
387 liveuser 90
	echo "--name [service name] 服務的名稱".PHP_EOL;
130 liveuser 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");
388 liveuser 180
$conf["skipableVariableName"]=array("runOnBoot","runNow","keepServiceUp","interval","checkListenIp","checkListenAddr","checkCmd","runAfterService");
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
 
388 liveuser 210
	#debug
211
	var_dump($checkArguments);
212
 
3 liveuser 213
	#提示用法
214
	help();
226 liveuser 215
 
3 liveuser 216
	#結束執行
217
	exit;
218
 
219
	}#if end
148 liveuser 220
 
221
#針對每個要執行的指令字串
222
foreach($parseArgu["content"]["cmd"] as $index => $cmdStr){
223
 
224
	#解析成程式與參數
225
	#函式說明:
226 liveuser 226
	#將指令字串解析成陣列,方便給予 external::callShell 使用
148 liveuser 227
	#回傳結果:
228
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
229
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
230
	#$result["function"],當前執行的函式名稱.
231
	#$result["content"],解析好的指令陣列.
232
	#$result["cmd"],解析好的指令名稱.
233
	#$result["argus"],解析好的參數陣列.
234
	#$result["argu"],所使用的參數.
235
	#必填參數
236
	#$conf["cmdStr"],字串,要解析的指令字串
237
	$conf["cmdStr"]=$cmdStr;
238
	#可省略參數:
239
	#無.
240
	#參考資料:
241
	#無.
242
	#備註:
243
	#無.
244
	$parseCmdString=cmd::parseCmdString($conf);
245
	unset($conf);
246
 
247
	#若執行失敗
248
	if($parseCmdString["status"]==="false"){
249
 
250
		#印出結果
251
		var_dump($parseCmdString);
226 liveuser 252
 
148 liveuser 253
		#結束執行
254
		exit;
255
 
256
		}#if end
226 liveuser 257
 
148 liveuser 258
	#取得要執行的程式
259
	$cmds[]=$parseCmdString["cmd"];
226 liveuser 260
 
148 liveuser 261
	#若存在程式對應的參數
262
	if(isset($parseCmdString["argus"])){
226 liveuser 263
 
148 liveuser 264
		#取得程式對應的參數
265
		$argus[]=$parseCmdString["argus"];
226 liveuser 266
 
148 liveuser 267
		}#if end
226 liveuser 268
 
148 liveuser 269
	#反之
270
	else{
226 liveuser 271
 
148 liveuser 272
		#設置無該程式的參數
273
		$argus[]=null;
226 liveuser 274
 
148 liveuser 275
		}#else end
3 liveuser 276
 
148 liveuser 277
	}#foreache end
278
 
279
#取得要執行服務的指令
280
#$cmds=;
281
 
3 liveuser 282
#取得目標參數
283
$serviceName=$parseArgu["content"]["name"][0];
284
 
285
#預設為 false
286
$runOnBoot="false";
287
 
148 liveuser 288
#如果 runOnBoot 參數是 yes
289
if($parseArgu["content"]["runOnBoot"][0]==="yes"){
3 liveuser 290
 
291
	#轉換為 "true"
292
	$runOnBoot="true";
293
 
294
	}#if end
295
 
296
#預設為 false
297
$runNow="false";
298
 
148 liveuser 299
#如果 runNow參數 是 yes
300
if($parseArgu["content"]["runNow"][0]==="yes"){
3 liveuser 301
 
302
	#轉換為 "true"
303
	$runNow="true";
226 liveuser 304
 
130 liveuser 305
	}#if end
3 liveuser 306
 
148 liveuser 307
#預設不用 cmd::keepServiceUp 來確保服務
308
$keepServiceUp="false";
309
 
130 liveuser 310
#如果有指定 keepServiceUp
311
if(isset($parseArgu["content"]["keepServiceUp"][0])){
312
 
148 liveuser 313
	#如果參數 keepServiceUp 為 "true"
314
	if($parseArgu["content"]["keepServiceUp"][0]==="yes"){
226 liveuser 315
 
148 liveuser 316
		#設置 keepServiceUp 為 "true"
317
		$keepServiceUp="true";
226 liveuser 318
 
148 liveuser 319
		}#if end
130 liveuser 320
 
3 liveuser 321
	}#if end
322
 
130 liveuser 323
#如果有指定 interval
324
if(isset($parseArgu["content"]["interval"][0])){
325
 
326
	#設定interval
327
	$interval=$parseArgu["content"]["interval"][0];
328
 
329
	}#if end
226 liveuser 330
 
152 liveuser 331
#初始化儲存判斷的方式
332
$checkListen=array();
226 liveuser 333
 
130 liveuser 334
#如果有指定 checkListenIp
335
if(isset($parseArgu["content"]["checkListenIp"])){
336
 
337
	#針對每個 checkListenIp
338
	foreach($parseArgu["content"]["checkListenIp"] as $checkListenIp){
226 liveuser 339
 
130 liveuser 340
		#記錄要檢查的ip & port & protocol
341
		$checkListen[]=array("ip"=>$checkListenIp);
226 liveuser 342
 
130 liveuser 343
		}#foreach end
226 liveuser 344
 
130 liveuser 345
	}#if end
226 liveuser 346
 
130 liveuser 347
#如果有指定 checkListenAddr
348
if(isset($parseArgu["content"]["checkListenAddr"])){
349
 
350
	#針對每個 checkListenAddr
351
	foreach($parseArgu["content"]["checkListenAddr"] as $checkListenAddr){
226 liveuser 352
 
130 liveuser 353
		#記錄要檢查的 unix domain socket 路徑
354
		$checkListen[]=array("addr"=>$checkListenAddr);
226 liveuser 355
 
130 liveuser 356
		}#foreach end
357
 
358
	}#if end
359
 
146 liveuser 360
#如果有指定 checkCmd
361
if(isset($parseArgu["content"]["checkCmd"])){
362
 
363
	#針對每個 checkCmd
364
	foreach($parseArgu["content"]["checkCmd"] as $checkCmd){
226 liveuser 365
 
148 liveuser 366
		#另外儲存成指定的格式
367
		$checkCmd=array("cmd"=>$checkCmd);
226 liveuser 368
 
146 liveuser 369
		#記錄用於檢查服務是否在線的指令
370
		$checkListen[]=array("cmd"=>$checkCmd);
226 liveuser 371
 
146 liveuser 372
		}#foreach end
373
 
374
	}#if end
375
 
387 liveuser 376
#如果有指定 runAftertService
377
if(isset($parseArgu["content"]["runAftertService"])){
287 liveuser 378
 
379
	#設置本服務要在哪些服務啟動後再啟動
387 liveuser 380
	$runAftertService=$parseArgu["content"]["runAftertService"];
287 liveuser 381
 
382
	}#if end
383
 
3 liveuser 384
#函式說明:
385
#將要執行的程式變成透過 systemd 來運行.
386
#回傳結果:
387
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
388
#$result["error"],錯誤訊息.
389
#$result["function"],當前執行的函式名稱.
390
#$result["argu"],所使用的參數.
391
#$result["content"],執行的結果.
392
#必填參數:
393
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
394
$conf["fileArgu"]=__FILE__;
395
#$conf["name"],字串,服務名稱,實際產生的system名稱會加上".service".
396
$conf["name"]=$serviceName;
148 liveuser 397
#$conf["cmds"],字串陣列,每個要執行的程式.
398
$conf["cmds"]=$cmds;
3 liveuser 399
#可省略參數:
148 liveuser 400
#$conf["params"],字串陣列,每個程式使用的參數.
401
$conf["params"]=$argus;
3 liveuser 402
#$conf["enable"],字串,預設為"true",代表該服務為enable.
403
$conf["enable"]=$runOnBoot;
404
#$conf["startNow"],字串,預設為"true",代表該服務要立刻執行.
405
$conf["startNow"]=$runNow;
130 liveuser 406
 
407
#如果存在 $keepServiceUp
408
if(isset($keepServiceUp)){
409
 
410
	#$conf["keepServiceUp"],字串,是否要另外增加確保服務有保持啟動的服務,預設為"false"代表否;反之為"true"代表是.
411
	$conf["keepServiceUp"]=$keepServiceUp;
412
 
413
	}#if end
414
 
415
#如果存在 $checkListen
416
if(isset($checkListen)){
417
 
146 liveuser 418
	#$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秒內結束為正常,反之為異常.
297 liveuser 419
	$conf["cmd::keepServiceUp"]["checkListen"]=$checkListen;
130 liveuser 420
 
421
	}#if end
226 liveuser 422
 
130 liveuser 423
#如果存在 $interval
424
if(isset($interval)){
425
 
426
	#$conf["cmd::keepServiceUp"]["interval"],字串,檢查沒問題後,要多少秒後再檢查一次,預設為30秒,亦即"30".
297 liveuser 427
	$conf["cmd::keepServiceUp"]["interval"]=$interval;
130 liveuser 428
 
429
	}#if end
430
 
387 liveuser 431
#如果有設定 $runAftertService
432
if(isset($runAftertService)){
287 liveuser 433
 
387 liveuser 434
	#$conf["runAftertService"],陣列,每個元素代表要在哪個服務啟動後再運行,預設不設定.
435
	$conf["runAftertService"]=$runAftertService;
287 liveuser 436
 
437
	}#if end
438
 
3 liveuser 439
#參考資料:
440
#無.
441
#備註:
442
#無.
443
$registerService=cmd::registerService($conf);
444
unset($conf);
445
 
446
#如果執行失敗
447
if($registerService["status"]==="false"){
448
 
449
	#debug
450
	var_dump($registerService);
451
 
452
	#結束執行,回傳 shell 1
453
	exit(1);
454
 
455
	}#if end