remove run* from cdist and put it into lib/exec

Signed-off-by: Nico Schottelius <nico@kr.ethz.ch>
This commit is contained in:
Nico Schottelius 2011-09-23 20:21:10 +02:00
parent d4406cece3
commit e14848ec1f
2 changed files with 70 additions and 49 deletions

View file

@ -106,55 +106,6 @@ class Cdist:
def cleanup(self): def cleanup(self):
self.path.cleanup() self.path.cleanup()
def shell_run_or_debug_fail(self, script, *args, **kargs):
# Manually execute /bin/sh, because sh -e does what we want
# and sh -c -e does not exit if /bin/false called
args[0][:0] = [ "/bin/sh", "-e" ]
remote = False
if "remote" in kargs:
if kargs["remote"]:
args[0][:0] = self.remote_prefix
remote = true
del kargs["remote"]
log.debug("Shell exec cmd: %s", args)
log.debug("Shell exec env: %s", kargs['env'])
try:
subprocess.check_call(*args, **kargs)
except subprocess.CalledProcessError:
log.error("Code that raised the error:\n")
if remote:
remote_cat(script)
else:
try:
script_fd = open(script)
print(script_fd.read())
script_fd.close()
except IOError as error:
raise CdistError(str(error))
raise CdistError("Command failed (shell): " + " ".join(*args))
except OSError as error:
raise CdistError(" ".join(*args) + ": " + error.args[1])
def run_or_fail(self, *args, **kargs):
if "remote" in kargs:
if kargs["remote"]:
args[0][:0] = self.remote_prefix
del kargs["remote"]
log.debug("Exec: " + " ".join(*args))
try:
subprocess.check_call(*args, **kargs)
except subprocess.CalledProcessError:
raise CdistError("Command failed: " + " ".join(*args))
except OSError as error:
raise CdistError(" ".join(*args) + ": " + error.args[1])
def remove_remote_dir(self, destination): def remove_remote_dir(self, destination):
self.run_or_fail(["rm", "-rf", destination], remote=True) self.run_or_fail(["rm", "-rf", destination], remote=True)

70
lib/cdist/exec.py Normal file
View file

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
#
# 2011 Nico Schottelius (nico-cdist at schottelius.org)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#
def shell_run_or_debug_fail(self, script, *args, **kargs):
# Manually execute /bin/sh, because sh -e does what we want
# and sh -c -e does not exit if /bin/false called
args[0][:0] = [ "/bin/sh", "-e" ]
remote = False
if "remote" in kargs:
if kargs["remote"]:
args[0][:0] = self.remote_prefix
remote = true
del kargs["remote"]
log.debug("Shell exec cmd: %s", args)
log.debug("Shell exec env: %s", kargs['env'])
try:
subprocess.check_call(*args, **kargs)
except subprocess.CalledProcessError:
log.error("Code that raised the error:\n")
if remote:
remote_cat(script)
else:
try:
script_fd = open(script)
print(script_fd.read())
script_fd.close()
except IOError as error:
raise CdistError(str(error))
raise CdistError("Command failed (shell): " + " ".join(*args))
except OSError as error:
raise CdistError(" ".join(*args) + ": " + error.args[1])
def run_or_fail(self, *args, **kargs):
if "remote" in kargs:
if kargs["remote"]:
args[0][:0] = self.remote_prefix
del kargs["remote"]
log.debug("Exec: " + " ".join(*args))
try:
subprocess.check_call(*args, **kargs)
except subprocess.CalledProcessError:
raise CdistError("Command failed: " + " ".join(*args))
except OSError as error:
raise CdistError(" ".join(*args) + ": " + error.args[1])