2011-10-12 13:17:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2015-03-05 14:02:26 +00:00
|
|
|
# 2011-2015 Nico Schottelius (nico-cdist at schottelius.org)
|
2017-07-01 21:59:51 +00:00
|
|
|
# 2016-2017 Darko Poljak (darko.poljak at gmail.com)
|
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 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
|
2013-08-18 23:38:28 +00:00
|
|
|
import tempfile
|
2017-07-01 21:59:51 +00:00
|
|
|
import time
|
|
|
|
import datetime
|
2011-10-12 13:17:06 +00:00
|
|
|
|
|
|
|
import cdist
|
2013-11-26 00:17:37 +00:00
|
|
|
import cdist.message
|
2011-10-12 14:46:54 +00:00
|
|
|
from cdist import core
|
2016-06-03 10:20:28 +00:00
|
|
|
import cdist.exec.util as exec_util
|
2011-10-12 13:17:06 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
CONF_SUBDIRS_LINKED = ["explorer", "files", "manifest", "type", ]
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
2013-08-18 22:52:15 +00:00
|
|
|
def __init__(self,
|
|
|
|
target_host,
|
2017-07-20 20:04:44 +00:00
|
|
|
target_host_tags,
|
2016-07-23 14:13:59 +00:00
|
|
|
base_root_path,
|
|
|
|
host_dir_name,
|
2013-08-19 09:21:53 +00:00
|
|
|
exec_path=sys.argv[0],
|
2013-08-18 22:52:15 +00:00
|
|
|
initial_manifest=None,
|
2017-07-01 21:59:51 +00:00
|
|
|
add_conf_dirs=None,
|
2017-06-29 08:18:46 +00:00
|
|
|
cache_path_pattern=None,
|
2017-08-30 21:02:17 +00:00
|
|
|
quiet_mode=False,
|
|
|
|
configuration=None):
|
2012-10-30 15:05:37 +00:00
|
|
|
|
2011-10-12 13:17:06 +00:00
|
|
|
self.target_host = target_host
|
2017-07-20 20:04:44 +00:00
|
|
|
if target_host_tags is None:
|
|
|
|
self.target_host_tags = ""
|
|
|
|
else:
|
|
|
|
self.target_host_tags = ",".join(target_host_tags)
|
2016-07-23 14:13:59 +00:00
|
|
|
self.hostdir = host_dir_name
|
|
|
|
self.base_path = os.path.join(base_root_path, "data")
|
2013-08-18 23:38:28 +00:00
|
|
|
|
2012-11-01 12:30:45 +00:00
|
|
|
self.exec_path = exec_path
|
2013-08-18 22:52:15 +00:00
|
|
|
self.custom_initial_manifest = initial_manifest
|
2012-11-01 12:46:01 +00:00
|
|
|
self._add_conf_dirs = add_conf_dirs
|
2017-07-01 21:59:51 +00:00
|
|
|
self.cache_path_pattern = cache_path_pattern
|
2017-06-29 08:18:46 +00:00
|
|
|
self.quiet_mode = quiet_mode
|
2017-08-30 21:02:17 +00:00
|
|
|
if configuration:
|
|
|
|
self.configuration = configuration
|
|
|
|
else:
|
|
|
|
self.configuration = {}
|
2011-10-12 13:17:06 +00:00
|
|
|
|
2016-07-23 14:13:59 +00:00
|
|
|
self._init_log()
|
|
|
|
self._init_permissions()
|
|
|
|
self.mkdir(self.base_path)
|
|
|
|
# FIXME: create dir that does not require moving later
|
|
|
|
self._init_cache_dir(None)
|
2012-10-30 15:05:37 +00:00
|
|
|
self._init_paths()
|
2015-03-05 14:02:26 +00:00
|
|
|
self._init_object_marker()
|
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):
|
2016-07-05 18:44:24 +00:00
|
|
|
return os.path.abspath(os.path.join(os.path.dirname(cdist.__file__),
|
|
|
|
"conf"))
|
2012-11-01 14:17:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def home_dir(self):
|
2017-07-01 21:59:51 +00:00
|
|
|
return cdist.home_dir()
|
2011-10-12 13:17:06 +00:00
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
def _init_log(self):
|
2016-08-10 21:56:56 +00:00
|
|
|
self.log = logging.getLogger(self.target_host[0])
|
2011-10-18 11:32:36 +00:00
|
|
|
|
2016-08-12 19:14:56 +00:00
|
|
|
# logger is not pickable, so remove it when we pickle
|
|
|
|
def __getstate__(self):
|
|
|
|
state = self.__dict__.copy()
|
|
|
|
if 'log' in state:
|
|
|
|
del state['log']
|
|
|
|
return state
|
|
|
|
|
|
|
|
# recreate logger when we unpickle
|
|
|
|
def __setstate__(self, state):
|
|
|
|
self.__dict__.update(state)
|
|
|
|
self._init_log()
|
|
|
|
|
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
|
2013-08-19 09:45:22 +00:00
|
|
|
self.bin_path = os.path.join(self.base_path, "bin")
|
|
|
|
self.conf_path = os.path.join(self.base_path, "conf")
|
2016-07-05 18:44:24 +00:00
|
|
|
self.global_explorer_out_path = os.path.join(self.base_path,
|
|
|
|
"explorer")
|
2013-08-19 09:45:22 +00:00
|
|
|
self.object_path = os.path.join(self.base_path, "object")
|
2013-11-26 00:01:30 +00:00
|
|
|
self.messages_path = os.path.join(self.base_path, "messages")
|
2016-06-11 10:02:13 +00:00
|
|
|
self.files_path = os.path.join(self.conf_path, "files")
|
2012-10-30 15:05:37 +00:00
|
|
|
|
|
|
|
# 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")
|
2013-08-18 22:52:15 +00:00
|
|
|
self.initial_manifest = (self.custom_initial_manifest or
|
2016-07-05 18:44:24 +00:00
|
|
|
os.path.join(self.manifest_path, "init"))
|
2013-08-18 22:52:15 +00:00
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
self.type_path = os.path.join(self.conf_path, "type")
|
|
|
|
|
2015-03-05 14:02:26 +00:00
|
|
|
def _init_object_marker(self):
|
|
|
|
self.object_marker_file = os.path.join(self.base_path, "object_marker")
|
|
|
|
|
|
|
|
# Does not need to be secure - just randomly different from .cdist
|
|
|
|
self.object_marker_name = tempfile.mktemp(prefix='.cdist-', dir='')
|
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
def _init_conf_dirs(self):
|
|
|
|
self.conf_dirs = []
|
|
|
|
|
2013-08-12 10:11:43 +00:00
|
|
|
self.conf_dirs.append(self.dist_conf_dir)
|
2012-10-30 15:05:37 +00:00
|
|
|
|
|
|
|
# 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
|
2017-08-30 21:02:17 +00:00
|
|
|
# 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)
|
|
|
|
if 'conf_dir' in self.configuration:
|
2017-08-30 21:57:47 +00:00
|
|
|
conf_dirs = self.configuration['conf_dir']
|
|
|
|
if conf_dirs:
|
|
|
|
self.conf_dirs.extend(conf_dirs)
|
2012-11-30 12:48:34 +00:00
|
|
|
|
2013-08-12 10:11:43 +00:00
|
|
|
# Add command line 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
|
|
|
|
2013-08-18 23:38:28 +00:00
|
|
|
def _init_directories(self):
|
|
|
|
self.mkdir(self.conf_path)
|
|
|
|
self.mkdir(self.global_explorer_out_path)
|
2015-03-05 14:02:26 +00:00
|
|
|
self.mkdir(self.object_path)
|
2013-08-18 23:38:28 +00:00
|
|
|
self.mkdir(self.bin_path)
|
2017-02-01 22:10:26 +00:00
|
|
|
self.mkdir(self.cache_path)
|
2013-08-18 23:38:28 +00:00
|
|
|
|
2013-08-19 06:48:41 +00:00
|
|
|
def create_files_dirs(self):
|
|
|
|
self._init_directories()
|
|
|
|
self._create_conf_path_and_link_conf_dirs()
|
2013-11-26 00:17:37 +00:00
|
|
|
self._create_messages()
|
2013-08-19 06:48:41 +00:00
|
|
|
self._link_types_for_emulator()
|
2015-03-05 14:02:26 +00:00
|
|
|
self._setup_object_marker_file()
|
|
|
|
|
|
|
|
def _setup_object_marker_file(self):
|
|
|
|
with open(self.object_marker_file, 'w') as fd:
|
|
|
|
fd.write("%s\n" % self.object_marker_name)
|
|
|
|
|
2017-06-28 21:36:42 +00:00
|
|
|
self.log.trace("Object marker %s saved in %s" % (
|
2016-07-05 18:44:24 +00:00
|
|
|
self.object_marker_name, self.object_marker_file))
|
2013-08-19 06:48:41 +00:00
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
def _init_cache_dir(self, cache_dir):
|
|
|
|
if cache_dir:
|
|
|
|
self.cache_path = cache_dir
|
2017-08-30 21:02:17 +00:00
|
|
|
elif self.home_dir:
|
|
|
|
self.cache_path = os.path.join(self.home_dir, "cache")
|
2012-10-30 15:05:37 +00:00
|
|
|
else:
|
2017-08-30 21:02:17 +00:00
|
|
|
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."""
|
2017-06-28 21:36:42 +00:00
|
|
|
self.log.trace("Local rmdir: %s", path)
|
2011-10-12 13:17:06 +00:00
|
|
|
shutil.rmtree(path)
|
|
|
|
|
|
|
|
def mkdir(self, path):
|
|
|
|
"""Create directory on the local side."""
|
2017-06-28 21:36:42 +00:00
|
|
|
self.log.trace("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
|
|
|
|
2016-07-12 18:15:08 +00:00
|
|
|
def run(self, command, env=None, return_output=False, message_prefix=None,
|
2017-09-07 10:10:17 +00:00
|
|
|
save_output=True, quiet_mode=False):
|
2011-10-12 13:17:06 +00:00
|
|
|
"""Run the given command with the given environment.
|
|
|
|
Return the output as a string.
|
|
|
|
|
|
|
|
"""
|
2016-07-05 18:44:24 +00:00
|
|
|
assert isinstance(command, (list, tuple)), (
|
|
|
|
"list or tuple argument expected, got: %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()
|
2016-08-10 21:56:56 +00:00
|
|
|
# Export __target_host, __target_hostname, __target_fqdn
|
|
|
|
# for use in __remote_{copy,exec} scripts
|
|
|
|
env['__target_host'] = self.target_host[0]
|
|
|
|
env['__target_hostname'] = self.target_host[1]
|
|
|
|
env['__target_fqdn'] = self.target_host[2]
|
2011-11-18 13:44:32 +00:00
|
|
|
|
2015-03-05 14:02:26 +00:00
|
|
|
# Export for emulator
|
|
|
|
env['__cdist_object_marker'] = self.object_marker_name
|
|
|
|
|
2013-11-26 00:17:37 +00:00
|
|
|
if message_prefix:
|
|
|
|
message = cdist.message.Message(message_prefix, self.messages_path)
|
2013-12-03 13:36:38 +00:00
|
|
|
env.update(message.env)
|
2013-11-26 00:01:30 +00:00
|
|
|
|
2017-06-29 07:22:59 +00:00
|
|
|
self.log.trace("Local run: %s", command)
|
2011-10-12 13:17:06 +00:00
|
|
|
try:
|
2017-09-07 10:10:17 +00:00
|
|
|
if self.quiet_mode or quiet_mode:
|
2017-06-29 08:18:46 +00:00
|
|
|
stderr = subprocess.DEVNULL
|
|
|
|
else:
|
|
|
|
stderr = None
|
2016-07-12 18:15:08 +00:00
|
|
|
if save_output:
|
2017-06-29 08:18:46 +00:00
|
|
|
output, errout = exec_util.call_get_output(
|
|
|
|
command, env=env, stderr=stderr)
|
2017-06-28 21:36:42 +00:00
|
|
|
self.log.trace("Local stdout: {}".format(output))
|
2016-07-15 06:22:05 +00:00
|
|
|
# Currently, stderr is not captured.
|
2017-06-28 21:36:42 +00:00
|
|
|
# self.log.trace("Local stderr: {}".format(errout))
|
2016-07-12 18:15:08 +00:00
|
|
|
if return_output:
|
|
|
|
return output.decode()
|
|
|
|
else:
|
|
|
|
# In some cases no output is saved.
|
|
|
|
# This is used for shell command, stdout and stderr
|
|
|
|
# must not be catched.
|
2017-09-07 10:10:17 +00:00
|
|
|
if self.quiet_mode or quiet_mode:
|
2017-06-29 08:18:46 +00:00
|
|
|
stdout = subprocess.DEVNULL
|
|
|
|
else:
|
|
|
|
stdout = None
|
|
|
|
subprocess.check_call(command, env=env, stderr=stderr,
|
|
|
|
stdout=stdout)
|
2016-06-03 10:20:28 +00:00
|
|
|
except subprocess.CalledProcessError as e:
|
2016-06-23 08:56:37 +00:00
|
|
|
exec_util.handle_called_process_error(e, command)
|
2011-10-12 13:17:06 +00:00
|
|
|
except OSError as error:
|
2016-03-18 17:22:14 +00:00
|
|
|
raise cdist.Error(" ".join(command) + ": " + error.args[1])
|
2013-11-26 00:01:30 +00:00
|
|
|
finally:
|
2013-11-26 00:17:37 +00:00
|
|
|
if message_prefix:
|
2013-11-26 00:01:30 +00:00
|
|
|
message.merge_messages()
|
2011-10-12 13:17:06 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
def run_script(self, script, env=None, return_output=False,
|
2017-01-20 14:22:25 +00:00
|
|
|
message_prefix=None, save_output=True):
|
2011-10-12 13:17:06 +00:00
|
|
|
"""Run the given script with the given environment.
|
|
|
|
Return the output as a string.
|
|
|
|
|
|
|
|
"""
|
2017-06-28 11:57:48 +00:00
|
|
|
if os.access(script, os.X_OK):
|
|
|
|
self.log.debug('%s is executable, running it', script)
|
2017-07-01 21:59:51 +00:00
|
|
|
command = [script]
|
2017-06-28 11:57:48 +00:00
|
|
|
else:
|
2017-08-30 21:02:17 +00:00
|
|
|
command = [self.configuration.get('local_shell', "/bin/sh"), "-e"]
|
2017-06-28 11:57:48 +00:00
|
|
|
self.log.debug('%s is NOT executable, running it with %s',
|
2017-07-01 21:59:51 +00:00
|
|
|
script, " ".join(command))
|
2017-06-28 11:57:48 +00:00
|
|
|
command.append(script)
|
2011-10-12 13:17:06 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
return self.run(command=command, env=env, return_output=return_output,
|
2017-01-20 14:22:25 +00:00
|
|
|
message_prefix=message_prefix, save_output=save_output)
|
2014-01-09 23:41:17 +00:00
|
|
|
|
2017-07-01 21:59:51 +00:00
|
|
|
def _cache_subpath_repl(self, matchobj):
|
|
|
|
if matchobj.group(2) == '%P':
|
|
|
|
repl = str(os.getpid())
|
|
|
|
elif matchobj.group(2) == '%h':
|
|
|
|
repl = self.hostdir
|
|
|
|
elif matchobj.group(2) == '%N':
|
|
|
|
repl = self.target_host[0]
|
|
|
|
|
|
|
|
return matchobj.group(1) + repl
|
|
|
|
|
|
|
|
def _cache_subpath(self, start_time=time.time(), path_format=None):
|
|
|
|
if path_format:
|
|
|
|
repl_func = self._cache_subpath_repl
|
|
|
|
cache_subpath = re.sub(r'([^%]|^)(%h|%P|%N)', repl_func,
|
|
|
|
path_format)
|
|
|
|
dt = datetime.datetime.fromtimestamp(start_time)
|
|
|
|
cache_subpath = dt.strftime(cache_subpath)
|
|
|
|
else:
|
|
|
|
cache_subpath = self.hostdir
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
while i < len(cache_subpath) and cache_subpath[i] == os.sep:
|
|
|
|
i += 1
|
|
|
|
cache_subpath = cache_subpath[i:]
|
|
|
|
if not cache_subpath:
|
|
|
|
cache_subpath = self.hostdir
|
|
|
|
return cache_subpath
|
|
|
|
|
|
|
|
def save_cache(self, start_time=time.time()):
|
2017-06-28 21:36:42 +00:00
|
|
|
self.log.trace("cache subpath pattern: {}".format(
|
2017-07-01 21:59:51 +00:00
|
|
|
self.cache_path_pattern))
|
|
|
|
cache_subpath = self._cache_subpath(start_time,
|
|
|
|
self.cache_path_pattern)
|
|
|
|
self.log.debug("cache subpath: {}".format(cache_subpath))
|
|
|
|
destination = os.path.join(self.cache_path, cache_subpath)
|
2017-06-29 12:46:35 +00:00
|
|
|
self.log.trace(("Saving cache: " + self.base_path + " to " +
|
|
|
|
destination))
|
2013-12-01 17:42:07 +00:00
|
|
|
|
2017-07-01 21:59:51 +00:00
|
|
|
if not os.path.exists(destination):
|
|
|
|
shutil.move(self.base_path, destination)
|
|
|
|
else:
|
|
|
|
for direntry in os.listdir(self.base_path):
|
|
|
|
srcentry = os.path.join(self.base_path, direntry)
|
|
|
|
destentry = os.path.join(destination, direntry)
|
|
|
|
try:
|
|
|
|
if os.path.isdir(destentry):
|
|
|
|
shutil.rmtree(destentry)
|
|
|
|
elif os.path.exists(destentry):
|
|
|
|
os.remove(destentry)
|
|
|
|
except (PermissionError, OSError) as e:
|
|
|
|
raise cdist.Error(
|
|
|
|
"Cannot delete old cache entry {}: {}".format(
|
|
|
|
destentry, e))
|
|
|
|
shutil.move(srcentry, destentry)
|
2013-12-01 17:42:07 +00:00
|
|
|
|
2016-08-21 15:10:24 +00:00
|
|
|
# add target_host since cache dir can be hash-ed target_host
|
2016-08-19 10:27:35 +00:00
|
|
|
host_cache_path = os.path.join(destination, "target_host")
|
|
|
|
with open(host_cache_path, 'w') as hostf:
|
|
|
|
print(self.target_host[0], file=hostf)
|
2012-10-30 15:05:37 +00:00
|
|
|
|
2013-11-26 00:17:37 +00:00
|
|
|
def _create_messages(self):
|
|
|
|
"""Create empty messages"""
|
|
|
|
with open(self.messages_path, "w"):
|
|
|
|
pass
|
|
|
|
|
2012-10-30 15:05:37 +00:00
|
|
|
def _create_conf_path_and_link_conf_dirs(self):
|
2014-04-12 17:40:41 +00:00
|
|
|
# Create destination directories
|
|
|
|
for sub_dir in CONF_SUBDIRS_LINKED:
|
2012-10-30 15:05:37 +00:00
|
|
|
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))
|
2014-04-12 17:40:41 +00:00
|
|
|
for sub_dir in CONF_SUBDIRS_LINKED:
|
2012-10-30 15:05:37 +00:00
|
|
|
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):
|
2016-07-05 18:44:24 +00:00
|
|
|
src = os.path.abspath(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)
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2017-06-28 21:36:42 +00:00
|
|
|
self.log.trace("Linking %s to %s ..." % (src, dst))
|
2012-10-30 15:05:37 +00:00
|
|
|
try:
|
|
|
|
os.symlink(src, dst)
|
|
|
|
except OSError as e:
|
2016-07-05 18:44:24 +00:00
|
|
|
raise cdist.Error("Linking %s %s to %s failed: %s" % (
|
|
|
|
sub_dir, src, dst, e.__str__()))
|
2012-10-30 15:05:37 +00:00
|
|
|
|
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)
|
2017-06-28 21:36:42 +00:00
|
|
|
self.log.trace("Linking emulator: %s to %s", src, dst)
|
2011-10-12 14:46:54 +00:00
|
|
|
|
2011-10-15 22:12:32 +00:00
|
|
|
try:
|
|
|
|
os.symlink(src, dst)
|
|
|
|
except OSError as e:
|
2016-07-05 18:44:24 +00:00
|
|
|
raise cdist.Error(
|
|
|
|
"Linking emulator from %s to %s failed: %s" % (
|
|
|
|
src, dst, e.__str__()))
|