add power module with reboot and shut down

This commit is contained in:
Joel Valleroy 2015-11-26 16:29:38 -05:00
parent 0220cff57c
commit eca36e6f96
9 changed files with 310 additions and 0 deletions

58
actions/power Executable file
View 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()

View File

@ -0,0 +1 @@
plinth.modules.power

View 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)

View 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... &raquo;" %}"/>
</form>
</br>
<form class="form" method="post" action="{% url 'power:shutdown' %}">
{% csrf_token %}
<input type="submit" class="btn btn-primary"
value="{% trans "Shut Down... &raquo;" %}"/>
</form>
{% endblock %}

View 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 &raquo;" %}"/>
</form>
{% endblock %}

View 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 &raquo;" %}"/>
</form>
{% endblock %}

View File

View 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'),
)

View 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'])