mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-04-15 09:51:21 +00:00
add power module with reboot and shut down
This commit is contained in:
parent
0220cff57c
commit
eca36e6f96
58
actions/power
Executable file
58
actions/power
Executable file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/python3
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Configuration helper for power controls.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import subprocess
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
"""Return parsed command line arguments as dictionary."""
|
||||
parser = argparse.ArgumentParser()
|
||||
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
|
||||
|
||||
subparsers.add_parser('reboot', help='Reboot the system')
|
||||
subparsers.add_parser('shutdown', help='Shut down the system')
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def subcommand_reboot(_):
|
||||
"""Reboot the system."""
|
||||
subprocess.call('reboot')
|
||||
|
||||
|
||||
def subcommand_shutdown(_):
|
||||
"""Shut down the system."""
|
||||
subprocess.call(['shutdown', 'now'])
|
||||
|
||||
|
||||
def main():
|
||||
"""Parse arguments and perform all duties."""
|
||||
arguments = parse_arguments()
|
||||
|
||||
subcommand = arguments.subcommand.replace('-', '_')
|
||||
subcommand_method = globals()['subcommand_' + subcommand]
|
||||
subcommand_method(arguments)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
1
data/etc/plinth/modules-enabled/power
Normal file
1
data/etc/plinth/modules-enabled/power
Normal file
@ -0,0 +1 @@
|
||||
plinth.modules.power
|
||||
33
plinth/modules/power/__init__.py
Normal file
33
plinth/modules/power/__init__.py
Normal file
@ -0,0 +1,33 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Plinth module for power controls.
|
||||
"""
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from plinth import cfg
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
|
||||
|
||||
def init():
|
||||
"""Initialize the power module."""
|
||||
menu = cfg.main_menu.get('system:index')
|
||||
menu.add_urlname(_('Power'), 'glyphicon-off',
|
||||
'power:index', 1000)
|
||||
51
plinth/modules/power/templates/power.html
Normal file
51
plinth/modules/power/templates/power.html
Normal file
@ -0,0 +1,51 @@
|
||||
{% extends "base.html" %}
|
||||
{% comment %}
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
{% endcomment %}
|
||||
|
||||
{% load bootstrap %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h2>{{ title }}</h2>
|
||||
|
||||
<p>
|
||||
{% blocktrans trimmed %}
|
||||
From here, it is possible to reboot or shut down the system.
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
|
||||
<form class="form" method="post" action="{% url 'power:reboot' %}">
|
||||
{% csrf_token %}
|
||||
|
||||
<input type="submit" class="btn btn-primary"
|
||||
value="{% trans "Reboot... »" %}"/>
|
||||
</form>
|
||||
|
||||
</br>
|
||||
|
||||
<form class="form" method="post" action="{% url 'power:shutdown' %}">
|
||||
{% csrf_token %}
|
||||
|
||||
<input type="submit" class="btn btn-primary"
|
||||
value="{% trans "Shut Down... »" %}"/>
|
||||
</form>
|
||||
|
||||
|
||||
{% endblock %}
|
||||
41
plinth/modules/power/templates/power_reboot.html
Normal file
41
plinth/modules/power/templates/power_reboot.html
Normal file
@ -0,0 +1,41 @@
|
||||
\{% extends "base.html" %}
|
||||
{% comment %}
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
{% endcomment %}
|
||||
|
||||
{% load bootstrap %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h2>{{ title }}</h2>
|
||||
|
||||
<p>
|
||||
{% blocktrans trimmed %}
|
||||
Are you sure you want to reboot?
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
|
||||
<form class="form" method="post" action="{% url 'power:reboot_now' %}">
|
||||
{% csrf_token %}
|
||||
|
||||
<input type="submit" class="btn btn-primary"
|
||||
value="{% trans "Reboot Now »" %}"/>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
42
plinth/modules/power/templates/power_shutdown.html
Normal file
42
plinth/modules/power/templates/power_shutdown.html
Normal file
@ -0,0 +1,42 @@
|
||||
{% extends "base.html" %}
|
||||
{% comment %}
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
{% endcomment %}
|
||||
|
||||
{% load bootstrap %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h2>{{ title }}</h2>
|
||||
|
||||
<p>
|
||||
{% blocktrans trimmed %}
|
||||
Are you sure you want to shut down?
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
|
||||
<form class="form" method="post" action="{% url 'power:shutdown_now' %}">
|
||||
{% csrf_token %}
|
||||
|
||||
<input type="submit" class="btn btn-primary"
|
||||
value="{% trans "Shut Down Now »" %}"/>
|
||||
</form>
|
||||
|
||||
|
||||
{% endblock %}
|
||||
0
plinth/modules/power/tests/__init__.py
Normal file
0
plinth/modules/power/tests/__init__.py
Normal file
31
plinth/modules/power/urls.py
Normal file
31
plinth/modules/power/urls.py
Normal file
@ -0,0 +1,31 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
URLs for the power module.
|
||||
"""
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
urlpatterns = patterns(
|
||||
'plinth.modules.power.views',
|
||||
url(r'^sys/power/$', 'index', name='index'),
|
||||
url(r'^sys/power/reboot$', 'reboot', name='reboot'),
|
||||
url(r'^sys/power/reboot/now$', 'reboot_now', name='reboot_now'),
|
||||
url(r'^sys/power/shutdown$', 'shutdown', name='shutdown'),
|
||||
url(r'^sys/power/shutdown/now$', 'shutdown_now', name='shutdown_now'),
|
||||
)
|
||||
53
plinth/modules/power/views.py
Normal file
53
plinth/modules/power/views.py
Normal file
@ -0,0 +1,53 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Plinth module for power module.
|
||||
"""
|
||||
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from plinth import actions
|
||||
|
||||
|
||||
def index(request):
|
||||
"""Serve power controls page."""
|
||||
return TemplateResponse(request, 'power.html',
|
||||
{'title': _('Power Control')})
|
||||
|
||||
|
||||
def reboot(request):
|
||||
"""Serve reboot confirmation page."""
|
||||
return TemplateResponse(request, 'power_reboot.html',
|
||||
{'title': _('Power Control')})
|
||||
|
||||
|
||||
def reboot_now(request):
|
||||
"""Reboot the system."""
|
||||
actions.superuser_run('power', ['reboot'])
|
||||
|
||||
|
||||
def shutdown(request):
|
||||
"""Serve shutdown confirmation page."""
|
||||
return TemplateResponse(request, 'power_shutdown.html',
|
||||
{'title': _('Power Control')})
|
||||
|
||||
|
||||
def shutdown_now(request):
|
||||
"""Shutdown the system."""
|
||||
actions.superuser_run('power', ['shutdown'])
|
||||
Loading…
x
Reference in New Issue
Block a user