2011-10-06 11:00:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2011-10-07 07:01:47 +00:00
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
|
|
|
# 2011 Nico Schottelius (nico-cdist at schottelius.org)
|
2011-10-06 11:00:17 +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 os
|
|
|
|
|
|
|
|
import cdist
|
|
|
|
|
|
|
|
|
2011-10-07 10:49:30 +00:00
|
|
|
# FIXME: i should not have to care about prefix directory, local, remote and such.
|
|
|
|
# I know what my internals look like, the outside is none of my business.
|
2011-10-06 11:00:17 +00:00
|
|
|
class Type(object):
|
2011-10-06 11:21:48 +00:00
|
|
|
"""Represents a cdist type.
|
|
|
|
|
|
|
|
All interaction with types in cdist should be done through this class.
|
|
|
|
Directly accessing an type through the file system from python code is
|
|
|
|
a bug.
|
|
|
|
|
|
|
|
"""
|
2011-10-06 11:00:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def base_dir():
|
|
|
|
"""Return the absolute path to the top level directory where types
|
|
|
|
are defined.
|
|
|
|
|
|
|
|
Requires the environment variable '__cdist_base_dir' to be set.
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return os.path.join(
|
|
|
|
os.environ['__cdist_base_dir'],
|
|
|
|
'conf',
|
|
|
|
'type'
|
|
|
|
)
|
|
|
|
except KeyError as e:
|
|
|
|
raise cdist.MissingEnvironmentVariableError(e.args[0])
|
|
|
|
|
2011-10-07 10:49:30 +00:00
|
|
|
@staticmethod
|
|
|
|
def remote_base_dir():
|
|
|
|
"""Return the absolute path to the top level directory where types
|
|
|
|
are kept on the remote/target host.
|
|
|
|
|
|
|
|
Requires the environment variable '__cdist_remote_base_dir' to be set.
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return os.path.join(
|
|
|
|
os.environ['__cdist_remote_base_dir'],
|
|
|
|
'conf',
|
|
|
|
'type'
|
|
|
|
)
|
|
|
|
except KeyError as e:
|
|
|
|
raise cdist.MissingEnvironmentVariableError(e.args[0])
|
|
|
|
|
|
|
|
# FIXME: probably wrong place for this
|
|
|
|
@property
|
|
|
|
def remote_explorer_dir(self):
|
|
|
|
return os.path.join(self.remote_path, "explorer")
|
|
|
|
|
2011-10-06 11:00:17 +00:00
|
|
|
@classmethod
|
|
|
|
def list_types(cls):
|
|
|
|
"""Return a list of type instances"""
|
|
|
|
for type_name in cls.list_type_names():
|
|
|
|
yield cls(type_name)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def list_type_names(cls):
|
|
|
|
"""Return a list of type names"""
|
|
|
|
return os.listdir(cls.base_dir())
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
self.__explorers = None
|
|
|
|
self.__required_parameters = None
|
|
|
|
self.__optional_parameters = None
|
|
|
|
|
2011-10-07 07:01:47 +00:00
|
|
|
self.transferred_explorers = False
|
|
|
|
|
2011-10-06 11:00:17 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '<Type name=%s>' % self.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return os.path.join(
|
|
|
|
self.base_dir(),
|
|
|
|
self.name
|
|
|
|
)
|
2011-10-07 10:49:30 +00:00
|
|
|
# FIXME: prefix directory should not leak into me
|
|
|
|
@property
|
|
|
|
def remote_path(self):
|
|
|
|
return os.path.join(
|
|
|
|
self.remote_base_dir(),
|
|
|
|
self.name
|
|
|
|
)
|
2011-10-06 11:00:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_singleton(self):
|
|
|
|
"""Check whether a type is a singleton."""
|
|
|
|
return os.path.isfile(os.path.join(self.path, "singleton"))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_install(self):
|
|
|
|
"""Check whether a type is used for installation (if not: for configuration)"""
|
|
|
|
return os.path.isfile(os.path.join(self.path, "install"))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def explorers(self):
|
|
|
|
"""Return a list of available explorers"""
|
|
|
|
if not self.__explorers:
|
|
|
|
try:
|
|
|
|
self.__explorers = os.listdir(os.path.join(self.path, "explorer"))
|
|
|
|
except EnvironmentError as e:
|
|
|
|
# error ignored
|
|
|
|
self.__explorers = []
|
|
|
|
return self.__explorers
|
|
|
|
|
|
|
|
@property
|
|
|
|
def required_parameters(self):
|
|
|
|
"""Return a list of required parameters"""
|
|
|
|
if not self.__required_parameters:
|
|
|
|
parameters = []
|
|
|
|
try:
|
|
|
|
with open(os.path.join(self.path, "parameter", "required")) as fd:
|
|
|
|
for line in fd:
|
|
|
|
parameters.append(line.strip())
|
|
|
|
except EnvironmentError as e:
|
|
|
|
# error ignored
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.__required_parameters = parameters
|
|
|
|
return self.__required_parameters
|
|
|
|
|
|
|
|
@property
|
|
|
|
def optional_parameters(self):
|
|
|
|
"""Return a list of optional parameters"""
|
|
|
|
if not self.__optional_parameters:
|
|
|
|
parameters = []
|
|
|
|
try:
|
|
|
|
with open(os.path.join(self.path, "parameter", "optional")) as fd:
|
|
|
|
for line in fd:
|
|
|
|
parameters.append(line.strip())
|
|
|
|
except EnvironmentError as e:
|
|
|
|
# error ignored
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.__optional_parameters = parameters
|
|
|
|
return self.__optional_parameters
|