2011-10-06 10:57:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2018-01-09 08:31:40 +00:00
|
|
|
# 2011-2017 Steven Armstrong (steven-cdist at armstrong.cc)
|
2015-03-05 14:02:26 +00:00
|
|
|
# 2011-2015 Nico Schottelius (nico-cdist at schottelius.org)
|
2014-01-27 15:19:01 +00:00
|
|
|
# 2014 Daniel Heule (hda at sfs.biz)
|
2011-10-06 10:57:38 +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/>.
|
|
|
|
#
|
|
|
|
#
|
2011-10-05 23:04:09 +00:00
|
|
|
|
2011-10-06 10:57:38 +00:00
|
|
|
import os
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-06 10:57:38 +00:00
|
|
|
import cdist
|
2011-10-10 14:39:48 +00:00
|
|
|
import cdist.core
|
|
|
|
from cdist.util import fsproperty
|
2011-10-06 15:15:45 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2011-10-14 09:25:39 +00:00
|
|
|
class IllegalObjectIdError(cdist.Error):
|
2011-10-14 12:04:56 +00:00
|
|
|
def __init__(self, object_id, message=None):
|
2011-10-14 09:25:39 +00:00
|
|
|
self.object_id = object_id
|
2011-10-14 09:39:03 +00:00
|
|
|
self.message = message or 'Illegal object id'
|
2011-10-14 09:25:39 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-03-30 05:56:38 +00:00
|
|
|
return '{}: {}'.format(self.message, self.object_id)
|
2011-10-14 09:25:39 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2013-02-05 21:50:22 +00:00
|
|
|
class MissingObjectIdError(cdist.Error):
|
|
|
|
def __init__(self, type_name):
|
|
|
|
self.type_name = type_name
|
2021-03-30 05:56:38 +00:00
|
|
|
self.message = ("Type {} requires object id (is not a "
|
|
|
|
"singleton type)").format(self.type_name)
|
2013-02-05 21:50:22 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-03-30 05:56:38 +00:00
|
|
|
return '{}'.format(self.message)
|
2013-02-05 21:50:22 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2020-06-20 19:16:23 +00:00
|
|
|
class CdistObject:
|
2011-10-06 10:57:38 +00:00
|
|
|
"""Represents a cdist object.
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-06 10:57:38 +00:00
|
|
|
All interaction with objects in cdist should be done through this class.
|
2011-10-18 11:32:36 +00:00
|
|
|
Directly accessing an object through the file system from python code is
|
2011-10-06 10:57:38 +00:00
|
|
|
a bug.
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-06 10:57:38 +00:00
|
|
|
"""
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-19 12:53:31 +00:00
|
|
|
# Constants for use with Object.state
|
2013-04-30 08:58:23 +00:00
|
|
|
STATE_UNDEF = ""
|
2011-10-19 12:53:31 +00:00
|
|
|
STATE_PREPARED = "prepared"
|
|
|
|
STATE_RUNNING = "running"
|
|
|
|
STATE_DONE = "done"
|
|
|
|
|
2015-03-05 14:02:26 +00:00
|
|
|
def __init__(self, cdist_type, base_path, object_marker, object_id):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.cdist_type = cdist_type # instance of Type
|
2012-12-18 16:16:26 +00:00
|
|
|
self.base_path = base_path
|
|
|
|
self.object_id = object_id
|
|
|
|
|
2014-05-05 22:28:18 +00:00
|
|
|
self.object_marker = object_marker
|
|
|
|
|
2012-12-18 16:16:26 +00:00
|
|
|
self.validate_object_id()
|
|
|
|
self.sanitise_object_id()
|
|
|
|
|
|
|
|
self.name = self.join_name(self.cdist_type.name, self.object_id)
|
2016-07-05 18:44:24 +00:00
|
|
|
self.path = os.path.join(self.cdist_type.path, self.object_id,
|
|
|
|
self.object_marker)
|
2015-03-05 14:02:26 +00:00
|
|
|
|
2012-12-18 16:16:26 +00:00
|
|
|
self.absolute_path = os.path.join(self.base_path, self.path)
|
|
|
|
self.code_local_path = os.path.join(self.path, "code-local")
|
|
|
|
self.code_remote_path = os.path.join(self.path, "code-remote")
|
|
|
|
self.parameter_path = os.path.join(self.path, "parameter")
|
2018-01-09 08:31:40 +00:00
|
|
|
self.stdout_path = os.path.join(self.absolute_path, "stdout")
|
|
|
|
self.stderr_path = os.path.join(self.absolute_path, "stderr")
|
2012-12-18 16:16:26 +00:00
|
|
|
|
2011-10-05 22:58:43 +00:00
|
|
|
@classmethod
|
2015-03-05 14:02:26 +00:00
|
|
|
def list_objects(cls, object_base_path, type_base_path, object_marker):
|
2011-10-05 22:58:43 +00:00
|
|
|
"""Return a list of object instances"""
|
2016-07-05 18:44:24 +00:00
|
|
|
for object_name in cls.list_object_names(
|
|
|
|
object_base_path, object_marker):
|
2011-11-02 06:52:08 +00:00
|
|
|
type_name, object_id = cls.split_name(object_name)
|
2015-03-05 14:02:26 +00:00
|
|
|
yield cls(cdist.core.CdistType(type_base_path, type_name),
|
2016-07-05 18:44:24 +00:00
|
|
|
base_path=object_base_path,
|
|
|
|
object_marker=object_marker,
|
|
|
|
object_id=object_id)
|
2011-10-05 22:58:43 +00:00
|
|
|
|
|
|
|
@classmethod
|
2015-03-05 14:02:26 +00:00
|
|
|
def list_object_names(cls, object_base_path, object_marker):
|
2011-10-05 22:58:43 +00:00
|
|
|
"""Return a list of object names"""
|
2011-10-07 13:42:04 +00:00
|
|
|
for path, dirs, files in os.walk(object_base_path):
|
2015-03-05 14:02:26 +00:00
|
|
|
if object_marker in dirs:
|
2011-10-07 13:42:04 +00:00
|
|
|
yield os.path.relpath(path, object_base_path)
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2015-03-05 14:02:26 +00:00
|
|
|
@classmethod
|
|
|
|
def list_type_names(cls, object_base_path):
|
|
|
|
"""Return a list of type names"""
|
2017-07-06 11:30:57 +00:00
|
|
|
return cdist.core.listdir(object_base_path)
|
2015-03-05 14:02:26 +00:00
|
|
|
|
2011-11-02 06:52:08 +00:00
|
|
|
@staticmethod
|
|
|
|
def split_name(object_name):
|
2019-02-15 19:34:45 +00:00
|
|
|
"""split_name('__type_name/the/object_id')
|
|
|
|
->
|
|
|
|
('__type_name', 'the/object_id')
|
2011-10-14 07:05:51 +00:00
|
|
|
|
2011-11-02 06:52:08 +00:00
|
|
|
Split the given object name into it's type and object_id parts.
|
2011-10-14 07:05:51 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
type_name = object_name.split(os.sep)[0]
|
|
|
|
object_id = os.sep.join(object_name.split(os.sep)[1:])
|
2011-11-02 06:52:08 +00:00
|
|
|
return type_name, object_id
|
2011-10-14 07:05:51 +00:00
|
|
|
|
2011-11-02 08:13:23 +00:00
|
|
|
@staticmethod
|
|
|
|
def join_name(type_name, object_id):
|
2019-02-15 19:34:45 +00:00
|
|
|
"""join_name('__type_name', 'the/object_id')
|
|
|
|
->
|
|
|
|
__type_name/the/object_id'
|
2011-11-02 08:13:23 +00:00
|
|
|
|
|
|
|
Join the given type_name and object_id into an object name.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return os.path.join(type_name, object_id)
|
|
|
|
|
2012-02-13 06:25:04 +00:00
|
|
|
def validate_object_id(self):
|
2014-02-09 16:10:43 +00:00
|
|
|
if self.cdist_type.is_singleton and self.object_id:
|
2016-07-05 18:44:24 +00:00
|
|
|
raise IllegalObjectIdError(('singleton objects can\'t have an '
|
|
|
|
'object_id'))
|
2012-02-13 06:45:32 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
"""Validate the given object_id and raise IllegalObjectIdError
|
|
|
|
if it's not valid.
|
2012-01-19 06:51:02 +00:00
|
|
|
"""
|
2012-02-13 06:45:32 +00:00
|
|
|
if self.object_id:
|
2014-05-05 22:28:18 +00:00
|
|
|
if self.object_marker in self.object_id.split(os.sep):
|
2016-07-05 18:44:24 +00:00
|
|
|
raise IllegalObjectIdError(
|
|
|
|
self.object_id, ('object_id may not contain '
|
2021-03-30 05:56:38 +00:00
|
|
|
'\'{}\'').format(self.object_marker))
|
2012-02-13 06:25:04 +00:00
|
|
|
if '//' in self.object_id:
|
2016-07-05 18:44:24 +00:00
|
|
|
raise IllegalObjectIdError(
|
|
|
|
self.object_id, 'object_id may not contain //')
|
2017-08-10 17:08:44 +00:00
|
|
|
|
|
|
|
_invalid_object_ids = ('.', '/', )
|
|
|
|
for ioid in _invalid_object_ids:
|
|
|
|
if self.object_id == ioid:
|
|
|
|
raise IllegalObjectIdError(
|
|
|
|
self.object_id,
|
|
|
|
'object_id may not be a {}'.format(ioid))
|
2012-01-19 06:51:02 +00:00
|
|
|
|
2012-02-13 06:25:04 +00:00
|
|
|
# If no object_id and type is not singleton => error out
|
2012-02-13 06:50:11 +00:00
|
|
|
if not self.object_id and not self.cdist_type.is_singleton:
|
2013-02-05 21:50:22 +00:00
|
|
|
raise MissingObjectIdError(self.cdist_type.name)
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
# Does not work:
|
|
|
|
# AttributeError:
|
|
|
|
# 'CdistObject' object has no attribute 'parameter_path'
|
2013-02-05 21:50:22 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
# "Type %s is not a singleton type - missing object id
|
|
|
|
# (parameters: %s)" % (self.cdist_type.name, self.parameters))
|
2012-02-13 06:25:04 +00:00
|
|
|
|
2011-11-02 06:52:08 +00:00
|
|
|
def object_from_name(self, object_name):
|
2019-02-15 19:34:45 +00:00
|
|
|
"""Convenience method for creating an object instance from an object
|
|
|
|
name.
|
2011-11-02 06:52:08 +00:00
|
|
|
|
|
|
|
Mainly intended to create objects when resolving requirements.
|
|
|
|
|
|
|
|
e.g:
|
2016-07-05 18:44:24 +00:00
|
|
|
<CdistObject __foo/bar>.object_from_name('__other/object') ->
|
|
|
|
<CdistObject __other/object>
|
2011-11-02 06:52:08 +00:00
|
|
|
|
|
|
|
"""
|
2012-02-13 06:56:37 +00:00
|
|
|
|
2011-11-09 17:41:46 +00:00
|
|
|
base_path = self.base_path
|
2012-02-13 06:56:37 +00:00
|
|
|
type_path = self.cdist_type.base_path
|
2015-03-05 17:32:47 +00:00
|
|
|
object_marker = self.object_marker
|
2012-02-13 06:56:37 +00:00
|
|
|
|
2011-11-02 06:52:08 +00:00
|
|
|
type_name, object_id = self.split_name(object_name)
|
2012-02-13 06:56:37 +00:00
|
|
|
|
|
|
|
cdist_type = self.cdist_type.__class__(type_path, type_name)
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
return self.__class__(cdist_type, base_path, object_marker,
|
|
|
|
object_id=object_id)
|
2011-11-01 21:46:46 +00:00
|
|
|
|
2012-02-13 07:51:40 +00:00
|
|
|
def __repr__(self):
|
2021-03-30 05:56:38 +00:00
|
|
|
return '<CdistObject {}>'.format(self.name)
|
2012-02-13 06:45:32 +00:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
"""define equality as 'name is the same'"""
|
|
|
|
return self.name == other.name
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2012-02-13 06:45:32 +00:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return isinstance(other, self.__class__) and self.name < other.name
|
|
|
|
|
2012-02-13 07:51:40 +00:00
|
|
|
def sanitise_object_id(self):
|
2012-02-11 23:05:21 +00:00
|
|
|
"""
|
|
|
|
Remove leading and trailing slash (one only)
|
|
|
|
"""
|
|
|
|
|
2012-02-13 09:38:36 +00:00
|
|
|
# Allow empty object id for singletons
|
|
|
|
if self.object_id:
|
|
|
|
# Remove leading slash
|
|
|
|
if self.object_id[0] == '/':
|
|
|
|
self.object_id = self.object_id[1:]
|
2012-02-11 23:05:21 +00:00
|
|
|
|
2012-02-13 09:38:36 +00:00
|
|
|
# Remove trailing slash
|
|
|
|
if self.object_id[-1] == '/':
|
|
|
|
self.object_id = self.object_id[:-1]
|
2012-02-11 23:05:21 +00:00
|
|
|
|
2011-10-11 13:44:31 +00:00
|
|
|
# FIXME: still needed?
|
2011-10-05 22:58:43 +00:00
|
|
|
@property
|
2011-10-07 13:42:04 +00:00
|
|
|
def explorer_path(self):
|
2011-10-11 08:28:10 +00:00
|
|
|
"""Create and return the relative path to this objects explorers"""
|
2011-10-07 13:42:04 +00:00
|
|
|
# create absolute path
|
|
|
|
path = os.path.join(self.absolute_path, "explorer")
|
2011-10-07 11:15:11 +00:00
|
|
|
if not os.path.isdir(path):
|
|
|
|
os.mkdir(path)
|
2011-10-07 13:42:04 +00:00
|
|
|
# return relative path
|
|
|
|
return os.path.join(self.path, "explorer")
|
2011-10-07 11:50:17 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
requirements = fsproperty.FileListProperty(
|
|
|
|
lambda obj: os.path.join(obj.absolute_path, 'require'))
|
|
|
|
autorequire = fsproperty.FileListProperty(
|
|
|
|
lambda obj: os.path.join(obj.absolute_path, 'autorequire'))
|
|
|
|
parameters = fsproperty.DirectoryDictProperty(
|
|
|
|
lambda obj: os.path.join(obj.base_path, obj.parameter_path))
|
|
|
|
explorers = fsproperty.DirectoryDictProperty(
|
|
|
|
lambda obj: os.path.join(obj.base_path, obj.explorer_path))
|
|
|
|
state = fsproperty.FileStringProperty(
|
|
|
|
lambda obj: os.path.join(obj.absolute_path, "state"))
|
|
|
|
source = fsproperty.FileListProperty(
|
|
|
|
lambda obj: os.path.join(obj.absolute_path, "source"))
|
|
|
|
code_local = fsproperty.FileStringProperty(
|
|
|
|
lambda obj: os.path.join(obj.base_path, obj.code_local_path))
|
|
|
|
code_remote = fsproperty.FileStringProperty(
|
|
|
|
lambda obj: os.path.join(obj.base_path, obj.code_remote_path))
|
Redefine/reimplement CDIST_ORDER_DEPENDENCY
CDIST_ORDER_DEPENDENCY now defines type order dependency context.
cdist (emulator) maintains global state variables, as files,
order_dep_state and typeorder_dep, and per object state variable,
as file, typeorder_dep.
If order_dep_state exists then this defines that order dependency is
turned on.
If order_dep_state does not exist then order dependency is turned off.
If order dependency is on then objects created after it is turned on are
recorded into:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
If order dependency is on then requirement is injected, where object
created before current, is read from:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
Every time order dependency is turned off, typeorder_dep files are
removed, which means that type order list is cleared, context is
cleaned.
In the end cdist cleans after itself, i.e. mentioned files are removed.
When running type manifest is finished typeorder_dep file is removed.
When running config finishes global typeorder_dep and order_dep_state
files are removed.
Global type order recording is untouched.
Furthermore, for completeness, type order is now recorded for each object
too.
2019-11-22 23:25:51 +00:00
|
|
|
typeorder = fsproperty.FileListProperty(
|
|
|
|
lambda obj: os.path.join(obj.absolute_path, 'typeorder'))
|
|
|
|
typeorder_dep = fsproperty.FileListProperty(
|
|
|
|
lambda obj: os.path.join(obj.absolute_path, 'typeorder_dep'))
|
2021-03-21 17:10:06 +00:00
|
|
|
# objects without parents are objects specified in init manifest
|
|
|
|
parents = fsproperty.FileListProperty(
|
|
|
|
lambda obj: os.path.join(obj.absolute_path, 'parents'))
|
|
|
|
# objects without children are object of types that do not reuse other
|
|
|
|
# types
|
|
|
|
children = fsproperty.FileListProperty(
|
|
|
|
lambda obj: os.path.join(obj.absolute_path, 'children'))
|
Redefine/reimplement CDIST_ORDER_DEPENDENCY
CDIST_ORDER_DEPENDENCY now defines type order dependency context.
cdist (emulator) maintains global state variables, as files,
order_dep_state and typeorder_dep, and per object state variable,
as file, typeorder_dep.
If order_dep_state exists then this defines that order dependency is
turned on.
If order_dep_state does not exist then order dependency is turned off.
If order dependency is on then objects created after it is turned on are
recorded into:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
If order dependency is on then requirement is injected, where object
created before current, is read from:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
Every time order dependency is turned off, typeorder_dep files are
removed, which means that type order list is cleared, context is
cleaned.
In the end cdist cleans after itself, i.e. mentioned files are removed.
When running type manifest is finished typeorder_dep file is removed.
When running config finishes global typeorder_dep and order_dep_state
files are removed.
Global type order recording is untouched.
Furthermore, for completeness, type order is now recorded for each object
too.
2019-11-22 23:25:51 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
try:
|
|
|
|
os.remove(os.path.join(self.absolute_path, 'typeorder_dep'))
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2011-10-11 08:28:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def exists(self):
|
|
|
|
"""Checks wether this cdist object exists on the file systems."""
|
|
|
|
return os.path.exists(self.absolute_path)
|
2011-10-11 08:29:06 +00:00
|
|
|
|
2014-01-31 16:56:55 +00:00
|
|
|
def create(self, allow_overwrite=False):
|
2011-10-11 08:29:06 +00:00
|
|
|
"""Create this cdist object on the filesystem.
|
|
|
|
"""
|
|
|
|
try:
|
2018-01-09 08:31:40 +00:00
|
|
|
for path in (self.absolute_path,
|
|
|
|
os.path.join(self.base_path, self.parameter_path),
|
|
|
|
self.stdout_path,
|
|
|
|
self.stderr_path):
|
|
|
|
os.makedirs(path, exist_ok=allow_overwrite)
|
2011-10-11 08:29:06 +00:00
|
|
|
except EnvironmentError as error:
|
2016-07-05 18:44:24 +00:00
|
|
|
raise cdist.Error(('Error creating directories for cdist object: '
|
2021-03-30 05:56:38 +00:00
|
|
|
'{}: {}').format(self, error))
|
2012-12-14 07:41:17 +00:00
|
|
|
|
2013-04-30 13:07:27 +00:00
|
|
|
def requirements_unfinished(self, requirements):
|
2013-05-15 07:16:52 +00:00
|
|
|
"""Return state whether requirements are satisfied"""
|
2013-04-30 08:58:23 +00:00
|
|
|
|
2013-04-30 13:07:27 +00:00
|
|
|
object_list = []
|
2013-04-30 08:58:23 +00:00
|
|
|
|
2013-04-30 09:04:01 +00:00
|
|
|
for requirement in requirements:
|
2013-04-30 08:58:23 +00:00
|
|
|
cdist_object = self.object_from_name(requirement)
|
|
|
|
|
|
|
|
if not cdist_object.state == self.STATE_DONE:
|
2013-04-30 13:07:27 +00:00
|
|
|
object_list.append(cdist_object)
|
2012-12-14 07:41:56 +00:00
|
|
|
|
2013-04-30 13:07:27 +00:00
|
|
|
return object_list
|