diff --git a/actions/power b/actions/power new file mode 100755 index 000000000..5de6b5371 --- /dev/null +++ b/actions/power @@ -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 . +# + +""" +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() diff --git a/data/etc/plinth/modules-enabled/power b/data/etc/plinth/modules-enabled/power new file mode 100644 index 000000000..ba70c184c --- /dev/null +++ b/data/etc/plinth/modules-enabled/power @@ -0,0 +1 @@ +plinth.modules.power diff --git a/plinth/modules/power/__init__.py b/plinth/modules/power/__init__.py new file mode 100644 index 000000000..9557512ef --- /dev/null +++ b/plinth/modules/power/__init__.py @@ -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 . +# + +""" +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) diff --git a/plinth/modules/power/templates/power.html b/plinth/modules/power/templates/power.html new file mode 100644 index 000000000..d91582f46 --- /dev/null +++ b/plinth/modules/power/templates/power.html @@ -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 . +# +{% endcomment %} + +{% load bootstrap %} +{% load i18n %} + +{% block content %} + +

{{ title }}

+ +

+ {% blocktrans trimmed %} + From here, it is possible to reboot or shut down the system. + {% endblocktrans %} +

+ +
+ {% csrf_token %} + + +
+ +
+ +
+ {% csrf_token %} + + +
+ + +{% endblock %} diff --git a/plinth/modules/power/templates/power_reboot.html b/plinth/modules/power/templates/power_reboot.html new file mode 100644 index 000000000..7e79ee8f5 --- /dev/null +++ b/plinth/modules/power/templates/power_reboot.html @@ -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 . +# +{% endcomment %} + +{% load bootstrap %} +{% load i18n %} + +{% block content %} + +

{{ title }}

+ +

+ {% blocktrans trimmed %} + Are you sure you want to reboot? + {% endblocktrans %} +

+ +
+ {% csrf_token %} + + +
+ +{% endblock %} diff --git a/plinth/modules/power/templates/power_shutdown.html b/plinth/modules/power/templates/power_shutdown.html new file mode 100644 index 000000000..7acb4f529 --- /dev/null +++ b/plinth/modules/power/templates/power_shutdown.html @@ -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 . +# +{% endcomment %} + +{% load bootstrap %} +{% load i18n %} + +{% block content %} + +

{{ title }}

+ +

+ {% blocktrans trimmed %} + Are you sure you want to shut down? + {% endblocktrans %} +

+ +
+ {% csrf_token %} + + +
+ + +{% endblock %} diff --git a/plinth/modules/power/tests/__init__.py b/plinth/modules/power/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plinth/modules/power/urls.py b/plinth/modules/power/urls.py new file mode 100644 index 000000000..e5f33d039 --- /dev/null +++ b/plinth/modules/power/urls.py @@ -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 . +# + +""" +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'), + ) diff --git a/plinth/modules/power/views.py b/plinth/modules/power/views.py new file mode 100644 index 000000000..d844f9498 --- /dev/null +++ b/plinth/modules/power/views.py @@ -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 . +# + +""" +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'])