2011-10-12 13:17:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2012-10-30 13:54:05 +00:00
|
|
|
# 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
|
2011-10-12 13:17:06 +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
|
2012-11-30 12:48:34 +00:00
|
|
|
import re
|
2011-10-12 13:17:06 +00:00
|
|
|
import subprocess
|
|
|
|
import shutil
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import cdist
|
2011-10-12 14:46:54 +00:00
|
|
|
from cdist import core
|
2011-10-12 13:17:06 +00:00
|
|
|
|
|
|
|
class Local(object):
|
|
|
|
"""Execute commands locally.
|
|
|
|
|
|
|
|
All interaction with the local side should be done through this class.
|
|
|
|
Directly accessing the local side from python code is a bug.
|
|
|
|
|
|
|
|
"""
|
2012-11-30 12:48:34 +00:00
|
|
|
def __init__(self, target_host, out_path, exec_path, add_conf_dirs=None, cache_dir=None):
|
2012-10-30 15:05:37 +00:00
|
|
|
|
2011-10-12 13:17:06 +00:00
|
|
|
self.target_host = target_host
|
2012-10-30 15:05:37 +00:00
|
|
|
self.out_path = out_path
|
2012-11-01 12:30:45 +00:00
|
|
|
self.exec_path = exec_path
|
|
|
|
|
2012-11-01 12:46:01 +00:00
|
|
|
self._add_conf_dirs = add_conf_dirs
|
2011-10-12 13:17:06 +00:00
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
self._init_log()
|
|
|
|
self._init_permissions()
|
|
|
|
self._init_paths()
|
2012-10-30 15:11:03 +00:00
|
|
|
self._init_cache_dir(cache_dir)
|
2012-10-30 15:05:37 +00:00
|
|
|
self._init_conf_dirs()
|
2011-10-12 13:17:06 +00:00
|
|
|
|
2012-11-01 14:17:46 +00:00
|
|
|
@property
|
|
|
|
def dist_conf_dir(self):
|
|
|
|
return os.path.abspath(os.path.join(os.path.dirname(cdist.__file__), "conf"))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def home_dir(self):
|
2012-10-30 15:05:37 +00:00
|
|
|
if 'HOME' in os.environ:
|
2012-11-01 14:17:46 +00:00
|
|
|
return os.path.join(os.environ['HOME'], ".cdist")
|
2012-10-30 15:05:37 +00:00
|
|
|
else:
|
2012-11-01 14:20:10 +00:00
|
|
|
return None
|
2011-10-12 13:17:06 +00:00
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
def _init_log(self):
|
2011-10-12 13:17:06 +00:00
|
|
|
self.log = logging.getLogger(self.target_host)
|
2011-10-18 11:32:36 +00:00
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
def _init_permissions(self):
|
2012-05-22 15:21:58 +00:00
|
|
|
# Setup file permissions using umask
|
2012-05-22 15:27:38 +00:00
|
|
|
os.umask(0o077)
|
2012-05-22 15:21:58 +00:00
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
def _init_paths(self):
|
|
|
|
# Depending on out_path
|
|
|
|
self.bin_path = os.path.join(self.out_path, "bin")
|
|
|
|
self.conf_path = os.path.join(self.out_path, "conf")
|
|
|
|
self.global_explorer_out_path = os.path.join(self.out_path, "explorer")
|
|
|
|
self.object_path = os.path.join(self.out_path, "object")
|
|
|
|
|
|
|
|
# Depending on conf_path
|
|
|
|
self.global_explorer_path = os.path.join(self.conf_path, "explorer")
|
|
|
|
self.manifest_path = os.path.join(self.conf_path, "manifest")
|
|
|
|
self.type_path = os.path.join(self.conf_path, "type")
|
|
|
|
|
|
|
|
def _init_conf_dirs(self):
|
|
|
|
self.conf_dirs = []
|
|
|
|
|
|
|
|
# Comes with the distribution
|
2012-11-01 14:17:46 +00:00
|
|
|
system_conf_dir = os.path.abspath(os.path.join(os.path.dirname(cdist.__file__), "conf"))
|
2012-10-30 15:05:37 +00:00
|
|
|
self.conf_dirs.append(system_conf_dir)
|
|
|
|
|
|
|
|
# Is the default place for user created explorer, type and manifest
|
|
|
|
if self.home_dir:
|
2012-11-01 14:17:46 +00:00
|
|
|
self.conf_dirs.append(self.home_dir)
|
2012-10-30 15:05:37 +00:00
|
|
|
|
2012-11-30 12:48:34 +00:00
|
|
|
# Add directories defined in the CDIST_PATH environment variable
|
|
|
|
if 'CDIST_PATH' in os.environ:
|
|
|
|
cdist_path_dirs = re.split(r'(?<!\\):', os.environ['CDIST_PATH'])
|
|
|
|
cdist_path_dirs.reverse()
|
|
|
|
self.conf_dirs.extend(cdist_path_dirs)
|
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
# Add user supplied directories
|
2012-11-01 12:30:45 +00:00
|
|
|
if self._add_conf_dirs:
|
|
|
|
self.conf_dirs.extend(self._add_conf_dirs)
|
2012-10-30 15:05:37 +00:00
|
|
|
|
|
|
|
def _init_cache_dir(self, cache_dir):
|
|
|
|
if cache_dir:
|
|
|
|
self.cache_path = cache_dir
|
|
|
|
else:
|
|
|
|
if self.home_dir:
|
|
|
|
self.cache_path = os.path.join(self.home_dir, "cache")
|
|
|
|
else:
|
|
|
|
raise cdist.Error("No homedir setup and no cache dir location given")
|
2011-10-12 13:17:06 +00:00
|
|
|
|
|
|
|
def rmdir(self, path):
|
|
|
|
"""Remove directory on the local side."""
|
|
|
|
self.log.debug("Local rmdir: %s", path)
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
|
|
|
def mkdir(self, path):
|
|
|
|
"""Create directory on the local side."""
|
|
|
|
self.log.debug("Local mkdir: %s", path)
|
2012-05-22 15:21:58 +00:00
|
|
|
os.makedirs(path, exist_ok=True)
|
2011-10-12 13:17:06 +00:00
|
|
|
|
2011-10-14 13:51:00 +00:00
|
|
|
def run(self, command, env=None, return_output=False):
|
2011-10-12 13:17:06 +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
|
|
|
|
self.log.debug("Local run: %s", command)
|
2011-11-18 13:44:32 +00:00
|
|
|
|
|
|
|
if env is None:
|
2011-11-18 13:56:59 +00:00
|
|
|
env = os.environ.copy()
|
2011-11-18 13:44:32 +00:00
|
|
|
# Export __target_host for use in __remote_{copy,exec} scripts
|
|
|
|
env['__target_host'] = self.target_host
|
|
|
|
|
2011-10-12 13:17:06 +00:00
|
|
|
try:
|
2011-10-14 13:51:00 +00:00
|
|
|
if return_output:
|
|
|
|
return subprocess.check_output(command, env=env).decode()
|
|
|
|
else:
|
|
|
|
subprocess.check_call(command, env=env)
|
2011-10-12 13:17:06 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
raise cdist.Error("Command failed: " + " ".join(command))
|
|
|
|
except OSError as error:
|
|
|
|
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
|
|
|
|
|
2011-10-14 13:51:00 +00:00
|
|
|
def run_script(self, script, env=None, return_output=False):
|
2011-10-12 13:17:06 +00:00
|
|
|
"""Run the given script with the given environment.
|
|
|
|
Return the output as a string.
|
|
|
|
|
|
|
|
"""
|
|
|
|
command = ["/bin/sh", "-e"]
|
|
|
|
command.append(script)
|
|
|
|
|
2012-02-15 15:25:18 +00:00
|
|
|
return self.run(command, env, return_output)
|
2011-10-12 14:46:54 +00:00
|
|
|
|
2012-10-30 15:06:27 +00:00
|
|
|
def create_files_dirs(self):
|
2012-10-30 15:05:37 +00:00
|
|
|
self._create_context_dirs()
|
|
|
|
self._create_conf_path_and_link_conf_dirs()
|
2012-11-01 12:30:45 +00:00
|
|
|
self._link_types_for_emulator()
|
2012-10-30 15:05:37 +00:00
|
|
|
|
|
|
|
def _create_context_dirs(self):
|
|
|
|
self.mkdir(self.out_path)
|
2012-11-01 14:17:46 +00:00
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
self.mkdir(self.conf_path)
|
|
|
|
self.mkdir(self.global_explorer_out_path)
|
|
|
|
self.mkdir(self.bin_path)
|
|
|
|
|
|
|
|
def _create_conf_path_and_link_conf_dirs(self):
|
|
|
|
# Link destination directories
|
|
|
|
for sub_dir in [ "explorer", "manifest", "type" ]:
|
|
|
|
self.mkdir(os.path.join(self.conf_path, sub_dir))
|
|
|
|
|
|
|
|
# Iterate over all directories and link the to the output dir
|
|
|
|
for conf_dir in self.conf_dirs:
|
2012-10-30 15:36:32 +00:00
|
|
|
self.log.debug("Checking conf_dir %s ..." % (conf_dir))
|
2012-10-30 15:05:37 +00:00
|
|
|
for sub_dir in [ "explorer", "manifest", "type" ]:
|
|
|
|
current_dir = os.path.join(conf_dir, sub_dir)
|
|
|
|
|
2012-10-30 15:16:59 +00:00
|
|
|
# Allow conf dirs to contain only partial content
|
|
|
|
if not os.path.exists(current_dir):
|
|
|
|
continue
|
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
for entry in os.listdir(current_dir):
|
|
|
|
rel_entry_path = os.path.join(sub_dir, entry)
|
2012-10-30 15:11:03 +00:00
|
|
|
src = os.path.join(conf_dir, sub_dir, entry)
|
2012-10-30 15:16:59 +00:00
|
|
|
dst = os.path.join(self.conf_path, sub_dir, entry)
|
2012-10-30 15:05:37 +00:00
|
|
|
|
|
|
|
# Already exists? remove and link
|
|
|
|
if os.path.exists(dst):
|
2012-10-30 15:11:03 +00:00
|
|
|
os.unlink(dst)
|
2012-10-30 15:16:59 +00:00
|
|
|
|
|
|
|
self.log.debug("Linking %s to %s ..." % (src, dst))
|
2012-10-30 15:05:37 +00:00
|
|
|
try:
|
|
|
|
os.symlink(src, dst)
|
|
|
|
except OSError as e:
|
|
|
|
raise cdist.Error("Linking %s %s to %s failed: %s" % (sub_dir, src, dst, e.__str__()))
|
|
|
|
|
2012-11-01 12:30:45 +00:00
|
|
|
def _link_types_for_emulator(self):
|
2011-10-12 14:46:54 +00:00
|
|
|
"""Link emulator to types"""
|
2012-11-01 12:30:45 +00:00
|
|
|
src = os.path.abspath(self.exec_path)
|
2012-02-12 00:27:25 +00:00
|
|
|
for cdist_type in core.CdistType.list_types(self.type_path):
|
2011-10-12 14:46:54 +00:00
|
|
|
dst = os.path.join(self.bin_path, cdist_type.name)
|
|
|
|
self.log.debug("Linking emulator: %s to %s", src, dst)
|
|
|
|
|
2011-10-15 22:12:32 +00:00
|
|
|
try:
|
|
|
|
os.symlink(src, dst)
|
|
|
|
except OSError as e:
|
2012-02-15 16:05:22 +00:00
|
|
|
raise cdist.Error("Linking emulator from %s to %s failed: %s" % (src, dst, e.__str__()))
|