NEW FEATURE: added http requests
This commit is contained in:
parent
2a5f1bd30c
commit
6ec0c18016
|
@ -0,0 +1,3 @@
|
|||
.crontype-inputs {
|
||||
display: none;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
$(function() {
|
||||
initDatePickers();
|
||||
initCronType();
|
||||
});
|
||||
|
||||
function initDatePickers()
|
||||
{
|
||||
$('#nextrunselector').datetimepicker({format: 'L LTS'});
|
||||
$('#lastrunselector').datetimepicker({format: 'L LTS'});
|
||||
}
|
||||
|
||||
function initCronType()
|
||||
{
|
||||
$('.crontype-item').on('click', function() {
|
||||
let type = $(this).data('type');
|
||||
$('.crontype').val(type);
|
||||
$('.crontype-inputs').hide();
|
||||
$('.crontype-' + type).show();
|
||||
})
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
.text-end {
|
||||
text-align: right;
|
||||
}
|
|
@ -27,6 +27,21 @@ class JobController extends Controller
|
|||
|
||||
public function addAction()
|
||||
{
|
||||
return $this->render('job/add.html.twig');
|
||||
if($this->getRequest()->getMethod() == 'GET') {
|
||||
return $this->render('job/add.html.twig');
|
||||
} elseif ($this->getRequest()->getMethod() == 'POST') {
|
||||
$allValues = $this->getRequest()->request->all();
|
||||
$jobRepo = new Job($this->getDbCon());
|
||||
$joboutput = $jobRepo->addJob($allValues);
|
||||
if($joboutput['success']) {
|
||||
$this->addFlash('success', $joboutput['message']);
|
||||
return new RedirectResponse($this->generateRoute('job_index'));
|
||||
} else {
|
||||
$this->addFlash('danger', $joboutput['message']);
|
||||
return new RedirectResponse($this->generateRoute('job_add'));
|
||||
}
|
||||
} else {
|
||||
return new Response('Not implemented yet', Response::HTTP_TOO_EARLY);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
namespace JeroenED\Webcron\Repository;
|
||||
|
||||
|
||||
use DateTime;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
class Job
|
||||
|
@ -26,4 +27,44 @@ class Job
|
|||
}
|
||||
return $jobs;
|
||||
}
|
||||
|
||||
public function addJob(array $values)
|
||||
{
|
||||
if(empty($values['type']) ||
|
||||
empty($values['name']) ||
|
||||
empty($values['delay']) ||
|
||||
empty($values['nextrun'])
|
||||
) {
|
||||
return ['success' => false, 'message' => 'Some fields are empty'];
|
||||
}
|
||||
|
||||
if(empty($values['lastrun'])) {
|
||||
$values['lastrun'] = NULL;
|
||||
} else {
|
||||
$values['lastrun'] = DateTime::createFromFormat('m/d/Y g:i:s A',$values['lastrun'])->getTimestamp();
|
||||
}
|
||||
|
||||
$values['nextrun'] = DateTime::createFromFormat('m/d/Y g:i:s A', $values['nextrun'])->getTimestamp();
|
||||
$data['type'] = $values['type'];
|
||||
|
||||
switch($data['type'])
|
||||
{
|
||||
case 'http':
|
||||
$parsedUrl = parse_url($values['url']);
|
||||
$data['url'] = $values['url'];
|
||||
if(empty($parsedUrl['host'])) {
|
||||
return ['success' => false, 'message' => 'Some data was invalid'];
|
||||
}
|
||||
$data['host'] = $parsedUrl['host'];
|
||||
break;
|
||||
}
|
||||
|
||||
$data = json_encode($data);
|
||||
$addJobSql = "INSERT INTO job(name, data, delay, nextrun, lastrun) VALUES (:name, :data, :delay, :nextrun, :lastrun)";
|
||||
|
||||
$addJobStmt = $this->dbcon->prepare($addJobSql);
|
||||
$addJobStmt->execute([':name' => $values['name'], ':data' => $data, ':delay' => $values['delay'], ':nextrun' => $values['nextrun'], ':lastrun' => $values['lastrun'], ]);
|
||||
|
||||
return ['success' => true, 'message' => 'Cronjob succesfully added'];
|
||||
}
|
||||
}
|
|
@ -7,12 +7,10 @@
|
|||
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="/css/site.css">
|
||||
{% block extrastyles %}{% endblock %}
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
|
||||
<script src="/js/site.js" type="text/javascript"></script>
|
||||
{% block extrascripts %}{% endblock %}
|
||||
|
||||
</head>
|
||||
|
@ -36,6 +34,7 @@
|
|||
</ul>
|
||||
</div>
|
||||
<div class="col-xs-12 col-sm-10">
|
||||
{{ include('flashes.html.twig') }}
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -8,16 +8,6 @@
|
|||
<label for="name">Name</label>
|
||||
<input type="text" name="name" class="form-control" id="name" placeholder="System update">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="url">url</label>
|
||||
<input type="text" name="url" class="form-control" id="url" placeholder="http://example.com/ or 'sudo yum update -y'">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="host">host</label>
|
||||
<input type="text" name="host" class="form-control" id="host" placeholder="localhost">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="name">Delay (in seconds)</label>
|
||||
<input type="number" class="form-control" id="delay" name="delay">
|
||||
|
@ -28,22 +18,41 @@
|
|||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="lastrun">Last run</label>
|
||||
<input type="datetime-local" id="nextrunselector" class="form-control" name="nextrun">
|
||||
<input type="text" id="lastrunselector" class="form-control datetimepicker-input" data-target="#lastrunselector" data-toggle="datetimepicker" name="lastrun">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="expected">Expected exit code</label>
|
||||
<input type="text" name="expected" class="form-control" id="host" placeholder="200">
|
||||
<div class="dropdown">
|
||||
<button class="btn btn-outline-primary dropdown-toggle" type="button" id="crontypeButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||
Cronjob type
|
||||
</button>
|
||||
<div class="dropdown-menu" aria-labelledby="crontypeButton">
|
||||
<a class="dropdown-item crontype-item" href="#" data-type="local">Local</a>
|
||||
<a class="dropdown-item crontype-item" href="#" data-type="ssh">Ssh</a>
|
||||
<a class="dropdown-item crontype-item" href="#" data-type="docker">Docker</a>
|
||||
<a class="dropdown-item crontype-item" href="#" data-type="docker-ssh">Docker + SSH</a>
|
||||
<a class="dropdown-item crontype-item" href="#" data-type="http">Http request</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-outline-primary">Submit</button>
|
||||
|
||||
<div class="crontype-http crontype-inputs">
|
||||
<div class="mb-3">
|
||||
<label for="url">Url</label>
|
||||
<input type="text" name="url" class="form-control" id="url" placeholder="https://scripts.example.com/">
|
||||
</div>
|
||||
</div>
|
||||
<input type="hidden" name="type" class="crontype" value=""><button type="submit" class="btn btn-outline-primary">Submit</button>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block extrastyles %}
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.39.0/css/tempusdominus-bootstrap-4.min.css" integrity="sha512-3JRrEUwaCkFUBLK1N8HehwQgu8e23jTH4np5NHOmQOobuC4ROQxFwFgBLTnhcnQRMs84muMh0PnnwXlPq5MGjg==" crossorigin="anonymous" />
|
||||
<link rel="stylesheet" href="/resources/job/add.css" />
|
||||
{% endblock %}
|
||||
|
||||
{% block extrascripts %}
|
||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.39.0/js/tempusdominus-bootstrap-4.min.js" integrity="sha512-k6/Bkb8Fxf/c1Tkyl39yJwcOZ1P4cRrJu77p83zJjN2Z55prbFHxPs9vN7q3l3+tSMGPDdoH51AEU8Vgo1cgAA==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.39.0/js/tempusdominus-bootstrap-4.min.js" integrity="sha512-k6/Bkb8Fxf/c1Tkyl39yJwcOZ1P4cRrJu77p83zJjN2Z55prbFHxPs9vN7q3l3+tSMGPDdoH51AEU8Vgo1cgAA==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" src="/resources/job/add.js"></script>
|
||||
{% endblock %}
|
|
@ -45,3 +45,7 @@
|
|||
<img src="/public/images/ajax-loader.gif" alt="loading">
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block extrastyles %}
|
||||
<link rel="stylesheet" href="/resources/job/index.css" />
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in New Issue