2011-09-23 18:21:10 +00:00
|
|
|
# -*- 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/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2011-09-26 08:17:02 +00:00
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
|
2011-09-26 08:28:57 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2011-09-23 18:35:13 +00:00
|
|
|
|
2011-09-26 13:42:39 +00:00
|
|
|
import cdist
|
|
|
|
|
2011-09-23 18:35:13 +00:00
|
|
|
def shell_run_or_debug_fail(script, *args, **kargs):
|
2011-09-23 18:21:10 +00:00
|
|
|
# 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" ]
|
|
|
|
|
2011-09-26 09:21:04 +00:00
|
|
|
remote = False
|
2011-09-26 08:17:02 +00:00
|
|
|
if "remote_prefix" in kargs:
|
|
|
|
remote = True
|
|
|
|
args[0][:0] = kargs["remote_prefix"]
|
2011-09-23 18:35:13 +00:00
|
|
|
del kargs["remote_prefix"]
|
2011-09-23 18:21:10 +00:00
|
|
|
|
|
|
|
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:
|
2011-09-26 08:17:02 +00:00
|
|
|
# FIXME: included in Path!
|
2011-09-23 18:21:10 +00:00
|
|
|
remote_cat(script)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
script_fd = open(script)
|
|
|
|
print(script_fd.read())
|
|
|
|
script_fd.close()
|
|
|
|
except IOError as error:
|
2011-09-26 13:42:39 +00:00
|
|
|
raise cdist.Error(str(error))
|
2011-09-23 18:21:10 +00:00
|
|
|
|
2011-09-26 13:42:39 +00:00
|
|
|
raise cdist.Error("Command failed (shell): " + " ".join(*args))
|
2011-09-23 18:21:10 +00:00
|
|
|
except OSError as error:
|
2011-09-26 13:42:39 +00:00
|
|
|
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
|
2011-09-23 18:21:10 +00:00
|
|
|
|
|
|
|
|
2011-09-26 08:17:02 +00:00
|
|
|
def run_or_fail(*args, **kargs):
|
|
|
|
if "remote_prefix" in kargs:
|
|
|
|
args[0][:0] = kargs["remote_prefix"]
|
2011-09-23 18:35:13 +00:00
|
|
|
del kargs["remote_prefix"]
|
2011-09-23 18:21:10 +00:00
|
|
|
|
|
|
|
log.debug("Exec: " + " ".join(*args))
|
|
|
|
try:
|
|
|
|
subprocess.check_call(*args, **kargs)
|
|
|
|
except subprocess.CalledProcessError:
|
2011-09-26 13:42:39 +00:00
|
|
|
raise cdist.Error("Command failed: " + " ".join(*args))
|
2011-09-23 18:21:10 +00:00
|
|
|
except OSError as error:
|
2011-09-26 13:42:39 +00:00
|
|
|
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
|