cdist-backup/lib/cdist/config_install.py
Steven Armstrong 6271e27eb5 transfer object parameter before running type explorers
Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
2011-10-13 17:38:09 +02:00

156 lines
6 KiB
Python

#!/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
import stat
import shutil
import sys
import tempfile
import time
from cdist import core
class ConfigInstall(object):
"""Cdist main class to hold arbitrary data"""
def __init__(self, context):
self.context = context
self.log = logging.getLogger(self.context.target_host)
# For easy access
self.local = context.local
self.remote = context.remote
# Initialise local directory structure
self.local.create_directories()
# Initialise remote directory structure
self.remote.create_directories()
self.explorer = core.Explorer(self.context.target_host, self.local, self.remote)
self.manifest = core.Manifest(self.context.target_host, self.local)
self.code = core.Code(self.context.target_host, self.local, self.remote)
def cleanup(self):
# FIXME: move to local?
self.log.debug("Saving " + self.local.out_path + " to " + self.local.cache_path)
if os.path.exists(self.local.cache_path):
shutil.rmtree(self.local.cache_path)
shutil.move(self.local.out_path, self.local.cache_path)
def deploy_to(self):
"""Mimic the old deploy to: Deploy to one host"""
self.log.info("Deploying to " + self.context.target_host)
self.stage_prepare()
self.stage_run()
def deploy_and_cleanup(self):
"""Do what is most often done: deploy & cleanup"""
start_time = time.time()
self.deploy_to()
self.cleanup()
self.log.info("Finished run of %s in %s seconds",
self.context.target_host, time.time() - start_time)
def stage_prepare(self):
"""Do everything for a deploy, minus the actual code stage"""
self.local.link_emulator(self.context.exec_path)
self.run_global_explorers()
self.manifest.run_initial_manifest(self.context.initial_manifest)
self.log.info("Running object manifests and type explorers")
# Continue process until no new objects are created anymore
new_objects_created = True
while new_objects_created:
new_objects_created = False
for cdist_object in core.Object.list_objects(self.local.object_path,
self.local.type_path):
if cdist_object.prepared:
self.log.debug("Skipping rerun of object %s", cdist_object)
continue
else:
self.object_prepare(cdist_object)
new_objects_created = True
def run_global_explorers(self):
"""Run global explorers and save output"""
# FIXME: move to explorer, pass global_explorer_out_path as argument
self.explorer.transfer_global_explorers()
for explorer in self.explorer.list_global_explorer_names():
output = self.explorer.run_global_explorer(explorer)
path = os.path.join(self.local.global_explorer_out_path, explorer)
with open(path, 'w') as fd:
fd.write(output)
def run_type_explorers(self, cdist_object):
"""Run type explorers and save output in object."""
self.explorer.transfer_type_explorers(cdist_object.type)
self.explorer.transfer_object_parameters(cdist_object)
for explorer in self.explorer.list_type_explorer_names(cdist_object.type):
output = self.explorer.run_type_explorer(explorer, cdist_object)
cdist_object.explorers[explorer] = output
def object_prepare(self, cdist_object):
"""Prepare object: Run type explorer + manifest"""
self.log.debug("Preparing object: " + cdist_object.name)
self.run_type_explorers(cdist_object)
self.manifest.run_type_manifest(cdist_object)
cdist_object.prepared = True
def object_run(self, cdist_object):
"""Run gencode and code for an object"""
self.log.debug("Running object %s", cdist_object)
# Catch requirements, which re-call us
if cdist_object.ran:
return
cdist_type = cdist_object.type
for requirement in cdist_object.requirements:
self.log.debug("Object %s requires %s", cdist_object, requirement)
# FIXME: requirement is a string, need to create object here
self.object_run(requirement)
# Generate
cdist_object.code_local = self.code.run_gencode_local(cdist_object)
cdist_object.code_remote = self.code.run_gencode_remote(cdist_object)
if cdist_object.code_local or cdist_object.code_remote:
cdist_object.changed = True
# Execute
if cdist_object.code_local:
self.code.run_code_local(cdist_object)
if cdist_object.code_remote:
self.code.transfer_code_remote(cdist_object)
self.code.run_code_remote(cdist_object)
def stage_run(self):
"""The final (and real) step of deployment"""
self.log.info("Generating and executing code")
for cdist_object in core.Object.list_objects(self.local.object_path,
self.local.type_path):
self.log.debug("Run object: %s", cdist_object)
self.object_run(cdist_object)