Subversion Repositories php-qbpwcf

Rev

Rev 130 | Rev 145 | 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;
3 liveuser 95
	#結束執行
96
	exit;
97
 
130 liveuser 98
	}#function help end
99
 
3 liveuser 100
#函式說明:
101
#解析參數.
102
#回傳結果:
103
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
104
#$reuslt["error"],執行不正常結束的錯訊息陣列.
105
#$result["function"],當前執行的函式名稱.
106
#$result["content"],解析好的參數陣列.
107
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
108
#$result["program"],字串,執行的程式名稱.
109
#必填參數:
110
#無.
111
#可省略參數:
112
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
113
$conf["helpFunc"]="help";
114
#$conf["parseFormVar"],解析表單變數(get\post),預設為"false"不使用;反之設置為"true".
115
$conf["parseFormVar"]="false";
116
#參考資料:
117
#無.
118
#備註:
119
#僅能在命令列底下執行.
120
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
121
$parseArgu=cmd::parseArgu($conf);
122
unset($conf);
123
 
124
#如果取得參數失敗
125
if($parseArgu["status"]==="false"){
126
 
127
	#設置執行失敗
128
	$result["status"]="false";
129
 
130
	#設置執行錯誤訊息
131
	$result["error"]=$parseArgu;
132
 
133
	#印出訊息
134
	var_dump($result);
135
 
136
	#結束執行
137
	exit;
138
 
139
	}#if end
140
 
141
#檢查參數
142
#函式說明:
143
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
144
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
145
#$reuslt["error"],執行不正常結束的錯訊息陣列.
146
#$result["function"],當前執行的函式名稱.
147
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
148
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
149
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
150
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
151
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
152
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
153
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
154
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
155
#必填寫的參數:
156
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
157
$conf["varInput"]=&$parseArgu["content"];
158
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
159
$conf["referenceVarKey"]="variableCheck::checkArguments";
160
#可以省略的參數:
161
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
162
$conf["mustBeFilledVariableName"]=array("cmd","name");
163
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
164
$conf["mustBeFilledVariableType"]=array("array","array");
165
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
166
#$conf["canBeEmptyString"]="false";
167
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
168
#$conf["canNotBeEmpty"]=array();
169
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
170
#$conf["canBeEmpty"]=array();
171
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
172
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
173
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
130 liveuser 174
$conf["skipableVariableName"]=array("runOnBoot","runNow","keepServiceUp","interval","checkListenIp","checkListenAddr");
3 liveuser 175
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
130 liveuser 176
$conf["skipableVariableType"]=array("array","array","array","array","array");
3 liveuser 177
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
130 liveuser 178
$conf["skipableVarDefaultValue"]=array(array("yes"),array("yes"),null,null,null,null);
3 liveuser 179
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
180
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
181
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
182
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
183
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
184
#$conf["arrayCountEqualCheck"][]=array();
185
#參考資料來源:
186
#array_keys=>http://php.net/manual/en/function.array-keys.php
187
$checkArguments=variableCheck::checkArguments($conf);
188
unset($conf);
189
 
190
#若執行失敗
191
if($checkArguments["status"]==="false"){
192
 
193
	#印出結果
194
	var_dump($checkArguments);
195
 
196
	#結束執行
197
	exit;
198
 
199
	}#if end
200
 
201
#若檢查不通過
202
if($checkArguments["passed"]==="false"){
203
 
204
	#debug
205
	var_dump($checkArguments);
206
 
207
	#提示用法
208
	help();
209
 
210
	#結束執行
211
	exit;
212
 
213
	}#if end
214
 
215
#取得指令陣列
216
$cmds=$parseArgu["content"]["cmd"];
217
 
218
#取得目標參數
219
$serviceName=$parseArgu["content"]["name"][0];
220
 
221
#取得runOnBoot參數
222
$runOnBoot=$parseArgu["content"]["runOnBoot"][0];
223
 
224
#預設為 false
225
$runOnBoot="false";
226
 
227
#如果是 yes
228
if($runOnBoot==="yes"){
229
 
230
	#轉換為 "true"
231
	$runOnBoot="true";
232
 
233
	}#if end
234
 
235
#取得runNow參數
236
$runNow=$parseArgu["content"]["runNow"][0];
237
 
