2011-10-04 16:45:29 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2012-10-30 15:06:27 +00:00
|
|
|
# 2010-2012 Nico Schottelius (nico-cdist at schottelius.org)
|
2011-10-04 16:45:29 +00:00
|
|
|
#
|
|
|
|
# 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
|
2012-01-19 06:51:02 +00:00
|
|
|
import itertools
|
|
|
|
import pprint
|
2011-10-04 16:45:29 +00:00
|
|
|
|
2011-10-19 13:49:32 +00:00
|
|
|
import cdist
|
2011-10-13 14:27:41 +00:00
|
|
|
from cdist import core
|
2012-01-19 06:51:02 +00:00
|
|
|
from cdist import resolver
|
2011-10-13 14:27:41 +00:00
|
|
|
|
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-18 11:32: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
|
|
|
# Initialise local directory structure
|
2012-12-19 20:10:18 +00:00
|
|
|
self.context.local.create_files_dirs()
|
2011-10-13 14:27:41 +00:00
|
|
|
# Initialise remote directory structure
|
2012-12-19 20:10:18 +00:00
|
|
|
self.context.remote.create_files_dirs()
|
2011-10-12 14:59:02 +00:00
|
|
|
|
2012-12-19 20:10:18 +00:00
|
|
|
self.explorer = core.Explorer(self.context.target_host, self.context.local, self.context.remote)
|
|
|
|
self.manifest = core.Manifest(self.context.target_host, self.context.local)
|
|
|
|
self.code = core.Code(self.context.target_host, self.context.local, self.context.remote)
|
2011-10-10 16:50:06 +00:00
|
|
|
|
2012-12-19 18:37:43 +00:00
|
|
|
# Add switch to disable code execution
|
|
|
|
self.dry_run = False
|
|
|
|
|
2011-10-06 14:52:56 +00:00
|
|
|
def cleanup(self):
|
2011-10-13 14:27:41 +00:00
|
|
|
# FIXME: move to local?
|
2012-12-19 20:10:18 +00:00
|
|
|
destination = os.path.join(self.context.local.cache_path, self.context.target_host)
|
|
|
|
self.log.debug("Saving " + self.context.local.out_path + " to " + destination)
|
2011-10-14 07:47:59 +00:00
|
|
|
if os.path.exists(destination):
|
|
|
|
shutil.rmtree(destination)
|
2012-12-19 20:10:18 +00:00
|
|
|
shutil.move(self.context.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()
|
2011-11-08 07:28:04 +00:00
|
|
|
self.log.info("Finished successful run in %s seconds",
|
2011-10-14 23:41:11 +00:00
|
|
|
time.time() - start_time)
|
2011-10-13 09:08:23 +00:00
|
|
|
|
|
|
|
def stage_prepare(self):
|
|
|
|
"""Do everything for a deploy, minus the actual code stage"""
|
2012-12-19 20:10:18 +00:00
|
|
|
self.explorer.run_global_explorers(self.context.local.global_explorer_out_path)
|
2011-10-13 14:27:41 +00:00
|
|
|
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
|
2012-12-19 20:10:18 +00:00
|
|
|
for cdist_object in core.CdistObject.list_objects(self.context.local.object_path,
|
|
|
|
self.context.local.type_path):
|
2012-02-11 23:08:18 +00:00
|
|
|
if cdist_object.state == core.CdistObject.STATE_PREPARED:
|
2011-10-16 13:08:00 +00:00
|
|
|
self.log.debug("Skipping re-prepare of object %s", cdist_object)
|
2011-10-13 09:08:23 +00:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
self.object_prepare(cdist_object)
|
|
|
|
new_objects_created = True
|
|
|
|
|
2011-10-11 11:47:41 +00:00
|
|
|
def object_prepare(self, cdist_object):
|
|
|
|
"""Prepare object: Run type explorer + manifest"""
|
2011-10-15 21:29:27 +00:00
|
|
|
self.log.info("Running manifest and explorers for " + cdist_object.name)
|
2011-10-20 09:10:30 +00:00
|
|
|
self.explorer.run_type_explorers(cdist_object)
|
2011-10-11 14:59:43 +00:00
|
|
|
self.manifest.run_type_manifest(cdist_object)
|
2012-02-11 23:08:18 +00:00
|
|
|
cdist_object.state = core.CdistObject.STATE_PREPARED
|
2011-10-11 11:47:41 +00:00
|
|
|
|
2012-12-19 18:36:00 +00:00
|
|
|
def object_run(self, cdist_object, dry_run=False):
|
2011-10-07 16:13:58 +00:00
|
|
|
"""Run gencode and code for an object"""
|
2011-10-19 14:47:52 +00:00
|
|
|
self.log.debug("Trying to run object " + cdist_object.name)
|
2012-02-11 23:08:18 +00:00
|
|
|
if cdist_object.state == core.CdistObject.STATE_DONE:
|
2012-01-19 06:51:02 +00:00
|
|
|
raise cdist.Error("Attempting to run an already finished object: %s", cdist_object)
|
2011-10-07 11:34:22 +00:00
|
|
|
|
2012-02-13 14:01:45 +00:00
|
|
|
cdist_type = cdist_object.cdist_type
|
2011-10-18 11:32:36 +00:00
|
|
|
|
2011-10-13 14:27:41 +00:00
|
|
|
# Generate
|
2011-10-19 14:47:52 +00:00
|
|
|
self.log.info("Generating and executing code for " + cdist_object.name)
|
2011-10-13 14:27:41 +00:00
|
|
|
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
|
2012-12-19 18:36:00 +00:00
|
|
|
if not dry_run:
|
|
|
|
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-19 13:48:22 +00:00
|
|
|
# Mark this object as done
|
2011-10-19 14:29:30 +00:00
|
|
|
self.log.debug("Finishing run of " + cdist_object.name)
|
2012-02-11 23:08:18 +00:00
|
|
|
cdist_object.state = core.CdistObject.STATE_DONE
|
2011-10-19 13:48:22 +00:00
|
|
|
|
2012-12-19 18:36:00 +00:00
|
|
|
def stage_run(self):
|
|
|
|
"""The final (and real) step of deployment"""
|
|
|
|
self.log.info("Generating and executing code")
|
|
|
|
|
2012-12-19 20:10:18 +00:00
|
|
|
# FIXME: think about parallel execution (same for stage_prepare)
|
|
|
|
self.all_resolved = False
|
2012-12-19 18:36:00 +00:00
|
|
|
while not self.all_resolved:
|
|
|
|
self.stage_run_iterate()
|
|
|
|
|
|
|
|
def stage_run_iterate(self):
|
|
|
|
"""
|
|
|
|
Run one iteration of the run
|
|
|
|
|
|
|
|
To be repeated until all objects are done
|
|
|
|
"""
|
2012-12-19 20:10:18 +00:00
|
|
|
objects = list(core.CdistObject.list_objects(self.context.local.object_path, self.context.local.type_path))
|
|
|
|
object_state_list=' '.join('%s:%s:%s:%s' % (o, o.state, o.all_requirements, o.satisfied_requirements) for o in objects)
|
2012-12-19 18:36:00 +00:00
|
|
|
|
2012-12-19 20:10:18 +00:00
|
|
|
self.log.debug("Object state (name:state:requirements:satisfied): %s" % object_state_list)
|
2012-12-19 18:36:00 +00:00
|
|
|
|
2012-12-19 20:10:18 +00:00
|
|
|
objects_changed = False
|
2012-12-19 18:36:00 +00:00
|
|
|
self.all_resolved = True
|
2012-12-19 20:10:18 +00:00
|
|
|
for cdist_object in objects:
|
2012-12-19 18:36:00 +00:00
|
|
|
if not cdist_object.state == cdist_object.STATE_DONE:
|
|
|
|
self.all_resolved = False
|
2012-12-19 20:10:18 +00:00
|
|
|
self.log.debug("Object %s not done" % cdist_object.name)
|
2012-12-19 18:36:00 +00:00
|
|
|
if cdist_object.satisfied_requirements:
|
2012-12-19 20:10:18 +00:00
|
|
|
self.log.debug("Running object %s with satisfied requirements" % cdist_object.name)
|
2012-12-19 18:37:43 +00:00
|
|
|
self.object_run(cdist_object, self.dry_run)
|
2012-12-19 20:10:18 +00:00
|
|
|
objects_changed = True
|
|
|
|
|
|
|
|
self.log.debug("All resolved: %s Objects changed: %s" % (self.all_resolved, objects_changed))
|
2012-12-19 18:36:00 +00:00
|
|
|
|
|
|
|
# Not all are resolved, but nothing has been changed => bad dependencies!
|
2012-12-19 20:10:18 +00:00
|
|
|
if not objects_changed and not self.all_resolved:
|
2012-12-19 18:36:00 +00:00
|
|
|
# Create list of unfinished objects + their requirements for print
|
|
|
|
|
|
|
|
evil_objects = []
|
|
|
|
good_objects = []
|
2012-12-19 20:10:18 +00:00
|
|
|
for cdist_object in objects:
|
2012-12-13 05:41:33 +00:00
|
|
|
if not cdist_object.state == cdist_object.STATE_DONE:
|
2012-12-19 18:36:00 +00:00
|
|
|
evil_objects.append("%s: required: %s, autorequired: %s" %
|
|
|
|
(cdist_object.name, cdist_object.requirements, cdist_object.autorequire))
|
|
|
|
else:
|
|
|
|
evil_objects.append("%s (%s): required: %s, autorequired: %s" %
|
|
|
|
(cdist_object.state, cdist_object.name,
|
|
|
|
cdist_object.requirements, cdist_object.autorequire))
|
|
|
|
|
|
|
|
errormessage = "Cannot solve requirements for the following objects: %s - solved: %s" % (",".join(evil_objects), ",".join(good_objects))
|
|
|
|
raise cdist.Error(errormessage)
|