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
|
2011-10-06 11:37:12 +00:00
|
|
|
import os
|
2011-09-26 08:17:02 +00:00
|
|
|
import subprocess
|
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
import cdist
|
|
|
|
|
2011-09-26 13:42:39 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
class Wrapper(object):
|
|
|
|
def __init__(self, target_host, remote_exec, remote_copy):
|
|
|
|
self.target_host = target_host
|
2011-10-11 13:47:32 +00:00
|
|
|
self.remote_exec = remote_exec
|
2011-10-11 14:06:21 +00:00
|
|
|
self.remote_copy = remote_copy
|
2011-10-11 14:31:05 +00:00
|
|
|
self.log = logging.getLogger(self.target_host)
|
2011-10-11 14:06:21 +00:00
|
|
|
|
|
|
|
def remote_mkdir(self, directory):
|
|
|
|
"""Create directory on remote side"""
|
|
|
|
self.run_or_fail(["mkdir", "-p", directory], remote=True)
|
2011-10-11 13:47:32 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
def remove_remote_path(self, destination):
|
|
|
|
"""Ensure path on remote side vanished"""
|
|
|
|
self.run_or_fail(["rm", "-rf", destination], remote=True)
|
2011-09-23 18:21:10 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
def transfer_path(self, source, destination):
|
|
|
|
"""Transfer directory and previously delete the remote destination"""
|
|
|
|
self.remove_remote_path(destination)
|
|
|
|
self.run_or_fail(os.environ['__remote_copy'].split() +
|
|
|
|
["-r", source, self.target_host + ":" + destination])
|
2011-09-23 18:21:10 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
def shell_run_or_debug_fail(self, script, *args, remote=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" ]
|
2011-09-26 14:03:53 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
if remote:
|
|
|
|
remote_prefix = self.remote_exec.split()
|
|
|
|
remote_prefix.append(self.target_host)
|
|
|
|
args[0][:0] = remote_prefix
|
2011-09-26 14:03:53 +00:00
|
|
|
|
2011-10-11 14:31:05 +00:00
|
|
|
self.log.debug("Shell exec cmd: %s", args)
|
2011-10-06 11:37:12 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
if 'env' in kargs:
|
2011-10-11 14:31:05 +00:00
|
|
|
self.log.debug("Shell exec env: %s", kargs['env'])
|
2011-09-26 14:21:56 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
try:
|
|
|
|
subprocess.check_call(*args, **kargs)
|
|
|
|
except subprocess.CalledProcessError:
|
2011-10-11 14:31:05 +00:00
|
|
|
self.log.error("Code that raised the error:\n")
|
2011-09-23 18:21:10 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
if remote:
|
|
|
|
self.run_or_fail(["cat", script], remote=remote)
|
2011-09-23 18:21:10 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
script_fd = open(script)
|
|
|
|
print(script_fd.read())
|
|
|
|
script_fd.close()
|
|
|
|
except IOError as error:
|
|
|
|
raise cdist.Error(str(error))
|
2011-09-23 18:21:10 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
raise cdist.Error("Command failed (shell): " + " ".join(*args))
|
|
|
|
except OSError as error:
|
|
|
|
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
|
2011-10-11 13:47:32 +00:00
|
|
|
|
2011-10-11 14:06:21 +00:00
|
|
|
def run_or_fail(self, *args, remote=False, **kargs):
|
|
|
|
if remote:
|
|
|
|
remote_prefix = self.remote_exec.split()
|
|
|
|
remote_prefix.append(self.target_host)
|
|
|
|
args[0][:0] = remote_prefix
|
2011-10-11 13:47:32 +00:00
|
|
|
|
2011-10-11 14:31:05 +00:00
|
|
|
self.log.debug("Exec: " + " ".join(*args))
|
2011-10-11 14:06:21 +00:00
|
|
|
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])
|