2020-08-19 01:22:03 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
from ..._constants import CONF_DIRECTORY_NAME
|
|
|
|
from ...config import LoadedConfig
|
2020-08-19 01:22:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Project:
|
|
|
|
__name = None
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.__name = name
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
@classmethod
|
|
|
|
def from_file_name(cls, file_name):
|
|
|
|
if os.path.isdir(file_name):
|
|
|
|
config = LoadedConfig.get()
|
|
|
|
|
|
|
|
if file_name.endswith(config['markers:disabled']):
|
|
|
|
file_name = file_name[:-len(config['markers:disabled'])]
|
|
|
|
|
|
|
|
if file_name.endswith(config['markers:project']):
|
|
|
|
file_name = file_name[:-len(config['markers:project'])]
|
|
|
|
return cls(file_name)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2020-08-19 01:22:03 +00:00
|
|
|
def get_name(self):
|
|
|
|
return self.__name
|
|
|
|
|
|
|
|
def dir_name(self):
|
2020-08-19 09:58:13 +00:00
|
|
|
if self.is_enabled():
|
|
|
|
return self.enabled_dir_name()
|
|
|
|
elif self.is_disabled():
|
|
|
|
return self.disabled_dir_name()
|
|
|
|
else:
|
|
|
|
return None
|
2020-08-19 01:22:03 +00:00
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
def enabled_dir_name(self):
|
|
|
|
return f"{self.__name}{LoadedConfig.get()['markers:project']}"
|
|
|
|
|
|
|
|
def disabled_dir_name(self):
|
2020-08-19 10:02:46 +00:00
|
|
|
return f"{self.enabled_dir_name()}{LoadedConfig.get()['markers:disabled']}"
|
2020-08-19 01:22:03 +00:00
|
|
|
|
|
|
|
def conf_dir_name(self):
|
|
|
|
return os.path.join(self.dir_name(), CONF_DIRECTORY_NAME)
|
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
def compose_file_name(self):
|
|
|
|
return os.path.join(self.dir_name(), 'docker-compose.yml')
|
|
|
|
|
2020-08-19 01:22:03 +00:00
|
|
|
def target_dir_name(self):
|
2020-08-19 09:58:13 +00:00
|
|
|
return os.path.join(LoadedConfig.get()['runtime:storage'], self.enabled_dir_name())
|
2020-08-19 01:22:03 +00:00
|
|
|
|
|
|
|
def exists(self):
|
2020-08-19 09:58:13 +00:00
|
|
|
return os.path.isdir(self.enabled_dir_name()) or os.path.isdir(self.disabled_dir_name())
|
2020-08-19 01:22:03 +00:00
|
|
|
|
|
|
|
def is_enabled(self):
|
2020-08-19 09:58:13 +00:00
|
|
|
return os.path.isdir(self.enabled_dir_name())
|
2020-08-19 01:22:03 +00:00
|
|
|
|
|
|
|
def is_disabled(self):
|
2020-08-19 09:58:13 +00:00
|
|
|
return os.path.isdir(self.disabled_dir_name())
|
2020-08-19 01:22:03 +00:00
|
|
|
|
|
|
|
def has_configs(self):
|
2020-08-19 09:58:13 +00:00
|
|
|
return os.path.isdir(self.conf_dir_name())
|
2020-08-19 01:22:03 +00:00
|
|
|
|
|
|
|
def enable(self):
|
|
|
|
if self.is_disabled():
|
|
|
|
logging.info(f"Enabling project '{self.get_name()}'")
|
2020-08-19 09:58:13 +00:00
|
|
|
os.rename(self.dir_name(), self.enabled_dir_name())
|
2020-08-19 01:22:03 +00:00
|
|
|
|
|
|
|
elif self.is_enabled():
|
|
|
|
logging.warning(f"Project '{self.get_name()}' is enabled!")
|
|
|
|
|
|
|
|
else:
|
|
|
|
logging.warning(f"Project '{self.get_name()}' not found in instance!")
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def disable(self):
|
|
|
|
if self.is_enabled():
|
|
|
|
logging.info(f"Disabling project '{self.get_name()}'")
|
2020-08-19 09:58:13 +00:00
|
|
|
os.rename(self.dir_name(), self.disabled_dir_name())
|
2020-08-19 01:22:03 +00:00
|
|
|
|
|
|
|
elif self.is_disabled():
|
|
|
|
logging.warning(f"Project '{self.get_name()}' is disabled!")
|
|
|
|
|
|
|
|
else:
|
|
|
|
logging.warning(f"Project '{self.get_name()}' not found in instance!")
|
|
|
|
return False
|
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class Projects:
|
|
|
|
__projects = None
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.__projects[item]
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str([
|
|
|
|
project.get_name()
|
|
|
|
for project
|
|
|
|
in self.__projects
|
|
|
|
])
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_names(cls, project_names):
|
|
|
|
result = cls()
|
|
|
|
result.__projects = [
|
2020-08-19 09:58:13 +00:00
|
|
|
Project(name)
|
2020-08-19 14:10:56 +00:00
|
|
|
for name in project_names if isinstance(name, str)
|
2020-08-19 09:58:13 +00:00
|
|
|
]
|
2020-08-19 14:10:56 +00:00
|
|
|
return result
|
2020-08-19 09:58:13 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
@classmethod
|
|
|
|
def from_projects(cls, projects):
|
|
|
|
result = cls()
|
|
|
|
result.__projects = [
|
|
|
|
project
|
|
|
|
for project in projects if isinstance(project, Project)
|
|
|
|
]
|
|
|
|
return result
|
2020-08-19 09:58:13 +00:00
|
|
|
|
|
|
|
@classmethod
|
2020-08-19 14:10:56 +00:00
|
|
|
def from_dir(cls):
|
|
|
|
return cls.from_projects([
|
|
|
|
Project.from_file_name(file_name)
|
2020-08-19 09:58:13 +00:00
|
|
|
for file_name in os.listdir()
|
|
|
|
])
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_args(cls, args):
|
|
|
|
if args is not None and 'projects' in args:
|
|
|
|
if isinstance(args.projects, list) and args.projects:
|
2020-08-19 14:10:56 +00:00
|
|
|
return cls.from_names(args.projects)
|
2020-08-19 09:58:13 +00:00
|
|
|
|
|
|
|
elif isinstance(args.projects, str):
|
2020-08-19 14:10:56 +00:00
|
|
|
return cls.from_names([args.projects])
|
|
|
|
|
|
|
|
return cls()
|
|
|
|
|
|
|
|
def empty(self):
|
|
|
|
return not self.__projects
|
2020-08-19 09:58:13 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def filter_exists(self):
|
|
|
|
result = Projects()
|
|
|
|
result.__projects = [
|
|
|
|
project
|
|
|
|
for project in self.__projects
|
|
|
|
if project.exists()
|
|
|
|
]
|
|
|
|
return result
|
|
|
|
|
|
|
|
def filter_enabled(self):
|
|
|
|
result = Projects()
|
|
|
|
result.__projects = [
|
|
|
|
project
|
|
|
|
for project in self.__projects
|
|
|
|
if project.is_enabled()
|
|
|
|
]
|
|
|
|
return result
|
|
|
|
|
|
|
|
def filter_disabled(self):
|
|
|
|
result = Projects()
|
|
|
|
result.__projects = [
|
|
|
|
project
|
|
|
|
for project in self.__projects
|
|
|
|
if project.is_disabled()
|
|
|
|
]
|
|
|
|
return result
|