You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.3 KiB
119 lines
4.3 KiB
13 years ago
|
# -*- 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
|
||
13 years ago
|
import os
|
||
13 years ago
|
import shutil
|
||
13 years ago
|
import sys
|
||
|
import tempfile
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
log = logging.getLogger(__name__)
|
||
13 years ago
|
|
||
13 years ago
|
import cdist.exec
|
||
13 years ago
|
|
||
13 years ago
|
class Path:
|
||
|
"""Class that handles path related configurations"""
|
||
|
|
||
13 years ago
|
def __init__(self, target_host, initial_manifest=False, debug=False):
|
||
|
|
||
|
self.target_host = target_host
|
||
13 years ago
|
|
||
13 years ago
|
# Base and Temp Base
|
||
13 years ago
|
if "__cdist_base_dir" in os.environ:
|
||
|
self.base_dir = os.environ['__cdist_base_dir']
|
||
13 years ago
|
else:
|
||
13 years ago
|
self.base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
|
||
13 years ago
|
|
||
13 years ago
|
# Input directories
|
||
13 years ago
|
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")
|
||
|
|
||
13 years ago
|
# 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")
|
||
13 years ago
|
|
||
13 years ago
|
# Output directories
|
||
13 years ago
|
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")
|
||
|
|
||
13 years ago
|
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")
|
||
|
|
||
13 years ago
|
# Remote directories
|
||
|
if "__cdist_remote_base_dir" in os.environ:
|
||
|
self.remote_base_dir = os.environ['__cdist_remote_base_dir']
|
||
|
else:
|
||
|
self.remote_base_dir = "/var/lib/cdist"
|
||
|
|
||
|
self.remote_conf_dir = os.path.join(self.remote_base_dir, "conf")
|
||
|
self.remote_object_dir = os.path.join(self.remote_base_dir, "object")
|
||
|
self.remote_type_dir = os.path.join(self.remote_conf_dir, "type")
|
||
|
self.remote_global_explorer_dir = os.path.join(self.remote_conf_dir, "explorer")
|
||
|
|
||
13 years ago
|
# Create directories
|
||
|
self.__init_out_dirs()
|
||
13 years ago
|
|
||
|
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)"
|
||
|
#
|
||
13 years ago
|
log.debug("Saving" + self.base_dir + "to " + self.cache_dir)
|
||
13 years ago
|
# Remove previous cache
|
||
|
if os.path.exists(self.cache_dir):
|
||
|
shutil.rmtree(self.cache_dir)
|
||
13 years ago
|
shutil.move(self.base_dir, self.cache_dir)
|
||
13 years ago
|
|
||
13 years ago
|
def __init_env(self):
|
||
|
"""Setup environment"""
|
||
|
os.environ['__cdist_out_dir'] = self.out_dir
|
||
|
|
||
13 years ago
|
def __init_out_dirs(self):
|
||
|
"""Initialise output directory structure"""
|
||
13 years ago
|
|
||
|
# Create base dir, if user supplied and not existing
|
||
|
if not os.isdir(self.base_dir):
|
||
|
os.mkdir(self.base_dir)
|
||
|
|
||
13 years ago
|
os.mkdir(self.out_dir)
|
||
|
os.mkdir(self.global_explorer_out_dir)
|
||
|
os.mkdir(self.bin_dir)
|
||
|
|
||
13 years ago
|
def remote_mkdir(self, directory):
|
||
|
"""Create directory on remote side"""
|
||
13 years ago
|
cdist.exec.run_or_fail(["mkdir", "-p", directory], remote_prefix=True)
|
||
13 years ago
|
|
||
|
def remove_remote_dir(self, destination):
|
||
13 years ago
|
cdist.exec.run_or_fail(["rm", "-rf", destination], remote_prefix=True)
|
||
13 years ago
|
|