2011-10-06 10:57:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2011-10-06 18:34:36 +00:00
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2012-02-13 13:30:27 +00:00
|
|
|
# 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
|
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 18:27:07 +00:00
|
|
|
import logging
|
2011-10-06 10:57:38 +00:00
|
|
|
import os
|
2011-10-06 15:15:45 +00:00
|
|
|
import collections
|
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
|
|
|
|
2011-10-06 18:27:07 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2011-10-06 15:15:45 +00:00
|
|
|
|
2011-11-09 19:38:22 +00:00
|
|
|
OBJECT_MARKER = '.cdist'
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-10 14:39:48 +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):
|
2011-10-14 12:04:56 +00:00
|
|
|
return '%s: %s' % (self.message, self.object_id)
|
2011-10-14 09:25:39 +00:00
|
|
|
|
|
|
|
|
2012-02-11 23:08:18 +00:00
|
|
|
class CdistObject(object):
|
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
|
|
|
|
STATE_PREPARED = "prepared"
|
|
|
|
STATE_RUNNING = "running"
|
|
|
|
STATE_DONE = "done"
|
|
|
|
|
2011-10-05 22:58:43 +00:00
|
|
|
@classmethod
|
2011-10-07 13:42:04 +00:00
|
|
|
def list_objects(cls, object_base_path, type_base_path):
|
2011-10-05 22:58:43 +00:00
|
|
|
"""Return a list of object instances"""
|
2011-10-07 13:42:04 +00:00
|
|
|
for object_name in cls.list_object_names(object_base_path):
|
2011-11-02 06:52:08 +00:00
|
|
|
type_name, object_id = cls.split_name(object_name)
|
2012-02-12 00:18:14 +00:00
|
|
|
yield cls(cdist.core.CdistType(type_base_path, type_name), object_base_path, object_id=object_id)
|
2011-10-05 22:58:43 +00:00
|
|
|
|
|
|
|
@classmethod
|
2011-10-07 13:42:04 +00:00
|
|
|
def list_type_names(cls, object_base_path):
|
2011-10-05 22:58:43 +00:00
|
|
|
"""Return a list of type names"""
|
2011-10-07 13:42:04 +00:00
|
|
|
return os.listdir(object_base_path)
|
2011-10-05 22:58:43 +00:00
|
|
|
|
|
|
|
@classmethod
|
2011-10-07 13:42:04 +00:00
|
|
|
def list_object_names(cls, object_base_path):
|
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):
|
2011-11-09 19:38:22 +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
|
|
|
|
2011-11-02 06:52:08 +00:00
|
|
|
@staticmethod
|
|
|
|
def split_name(object_name):
|
|
|
|
"""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]
|
2011-11-02 06:52:08 +00:00
|
|
|
# FIXME: allow object without object_id? e.g. for singleton
|
2011-10-14 07:05:51 +00:00
|
|
|
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):
|
|
|
|
"""join_name('__type_name', 'the/object_id') -> __type_name/the/object_id'
|
|
|
|
|
|
|
|
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):
|
2012-02-13 06:45:32 +00:00
|
|
|
# FIXME: also check that there is no object ID when type is singleton?
|
|
|
|
|
2012-01-19 06:51:02 +00:00
|
|
|
"""Validate the given object_id and raise IllegalObjectIdError if it's not valid.
|
|
|
|
"""
|
2012-02-13 06:45:32 +00:00
|
|
|
if self.object_id:
|
2012-02-13 06:25:04 +00:00
|
|
|
if self.object_id.startswith('/'):
|
2012-02-13 06:45:32 +00:00
|
|
|
raise IllegalObjectIdError(self.object_id, 'object_id may not start with /')
|
2012-02-13 06:25:04 +00:00
|
|
|
if OBJECT_MARKER in self.object_id.split(os.sep):
|
2012-02-13 06:45:32 +00:00
|
|
|
raise IllegalObjectIdError(self.object_id, 'object_id may not contain \'%s\'' % OBJECT_MARKER)
|
2012-02-13 06:25:04 +00:00
|
|
|
if '//' in self.object_id:
|
2012-02-13 06:45:32 +00:00
|
|
|
raise IllegalObjectIdError(self.object_id, 'object_id may not contain //')
|
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:
|
2012-02-13 06:45:32 +00:00
|
|
|
raise IllegalObjectIdError(self.object_id,
|
2012-02-13 06:25:04 +00:00
|
|
|
"Missing object_id and type is not a singleton.")
|
|
|
|
|
2012-01-19 06:51:02 +00:00
|
|
|
def __init__(self, cdist_type, base_path, object_id=None):
|
2012-02-13 06:50:11 +00:00
|
|
|
self.cdist_type = cdist_type # instance of Type
|
2011-10-07 13:42:04 +00:00
|
|
|
self.base_path = base_path
|
2011-10-05 22:58:43 +00:00
|
|
|
self.object_id = object_id
|
2012-02-11 23:05:21 +00:00
|
|
|
|
|
|
|
self.sanitise_object_id()
|
2012-02-13 06:25:04 +00:00
|
|
|
self.validate_object_id()
|
2012-02-11 23:05:21 +00:00
|
|
|
|
2012-02-13 06:50:11 +00:00
|
|
|
self.name = self.join_name(self.cdist_type.name, self.object_id)
|
|
|
|
self.path = os.path.join(self.cdist_type.path, self.object_id, OBJECT_MARKER)
|
2011-10-07 14:59:52 +00:00
|
|
|
self.absolute_path = os.path.join(self.base_path, self.path)
|
2011-10-07 13:42:04 +00:00
|
|
|
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")
|
2011-10-06 15:15:45 +00:00
|
|
|
|
2011-11-02 06:52:08 +00:00
|
|
|
def object_from_name(self, object_name):
|
|
|
|
"""Convenience method for creating an object instance from an object name.
|
|
|
|
|
|
|
|
Mainly intended to create objects when resolving requirements.
|
|
|
|
|
|
|
|
e.g:
|
2012-02-11 23:08:18 +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
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
return self.__class__(cdist_type, base_path, object_id=object_id)
|
2011-11-01 21:46:46 +00:00
|
|
|
|
2012-02-13 07:51:40 +00:00
|
|
|
def __repr__(self):
|
2012-02-13 06:45:32 +00:00
|
|
|
return '<CdistObject %s>' % self.name
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
"""define equality as 'name is the same'"""
|
|
|
|
return self.name == other.name
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-10-10 14:39:48 +00:00
|
|
|
requirements = fsproperty.FileListProperty(lambda obj: os.path.join(obj.absolute_path, 'require'))
|
2011-10-13 12:37:50 +00:00
|
|
|
parameters = fsproperty.DirectoryDictProperty(lambda obj: os.path.join(obj.base_path, obj.parameter_path))
|
2011-10-11 13:44:31 +00:00
|
|
|
explorers = fsproperty.DirectoryDictProperty(lambda obj: os.path.join(obj.base_path, obj.explorer_path))
|
2011-10-10 14:39:48 +00:00
|
|
|
changed = fsproperty.FileBooleanProperty(lambda obj: os.path.join(obj.absolute_path, "changed"))
|
2011-10-19 12:53:31 +00:00
|
|
|
state = fsproperty.FileStringProperty(lambda obj: os.path.join(obj.absolute_path, "state"))
|
2011-10-11 08:30:30 +00:00
|
|
|
source = fsproperty.FileListProperty(lambda obj: os.path.join(obj.absolute_path, "source"))
|
2011-10-13 12:37:50 +00:00
|
|
|
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))
|
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
|
|
|
|
|
|
|
def create(self):
|
|
|
|
"""Create this cdist object on the filesystem.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
os.makedirs(self.absolute_path, exist_ok=False)
|
|
|
|
absolute_parameter_path = os.path.join(self.base_path, self.parameter_path)
|
|
|
|
os.makedirs(absolute_parameter_path, exist_ok=False)
|
|
|
|
except EnvironmentError as error:
|
|
|
|
raise cdist.Error('Error creating directories for cdist object: %s: %s' % (self, error))
|