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-19 13:49:32 +00:00
|
|
|
import cdist
|
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-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
|
|
|
# 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()
|
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"""
|
2011-10-13 14:53:48 +00:00
|
|
|
self.local.link_emulator(self.context.exec_path)
|
2011-10-20 09:10:30 +00:00
|
|
|
self.explorer.run_global_explorers(self.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
|
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-19 13:39:38 +00:00
|
|
|
if cdist_object.state == core.Object.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)
|
2011-10-19 13:39:38 +00:00
|
|
|
cdist_object.state = core.Object.STATE_PREPARED
|
2011-10-11 11:47:41 +00:00
|
|
|
|
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-19 14:47:52 +00:00
|
|
|
self.log.debug("Trying to run object " + cdist_object.name)
|
2011-10-19 13:48:22 +00:00
|
|
|
if cdist_object.state == core.Object.STATE_RUNNING:
|
2011-11-09 17:30:12 +00:00
|
|
|
# FIXME: resolve dependency circle / show problem source
|
2011-10-19 13:48:22 +00:00
|
|
|
raise cdist.Error("Detected circular dependency in " + cdist_object.name)
|
|
|
|
elif cdist_object.state == core.Object.STATE_DONE:
|
|
|
|
self.log.debug("Ignoring run of already finished object %s", cdist_object)
|
2011-10-07 11:34:22 +00:00
|
|
|
return
|
2011-10-16 13:08:00 +00:00
|
|
|
else:
|
2011-10-19 13:48:22 +00:00
|
|
|
cdist_object.state = core.Object.STATE_RUNNING
|
2011-10-07 11:34:22 +00:00
|
|
|
|
2011-10-07 14:55:19 +00:00
|
|
|
cdist_type = cdist_object.type
|
2011-10-18 11:32:36 +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-14 07:06:32 +00:00
|
|
|
required_object = cdist_object.object_from_name(requirement)
|
2011-11-09 17:30:12 +00:00
|
|
|
|
|
|
|
# The user may have created dependencies without satisfying them
|
2011-11-09 19:28:51 +00:00
|
|
|
if not required_object.exists:
|
2011-11-09 19:29:36 +00:00
|
|
|
raise cdist.Error(cdist_object.name + " requires non-existing " + required_object.name)
|
2011-11-09 19:28:04 +00:00
|
|
|
else:
|
2011-11-14 14:29:07 +00:00
|
|
|
self.log.debug("Required object %s exists", required_object.name)
|
2011-11-09 17:30:12 +00:00
|
|
|
|
2011-10-19 14:15:52 +00:00
|
|
|
self.object_run(required_object)
|
2011-10-06 14:52:56 +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
|
|
|
|
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)
|
|
|
|
cdist_object.state = core.Object.STATE_DONE
|
2011-10-19 13:48:22 +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)
|