-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatejob.php
401 lines (344 loc) · 17.4 KB
/
createjob.php
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
require __DIR__ . '/vendor/autoload.php';
session_start();
if (isset($_SESSION['connected'])) {
include 'header.php';
include 'core.php';
$errorMessage = "";
$throwError = False;
$successMessage = "";
$throwSuccess = False;
// On check si on a soumit le formulaire pour run les containers
if (isset($_POST['submitJob']) && !empty($_POST['submitJob'])) {
$data = unserialize(urldecode($_POST['value']));
$cifsEnabled = false;
if (isset($_POST['contextarguments'])) {
$contextarguments = $_POST['contextarguments'];
} else {
$contextarguments = "";
}
$envArray = array('JOBTORUN' => $_POST['ressourceToRun'],
'ROOTFOLDER' => '/folderrunner',
'CONTEXTARGUMENTS' => base64_encode($contextarguments));
// On check la configuration CIFS
if (@$_POST['switchmontagecifs'] == "on") {
$endpoint = $_POST['CIFSendpoint'];
$source = $_POST['CIFSsourcePath'];
$dest = $_POST['CIFSdest'];
if (($source == "") || ($dest == "")) {
$throwError = True;
$errorMessage = "You need to set a source and a destination path for the CIFS mount.";
} else {
$cifsEnabled = true;
// On load le fichier de credentials cifs
$string = file_get_contents("./conf/conf_cifs.json");
$cifsData = json_decode($string,true);
$cifsData = $cifsData[$endpoint];
$cifs['endpoint'] = $endpoint;
$cifs['user'] = $cifsData['user'];
$cifs['password'] = $cifsData['password'];
$cifs['domain'] = $cifsData['domain'];
$cifs['source'] = $source;
$cifs['dest'] = $dest;
$cifsArray = array('CIFSENDPOINT' => $cifs['endpoint'],
'CIFSUSER' => $cifs['user'],
'CIFSPASSWORD' => $cifs['password'],
'CIFSDOMAIN' => $cifs['domain'],
'CIFSSOURCE' => $cifs['source'],
'CIFSDEST' => $cifs['dest']);
// On ajoute aux variables d'environnement celles pour le montage CIFS
$envArray = array_merge($envArray,$cifsArray);
}
}
// On check si on a besoin des creds de proxy
if (@$_POST['switchproxy'] == "on") {
$proxyArray = array('HTTP_PROXY' => PROXY_CONF,
'HTTPS_PROXY' => PROXY_CONF,
'NO_PROXY' => NO_PROXY_CONF);
// On ajoute aux variables d'environnement celles pour le montage CIFS
$envArray = array_merge($envArray,$proxyArray);
}
// Creation de la configmap
if(isset($_POST['secretmap']) && ($_POST['secretmap'] != "")) {
$configName = "config";
if (isset($_POST['configName']) && ($_POST['configName'] != "")) {
$configName = $_POST['configName'];
}
$jsonData = '{"yaml":"apiVersion: v1\nkind: Secret\nmetadata:\n name: '.$_POST['jobName'].'-secret\n namespace: '.KUBERNETES_NAMESPACE.'\ndata:\n '.$configName.': '.base64_encode($_POST['secretmap']).'\n"}';
$response = executeDeployement($jsonData);
$message = json_decode($response,True)['message'];
$lastElem = preg_split('/ /',$message);
$lastElem = end($lastElem);
if($lastElem == "created\n") {
$throwSuccess = True;
$successMessage .= " Config Created\n";
}
}
// Check si l'expression CRON est bien renseignée
if (($_POST['runStrategy'] == "cron-recurrent-job") && ($_POST['cronSchedule'] == "")) {
$throwError = True;
$errorMessage = "You need to set a CRON schedule expression in order to run a CRON JOB";
}
else {
$memoryLimit="80000Mi";
$cpuLimit="25";
$memoryRequest="1000Mi";
$cpuRequest="1";
// Pour les CRON JOBS
if ($_POST['runStrategy'] == "cron-recurrent-job") {
$version="v1beta1";
$kind="CronJob";
$spec='\n concurrencyPolicy: Allow\n failedJobsHistoryLimit: 10\n jobTemplate:\n spec:\n activeDeadlineSeconds: 10\n backoffLimit: 0\n completions: 1\n parallelism: 1';
$env='\n - env:';
// On ajoute les variables d'environnement
foreach ($envArray as $name => $value) {
$env .= '\n - name: '.$name.'\n value: '.$value;
}
// On ajoute à la main les secrets pour la connexion NEXUS
$env .='\n - name: PASSWORDNEXUS\n valueFrom:\n secretKeyRef:\n key: PASSWORDNEXUS\n name: nexus-id\n - name: USERNEXUS\n valueFrom:\n secretKeyRef:\n key: USERNEXUS\n name: nexus-id';
// Construction du yaml
$jsonData = '{"yaml":"apiVersion: batch/'.$version.'\nkind: '.$kind.'\nmetadata:\n labels:\n cattle.io/creator: kubernetes-job-orchestrator\n name: '.$_POST['jobName'].'\n namespace: '.KUBERNETES_NAMESPACE.'\nspec:'.$spec.'\n template:\n spec:\n containers:'.$env.'\n image: '.$_POST['selectRunner'].'\n imagePullPolicy: IfNotPresent\n name: '.$_POST['jobName'].'\n resources:\n limits:\n cpu: \"'.$cpuLimit.'\"\n memory: '.$memoryLimit.'\n requests:\n cpu: \"'.$cpuRequest.'\"\n memory: '.$memoryRequest.'\n securityContext:\n allowPrivilegeEscalation: true\n privileged: true\n';
if(isset($_POST['secretmap']) && ($_POST['secretmap'] != "")) {
// Données de la configmaps
$configmap = ' volumeMounts:\n - mountPath: /folderrunner/conf\n name: '.$_POST['jobName'].'-secret\n volumes:\n - name: '.$_POST['jobName'].'-secret\n secret:\n secretName: '.$_POST['jobName'].'-secret\n';
$jsonData .= $configmap;
}
$jsonData .= ' imagePullSecrets:\n - name: registrygitlab-curie\n restartPolicy: Never\n schedule: \''.$_POST['cronSchedule'].'\' \n suspend: false","defaultNamespace":"'.KUBERNETES_NAMESPACE.'"}';
} else {
$version="v1";
$kind="Job";
$spec='\n backoffLimit: 0\n completions: 1\n parallelism: 1';
$env='\n - env:';
// On ajoute les variables d'environnement
foreach ($envArray as $name => $value) {
$env .= '\n - name: '.$name.'\n value: '.$value;
}
// On ajoute à la main les secrets pour la connexion NEXUS
$env .='\n - name: PASSWORDNEXUS\n valueFrom:\n secretKeyRef:\n key: PASSWORDNEXUS\n name: nexus-id\n - name: USERNEXUS\n valueFrom:\n secretKeyRef:\n key: USERNEXUS\n name: nexus-id';
// On génère la ressource Kubernetes à envoyer à l'API
$jsonData = '{"yaml":"apiVersion: batch/'.$version.'\nkind: '.$kind.'\nmetadata:\n labels:\n cattle.io/creator: kubernetes-job-orchestrator\n name: '.$_POST['jobName'].'\n namespace: '.KUBERNETES_NAMESPACE.'\nspec:'.$spec.'\n template:\n metadata:\n labels:\n job-name: '.$_POST['jobName'].'\n spec:\n containers:'.$env.'\n image: '.$_POST['selectRunner'].'\n imagePullPolicy: IfNotPresent\n name: '.$_POST['jobName'].'\n resources:\n limits:\n cpu: \"'.$cpuLimit.'\"\n memory: '.$memoryLimit.'\n requests:\n cpu: \"'.$cpuRequest.'\"\n memory: '.$memoryRequest.'\n securityContext:\n allowPrivilegeEscalation: true\n privileged: true\n';
if(isset($_POST['secretmap']) && ($_POST['secretmap'] != "")) {
// Données de la configmaps
$configmap = ' volumeMounts:\n - mountPath: /folderrunner/conf\n name: '.$_POST['jobName'].'-secret\n volumes:\n - name: '.$_POST['jobName'].'-secret\n secret:\n secretName: '.$_POST['jobName'].'-secret\n';
$jsonData .= $configmap;
}
$jsonData .= ' imagePullSecrets:\n - name: registrygitlab-curie\n restartPolicy: Never","defaultNamespace":"'.KUBERNETES_NAMESPACE.'"}';
}
$response = executeDeployement($jsonData);
$message = json_decode($response,True)['message'];
$lastElem = preg_split('/ /',$message);
$lastElem = end($lastElem);
if($lastElem == "created\n") {
$throwSuccess = True;
$successMessage .= "Job Created. You can go to the list.";
}
}
}
// On récupère les données passées en POST
$data = unserialize(urldecode($_POST['value']));
if ($throwError) {
echo('<div id="error">'.$errorMessage.'</div>');
}
if ($throwSuccess) {
echo('<div id="success">'.$successMessage.'</div>');
} elseif(isset($message)) {
echo('<div id="primary">'.$message.'</div>');
}
// On load le fichier de credentials cifs
$string = file_get_contents("./conf/conf_cifs.json");
$cifsData = json_decode($string,true);
if (!$throwSuccess) {
?>
<form action="#" method="post">
<div class="container-fluid" style="max-width: 60%;">
<div class="row p-4" style="background-color: #f9f9f9;">
<a class="btn btn-secondary mb-2" href="index.php">Retour</a>
<div class="col-12 m-2">
<h1>Configuration of the job :</h1>
</div>
<div class="col-12">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item active" aria-current="page">Repository : <span class="badge badge-primary"><?php echo $data['repository']; ?></span></li>
<li class="breadcrumb-item active" aria-current="page">Format : <span class="badge badge-primary"><?php echo $data['format']; ?></span></li>
<li class="breadcrumb-item active" aria-current="page">Group : <span class="badge badge-primary"><?php
if (isset($data['group'])) {
echo $data['group'];
} ?>
</span></li>
<li class="breadcrumb-item active" aria-current="page">Name : <span class="badge badge-primary"><?php
if (isset($data['name'])) {
echo $data['name'];
} ?>
</span></li>
<li class="breadcrumb-item active" aria-current="page">Version : <span class="badge badge-primary"><?php
if (isset($data['version'])) {
echo $data['version'];
} ?>
</span></li>
</ol>
</nav>
</div>
<!-- RESSOURCE -->
<div class="col-6">
<fieldset class="border p-3 mb-1">
<legend class="w-auto">1. Ressource to Run : </legend>
<div class="form-group">
<p class="description">The resources to run is the package or compiled runnable file that you want to execute. Basically it's your job. It has a generated job name that you can edit. You can select the exact Nexus resource to run just bellow.</p>
<label for="jobName">Name of the job : </label>
<input type="text" required class="form-control" id="jobName" name="jobName" value="<?php
if (isset($data['version'])) {
echo(str_replace("_","-",str_replace(".","-",str_replace("/","-",strtolower($data['name'])))."-".str_replace(".","-",strtolower($data['version']))));
} else {
echo(str_replace("_","-",str_replace(".","-",str_replace("/","-",strtolower($data['name'])))));
}
?>">
</div>
<div class="form-group">
<label for="ressourceToRun">Select Ressource to run : </label>
<select class="form-control" id="ressourceToRun" name="ressourceToRun">
<?php
if (isset($_POST['standalone'])) {
echo('<option value="'.$data['downloadUrl'].'">'.end(preg_split('/\//',$data['downloadUrl'])).'</option>');
} else {
foreach ($data['assets'] as $key => $asset) {
echo('<option value="'.$asset['downloadUrl'].'">'.end(preg_split('/\//',$asset['downloadUrl'])).'</option>');
}
}
?>
</select>
</div>
</fieldset>
</div>
<!-- RUNNER -->
<div class="col-6">
<fieldset class="border p-3 mb-1">
<legend class="w-auto">2. Runner : </legend>
<div class="form-group">
<p class="description">The runner is the docker container in which your job will run. Be careful to choose a container that fits your executable requirements.</p>
<label for="selectRunner">Select Runner :</label>
<select class="form-control" id="selectRunner" name="selectRunner">
<?php
foreach (getDockerImages()['projects'] as $project) {
if(preg_match('/runner/', $project['name']) === 1) {
$projectId = $project['id'];
$repoId = getRegistries($projectId)[0]['id'];
$dockerImages = getTags($projectId,$repoId);
foreach ($dockerImages as $image) {
echo('<option value="'.$image['location'].'">'.$project['name'].'-'.str_replace(".","-",$image['name']).'</option>');
}
}
}
?>
</select>
</div>
</fieldset>
</div>
<!-- CONFIGURE -->
<div class="col-12">
<fieldset class="border p-3 mb-1">
<legend class="w-auto">3. Runner Configuration : </legend>
<p class="description">The runner configuration is all the settings around the container. It lets you set up CRON scheduling, mounting points etc...</p>
<div class="container">
<div class="row">
<div class="col-6">
<fieldset class="border p-3 mb-1">
<legend class="w-auto">3.1 Run Strategy</legend>
<div class="form-group">
<label for="runStrategy">Run Strategy :</label>
<select class="form-control" id="runStrategy" name="runStrategy">
<option>oneshot-running</option>
<option>cron-recurrent-job</option>
</select>
</div>
<div class="form-group">
<label for="cronJobSchedule">If CRON Job set value See syntax <a href="https://crontab.guru/" target="_blank" >crontab</a> :</label>
<input type="text" class="form-control" name="cronSchedule" placeholder="5 4 * * *">
</div>
</fieldset>
</div>
<div class="col-6">
<fieldset class="border p-3 mb-1">
<legend class="w-auto">3.2 CIFS</legend>
<div class="form-group">
<label for="switchmontagecifs">Montage CIFS :</label>
<input type="checkbox" data-toggle="toggle-off" name="switchmontagecifs" id="switchmontagecifs">
</div>
<div class="form-group">
<label for="CIFSendpoint">CIFS Endpoint :</label>
<select class="form-control" id="runStrategy" name="CIFSendpoint">
<?php
foreach ($cifsData as $value) {
echo "<option>".$value['name']."</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="CIFSsourcePath">Source Path (on the container) : </label>
<input type="text" class="form-control" id="CIFSsourcePath" name="CIFSsourcePath" placeholder="exemple : /data">
</div>
<div class="form-group">
<label for="CIFSdest">Dest Path (on the remote server) : </label>
<input type="text" class="form-control" id="CIFSdest" name="CIFSdest" placeholder="exemple : /Transverse/Direction_Data/Data_Services">
</div>
</fieldset>
</div>
<div class="col-6">
<fieldset class="border p-3 mb-1">
<legend class="w-auto">3.3 Configmap</legend>
<div class="form-group">
<label for="configName">Name :</label>
<input type="text" class="form-control" id="configName" name="configName" placeholder="config">
<label for="secretmap">Content of configmap :</label>
<textarea class="form-control" id="secretmap" name="secretmap" rows="5" cols="33"></textarea>
</div>
</fieldset>
</div>
<div class="col-6">
<fieldset class="border p-3 mb-1">
<legend class="w-auto">3.4 Arguments for Execution Context</legend>
<div class="form-group">
<label for="contextarguments">Arguments command :</label>
<textarea class="form-control" id="contextarguments" name="contextarguments" rows="5" cols="33"></textarea>
</div>
</fieldset>
</div>
<div class="col-6">
<fieldset class="border p-3 mb-1">
<legend class="w-auto">3.5 Proxy</legend>
<div class="form-group">
<label for="switchproxy">Use Proxy link to fetch packages or external content :</label>
<input type="checkbox" data-toggle="toggle-off" name="switchproxy" id="switchproxy">
</div>
</fieldset>
</div>
</div>
</div>
</fieldset>
</div>
<!-- DEPLOY -->
<div class="col-12">
<fieldset class="border p-3 mb-1">
<legend class="w-auto">4. Deploy : </legend>
<p class="description">When executing the deployment,a Kubernetes resource will be created and all the configurations will be applied. It will then run the chosen container. In the container, it will mount the CIFS volume if configured and pull the Nexus resource. It will then execute the resource.</p>
<input type="submit" name="submitJob" class="btn btn-primary btn-block" value="Execute the deployement">
<input type="hidden" name="value" value="<?php echo(urlencode(serialize($data))) ?>">
<?php
if (isset($_POST['standalone'])) {
echo(' <input type="hidden" name="standalone" value="standalone"> ');
}
?>
</fieldset>
</div>
</div>
</div>
</div>
</form>
<?php
}
include 'footer.php';
} else {
include 'login.php';
}
?>