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:
|
2011-10-07 13:03:26 +00:00
|
|
|
"""Storing context information"""
|
2011-09-23 15:00:48 +00:00
|
|
|
|
2011-10-07 13:03:26 +00:00
|
|
|
def __init__(self,
|
|
|
|
target_host,
|
|
|
|
initial_manifest=False,
|
|
|
|
base_path=False,
|
|
|
|
out_path=False,
|
2011-10-07 13:05:52 +00:00
|
|
|
remote_base_path=False,
|
2011-10-07 11:45:19 +00:00
|
|
|
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 13:03:26 +00:00
|
|
|
if base_path:
|
|
|
|
self.base_path = base_path
|
2011-09-23 15:00:48 +00:00
|
|
|
else:
|
2011-10-07 13:03:26 +00:00
|
|
|
self.base_path =
|
|
|
|
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-07 13:05:52 +00:00
|
|
|
# Local input directories
|
2011-10-07 13:03:26 +00:00
|
|
|
self.cache_path = os.path.join(self.base_path, "cache", target_host)
|
|
|
|
self.conf_path = os.path.join(self.base_path, "conf")
|
2011-10-07 10:43:17 +00:00
|
|
|
|
2011-10-07 13:03:26 +00:00
|
|
|
self.global_explorer_path = os.path.join(self.conf_path, "explorer")
|
|
|
|
self.manifest_path = os.path.join(self.conf_path, "manifest")
|
|
|
|
self.type_base_path = os.path.join(self.conf_path, "type")
|
|
|
|
self.lib_path = os.path.join(self.base_path, "lib")
|
2011-09-23 15:06:42 +00:00
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
if initial_manifest:
|
|
|
|
self.initial_manifest = initial_manifest
|
|
|
|
else:
|
2011-10-07 13:03:26 +00:00
|
|
|
self.initial_manifest = os.path.join(self.manifest_path, "init")
|
2011-09-23 15:06:42 +00:00
|
|
|
|
2011-10-07 13:05:52 +00:00
|
|
|
# Local output directories
|
2011-10-07 13:03:26 +00:00
|
|
|
if out_path:
|
|
|
|
self.out_path = out_path:
|
2011-10-07 07:38:52 +00:00
|
|
|
else:
|
2011-10-07 13:03:26 +00:00
|
|
|
self.out_path = os.path.join(tempfile.mkdtemp(), "out")
|
2011-10-07 07:38:52 +00:00
|
|
|
|
2011-10-07 13:05:52 +00:00
|
|
|
self.bin_path = os.path.join(self.out_path, "bin")
|
2011-10-07 13:03:26 +00:00
|
|
|
self.global_explorer_out_path = os.path.join(self.out_path, "explorer")
|
2011-10-07 13:05:52 +00:00
|
|
|
self.object_base_path = os.path.join(self.out_path, "object")
|
2011-09-23 15:06:42 +00:00
|
|
|
|
2011-10-07 07:53:24 +00:00
|
|
|
# Remote directories
|
2011-10-07 13:05:52 +00:00
|
|
|
if remote_base_path:
|
|
|
|
self.remote_base_path = remote_base_path
|
2011-10-07 07:53:24 +00:00
|
|
|
else:
|
2011-10-07 13:03:26 +00:00
|
|
|
self.remote_base_path = "/var/lib/cdist"
|
2011-10-07 07:53:24 +00:00
|
|
|
|
2011-10-07 13:03:26 +00:00
|
|
|
self.remote_conf_path = os.path.join(self.remote_base_path, "conf")
|
2011-10-07 13:05:52 +00:00
|
|
|
self.remote_object_path = os.path.join(self.remote_base_path, "object")
|
|
|
|
self.remote_type_path = os.path.join(self.remote_conf_path, "type")
|
|
|
|
self.remote_global_explorer_path = os.path.join(self.remote_conf_path, "explorer")
|
2011-10-07 07:53:24 +00:00
|
|
|
|
2011-10-05 14:49:20 +00:00
|
|
|
# Create directories
|
2011-10-07 13:05:52 +00:00
|
|
|
self.__init_out_paths()
|
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 13:03:26 +00:00
|
|
|
log.debug("Saving" + self.base_path + "to " + self.cache_path)
|
2011-09-23 15:06:42 +00:00
|
|
|
# Remove previous cache
|
2011-10-07 13:03:26 +00:00
|
|
|
if os.path.exists(self.cache_path):
|
|
|
|
shutil.rmtree(self.cache_path)
|
|
|
|
shutil.move(self.base_path, self.cache_path)
|
2011-10-06 10:34:34 +00:00
|
|
|
|
2011-10-07 13:20:38 +00:00
|
|
|
#def __init_env(self):
|
|
|
|
# """Setup environment"""
|
|
|
|
# os.environ['__cdist_out_path'] = self.out_path
|
|
|
|
# os.environ['__cdist_base_path'] = self.base_path
|
2011-10-07 07:27:46 +00:00
|
|
|
|
2011-10-07 13:05:52 +00:00
|
|
|
def __init_out_paths(self):
|
2011-10-05 14:49:20 +00:00
|
|
|
"""Initialise output directory structure"""
|
2011-10-07 07:38:52 +00:00
|
|
|
|
|
|
|
# Create base dir, if user supplied and not existing
|
2011-10-07 13:03:26 +00:00
|
|
|
if not os.path.isdir(self.base_path):
|
|
|
|
os.mkdir(self.base_path)
|
2011-10-07 07:38:52 +00:00
|
|
|
|
2011-10-07 13:05:52 +00:00
|
|
|
os.mkdir(self.out_path)
|
|
|
|
os.mkdir(self.global_explorer_out_path)
|
|
|
|
os.mkdir(self.bin_path)
|
2011-10-05 14:49:20 +00:00
|
|
|
|
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
|
|
|
|
2011-10-07 13:05:52 +00:00
|
|
|
def remove_remote_path(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
|
|
|
|