cdist/lib/cdist/exec.py
Nico Schottelius d9d9e6bff6 move back transfer stuff to exec
Signed-off-by: Nico Schottelius <nico@kr.ethz.ch>
2011-10-11 15:47:32 +02:00

97 lines
3.2 KiB
Python

# -*- 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/>.
#
#
import logging
import os
import subprocess
log = logging.getLogger(__name__)
import cdist
class ExecWrapper(object):
def __init__(self, remote_exec, remote_copy, target_host):
self.remote_exec = remote_exec
def shell_run_or_debug_fail(script, *args, remote_prefix=False, **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" ]
if remote_prefix:
remote_prefix = os.environ['__remote_exec'].split()
remote_prefix.append(os.environ['__target_host'])
args[0][:0] = remote_prefix
log.debug("Shell exec cmd: %s", args)
if 'env' in kargs:
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_prefix:
run_or_fail(["cat", script], remote_prefix=remote_prefix)
else:
try:
script_fd = open(script)
print(script_fd.read())
script_fd.close()
except IOError as error:
raise cdist.Error(str(error))
raise cdist.Error("Command failed (shell): " + " ".join(*args))
except OSError as error:
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
def run_or_fail(*args, remote_prefix=False, **kargs):
if remote_prefix:
remote_prefix = os.environ['__remote_exec'].split()
remote_prefix.append(os.environ['__target_host'])
args[0][:0] = remote_prefix
log.debug("Exec: " + " ".join(*args))
try:
subprocess.check_call(*args, **kargs)
except subprocess.CalledProcessError:
raise cdist.Error("Command failed: " + " ".join(*args))
except OSError as error:
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
def remote_mkdir(self, directory):
"""Create directory on remote side"""
cdist.exec.run_or_fail(["mkdir", "-p", directory], remote_prefix=True)
def remove_remote_path(self, destination):
"""Ensure path on remote side vanished"""
cdist.exec.run_or_fail(["rm", "-rf", destination], remote_prefix=True)
def transfer_path(self, source, destination):
"""Transfer directory and previously delete the remote destination"""
self.remove_remote_path(destination)
cdist.exec.run_or_fail(os.environ['__remote_copy'].split() +
["-r", source, self.target_host + ":" + destination])