2011-10-04 16:45:29 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2013-04-30 08:58:23 +00:00
|
|
|
# 2010-2013 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
|
2011-10-10 16:50:06 +00:00
|
|
|
import shutil
|
2013-08-07 07:24:10 +00:00
|
|
|
import sys
|
2011-10-08 01:24:05 +00:00
|
|
|
import time
|
2012-01-19 06:51:02 +00:00
|
|
|
import pprint
|
2011-10-04 16:45:29 +00:00
|
|
|
|
2011-10-19 13:49:32 +00:00
|
|
|
import cdist
|
2013-08-18 22:33:58 +00:00
|
|
|
|
|
|
|
import cdist.exec.local
|
|
|
|
import cdist.exec.remote
|
|
|
|
|
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"""
|
|
|
|
|
2013-08-18 22:33:58 +00:00
|
|
|
def __init__(self, local, remote, dry_run=False):
|
2011-10-06 14:52:56 +00:00
|
|
|
|
2013-08-18 22:33:58 +00:00
|
|
|
self.local = local
|
|
|
|
self.remote = remote
|
2013-08-18 22:52:15 +00:00
|
|
|
self.log = logging.getLogger(self.local.target_host)
|
2013-08-18 22:33:58 +00:00
|
|
|
self.dry_run = dry_run
|
2011-10-10 09:31:37 +00:00
|
|
|
|
2013-08-18 23:38:28 +00:00
|
|
|
self.explorer = core.Explorer(self.local.target_host, self.local, self.remote)
|
|
|
|
self.manifest = core.Manifest(self.local.target_host, self.local)
|
|
|
|
self.code = core.Code(self.local.target_host, self.local, self.remote)
|
2012-12-19 18:37:43 +00:00
|
|
|
|
2013-06-21 14:35:19 +00:00
|
|
|
def _init_files_dirs(self):
|
|
|
|
"""Prepare files and directories for the run"""
|
2013-08-18 22:33:58 +00:00
|
|
|
self.local.create_files_dirs()
|
|
|
|
self.remote.create_files_dirs()
|
2013-06-21 14:35:19 +00:00
|
|
|
|
2013-08-07 07:24:10 +00:00
|
|
|
@classmethod
|
|
|
|
def commandline(cls, args):
|
|
|
|
"""Configure or install remote system"""
|
|
|
|
import multiprocessing
|
|
|
|
|
|
|
|
# FIXME: Refactor relict - remove later
|
|
|
|
log = logging.getLogger("cdist")
|
|
|
|
|
|
|
|
initial_manifest_tempfile = None
|
|
|
|
if args.manifest == '-':
|
|
|
|
# read initial manifest from stdin
|
|
|
|
import tempfile
|
|
|
|
try:
|
|
|
|
handle, initial_manifest_temp_path = tempfile.mkstemp(prefix='cdist.stdin.')
|
|
|
|
with os.fdopen(handle, 'w') as fd:
|
|
|
|
fd.write(sys.stdin.read())
|
|
|
|
except (IOError, OSError) as e:
|
|
|
|
raise cdist.Error("Creating tempfile for stdin data failed: %s" % e)
|
|
|
|
|
|
|
|
args.manifest = initial_manifest_temp_path
|
|
|
|
import atexit
|
|
|
|
atexit.register(lambda: os.remove(initial_manifest_temp_path))
|
|
|
|
|
|
|
|
process = {}
|
|
|
|
failed_hosts = []
|
|
|
|
time_start = time.time()
|
|
|
|
|
|
|
|
for host in args.host:
|
|
|
|
if args.parallel:
|
|
|
|
log.debug("Creating child process for %s", host)
|
|
|
|
process[host] = multiprocessing.Process(target=cls.onehost, args=(host, args, True))
|
|
|
|
process[host].start()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
cls.onehost(host, args, parallel=False)
|
|
|
|
except cdist.Error as e:
|
|
|
|
failed_hosts.append(host)
|
|
|
|
|
|
|
|
# Catch errors in parallel mode when joining
|
|
|
|
if args.parallel:
|
|
|
|
for host in process.keys():
|
|
|
|
log.debug("Joining process %s", host)
|
|
|
|
process[host].join()
|
|
|
|
|
|
|
|
if not process[host].exitcode == 0:
|
|
|
|
failed_hosts.append(host)
|
|
|
|
|
|
|
|
time_end = time.time()
|
|
|
|
log.info("Total processing time for %s host(s): %s", len(args.host),
|
|
|
|
(time_end - time_start))
|
|
|
|
|
|
|
|
if len(failed_hosts) > 0:
|
|
|
|
raise cdist.Error("Failed to configure the following hosts: " +
|
|
|
|
" ".join(failed_hosts))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def onehost(cls, host, args, parallel):
|
|
|
|
"""Configure or install ONE system"""
|
2013-08-18 23:38:28 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(host)
|
2013-08-07 07:24:10 +00:00
|
|
|
|
|
|
|
try:
|
2013-08-18 23:38:28 +00:00
|
|
|
local = cdist.exec.local.Local(
|
2013-08-07 07:24:10 +00:00
|
|
|
target_host=host,
|
|
|
|
exec_path=sys.argv[0],
|
2013-08-18 22:52:15 +00:00
|
|
|
initial_manifest=args.manifest,
|
2013-08-18 23:38:28 +00:00
|
|
|
out_path=args.out_path,
|
2013-08-18 22:52:15 +00:00
|
|
|
add_conf_dirs=args.conf_dir)
|
2013-08-18 22:33:58 +00:00
|
|
|
|
2013-08-18 23:38:28 +00:00
|
|
|
remote = cdist.exec.remote.Remote(
|
2013-08-18 22:33:58 +00:00
|
|
|
target_host=host,
|
|
|
|
remote_exec=args.remote_exec,
|
|
|
|
remote_copy=args.remote_copy)
|
2013-08-07 07:24:10 +00:00
|
|
|
|
2013-08-18 22:33:58 +00:00
|
|
|
c = cls(local, remote)
|
2013-08-07 07:24:10 +00:00
|
|
|
c.run()
|
|
|
|
|
|
|
|
except cdist.Error as e:
|
2013-08-18 23:38:28 +00:00
|
|
|
log.error(e)
|
2013-08-07 07:24:10 +00:00
|
|
|
if parallel:
|
|
|
|
# We are running in our own process here, need to sys.exit!
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
# Ignore in parallel mode, we are existing anyway
|
|
|
|
if parallel:
|
|
|
|
sys.exit(0)
|
|
|
|
# Pass back to controlling code in sequential mode
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2013-05-15 07:19:36 +00:00
|
|
|
def run(self):
|
2013-04-30 13:15:22 +00:00
|
|
|
"""Do what is most often done: deploy & cleanup"""
|
|
|
|
start_time = time.time()
|
2013-04-30 08:58:23 +00:00
|
|
|
|
2013-06-21 14:35:19 +00:00
|
|
|
self._init_files_dirs()
|
|
|
|
|
2013-08-18 22:52:15 +00:00
|
|
|
self.explorer.run_global_explorers(self.local.global_explorer_out_path)
|
|
|
|
self.manifest.run_initial_manifest(self.local.initial_manifest)
|
2013-04-30 08:58:23 +00:00
|
|
|
self.iterate_until_finished()
|
|
|
|
|
2013-08-18 22:33:58 +00:00
|
|
|
self.local.save_cache()
|
2013-05-15 07:19:36 +00:00
|
|
|
self.log.info("Finished successful run in %s seconds", time.time() - start_time)
|
|
|
|
|
|
|
|
|
2013-04-30 08:58:23 +00:00
|
|
|
def object_list(self):
|
|
|
|
"""Short name for object list retrieval"""
|
2013-08-18 22:52:15 +00:00
|
|
|
for cdist_object in core.CdistObject.list_objects(self.local.object_path,
|
|
|
|
self.local.type_path):
|
2013-04-30 08:58:23 +00:00
|
|
|
yield cdist_object
|
|
|
|
|
2013-06-20 09:24:47 +00:00
|
|
|
def iterate_once(self):
|
|
|
|
"""
|
|
|
|
Iterate over the objects once - helper method for
|
|
|
|
iterate_until_finished
|
|
|
|
"""
|
|
|
|
objects_changed = False
|
2013-04-30 08:58:23 +00:00
|
|
|
|
2013-06-20 09:24:47 +00:00
|
|
|
for cdist_object in self.object_list():
|
|
|
|
if cdist_object.requirements_unfinished(cdist_object.requirements):
|
|
|
|
"""We cannot do anything for this poor object"""
|
|
|
|
continue
|
|
|
|
|
|
|
|
if cdist_object.state == core.CdistObject.STATE_UNDEF:
|
|
|
|
"""Prepare the virgin object"""
|
|
|
|
|
|
|
|
self.object_prepare(cdist_object)
|
|
|
|
objects_changed = True
|
|
|
|
|
|
|
|
if cdist_object.requirements_unfinished(cdist_object.autorequire):
|
|
|
|
"""The previous step created objects we depend on - wait for them"""
|
|
|
|
continue
|
|
|
|
|
|
|
|
if cdist_object.state == core.CdistObject.STATE_PREPARED:
|
|
|
|
self.object_run(cdist_object)
|
|
|
|
objects_changed = True
|
|
|
|
|
|
|
|
return objects_changed
|
2013-04-30 08:58:23 +00:00
|
|
|
|
|
|
|
|
2013-06-20 09:24:47 +00:00
|
|
|
def iterate_until_finished(self):
|
|
|
|
"""
|
|
|
|
Go through all objects and solve them
|
|
|
|
one after another
|
|
|
|
"""
|
2013-04-30 08:58:23 +00:00
|
|
|
|
2013-06-20 09:24:47 +00:00
|
|
|
objects_changed = True
|
2013-04-30 08:58:23 +00:00
|
|
|
|
2013-06-20 09:24:47 +00:00
|
|
|
while objects_changed:
|
|
|
|
objects_changed = self.iterate_once()
|
2013-04-30 08:58:23 +00:00
|
|
|
|
|
|
|
# Check whether all objects have been finished
|
2013-04-30 13:07:27 +00:00
|
|
|
unfinished_objects = []
|
2013-04-30 08:58:23 +00:00
|
|
|
for cdist_object in self.object_list():
|
|
|
|
if not cdist_object.state == cdist_object.STATE_DONE:
|
2013-04-30 13:07:27 +00:00
|
|
|
unfinished_objects.append(cdist_object)
|
|
|
|
|
|
|
|
if unfinished_objects:
|
|
|
|
info_string = []
|
|
|
|
|
|
|
|
for cdist_object in unfinished_objects:
|
|
|
|
|
|
|
|
requirement_names = []
|
|
|
|
autorequire_names = []
|
|
|
|
|
|
|
|
for requirement in cdist_object.requirements_unfinished(cdist_object.requirements):
|
|
|
|
requirement_names.append(requirement.name)
|
|
|
|
|
|
|
|
for requirement in cdist_object.requirements_unfinished(cdist_object.autorequire):
|
|
|
|
autorequire_names.append(requirement.name)
|
|
|
|
|
|
|
|
requirements = ", ".join(requirement_names)
|
|
|
|
autorequire = ", ".join(autorequire_names)
|
|
|
|
info_string.append("%s requires: %s autorequires: %s" % (cdist_object.name, requirements, autorequire))
|
2013-04-30 08:58:23 +00:00
|
|
|
|
2013-06-21 20:39:20 +00:00
|
|
|
raise cdist.UnresolvableRequirementsError("The requirements of the following objects could not be resolved: %s" %
|
2013-04-30 13:07:27 +00:00
|
|
|
("; ".join(info_string)))
|
2013-04-30 08:58:23 +00:00
|
|
|
|
2013-05-15 08:08:23 +00:00
|
|
|
def object_prepare(self, cdist_object):
|
|
|
|
"""Prepare object: Run type explorer + manifest"""
|
|
|
|
self.log.info("Running manifest and explorers for " + cdist_object.name)
|
|
|
|
self.explorer.run_type_explorers(cdist_object)
|
|
|
|
self.manifest.run_type_manifest(cdist_object)
|
|
|
|
cdist_object.state = core.CdistObject.STATE_PREPARED
|
|
|
|
|
2013-07-05 13:27:34 +00:00
|
|
|
def object_run(self, cdist_object):
|
2013-04-30 13:15:22 +00:00
|
|
|
"""Run gencode and code for an object"""
|
2013-07-05 13:00:23 +00:00
|
|
|
|
2013-07-05 13:02:36 +00:00
|
|
|
self.log.debug("Trying to run object %s" % (cdist_object.name))
|
2013-04-30 13:15:22 +00:00
|
|
|
if cdist_object.state == core.CdistObject.STATE_DONE:
|
|
|
|
raise cdist.Error("Attempting to run an already finished object: %s", cdist_object)
|
|
|
|
|
|
|
|
cdist_type = cdist_object.cdist_type
|
|
|
|
|
|
|
|
# Generate
|
2013-07-05 13:12:49 +00:00
|
|
|
self.log.info("Generating and executing code for %s" % (cdist_object.name))
|
2013-04-30 13:15:22 +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
|
2013-07-05 13:12:49 +00:00
|
|
|
if not self.dry_run:
|
2013-04-30 13:15:22 +00:00
|
|
|
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)
|
2013-07-05 13:12:49 +00:00
|
|
|
else:
|
|
|
|
self.log.info("Skipping code execution due to DRY RUN")
|
|
|
|
|
2013-04-30 13:15:22 +00:00
|
|
|
|
|
|
|
# Mark this object as done
|
|
|
|
self.log.debug("Finishing run of " + cdist_object.name)
|
|
|
|
cdist_object.state = core.CdistObject.STATE_DONE
|