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:11:03 +00:00
|
|
|
|
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-10-07 10:43:17 +00:00
|
|
|
class Context:
|
|
|
|
"""Storing context dependent information"""
|
2011-09-23 15:00:48 +00:00
|
|
|
|
2011-10-07 11:45:19 +00:00
|
|
|
def __init__(self, target_host, initial_manifest=False, base_dir=False,
|
|
|
|
debug=False):
|
2011-10-07 07:38:52 +00:00
|
|
|
|
|
|
|
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 11:45:19 +00:00
|
|
|
if base_dir:
|
|
|
|
self.base_dir = base_dir
|
|
|
|
elif "__cdist_base_dir" in os.environ:
|
2011-10-07 07:38:52 +00:00
|
|
|
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-10-07 11:50:37 +00:00
|
|
|
|
2011-09-23 15:00:48 +00:00
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
# Input directories
|
2011-10-07 10:43:17 +00:00
|
|
|
self.cache_dir = os.path.join(self.base_dir, "cache", target_host)
|
2011-10-07 11:46:30 +00:00
|
|
|
self.conf_dir = os.path.join(self.base_dir, "conf")
|
2011-09-23 15:06:42 +00:00
|
|
|
self.manifest_dir = os.path.join(self.conf_dir, "manifest")
|
2011-10-07 10:43:17 +00:00
|
|
|
|
|
|
|
# Probably unused paths
|
|
|
|
# self.global_explorer_dir = os.path.join(self.conf_dir, "explorer")
|
|
|
|
# self.lib_dir = os.path.join(self.base_dir, "lib")
|
|
|
|
# self.type_base_dir = os.path.join(self.conf_dir, "type")
|
2011-09-23 15:06:42 +00:00
|
|
|
|
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-07 07:53:24 +00:00
|
|
|
# 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")
|
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
# Create directories
|
|
|
|
self.__init_out_dirs()
|
2011-09-23 15:06:42 +00:00
|
|
|
|
2011-10-07 11:50:37 +00:00
|
|
|
self.__init_env()
|
|
|
|
|
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"""
|
2011-10-07 11:50:37 +00:00
|
|
|
os.environ['__cdist_out_dir'] = self.out_dir
|
|
|
|
os.environ['__cdist_base_dir'] = self.base_dir
|
2011-10-07 07:27:46 +00:00
|
|
|
|
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
|
2011-10-07 11:48:10 +00:00
|
|
|
if not os.path.isdir(self.base_dir):
|
2011-10-07 07:38:52 +00:00
|
|
|
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
|
|
|
|