2011-10-12 12:30:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2013-08-28 13:39:17 +00:00
|
|
|
# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org)
|
2011-10-12 12:30:10 +00:00
|
|
|
#
|
|
|
|
# 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 io
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import cdist
|
|
|
|
|
2011-10-14 23:11:54 +00:00
|
|
|
class DecodeError(cdist.Error):
|
|
|
|
def __init__(self, command):
|
|
|
|
self.command = command
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Cannot decode output of " + " ".join(self.command)
|
|
|
|
|
2011-10-12 12:30:10 +00:00
|
|
|
|
|
|
|
class Remote(object):
|
|
|
|
"""Execute commands remotely.
|
|
|
|
|
|
|
|
All interaction with the remote side should be done through this class.
|
|
|
|
Directly accessing the remote side from python code is a bug.
|
|
|
|
|
|
|
|
"""
|
2013-08-19 09:33:44 +00:00
|
|
|
def __init__(self,
|
|
|
|
target_host,
|
|
|
|
remote_exec,
|
|
|
|
remote_copy,
|
|
|
|
base_path=None):
|
2011-10-12 12:30:10 +00:00
|
|
|
self.target_host = target_host
|
|
|
|
self._exec = remote_exec
|
|
|
|
self._copy = remote_copy
|
|
|
|
|
2013-08-19 09:33:44 +00:00
|
|
|
if base_path:
|
|
|
|
self.base_path = base_path
|
|
|
|
else:
|
|
|
|
self.base_path = "/var/lib/cdist"
|
|
|
|
|
2011-10-12 12:30:10 +00:00
|
|
|
self.conf_path = os.path.join(self.base_path, "conf")
|
|
|
|
self.object_path = os.path.join(self.base_path, "object")
|
|
|
|
|
|
|
|
self.type_path = os.path.join(self.conf_path, "type")
|
|
|
|
self.global_explorer_path = os.path.join(self.conf_path, "explorer")
|
|
|
|
|
|
|
|
self.log = logging.getLogger(self.target_host)
|
2011-10-18 11:32:36 +00:00
|
|
|
|
2013-08-18 22:36:25 +00:00
|
|
|
self._init_env()
|
|
|
|
|
|
|
|
def _init_env(self):
|
2013-08-18 23:38:28 +00:00
|
|
|
"""Setup environment for scripts - HERE????"""
|
|
|
|
# FIXME: better do so in exec functions that require it!
|
|
|
|
os.environ['__remote_copy'] = self._copy
|
|
|
|
os.environ['__remote_exec'] = self._exec
|
2013-08-18 22:36:25 +00:00
|
|
|
|
|
|
|
|
2012-10-30 15:07:00 +00:00
|
|
|
def create_files_dirs(self):
|
2011-10-12 12:30:10 +00:00
|
|
|
self.rmdir(self.base_path)
|
|
|
|
self.mkdir(self.base_path)
|
2013-04-08 17:57:28 +00:00
|
|
|
self.run(["chmod", "0700", self.base_path])
|
2011-10-12 12:30:10 +00:00
|
|
|
self.mkdir(self.conf_path)
|
|
|
|
|
|
|
|
def rmdir(self, path):
|
|
|
|
"""Remove directory on the remote side."""
|
|
|
|
self.log.debug("Remote rmdir: %s", path)
|
|
|
|
self.run(["rm", "-rf", path])
|
|
|
|
|
|
|
|
def mkdir(self, path):
|
|
|
|
"""Create directory on the remote side."""
|
|
|
|
self.log.debug("Remote mkdir: %s", path)
|
|
|
|
self.run(["mkdir", "-p", path])
|
|
|
|
|
|
|
|
def transfer(self, source, destination):
|
|
|
|
"""Transfer a file or directory to the remote side."""
|
|
|
|
self.log.debug("Remote transfer: %s -> %s", source, destination)
|
|
|
|
self.rmdir(destination)
|
|
|
|
command = self._copy.split()
|
2012-05-29 09:02:23 +00:00
|
|
|
command.extend(["-r", source, self.target_host + ":" + destination])
|
2011-11-18 13:40:25 +00:00
|
|
|
self._run_command(command)
|
2011-10-12 12:30:10 +00:00
|
|
|
|
2012-02-15 16:01:03 +00:00
|
|
|
def run_script(self, script, env=None, return_output=False):
|
|
|
|
"""Run the given script with the given environment on the remote side.
|
|
|
|
Return the output as a string.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
command = ["/bin/sh", "-e"]
|
|
|
|
command.append(script)
|
|
|
|
|
|
|
|
return self.run(command, env, return_output)
|
|
|
|
|
2011-10-14 13:52:16 +00:00
|
|
|
def run(self, command, env=None, return_output=False):
|
2011-10-12 12:30:10 +00:00
|
|
|
"""Run the given command with the given environment on the remote side.
|
|
|
|
Return the output as a string.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# prefix given command with remote_exec
|
|
|
|
cmd = self._exec.split()
|
|
|
|
cmd.append(self.target_host)
|
2012-02-15 16:01:03 +00:00
|
|
|
|
2012-05-22 15:27:38 +00:00
|
|
|
# FIXME: replace this by -o SendEnv name -o SendEnv name ... to ssh?
|
2012-02-15 16:01:03 +00:00
|
|
|
# can't pass environment to remote side, so prepend command with
|
|
|
|
# variable declarations
|
|
|
|
if env:
|
|
|
|
remote_env = ["%s=%s" % item for item in env.items()]
|
2012-02-15 16:02:21 +00:00
|
|
|
cmd.extend(remote_env)
|
2012-02-15 16:01:03 +00:00
|
|
|
|
2011-10-12 12:30:10 +00:00
|
|
|
cmd.extend(command)
|
2012-02-15 16:01:03 +00:00
|
|
|
|
2011-11-18 13:40:25 +00:00
|
|
|
return self._run_command(cmd, env=env, return_output=return_output)
|
2011-10-12 12:30:10 +00:00
|
|
|
|
2011-11-18 13:40:25 +00:00
|
|
|
def _run_command(self, command, env=None, return_output=False):
|
2011-10-12 12:30:10 +00:00
|
|
|
"""Run the given command with the given environment.
|
|
|
|
Return the output as a string.
|
|
|
|
|
|
|
|
"""
|
|
|
|
assert isinstance(command, (list, tuple)), "list or tuple argument expected, got: %s" % command
|
2011-10-18 11:32:36 +00:00
|
|
|
|
2011-10-21 13:17:19 +00:00
|
|
|
# export target_host for use in __remote_{exec,copy} scripts
|
|
|
|
os_environ = os.environ.copy()
|
|
|
|
os_environ['__target_host'] = self.target_host
|
|
|
|
|
2011-10-12 12:30:10 +00:00
|
|
|
self.log.debug("Remote run: %s", command)
|
|
|
|
try:
|
2011-10-14 13:52:16 +00:00
|
|
|
if return_output:
|
2011-10-21 13:17:19 +00:00
|
|
|
return subprocess.check_output(command, env=os_environ).decode()
|
2011-10-14 13:52:16 +00:00
|
|
|
else:
|
2011-10-21 13:50:19 +00:00
|
|
|
subprocess.check_call(command, env=os_environ)
|
2011-10-12 12:30:10 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
raise cdist.Error("Command failed: " + " ".join(command))
|
|
|
|
except OSError as error:
|
2013-08-19 09:45:43 +00:00
|
|
|
raise cdist.Error(" ".join(command) + ": " + error.args[1])
|
2011-10-14 23:11:54 +00:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
raise DecodeError(command)
|