2011-10-12 23:25:31 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
|
|
|
# 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
|
|
|
|
|
|
|
|
import cdist
|
|
|
|
|
|
|
|
'''
|
|
|
|
common:
|
2011-10-13 09:06:03 +00:00
|
|
|
runs only remotely, needs local and remote to construct paths
|
2011-10-12 23:25:31 +00:00
|
|
|
|
|
|
|
env:
|
2011-10-13 09:06:03 +00:00
|
|
|
__explorer: full qualified path to other global explorers on remote side
|
|
|
|
-> remote.global_explorer_path
|
2011-10-12 23:25:31 +00:00
|
|
|
|
2011-10-13 09:06:03 +00:00
|
|
|
a global explorer is:
|
|
|
|
- a script
|
|
|
|
- executed on the remote side
|
|
|
|
- returns its output as a string
|
2011-10-12 23:25:31 +00:00
|
|
|
|
|
|
|
env:
|
|
|
|
|
2011-10-13 09:06:03 +00:00
|
|
|
creates: nothing, returns output
|
2011-10-12 23:25:31 +00:00
|
|
|
|
|
|
|
type explorer is:
|
2011-10-13 09:06:03 +00:00
|
|
|
- a script
|
|
|
|
- executed on the remote side for each object instance
|
|
|
|
- returns its output as a string
|
2011-10-12 23:25:31 +00:00
|
|
|
|
|
|
|
env:
|
|
|
|
__object: full qualified path to the object's remote dir
|
|
|
|
__object_id: the objects id
|
|
|
|
__object_fq: full qualified object id, iow: $type.name + / + object_id
|
|
|
|
__type_explorer: full qualified path to the other type explorers on remote side
|
|
|
|
|
2011-10-13 09:06:03 +00:00
|
|
|
creates: nothing, returns output
|
|
|
|
|
2011-10-12 23:25:31 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
class Explorer(object):
|
|
|
|
"""Executes cdist explorers.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def __init__(self, target_host, local, remote):
|
|
|
|
self.target_host = target_host
|
2011-10-14 23:41:11 +00:00
|
|
|
|
|
|
|
self.log = logging.getLogger(target_host)
|
|
|
|
|
2011-10-12 23:25:31 +00:00
|
|
|
self.local = local
|
|
|
|
self.remote = remote
|
|
|
|
self.env = {
|
|
|
|
'__target_host': self.target_host,
|
|
|
|
'__explorer': self.remote.global_explorer_path,
|
|
|
|
}
|
2011-10-14 22:44:54 +00:00
|
|
|
self._type_explorers_transferred = []
|
2011-10-12 23:25:31 +00:00
|
|
|
|
2011-10-13 11:04:23 +00:00
|
|
|
### global
|
|
|
|
|
2011-10-13 11:31:42 +00:00
|
|
|
def list_global_explorer_names(self):
|
|
|
|
"""Return a list of global explorer names."""
|
|
|
|
return os.listdir(self.local.global_explorer_path)
|
|
|
|
|
2011-10-20 09:05:50 +00:00
|
|
|
def run_global_explorers(self, out_path):
|
|
|
|
"""Run global explorers and save output to files in the given
|
|
|
|
out_path directory.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.log.info("Running global explorers")
|
|
|
|
self.transfer_global_explorers()
|
|
|
|
for explorer in self.list_global_explorer_names():
|
|
|
|
output = self.run_global_explorer(explorer)
|
|
|
|
path = os.path.join(out_path, explorer)
|
|
|
|
with open(path, 'w') as fd:
|
|
|
|
fd.write(output)
|
|
|
|
|
2011-10-12 23:25:31 +00:00
|
|
|
def transfer_global_explorers(self):
|
|
|
|
"""Transfer the global explorers to the remote side."""
|
|
|
|
self.remote.mkdir(self.remote.global_explorer_path)
|
|
|
|
self.remote.transfer(self.local.global_explorer_path, self.remote.global_explorer_path)
|
|
|
|
|
|
|
|
def run_global_explorer(self, explorer):
|
|
|
|
"""Run the given global explorer and return it's output."""
|
|
|
|
script = os.path.join(self.remote.global_explorer_path, explorer)
|
2011-10-14 14:07:50 +00:00
|
|
|
return self.remote.run_script(script, env=self.env, return_output=True)
|
2011-10-12 23:25:31 +00:00
|
|
|
|
2011-10-13 11:04:23 +00:00
|
|
|
### type
|
|
|
|
|
2011-10-13 11:34:06 +00:00
|
|
|
def list_type_explorer_names(self, cdist_type):
|
|
|
|
"""Return a list of explorer names for the given type."""
|
|
|
|
source = os.path.join(self.local.type_path, cdist_type.explorer_path)
|
2011-10-13 15:05:34 +00:00
|
|
|
try:
|
|
|
|
return os.listdir(source)
|
|
|
|
except EnvironmentError:
|
|
|
|
return []
|
2011-10-13 11:34:06 +00:00
|
|
|
|
2011-10-20 09:05:50 +00:00
|
|
|
def run_type_explorers(self, cdist_object):
|
|
|
|
"""Run the type explorers for the given object and save their output
|
|
|
|
in the object.
|
|
|
|
|
|
|
|
"""
|
2012-02-13 07:54:09 +00:00
|
|
|
self.log.debug("Transfering type explorers for type: %s", cdist_object.cdist_type)
|
|
|
|
self.transfer_type_explorers(cdist_object.cdist_type)
|
2011-10-20 09:05:50 +00:00
|
|
|
self.log.debug("Transfering object parameters for object: %s", cdist_object.name)
|
|
|
|
self.transfer_object_parameters(cdist_object)
|
2012-02-13 07:54:09 +00:00
|
|
|
for explorer in self.list_type_explorer_names(cdist_object.cdist_type):
|
2011-10-20 09:05:50 +00:00
|
|
|
output = self.run_type_explorer(explorer, cdist_object)
|
|
|
|
self.log.debug("Running type explorer '%s' for object '%s'", explorer, cdist_object.name)
|
|
|
|
cdist_object.explorers[explorer] = output
|
|
|
|
|
2012-02-09 18:08:32 +00:00
|
|
|
def run_type_explorer(self, explorer, cdist_object):
|
|
|
|
"""Run the given type explorer for the given object and return it's output."""
|
2012-02-13 07:54:09 +00:00
|
|
|
cdist_type = cdist_object.cdist_type
|
2012-02-09 18:08:32 +00:00
|
|
|
env = self.env.copy()
|
|
|
|
env.update({
|
|
|
|
'__object': os.path.join(self.remote.object_path, cdist_object.path),
|
|
|
|
'__object_id': cdist_object.object_id,
|
2012-09-07 12:02:43 +00:00
|
|
|
'__object_name': cdist_object.name,
|
2012-02-09 18:08:32 +00:00
|
|
|
'__object_fq': cdist_object.path,
|
|
|
|
'__type_explorer': os.path.join(self.remote.type_path, cdist_type.explorer_path)
|
|
|
|
})
|
|
|
|
script = os.path.join(self.remote.type_path, cdist_type.explorer_path, explorer)
|
|
|
|
return self.remote.run_script(script, env=env, return_output=True)
|
|
|
|
|
2011-10-12 23:25:31 +00:00
|
|
|
def transfer_type_explorers(self, cdist_type):
|
|
|
|
"""Transfer the type explorers for the given type to the remote side."""
|
2011-10-13 15:05:34 +00:00
|
|
|
if cdist_type.explorers:
|
2011-10-14 22:44:54 +00:00
|
|
|
if cdist_type.name in self._type_explorers_transferred:
|
2011-10-15 10:00:46 +00:00
|
|
|
self.log.debug("Skipping retransfer of type explorers for: %s", cdist_type)
|
2011-10-14 21:47:05 +00:00
|
|
|
else:
|
|
|
|
source = os.path.join(self.local.type_path, cdist_type.explorer_path)
|
|
|
|
destination = os.path.join(self.remote.type_path, cdist_type.explorer_path)
|
|
|
|
self.remote.mkdir(destination)
|
|
|
|
self.remote.transfer(source, destination)
|
2011-10-14 22:44:54 +00:00
|
|
|
self._type_explorers_transferred.append(cdist_type.name)
|
2011-10-12 23:25:31 +00:00
|
|
|
|
|
|
|
def transfer_object_parameters(self, cdist_object):
|
2011-10-13 11:04:23 +00:00
|
|
|
"""Transfer the parameters for the given object to the remote side."""
|
2011-10-20 09:07:48 +00:00
|
|
|
if cdist_object.parameters:
|
|
|
|
source = os.path.join(self.local.object_path, cdist_object.parameter_path)
|
|
|
|
destination = os.path.join(self.remote.object_path, cdist_object.parameter_path)
|
|
|
|
self.remote.mkdir(destination)
|
|
|
|
self.remote.transfer(source, destination)
|