-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodeRunner.php
More file actions
executable file
·224 lines (186 loc) · 5.83 KB
/
nodeRunner.php
File metadata and controls
executable file
·224 lines (186 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env php
<?php declare(strict_types=1);
/* {{{ */
/**
* runner/manager for the 'cluster-queue' project
* ----------------------------------------------------------------------------
* @license LGPL Version 3 http://www.gnu.org/licenses/lgpl-3.0.txt
* @copyright Copyright (c) 2020 by Florian Blasel
* @author Florian Blasel <flobee.code@gmail.com>
* ----------------------------------------------------------------------------
* @category cli
* @package cluster-queue
* @subpackage core
* Created: 2020-11-07
*/
/* }}} */
/**
* php cluster queue manager.
*
* - Scann for server configs,
* - replace params and
* - create a concrete config structure for a configured node
* for a specific node
* - deploy build configs and configs from skel/
* - archive stuff we may need later or for backup
* - execute commands to install, manage, maintain a node
* - in specific order
*
* Requirements:
* - Mode: Admins
* - root access required
* - pub key auth must be possible at the nodes
*/
//
// pre check
//
if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) {
$mesg = 'Warning: This program should be called in the CLI version of PHP! '
. 'Not within the ' . PHP_SAPI . ' SAPI' . PHP_EOL;
exit( $mesg );
}
//
// boot
//
$inputCnt = $_SERVER['argc'];
$shortopts = '';
$longopts = array(
// Configuration suffix to load eg: debnode configs:
// - loads 'nodeconfigs_debnode.php' 'nodeconfigs_debnode_default.php'
"config:", // Required value
// action to perform
"action:", // Required value
// Optional flag for a REAL execution! **Danger**! Without: It gives you a job list.
"execute",
// Flag to confirm each execution (default: disabled), if --execute set
"confirm",
// Optional flag to reduce shell output to a minimum (logging always enabled)
"no-debug",
);
$actions = array(
// handle all in default config exists for _ALL_ nodes (actions, deploy, archive).
'defaults',
// creates/ builds config/script files to be deployed or executed to/for the target
'create','build',
// node configs which may need the create/build part.
'configs',
//
// -- custom ---
//
// outputs ONLY deploy actions to deploy to the target
'deploy',
// outputs ONLY archive actions to archive from the target
'archive',
// output ONLY actions to execute at the target to setup/configure a node/ server/ service details
'actions'
);
$input = getopt( $shortopts, $longopts );
$usageMesg = 'Usage: ./php-cluster-queue [--config=<nodes config>] [--action= ' . implode( '|', $actions ) . ']'
. PHP_EOL . 'eg: ./php-cluster-queue --config=debnode --action=create' . PHP_EOL;
if ( $inputCnt < 3 ) {
exit( $usageMesg );
}
//
// check/ validate input:
//
try {
if ( isset($input['config']) && ctype_alnum( $input['config'] ) ) {
$configNodesLoc = 'src/config/nodeconfigs_' . $input['config'] . '.php';
if ( !file_exists( $configNodesLoc ) ) {
throw new Exception( 'Invalid config location: "' . $configNodesLoc . '"' );
} else {
$configNodes = require_once $configNodesLoc;
}
$configsDefaultLoc = 'src/config/nodeconfigs_' . $input['config'] . '_default.php';
if ( !file_exists( $configsDefaultLoc ) ) {
// no but, but to report
echo 'Invalid default config location: "' . $configsDefaultLoc . '"' . PHP_EOL;
$configsDefault = array();
} else {
$configsDefault = require_once $configsDefaultLoc;
}
} else {
throw new Exception( 'Invalid/ missing config value' );
}
if ( !isset($input['action']) || !in_array( $input['action'], $actions ) ) {
throw new Exception( 'Invalid action value ( ' . implode( ' | ', $actions ) . ' )' );
}
}
catch ( Exception $ex ) {
echo $ex->getMessage() . PHP_EOL;
echo $usageMesg;
exit( 1 );
}
//
// boot
//
require_once 'vendor/autoload.php';
$logOptions = array(
'logfile' => './logs/'. $input['config'] .'.log',
'way' => 'a',
'logLevel' => 7,
'lineFormat'=>'%5$s',
'maxfilesize' => (1024 * 1024 * 1), // 1M
);
$logger = new Mumsys_Logger_File( $logOptions );
/** @var QueueHelper Helper/manager object */
$runner = new QueueHelper( $input, $configNodes, $configsDefault, $logger );
//
// run
//
switch ( $input['action'] ) {
case 'create':
case 'build':
$runner->configsCreate();
break;
case 'defaults':
$runner->queueDefaults();
$runner->buildQueue();
$runner->run();
break;
case 'configs':
$runner->configsCreate();
$runner->queueConfigs();
$runner->buildQueue();
$runner->run();
break;
// --- custom ---
// use only all deploy actions
// split by type vs queue dependencies: difficult! this only outputs deploy tasks
case 'deploy':
$runner->configsCreate();
$runner->queueDefaults();
$runner->queueConfigs();
$runner->buildQueue();
$runner->run('deploy');
break;
// like deploy for archive
case 'archive':
$runner->configsCreate();
$runner->queueDefaults();
$runner->queueConfigs();
$runner->buildQueue();
$runner->run('archive');
break;
// like deploy for actions/excecutions
case 'actions':
case 'excecutions':
$runner->configsCreate();
$runner->queueDefaults();
$runner->queueConfigs();
$runner->buildQueue();
$runner->run('execute');
break;
// may does not make sence because of the defaults for all nodes (if there are
// dependencies between some nodes)
// case 'all':
// $runner->configsCreate();
//
// $runner->queueDefaults();
// $runner->queueConfigs();
//
// $runner->buildQueue();
//
// $runner->run();
// break;
}