238
#預設為 false
239
$runNow="false";
240
 
241
#如果是 yes
242
if($runNow==="yes"){
243
 
244
	#轉換為 "true"
245
	$runNow="true";
130 liveuser 246
 
247
	}#if end
3 liveuser 248
 
130 liveuser 249
#如果有指定 keepServiceUp
250
if(isset($parseArgu["content"]["keepServiceUp"][0])){
251
 
252
	#設定keepServiceUp
253
	$keepServiceUp=$parseArgu["content"]["keepServiceUp"][0];
254
 
3 liveuser 255
	}#if end
256
 
130 liveuser 257
#如果有指定 interval
258
if(isset($parseArgu["content"]["interval"][0])){
259
 
260
	#設定interval
261
	$interval=$parseArgu["content"]["interval"][0];
262
 
263
	}#if end
264
 
265
#初始化要檢查的服務名稱
266
$checkListen=array("name"=>$conf["name"]);
267
 
268
#如果有指定 checkListenIp
269
if(isset($parseArgu["content"]["checkListenIp"])){
270
 
271
	#針對每個 checkListenIp
272
	foreach($parseArgu["content"]["checkListenIp"] as $checkListenIp){
273
 
274
		#記錄要檢查的ip & port & protocol
275
		$checkListen[]=array("ip"=>$checkListenIp);
276
 
277
		}#foreach end
278
 
279
	}#if end
280
 
281
#如果有指定 checkListenAddr
282
if(isset($parseArgu["content"]["checkListenAddr"])){
283
 
284
	#針對每個 checkListenAddr
285
	foreach($parseArgu["content"]["checkListenAddr"] as $checkListenAddr){
286
 
287
		#記錄要檢查的 unix domain socket 路徑
288
		$checkListen[]=array("addr"=>$checkListenAddr);
289
 
290
		}#foreach end
291
 
292
	}#if end
293
 
3 liveuser 294
#建立暫存檔案
295
#函式說明:
296
#於本套件位置底下的tmp資料夾下建立與回傳暫存檔案名稱路徑 
297
#回傳結果:
298
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
299
#$result["error"],錯誤訊息.
300
#$result["function"],當前執行的函數名稱.
301
#$result["content"],暫存檔案的路徑與名稱.
302
#$result["fileName"],暫存檔案的名稱.
303
#$result["path"],暫存檔案的路徑.
304
#必填參數:
305
#無.
306
#可省略參數:
307
#無.
308
#參考資料:
309
#無.
310
#備註:
311
#無.
38 liveuser 312
$conf=array();
313
$createTempFile=fileAccess::createTempFile($conf);
314
unset($conf);
3 liveuser 315
 
316
#如果建立暫存檔案失敗
317
if($createTempFile["status"]==="false"){
318
 
319
	#debug
320
	var_dump($createTempFile);
321
 
322
	#結束執行,回傳 shell 1
323
	exit(1);
324
 
325
	}#if end
326
 
327
#撰寫要執行的程式內容
328
#函式說明:
329
#將多行字串寫入到檔案
330
#回傳結果:
331
#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
332
#$result["error"],錯誤訊息陣列.
333
#$result["function"],當前執行函數的名稱.
334
#必填參數:
335
#$conf["fileName"],字串,爲要編輯的檔案名稱
336
$conf["fileName"]=$createTempFile["content"];
337
#$conf["inputString"],字串陣列,爲要寫入到 $conf["fileName"] 裏面的內容. $conf["inputString"][$i] 代表第 $i+1 行。
338
$conf["inputString"]=$cmds;
339
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
340
$conf["fileArgu"]=__FILE__;
341
#可省略參數:
342
#$conf["writeMethod"]="a";#爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入。
343
#參考資料:
344
#無.
345
#備註:
346
#無.
347
$writeMultiLine=fileAccess::writeMultiLine($conf);
348
unset($conf);
349
 
350
#如果建立檔案失敗
351
if($writeMultiLine["status"]==="false"){
352
 
353
	#debug
354
	var_dump($writeMultiLine);
355
 
356
	#結束執行,回傳 shell 1
357
	exit(1);
358
 
359
	}#if end
131 liveuser 360
 
361
#path of bin for systemctl 
362
$pathOfBinForSystemctl="/var/qbpwcf/bin_for_systemctl/";
363
 
