2011-09-23 18:08:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2010-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
|
2011-09-23 17:10:07 +00:00
|
|
|
import os
|
2011-09-26 09:25:22 +00:00
|
|
|
import shutil
|
2011-09-23 17:10:07 +00:00
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
2011-09-23 15:19:16 +00:00
|
|
|
# Hardcoded paths usually not changable
|
2011-09-23 15:11:03 +00:00
|
|
|
REMOTE_BASE_DIR = "/var/lib/cdist"
|
|
|
|
REMOTE_CONF_DIR = os.path.join(REMOTE_BASE_DIR, "conf")
|
|
|
|
REMOTE_OBJECT_DIR = os.path.join(REMOTE_BASE_DIR, "object")
|
|
|
|
REMOTE_TYPE_DIR = os.path.join(REMOTE_CONF_DIR, "type")
|
|
|
|
REMOTE_GLOBAL_EXPLORER_DIR = os.path.join(REMOTE_CONF_DIR, "explorer")
|
|
|
|
|
|
|
|
DOT_CDIST = ".cdist"
|
|
|
|
|
2011-09-26 08:28:57 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2011-09-23 18:08:55 +00:00
|
|
|
|
2011-09-23 18:53:09 +00:00
|
|
|
import cdist.exec
|
2011-09-23 18:08:55 +00:00
|
|
|
|
2011-09-23 15:00:48 +00:00
|
|
|
class Path:
|
|
|
|
"""Class that handles path related configurations"""
|
|
|
|
|
2011-10-07 07:38:52 +00:00
|
|
|
def __init__(self, target_host, initial_manifest=False, debug=False):
|
|
|
|
|
|
|
|
self.target_host = target_host
|
2011-09-23 18:08:55 +00:00
|
|
|
|
2011-09-23 15:06:42 +00:00
|
|
|
# Base and Temp Base
|
2011-10-07 07:38:52 +00:00
|
|
|
if "__cdist_base_dir" in os.environ:
|
|
|
|
self.base_dir = os.environ['__cdist_base_dir']
|
2011-09-23 15:00:48 +00:00
|
|
|
else:
|
2011-09-23 17:13:36 +00:00
|
|
|
self.base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
|
2011-09-23 15:00:48 +00:00
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
# Input directories
|
2011-09-23 15:06:42 +00:00
|
|
|
self.conf_dir = os.path.join(self.base_dir, "conf")
|
|
|
|
self.cache_base_dir = os.path.join(self.base_dir, "cache")
|
|
|
|
self.cache_dir = os.path.join(self.cache_base_dir, target_host)
|
|
|
|
self.global_explorer_dir = os.path.join(self.conf_dir, "explorer")
|
|
|
|
self.lib_dir = os.path.join(self.base_dir, "lib")
|
|
|
|
self.manifest_dir = os.path.join(self.conf_dir, "manifest")
|
|
|
|
self.type_base_dir = os.path.join(self.conf_dir, "type")
|
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
# Mostly static, but can be overwritten on user demand
|
|
|
|
if initial_manifest:
|
|
|
|
self.initial_manifest = initial_manifest
|
|
|
|
else:
|
|
|
|
self.initial_manifest = os.path.join(self.manifest_dir, "init")
|
2011-09-23 15:06:42 +00:00
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
# Output directories
|
2011-10-07 07:38:52 +00:00
|
|
|
if "__cdist_out_dir" in os.environ:
|
|
|
|
self.out_dir = os.environ['__cdist_out_dir']
|
|
|
|
else:
|
|
|
|
self.out_dir = os.path.join(tempfile.mkdtemp(), "out")
|
|
|
|
|
2011-09-23 15:06:42 +00:00
|
|
|
self.global_explorer_out_dir = os.path.join(self.out_dir, "explorer")
|
|
|
|
self.object_base_dir = os.path.join(self.out_dir, "object")
|
|
|
|
self.bin_dir = os.path.join(self.out_dir, "bin")
|
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
# Create directories
|
|
|
|
self.__init_out_dirs()
|
2011-09-23 15:06:42 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
# Do not use in __del__:
|
|
|
|
# http://docs.python.org/reference/datamodel.html#customization
|
|
|
|
# "other globals referenced by the __del__() method may already have been deleted
|
|
|
|
# or in the process of being torn down (e.g. the import machinery shutting down)"
|
|
|
|
#
|
2011-10-07 07:38:52 +00:00
|
|
|
log.debug("Saving" + self.base_dir + "to " + self.cache_dir)
|
2011-09-23 15:06:42 +00:00
|
|
|
# Remove previous cache
|
|
|
|
if os.path.exists(self.cache_dir):
|
|
|
|
shutil.rmtree(self.cache_dir)
|
2011-10-07 07:38:52 +00:00
|
|
|
shutil.move(self.base_dir, self.cache_dir)
|
2011-10-06 10:34:34 +00:00
|
|
|
|
2011-10-07 07:27:46 +00:00
|
|
|
def __init_env(self):
|
|
|
|
"""Setup environment"""
|
|
|
|
os.environ['__cdist_out_dir'] = self.out_dir
|
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
def __init_out_dirs(self):
|
|
|
|
"""Initialise output directory structure"""
|
2011-10-07 07:38:52 +00:00
|
|
|
|
|
|
|
# Create base dir, if user supplied and not existing
|
|
|
|
if not os.isdir(self.base_dir):
|
|
|
|
os.mkdir(self.base_dir)
|
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
os.mkdir(self.out_dir)
|
|
|
|
os.mkdir(self.global_explorer_out_dir)
|
|
|
|
os.mkdir(self.bin_dir)
|
|
|
|
|
2011-09-23 15:11:03 +00:00
|
|
|
def remote_mkdir(self, directory):
|
|
|
|
"""Create directory on remote side"""
|
2011-10-06 12:02:37 +00:00
|
|
|
cdist.exec.run_or_fail(["mkdir", "-p", directory], remote_prefix=True)
|
2011-09-23 15:11:03 +00:00
|
|
|
|
|
|
|
def remove_remote_dir(self, destination):
|
2011-10-06 12:02:37 +00:00
|
|
|
cdist.exec.run_or_fail(["rm", "-rf", destination], remote_prefix=True)
|
2011-09-23 15:11:03 +00:00
|
|
|
|
2011-10-07 07:46:06 +00:00
|
|
|
# FIXME Move into configinstall
|
2011-09-23 15:11:03 +00:00
|
|
|
def transfer_object_parameter(self, cdist_object):
|
|
|
|
"""Transfer the object parameter to the remote destination"""
|
|
|
|
# Create base path before using mkdir -p
|
|
|
|
self.remote_mkdir(self.remote_object_parameter_dir(cdist_object))
|
|
|
|
|
|
|
|
# Synchronise parameter dir afterwards
|
|
|
|
self.transfer_dir(self.object_parameter_dir(cdist_object),
|
|
|
|
self.remote_object_parameter_dir(cdist_object))
|
|
|
|
|
2011-10-07 07:46:06 +00:00
|
|
|
# FIXME Move into configinstall
|
2011-09-23 15:11:03 +00:00
|
|
|
def transfer_global_explorers(self):
|
|
|
|
"""Transfer the global explorers"""
|
|
|
|
self.remote_mkdir(REMOTE_GLOBAL_EXPLORER_DIR)
|
|
|
|
self.transfer_dir(self.global_explorer_dir, REMOTE_GLOBAL_EXPLORER_DIR)
|
|
|
|
|
2011-10-07 07:46:06 +00:00
|
|
|
# FIXME Move into configinstall
|
2011-09-23 15:11:03 +00:00
|
|
|
def transfer_type_explorers(self, type):
|
|
|
|
"""Transfer explorers of a type, but only once"""
|
2011-10-07 07:01:47 +00:00
|
|
|
if type.transferred_explorers:
|
2011-09-23 15:11:03 +00:00
|
|
|
log.debug("Skipping retransfer for explorers of %s", type)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
# Do not retransfer
|
2011-10-07 07:01:47 +00:00
|
|
|
type.transferred_explorers = True
|
2011-09-23 15:11:03 +00:00
|
|
|
|
2011-10-06 18:52:34 +00:00
|
|
|
# FIXME: Can be explorer_path or explorer_dir, I don't care.
|
2011-10-06 18:51:31 +00:00
|
|
|
src = type.explorer_path()
|
|
|
|
dst = type.remote_explorer_path()
|
2011-09-23 15:11:03 +00:00
|
|
|
|
2011-10-06 18:51:31 +00:00
|
|
|
# Transfer if there is at least one explorer
|
|
|
|
if len(type.explorers) > 0:
|
2011-09-23 15:11:03 +00:00
|
|
|
# Ensure that the path exists
|
2011-10-06 18:51:31 +00:00
|
|
|
self.remote_mkdir(dst)
|
2011-09-23 15:11:03 +00:00
|
|
|
self.transfer_dir(src, dst)
|