1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-24 05:33:00 +00:00
kiwi-scp/kiwi_scp/project.py

134 lines
4 KiB
Python
Raw Normal View History

2020-08-19 01:22:03 +00:00
import logging
import os
2020-08-19 15:15:00 +00:00
from ._constants import CONF_DIRECTORY_NAME
from .config import LoadedConfig
2021-09-22 01:06:43 +00:00
from .executable import Executable
2020-08-19 01:22:03 +00:00
class Project:
__name = None
def __init__(self, name):
self.__name = name
@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):
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
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)
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):
return os.path.join(LoadedConfig.get()['runtime:storage'], self.enabled_dir_name())
2020-08-19 01:22:03 +00:00
def exists(self):
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):
return os.path.isdir(self.enabled_dir_name())
2020-08-19 01:22:03 +00:00
def is_disabled(self):
return os.path.isdir(self.disabled_dir_name())
2020-08-19 01:22:03 +00:00
def has_configs(self):
return os.path.isdir(self.conf_dir_name())
2020-08-19 01:22:03 +00:00
2020-08-19 15:09:43 +00:00
def __update_kwargs(self, kwargs):
if not self.is_enabled():
# cannot compose in a disabled project
logging.warning(f"Project '{self.get_name()}' is not enabled!")
return False
config = LoadedConfig.get()
# execute command in project directory
kwargs['cwd'] = self.dir_name()
# ensure there is an environment
if 'env' not in kwargs:
kwargs['env'] = {}
# create environment variables for docker commands
kwargs['env'].update({
'COMPOSE_PROJECT_NAME': self.get_name(),
'KIWI_HUB_NAME': config['network:name'],
2020-08-25 14:33:50 +00:00
'TARGETROOT': config['runtime:storage'],
2020-08-19 15:09:43 +00:00
'CONFDIR': os.path.join(config['runtime:storage'], CONF_DIRECTORY_NAME),
'TARGETDIR': self.target_dir_name()
})
2020-08-20 13:36:28 +00:00
# add common environment from config
if config['runtime:env'] is not None:
kwargs['env'].update(config['runtime:env'])
2020-08-19 15:09:43 +00:00
logging.debug(f"kwargs updated: {kwargs}")
return True
def compose_run(self, compose_args, **kwargs):
if self.__update_kwargs(kwargs):
Executable('docker-compose').run(compose_args, **kwargs)
def compose_run_less(self, compose_args, **kwargs):
if self.__update_kwargs(kwargs):
Executable('docker-compose').run_less(compose_args, **kwargs)
2020-08-19 01:22:03 +00:00
def enable(self):
if self.is_disabled():
logging.info(f"Enabling project '{self.get_name()}'")
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()}'")
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
return True