364
#service path
365
$servicePath=$pathOfBinForSystemctl.$serviceName.".sh";
366
 
367
#移動程式到 /var/qbpwcf/bin_for_systemctl/ 底下
3 liveuser 368
#函式說明:
369
#移動檔案
370
#回傳結果:
371
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
372
#$result["function"],當前執行的函數名稱
373
#$result["error"],錯誤訊息.
374
#$result["content"],檔案輸出後的位置與名稱.
375
#必填參數:
376
#$conf["from"],字串,要移動的檔案名稱與位置.
377
$conf["from"]=$createTempFile["content"];
378
#$conf["to"],字串,要移動到的位置與名稱
131 liveuser 379
$conf["to"]=$servicePath;
3 liveuser 380
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
381
$conf["fileArgu"]=__FILE__;
382
#可省略參數:
383
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
384
#$conf["commentsArray"]=array("");
385
#參考資料:
386
#無.
387
#備註:
388
#無.
389
$mv=fileAccess::mv($conf);
390
unset($conf);
391
 
392
#如果移動檔案失敗
393
if($mv["status"]==="false"){
394
 
395
	#debug
396
	var_dump($mv);
397
 
398
	#結束執行,回傳 shell 1
399
	exit(1);
400
 
401
	}#if end
402
 
403
#函式說明:
404
#將要執行的程式變成透過 systemd 來運行.
405
#回傳結果:
406
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
407
#$result["error"],錯誤訊息.
408
#$result["function"],當前執行的函式名稱.
409
#$result["argu"],所使用的參數.
410
#$result["content"],執行的結果.
411
#必填參數:
412
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
413
$conf["fileArgu"]=__FILE__;
414
#$conf["name"],字串,服務名稱,實際產生的system名稱會加上".service".
415
$conf["name"]=$serviceName;
416
#$conf["cmd"],字串,要執行的指令.
131 liveuser 417
$conf["cmd"]=$servicePath;
3 liveuser 418
#可省略參數:
419
#$conf["params"],字串陣列,指令要使用的參數.
420
#$conf["params"]=array("");
421
#$conf["enable"],字串,預設為"true",代表該服務為enable.
422
$conf["enable"]=$runOnBoot;
423
#$conf["startNow"],字串,預設為"true",代表該服務要立刻執行.
424
$conf["startNow"]=$runNow;
130 liveuser 425
 
426
#如果存在 $keepServiceUp
427
if(isset($keepServiceUp)){
428
 
429
	#$conf["keepServiceUp"],字串,是否要另外增加確保服務有保持啟動的服務,預設為"false"代表否;反之為"true"代表是.
430
	$conf["keepServiceUp"]=$keepServiceUp;
431
 
432
	}#if end
433
 
434
#如果存在 $checkListen
435
if(isset($checkListen)){
436
 
437
	#$conf["cmd::keepServiceUp"]["checkListen"],陣列,用來判斷服務是否有正確啟動的條件,一個元素代表,其中要有一個socket info符合之,若元素的key為"ip",則其數值為[ipv4/ipv6]:port:protocol(tcp/udp);若元素的key為"addr",則其數值為unix domain socket 的位置與名稱;若元素的key為"name",則其數值為程序名稱.
438
	$conf["cmd::keepServiceUp"]["checkListen"]=array(array("ip"=>169.254.1.1:443/tcp,"addr"=>/usr/lib/qbpwcf/qbpwcf-usock.sock,"name"=>"addr"),"name"=>"httpd");
439
 
440
	}#if end
441
 
442
#如果存在 $interval
443
if(isset($interval)){
444
 
445
	#$conf["cmd::keepServiceUp"]["interval"],字串,檢查沒問題後,要多少秒後再檢查一次,預設為30秒,亦即"30".
446
	$conf["cmd::keepServiceUp"]["interval"]=$interval;
447
 
448
	}#if end
449
 
3 liveuser 450
#參考資料:
451
#無.
452
#備註:
453
#無.
454
$registerService=cmd::registerService($conf);
455
unset($conf);
456
 
457
#如果執行失敗
458
if($registerService["status"]==="false"){
459
 
460
	#debug
461
	var_dump($registerService);
462
 
463
	#結束執行,回傳 shell 1
464
	exit(1);
465
 
466
	}#if end