This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathmanageProject.php
More file actions
219 lines (191 loc) · 6.09 KB
/
manageProject.php
File metadata and controls
219 lines (191 loc) · 6.09 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
<?php
/**
* Manage project.
*
* @author Timo Tijhof
* @since 1.0.0
* @package TestSwarm
*/
define( 'SWARM_ENTRY', 'SCRIPT' );
require_once __DIR__ . '/../inc/init.php';
class ManageProjectScript extends MaintenanceScript {
protected function init() {
$this->setDescription(
'Create or update a TestSwarm project.'
);
$this->registerOption( 'create', 'boolean', 'Pass this to the create if it doesn\'t exist.' );
$this->registerOption( 'delete', 'boolean', 'Pass this to remove a project and recursively delete all its jobs and runs.' );
$this->registerOption( 'id', 'value', 'ID of project (must be in format: "' . LoginAction::getNameValidationRegex() . '").' );
$this->registerOption( 'display-title', 'value', 'Display title (free form text, max: 255 chars)' );
$this->registerOption( 'password', 'value', 'Password for this project (omit to enter in interactive mode)' );
$this->registerOption( 'site-url', 'value', 'URL for this project (optional)' );
}
protected function execute() {
if ( $this->getOption( 'create' ) ) {
$this->create();
} elseif ( $this->getOption( 'delete' ) ) {
$this->delete();
} else {
$this->update();
}
}
protected function create() {
$action = ProjectAction::newFromContext( $this->getContext() );
$id = $this->getOption( 'id' );
$displayTitle = $this->getOption( 'display-title' );
$password = $this->getOption( 'password' );
$siteUrl = $this->getOption( 'site-url' );
if ( !$id || !$displayTitle ) {
$this->error( '--id and --display-title are required.' );
}
if ( !$password ) {
$inputConfirm = null;
$this->out( 'Enter password for this project (leave blank to abort):' );
while ( ( $input = $this->cliInputSecret() ) !== '' && $input !== $inputConfirm ) {
if ( !is_string( $inputConfirm ) ) {
$inputConfirm = $input;
$this->out( 'Re-enter password to confirm:' );
} else {
$inputConfirm = null;
$this->out( 'Passwords don\'t match, please try again:' );
}
}
if ( $input === '' ) {
$this->error( 'Password is required.' );
}
$password = $input;
}
$data = $action->create( $id, array(
'password' => $password,
'displayTitle' => $displayTitle,
'siteUrl' => $siteUrl,
) ) ;
/** @var array|null $data */
$error = $action->getError();
if ( $error ) {
$this->error( $error['info'] );
}
/** @var array $data */
$this->out(
'Project ' . $displayTitle . ' has been successfully created!' . PHP_EOL
. 'The following auth token has been generated for this project:' . PHP_EOL
. PHP_EOL
. "\t" . $data['authToken'] . PHP_EOL
. PHP_EOL
. 'You will need it to perform actions that require authentication.' . PHP_EOL
. 'If you ever loose it, you can generate a new token with the refreshProjectToken.php script.'
);
}
protected function update() {
$db = $this->getContext()->getDB();
$id = $this->getOption( 'id' );
$displayTitle = $this->getOption( 'display-title' );
$siteUrl = $this->getOption( 'site-url' );
$password = $this->getOption( 'password' );
if ( !$id ) {
$this->error( '--id is required.' );
}
// Check if this project exists.
$field = $db->getOne(str_queryf( 'SELECT id FROM projects WHERE id = %s;', $id ));
if ( !$field ) {
$this->error( 'Project does not exist. Set --create to create a project.' );
}
if ( !$displayTitle && !$siteUrl && !$password ) {
$this->error( 'Unable to perform update. No values provided.' );
}
if ( $displayTitle ) {
$isUpdated = $db->query(str_queryf(
'UPDATE projects SET display_title = %s, updated = %s WHERE id = %s;',
$displayTitle,
swarmdb_dateformat( SWARM_NOW ),
$id
));
if ( !$isUpdated ) {
$this->error( 'Failed to update database.' );
}
}
if ( $siteUrl ) {
$isUpdated = $db->query(str_queryf(
'UPDATE projects SET site_url = %s, updated = %s WHERE id = %s;',
$siteUrl,
swarmdb_dateformat( SWARM_NOW ),
$id
));
if ( !$isUpdated ) {
$this->error( 'Failed to update database.' );
}
}
if ( $password ) {
$isUpdated = $db->query(str_queryf(
'UPDATE projects SET password = %s, updated = %s WHERE id = %s;',
LoginAction::generatePasswordHash( $password ),
swarmdb_dateformat( SWARM_NOW ),
$id
));
if ( !$isUpdated ) {
$this->error( 'Failed to update database.' );
}
}
$this->out( 'Project has been updated.' );
}
public function delete() {
$db = $this->getContext()->getDB();
$projectID = $this->getOption( 'id' );
if ( !$projectID ) {
$this->error( '--id is required.' );
}
// Ensure the project exists.
$field = $db->getOne(str_queryf( 'SELECT id FROM projects WHERE id = %s;', $projectID ));
if ( !$field ) {
$this->error( 'Project does not exist.' );
}
$this->out( "Are you sure you want to remove project '$projectID' and all associated jobs and runs? (Y/N)" );
$answer = $this->cliInput();
if ( $answer !== 'Y' ) {
$this->error( 'Aborted.' );
return;
}
$batchSize = 100;
$stats = array();
while ( true ) {
$jobRows = $db->getRows(str_queryf(
'SELECT id
FROM jobs
WHERE project_id = %s
ORDER BY id
LIMIT %u;',
$projectID,
$batchSize
));
if ( !$jobRows ) {
// Done
break;
}
$jobIDs = array_map( function ( $row ) { return $row->id; }, $jobRows );
$this->out( '...deleting ' . count( $jobIDs ) . ' jobs' );
$action = WipejobAction::newFromContext( $this->getContext() );
$result = $action->doWipeJobs( 'delete', $jobIDs, $batchSize );
$this->mergeStats( $stats, $result );
}
foreach ( $stats as $key => $val ) {
$this->out( "deleted $key rows: $val");
}
$this->out( '' );
$this->out( '...deleting project credentials' );
$db->query(str_queryf( 'DELETE FROM projects WHERE id = %s;', $projectID ));
$this->out( 'project has been deleted.' );
$this->out( '' );
$this->out( 'Done!' );
}
protected function mergeStats( array &$target, array $add ) {
foreach ( $add as $key => $val ) {
if ( isset( $target[$key] ) ) {
$target[$key] += $val;
} else {
$target[$key] = $val;
}
}
}
}
$script = ManageProjectScript::newFromContext( $swarmContext );
$script->run();