systemd control

This commit is contained in:
jb-alvarado 2020-04-26 20:57:40 +02:00
parent fb271ed727
commit d6c1261cec
3 changed files with 94 additions and 17 deletions

View File

@ -1,6 +1,7 @@
import json
import os
from platform import uname
from subprocess import check_output
from time import sleep
import psutil
@ -53,6 +54,45 @@ def sizeof_fmt(num, suffix='B'):
return "%.1f%s%s" % (num, 'Yi', suffix)
class PlayoutService:
def __init__(self):
self.service = ['ffplayout-engine.service']
self.cmd = ['sudo', '/bin/systemctl']
self.proc = None
def run_cmd(self):
self.proc = check_output(self.cmd + self.service)
def start(self):
self.cmd.append('start')
self.run_cmd()
def stop(self):
self.cmd.append('stop')
self.run_cmd()
def reload(self):
self.cmd.append('reload')
self.run_cmd()
def restart(self):
self.cmd.append('restart')
self.run_cmd()
def status(self):
self.cmd.append('status')
self.run_cmd()
return self.proc
def log(self):
self.cmd = ['sudo', '/bin/systemctl', 'journalctl',
'-n', '1000', '-u'] + self.service
self.run_cmd()
return self.proc
class SystemStats:
def __init__(self):
self.config = GuiSettings.objects.filter(id=1).values()[0]

View File

@ -6,14 +6,13 @@ from api.models import GuiSettings
from api.serializers import GuiSettingsSerializer, UserSerializer
from django.contrib.auth.models import User
from django_filters import rest_framework as filters
from pystemd.systemd1 import Unit
from rest_framework import viewsets
from rest_framework.parsers import FileUploadParser, JSONParser
from rest_framework.response import Response
from rest_framework.views import APIView
from .utils import (SystemStats, get_media_path, read_json, read_yaml,
write_json, write_yaml)
from .utils import (PlayoutService, SystemStats, get_media_path, read_json,
read_yaml, write_json, write_yaml)
class CurrentUserView(APIView):
@ -78,20 +77,27 @@ class SystemCtl(APIView):
"""
def post(self, request, *args, **kwargs):
if 'data' in request.data and 'run' in request.data['data']:
unit = Unit(b'ffplayout-engine.service', _autoload=True)
if request.data['data']['run'] == 'start':
unit.Unit.Start(b'replace')
if 'run' in request.data:
service = PlayoutService()
if request.data['run'] == 'start':
service.start()
return Response({"success": True})
elif request.data['data']['run'] == 'stop':
unit.Unit.Stop(b'replace')
elif request.data['run'] == 'stop':
service.stop()
return Response({"success": True})
elif request.data['data']['run'] == 'reload':
unit.Unit.Reload(b'replace')
elif request.data['run'] == 'reload':
service.reload()
return Response({"success": True})
elif request.data['data']['run'] == 'restart':
unit.Unit.Restart(b'replace')
elif request.data['run'] == 'restart':
service.restart()
return Response({"success": True})
elif request.data['run'] == 'status':
status = service.status()
return Response({"data": status})
elif request.data['run'] == 'log':
log = service.log()
return Response({"data": log})
else:
Response({"success": False})

View File

@ -37,14 +37,26 @@
<b-row class="control-unit-row">
<b-col>
<div>
<b-button v-b-tooltip.hover title="Start Playout Service" class="control-button control-button-play" variant="primary">
<b-button
v-b-tooltip.hover
title="Start Playout Service"
class="control-button control-button-play"
variant="primary"
@click="playoutControl('start')"
>
<b-icon-play />
</b-button>
</div>
</b-col>
<b-col>
<div>
<b-button v-b-tooltip.hover title="Stop Playout Service" class="control-button control-button-stop" variant="primary">
<b-button
v-b-tooltip.hover
title="Stop Playout Service"
class="control-button control-button-stop"
variant="primary"
@click="playoutControl('stop')"
>
<b-icon-stop />
</b-button>
</div>
@ -52,14 +64,26 @@
<div class="w-100" />
<b-col>
<div>
<b-button v-b-tooltip.hover title="Reload Playout Service" class="control-button control-button-reload" variant="primary">
<b-button
v-b-tooltip.hover
title="Reload Playout Service"
class="control-button control-button-reload"
variant="primary"
@click="playoutControl('reload')"
>
<b-icon-arrow-repeat />
</b-button>
</div>
</b-col>
<b-col>
<div>
<b-button v-b-tooltip.hover title="Restart Playout Service" class="control-button control-button-restart" variant="primary">
<b-button
v-b-tooltip.hover
title="Restart Playout Service"
class="control-button control-button-restart"
variant="primary"
@click="playoutControl('restart')"
>
<b-icon-arrow-clockwise />
</b-button>
</div>
@ -346,6 +370,13 @@ export default {
await this.$store.dispatch('media/getTree', { extensions, path })
this.isLoading = false
},
async playoutControl (state) {
await this.$axios.post(
'api/system/',
{ run: state },
{ headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }
)
},
async getPlaylist () {
await this.$store.dispatch('auth/inspectToken')
await this.$store.dispatch('playlist/getPlaylist', { dayStart: this.configPlayout.playlist.day_start, date: this.today })