2011-10-04 16:45:29 +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
|
|
|
|
import stat
|
2011-10-10 16:50:06 +00:00
|
|
|
import shutil
|
2011-10-04 16:45:29 +00:00
|
|
|
import sys
|
2011-10-10 09:31:37 +00:00
|
|
|
import tempfile
|
2011-10-08 01:24:05 +00:00
|
|
|
import time
|
2011-10-04 16:45:29 +00:00
|
|
|
|
2011-10-13 14:27:41 +00:00
|
|
|
from cdist import core
|
|
|
|
|
2011-10-06 16:26:55 +00:00
|
|
|
|
2011-10-11 14:45:36 +00:00
|
|
|
class ConfigInstall(object):
|
2011-10-06 14:52:56 +00:00
|
|
|
"""Cdist main class to hold arbitrary data"""
|
|
|
|
|
2011-10-11 14:45:36 +00:00
|
|
|
def __init__(self, context):
|
2011-10-06 14:52:56 +00:00
|
|
|
|
2011-10-11 14:45:36 +00:00
|
|
|
self.context = context
|
2011-10-11 15:09:47 +00:00
|
|
|
self.log = logging.getLogger(self.context.target_host)
|
2011-10-10 09:31:37 +00:00
|
|
|
|
2011-10-13 14:27:41 +00:00
|
|
|
# For easy access
|
|
|
|
self.local = context.local
|
|
|
|
self.remote = context.remote
|
2011-10-12 18:29:53 +00:00
|
|
|
|
2011-10-13 14:27:41 +00:00
|
|
|
# Initialise local directory structure
|
|
|
|
self.local.create_directories()
|
|
|
|
# Initialise remote directory structure
|
|
|
|
self.remote.create_directories()
|
2011-10-12 14:59:02 +00:00
|
|
|
|
2011-10-13 14:27:41 +00:00
|
|
|
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)
|
2011-10-10 16:50:06 +00:00
|
|
|
|
2011-10-06 14:52:56 +00:00
|
|
|
def cleanup(self):
|
2011-10-13 14:27:41 +00:00
|
|
|
# FIXME: move to local?
|
2011-10-14 07:47:59 +00:00
|
|
|
destination = os.path.join(self.local.cache_path, self.context.target_host)
|
|
|
|
self.log.debug("Saving " + self.local.out_path + " to " + destination)
|
|
|
|
if os.path.exists(destination):
|
|
|
|
shutil.rmtree(destination)
|
|
|
|
shutil.move(self.local.out_path, destination)
|
2011-10-06 14:52:56 +00:00
|
|
|
|
2011-10-13 09:08:23 +00:00
|
|
|
def deploy_to(self):
|
|
|
|
"""Mimic the old deploy to: Deploy to one 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"""
|
2011-10-13 14:53:48 +00:00
|
|
|
self.local.link_emulator(self.context.exec_path)
|
2011-10-13 14:27:41 +00:00
|
|
|
self.run_global_explorers()
|
|
|
|
self.manifest.run_initial_manifest(self.context.initial_manifest)
|
2011-10-13 09:08:23 +00:00
|
|
|
|
|
|
|
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
|
2011-10-13 14:27:41 +00:00
|
|
|
for cdist_object in core.Object.list_objects(self.local.object_path,
|
|
|
|
self.local.type_path):
|
2011-10-13 09:08:23 +00:00
|
|
|
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
|
|
|
|
|
2011-10-13 14:27:41 +00:00
|
|
|
def run_global_explorers(self):
|
2011-10-13 09:08:23 +00:00
|
|
|
"""Run global explorers and save output"""
|
2011-10-13 14:27:41 +00:00
|
|
|
# FIXME: move to explorer, pass global_explorer_out_path as argument
|
2011-10-14 22:14:25 +00:00
|
|
|
self.log.info("Running global explorers")
|
2011-10-13 14:27:41 +00:00
|
|
|
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)
|
2011-10-13 09:08:23 +00:00
|
|
|
|
2011-10-13 14:53:48 +00:00
|
|
|
def run_type_explorers(self, cdist_object):
|
|
|
|
"""Run type explorers and save output in object."""
|
2011-10-13 19:38:37 +00:00
|
|
|
self.log.debug("Transfering type explorers for type: %s", cdist_object.type)
|
2011-10-13 14:53:48 +00:00
|
|
|
self.explorer.transfer_type_explorers(cdist_object.type)
|
2011-10-13 19:38:37 +00:00
|
|
|
self.log.debug("Transfering object parameters for object: %s", cdist_object.name)
|
2011-10-13 15:38:09 +00:00
|
|
|
self.explorer.transfer_object_parameters(cdist_object)
|
2011-10-13 14:53:48 +00:00
|
|
|
for explorer in self.explorer.list_type_explorer_names(cdist_object.type):
|
|
|
|
output = self.explorer.run_type_explorer(explorer, cdist_object)
|
2011-10-13 19:38:37 +00:00
|
|
|
self.log.debug("Running type explorer '%s' for object '%s'", explorer, cdist_object.name)
|
2011-10-13 14:53:48 +00:00
|
|
|
cdist_object.explorers[explorer] = output
|
|
|
|
|
2011-10-11 11:47:41 +00:00
|
|
|
def object_prepare(self, cdist_object):
|
|
|
|
"""Prepare object: Run type explorer + manifest"""
|
2011-10-11 15:09:47 +00:00
|
|
|
self.log.debug("Preparing object: " + cdist_object.name)
|
2011-10-13 14:53:48 +00:00
|
|
|
self.run_type_explorers(cdist_object)
|
2011-10-11 14:59:43 +00:00
|
|
|
self.manifest.run_type_manifest(cdist_object)
|
2011-10-11 11:47:41 +00:00
|
|
|
cdist_object.prepared = True
|
|
|
|
|
2011-10-07 11:34:22 +00:00
|
|
|
def object_run(self, cdist_object):
|
2011-10-07 16:13:58 +00:00
|
|
|
"""Run gencode and code for an object"""
|
2011-10-11 15:09:47 +00:00
|
|
|
self.log.debug("Running object %s", cdist_object)
|
2011-10-06 17:59:07 +00:00
|
|
|
|
2011-10-07 11:34:22 +00:00
|
|
|
# Catch requirements, which re-call us
|
|
|
|
if cdist_object.ran:
|
|
|
|
return
|
|
|
|
|
2011-10-07 14:55:19 +00:00
|
|
|
cdist_type = cdist_object.type
|
2011-10-06 14:52:56 +00:00
|
|
|
|
2011-10-07 11:34:22 +00:00
|
|
|
for requirement in cdist_object.requirements:
|
2011-10-11 15:09:47 +00:00
|
|
|
self.log.debug("Object %s requires %s", cdist_object, requirement)
|
2011-10-13 14:27:41 +00:00
|
|
|
# FIXME: requirement is a string, need to create object here
|
2011-10-14 07:06:32 +00:00
|
|
|
required_object = cdist_object.object_from_name(requirement)
|
|
|
|
self.object_run(required_object)
|
2011-10-06 14:52:56 +00:00
|
|
|
|
2011-10-13 14:27:41 +00:00
|
|
|
# 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)
|
2011-10-07 10:10:57 +00:00
|
|
|
|
2011-10-11 11:47:41 +00:00
|
|
|
def stage_run(self):
|
|
|
|
"""The final (and real) step of deployment"""
|
2011-10-11 15:09:47 +00:00
|
|
|
self.log.info("Generating and executing code")
|
2011-10-13 14:53:48 +00:00
|
|
|
for cdist_object in core.Object.list_objects(self.local.object_path,
|
2011-10-13 14:27:41 +00:00
|
|
|
self.local.type_path):
|
2011-10-11 15:09:47 +00:00
|
|
|
self.log.debug("Run object: %s", cdist_object)
|
2011-10-11 11:47:41 +00:00
|
|
|
self.object_run(cdist_object)
|