2011-10-11 13:59:26 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- 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
|
|
|
|
import os
|
2011-10-11 14:45:45 +00:00
|
|
|
import sys
|
|
|
|
import tempfile
|
2011-10-13 15:04:07 +00:00
|
|
|
import shutil
|
|
|
|
|
|
|
|
from cdist.exec import local
|
|
|
|
from cdist.exec import remote
|
|
|
|
|
2011-10-11 13:59:26 +00:00
|
|
|
|
2011-10-11 14:45:45 +00:00
|
|
|
class Context(object):
|
2011-10-11 14:04:09 +00:00
|
|
|
"""Hold information about current context"""
|
2011-10-11 13:59:26 +00:00
|
|
|
|
2011-10-11 14:45:45 +00:00
|
|
|
def __init__(self,
|
2011-10-11 13:59:26 +00:00
|
|
|
target_host,
|
2012-05-22 12:55:11 +00:00
|
|
|
remote_copy,
|
|
|
|
remote_exec,
|
2011-10-11 13:59:26 +00:00
|
|
|
initial_manifest=False,
|
2012-10-30 14:59:21 +00:00
|
|
|
conf_dirs=[],
|
2011-10-11 14:19:27 +00:00
|
|
|
exec_path=sys.argv[0],
|
2011-10-11 14:45:45 +00:00
|
|
|
debug=False):
|
2011-10-11 13:59:26 +00:00
|
|
|
|
2012-10-30 14:59:21 +00:00
|
|
|
self.debug = debug
|
2011-10-11 13:59:26 +00:00
|
|
|
self.target_host = target_host
|
|
|
|
self.exec_path = exec_path
|
|
|
|
|
2011-10-11 14:05:32 +00:00
|
|
|
# Context logging
|
2011-10-11 14:26:01 +00:00
|
|
|
self.log = logging.getLogger(self.target_host)
|
2011-10-11 14:05:32 +00:00
|
|
|
self.log.addFilter(self)
|
2011-10-11 13:59:26 +00:00
|
|
|
|
2011-10-13 14:25:44 +00:00
|
|
|
# Local temp directory
|
|
|
|
# FIXME: if __cdist_out_dir can be given from the outside, the same directory will be used for all hosts
|
2011-10-11 13:59:26 +00:00
|
|
|
if '__cdist_out_dir' in os.environ:
|
|
|
|
self.out_path = os.environ['__cdist_out_dir']
|
2011-10-11 14:45:45 +00:00
|
|
|
self.temp_dir = None
|
2011-10-11 13:59:26 +00:00
|
|
|
else:
|
2011-10-11 14:45:45 +00:00
|
|
|
self.temp_dir = tempfile.mkdtemp()
|
|
|
|
self.out_path = os.path.join(self.temp_dir, "out")
|
|
|
|
|
2012-10-30 14:59:21 +00:00
|
|
|
self.local = local.Local(self.target_host, self.conf_dirs, self.out_path)
|
2011-10-11 14:19:27 +00:00
|
|
|
|
2011-10-13 14:25:44 +00:00
|
|
|
self.initial_manifest = (initial_manifest or
|
|
|
|
os.path.join(self.local.manifest_path, "init"))
|
|
|
|
|
2012-05-29 08:12:00 +00:00
|
|
|
self._init_remote(remote_copy, remote_exec)
|
2012-05-22 12:55:11 +00:00
|
|
|
|
|
|
|
# Remote stuff
|
|
|
|
def _init_remote(self, remote_copy, remote_exec):
|
|
|
|
|
2011-10-13 14:25:44 +00:00
|
|
|
self.remote_base_path = os.environ.get('__cdist_remote_out_dir', "/var/lib/cdist")
|
2012-05-22 12:55:11 +00:00
|
|
|
self.remote_copy = remote_copy
|
|
|
|
self.remote_exec = remote_exec
|
|
|
|
|
|
|
|
os.environ['__remote_copy'] = self.remote_copy
|
|
|
|
os.environ['__remote_exec'] = self.remote_exec
|
|
|
|
|
2011-10-18 11:32:36 +00:00
|
|
|
self.remote = remote.Remote(self.target_host, self.remote_base_path,
|
2011-10-13 14:25:44 +00:00
|
|
|
self.remote_exec, self.remote_copy)
|
2011-10-11 14:19:27 +00:00
|
|
|
|
2011-10-11 13:59:26 +00:00
|
|
|
def cleanup(self):
|
2011-10-11 14:45:45 +00:00
|
|
|
"""Remove temp stuff"""
|
|
|
|
if self.temp_dir:
|
|
|
|
shutil.rmtree(self.temp_dir)
|
2011-10-11 13:59:26 +00:00
|
|
|
|
|
|
|
def filter(self, record):
|
|
|
|
"""Add hostname to logs via logging Filter"""
|
|
|
|
|
|
|
|
record.msg = self.target_host + ": " + record.msg
|
|
|
|
|
|
|
|
return True
|