Commit 15675348 authored by Jérome Perrin's avatar Jérome Perrin

Fix slapos node prune usages with root slapos

- also write pid file to prevent race condition
 - check user is root

See merge request nexedi/slapos.core!267
parents b7c3ae84 e64714f6
......@@ -32,9 +32,11 @@ import glob
import os
import six.moves.configparser as configparser
from slapos.cli.command import check_root_user
from slapos.cli.config import ConfigCommand
from slapos.grid.slapgrid import merged_options
from slapos.util import rmtree
from slapos.grid.utils import setRunning, setFinished
from slapos.util import rmtree, string_to_boolean
class PruneCommand(ConfigCommand):
......@@ -60,13 +62,22 @@ class PruneCommand(ConfigCommand):
self.app.log.error('No shared_part_list options in slapos config')
sys.exit(-1)
if string_to_boolean(options.get('root_check', 'True').lower()):
check_root_user(self)
pidfile_software = options.get('pidfile_software')
if not args.dry_run and pidfile_software and os.path.exists(
pidfile_software):
self.app.log.error('Cannot prune while software is running')
sys.exit(-1)
sys.exit(do_prune(self.app.log, options, args.dry_run))
if pidfile_software:
setRunning(logger=self.app.log, pidfile=pidfile_software)
try:
do_prune(self.app.log, options, args.dry_run)
finally:
if pidfile_software:
setFinished(pidfile_software)
def _prune(
......
......@@ -399,6 +399,28 @@ class TestCliNode(CliMixin):
pidfile='/opt/slapos/slapgrid-cp.pid')
processComputerPartitionList.assert_called_once()
def test_node_prune(self):
"""slapos node prune command
"""
app = slapos.cli.entry.SlapOSApp()
with patch('slapos.cli.prune.check_root_user', return_value=True) as checked_root_user, \
patch('slapos.cli.prune.setRunning') as write_pid_file, \
patch('slapos.cli.prune.merged_options', return_value={
'shared_part_list': 'something',
'root_check': 'true',
'pidfile_software': 'pidfile_software.pid',
}), \
patch('slapos.cli.prune.do_prune') as do_prune:
app.run(('node', 'prune'))
checked_root_user.assert_called_once()
write_pid_file.assert_called_once_with(
logger=mock.ANY,
pidfile='pidfile_software.pid')
do_prune.assert_called_once()
class TestCliList(CliMixin):
def test_list(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment