diff --git a/slapos/cli/bang.py b/slapos/cli/bang.py index 8c4e0a3b2be229846f84ba6f402bcb9e22367ca1..ddd811144d4a64fd368a6e9aa9d574b2fee25a0b 100644 --- a/slapos/cli/bang.py +++ b/slapos/cli/bang.py @@ -2,6 +2,7 @@ import logging +from slapos.cli.command import must_be_root from slapos.cli.config import ConfigCommand from slapos.bang import do_bang @@ -19,6 +20,7 @@ class BangCommand(ConfigCommand): help='Message for bang') return ap + @must_be_root def take_action(self, args): configp = self.fetch_config(args) do_bang(configp, args.message) diff --git a/slapos/cli/command.py b/slapos/cli/command.py index ee3d39c650d6f26373361a45f8a59367e85f7205..30ecca0d20b7ff9bcbba9540a8cc03257bfa7f75 100644 --- a/slapos/cli/command.py +++ b/slapos/cli/command.py @@ -1,11 +1,15 @@ # -*- coding: utf-8 -*- import argparse +import functools +import os +import sys import cliff class Command(cliff.command.Command): + def get_parser(self, prog_name): parser = argparse.ArgumentParser( description=self.get_description(), @@ -14,3 +18,12 @@ class Command(cliff.command.Command): ) return parser + + +def must_be_root(func): + @functools.wraps(func) + def func(self, *args, **kw): + if os.getuid() != 0: + self.app.log.error('This slapos command must be run as root.') + sys.exit(5) + return func diff --git a/slapos/cli/format.py b/slapos/cli/format.py index 6fdd280aa84b0ab13c41b4b8c80fa8ce8e434bf8..0f8ffb2a400165e0f753fe73d4bc25c3d0a109f4 100644 --- a/slapos/cli/format.py +++ b/slapos/cli/format.py @@ -3,6 +3,7 @@ import logging import sys +from slapos.cli.command import must_be_root from slapos.cli.config import ConfigCommand from slapos.format import do_format, FormatConfig, tracing_monkeypatch, UsageError @@ -55,6 +56,7 @@ class FormatCommand(ConfigCommand): return ap + @must_be_root def take_action(self, args): configp = self.fetch_config(args) diff --git a/slapos/cli/register.py b/slapos/cli/register.py index 76f6c7c2e030227005ed640fab79fccd4748a04d..5fd05164e379c07b19280cc1c98dde7312f99cd1 100644 --- a/slapos/cli/register.py +++ b/slapos/cli/register.py @@ -3,7 +3,7 @@ import logging import sys -from slapos.cli.command import Command +from slapos.cli.command import Command, must_be_root from slapos.register.register import do_register, RegisterConfig @@ -66,6 +66,7 @@ class RegisterCommand(Command): return ap + @must_be_root def take_action(self, args): try: conf = RegisterConfig(logger=self.log) diff --git a/slapos/cli/slapgrid.py b/slapos/cli/slapgrid.py index 1fbf53ee5bfaa50579a4ee540f87151f6d0aba8e..4a32c6ff361a156496fb51063b85547cd2b1c94c 100644 --- a/slapos/cli/slapgrid.py +++ b/slapos/cli/slapgrid.py @@ -2,6 +2,7 @@ import logging +from slapos.cli.command import must_be_root from slapos.cli.config import ConfigCommand from slapos.grid.utils import setRunning, setFinished @@ -57,6 +58,7 @@ class SlapgridCommand(ConfigCommand): help='Launch slapgrid without delay. Default behavior.') return ap + @must_be_root def take_action(self, args): configp = self.fetch_config(args) options = merged_options(args, configp) diff --git a/slapos/cli/supervisorctl.py b/slapos/cli/supervisorctl.py index ecdb54348766bb2788d58c0814cfb824ff403b20..24b13799e3b352bffad55f9e86fc3c7956d5fcea 100644 --- a/slapos/cli/supervisorctl.py +++ b/slapos/cli/supervisorctl.py @@ -4,6 +4,7 @@ import argparse import logging import os +from slapos.cli.command import must_be_root from slapos.cli.config import ConfigCommand from slapos.grid.svcbackend import launchSupervisord @@ -22,6 +23,7 @@ class SupervisorctlCommand(ConfigCommand): help='parameters passed to supervisorctl') return ap + @must_be_root def take_action(self, args): configp = self.fetch_config(args) instance_root = configp.get('slapos', 'instance_root') diff --git a/slapos/cli/supervisord.py b/slapos/cli/supervisord.py index ffaef7b781986b2c5b1673a6bec97106376f6c82..b8049484896547c9c789596dcc18ec5ed9941fea 100644 --- a/slapos/cli/supervisord.py +++ b/slapos/cli/supervisord.py @@ -3,6 +3,7 @@ import logging import os +from slapos.cli.command import must_be_root from slapos.cli.config import ConfigCommand from slapos.grid.svcbackend import launchSupervisord @@ -12,6 +13,7 @@ class SupervisordCommand(ConfigCommand): log = logging.getLogger('supervisord') + @must_be_root def take_action(self, args): configp = self.fetch_config(args) instance_root = configp.get('slapos', 'instance_root')