This commit is contained in:
Bozhidar Slaveykov 2023-11-27 16:06:37 +02:00
parent b831e6f464
commit 98b25b8e93
3 changed files with 58 additions and 2 deletions

17
bin/add-cron-job.sh Normal file
View file

@ -0,0 +1,17 @@
username=$1
schedule=$2
command=$3
# Create a temporary file to hold the existing user's crontab
crontab -u $username -l > /tmp/temp_crontab
# Add a new cron job to the temporary file
echo "$schedule $command" >> /tmp/temp_crontab
# Install the modified crontab from the temporary file
crontab -u $username /tmp/temp_crontab
# Remove the temporary file
rm /tmp/temp_crontab
echo "done!"

View file

@ -23,7 +23,16 @@ class CronJobResource extends Resource
{ {
return $form return $form
->schema([ ->schema([
// Forms\Components\TextInput::make('schedule')
->autofocus()
->required()
->label('Schedule'),
Forms\Components\TextInput::make('command')
->required()
->label('Command'),
Forms\Components\TextInput::make('user')
->required()
->label('User'),
]); ]);
} }

View file

@ -10,9 +10,38 @@ class CronJob extends Model
{ {
use Sushi; use Sushi;
protected $fillable = [
'schedule',
'command',
'user',
];
protected $schema = [
'schedule' => 'string',
'command' => 'string',
'user' => 'string',
];
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$args = escapeshellarg($model->user) .' '. escapeshellarg($model->schedule) . ' ' . escapeshellarg($model->command);
$addCron = shell_exec('/usr/local/phyre/bin/add-cron-job.sh ' . $args);
if (empty($addCron)) {
return false;
}
});
}
protected function sushiShouldCache()
{
return true;
}
public function getRows() public function getRows()
{ {
$user = shell_exec('whoami'); $user = shell_exec('whoami');
$cronList = shell_exec('/usr/local/phyre/bin/list-cron-jobs.sh ' . $user); $cronList = shell_exec('/usr/local/phyre/bin/list-cron-jobs.sh ' . $user);
@ -26,6 +55,7 @@ class CronJob extends Model
'schedule' => $cron['schedule'], 'schedule' => $cron['schedule'],
'command' => $cron['command'], 'command' => $cron['command'],
'user' => $user, 'user' => $user,
'time'=> time(),
]; ];
} }
} }