"""YuanRong datasystem CLI generate_config command."""
import os
import shutil
from yr.datasystem.cli.command import BaseCommand
import yr.datasystem.cli.common.util as util
class Command(BaseCommand):
"""
Generate yuanrong datasystem configuration files.
"""
name = "generate_config"
description = "generate yuanrong datasystem cluster and worker configuration files"
@staticmethod
def add_arguments(parser):
"""
Add arguments to parser.
Args:
parser (ArgumentParser): Specify parser to which arguments are added.
"""
parser.add_argument(
"-o", "--output_path",
default=os.getcwd(),
help="path to save the generated configuration files, default path is the current directory"
)
def run(self, args):
"""
Execute for generate_config command.
Args:
args (Namespace): Parsed arguments containing the output path.
Raises:
FileNotFoundError: If the source configuration file does not exist.
NotADirectoryError: If the output path is not a directory.
"""
try:
output_dir = os.path.normpath(os.path.realpath(args.output_path))
output_dir = util.valid_safe_path(output_dir)
if not os.path.isdir(output_dir):
raise NotADirectoryError(f"Path is not a directory: {output_dir}")
os.makedirs(output_dir, exist_ok=True)
cluster_file = "cluster_config.json"
src_cluster = os.path.join(self._base_dir, cluster_file)
if not os.path.exists(src_cluster):
raise FileNotFoundError(f"Source cluster configuration file {src_cluster} does not exist")
dest_cluster = os.path.join(output_dir, cluster_file)
shutil.copyfile(src_cluster, dest_cluster)
self.logger.info(f"Cluster configuration file has been generated to {dest_cluster}")
worker_file = "worker_config.json"
src_worker = os.path.join(self._base_dir, worker_file)
if not os.path.exists(src_worker):
raise FileNotFoundError(f"Source worker configuration file {src_worker} does not exist")
dest_worker = os.path.join(output_dir, worker_file)
shutil.copyfile(src_worker, dest_worker)
self.logger.info(f"Worker configuration file has been generated to {dest_worker}")
self.logger.info("Configuration generation completed successfully")
except Exception as e:
self.logger.error(f"Generate failed: {e}")
return self.FAILURE
return self.SUCCESS