Skip to content

Commit

Permalink
Break out into smaller modules, make a bit more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
stvoutsin committed May 19, 2024
1 parent 5dbb265 commit b484f96
Show file tree
Hide file tree
Showing 20 changed files with 419 additions and 255 deletions.
39 changes: 3 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ This module provides functionality to customize environment configuration files

cd phalanx

4. Execute the environment_customizer.py script:
4. Execute the customizer script:

python phalanx_customizer.py <phalanx_repo_path> <base_env_yaml> <new_env_yaml>
phalanx_customizer <phalanx_repo_path> <base_env_yaml> <new_env_yaml>


4. The customized environment files will be created based on the provided configurations.
The customized environment files will be created based on the provided configurations.

## Class Documentation

Expand All @@ -46,39 +46,6 @@ A class to customize environment configuration files for the Phalanx project.

- `phalanx_repo_path` (str): Path to the Phalanx repository.

#### Methods

- `create_environment_from_yaml(base_env_yaml: str, new_env_yaml: str) -> None`:

Customize the environment configuration files using YAML files.

- `parse_environment_yaml(yaml_file: str) -> Environment`:

Parse the environment configuration from a YAML file.

- `create_environment(base_env: Environment, new_env: Environment) -> None`:

Customize the environment configuration files.

- `find_matching_files(environment_base: str) -> List[str]`:

Find all configuration files that match the base environment name.

- `create_custom_file(file_path: str, environment_base: str, new_environment_name: str) -> str`:

Create a customized copy of the configuration file.

- `replace_string_in_file(file_path: str, old_string: str, new_string: str) -> None`:

Replace a string in a file.

- `update_tap(new_env: Environment) -> None`:

Update the tap config file with new settings.

- `update_nginx_config(new_env: Environment) -> None`:

Update the nginx-ingress config file with new settings.

## License

Expand Down
1 change: 0 additions & 1 deletion phalanx_customizer/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .phalanx_customizer import Environment, EnvironmentCustomizer
Empty file.
13 changes: 13 additions & 0 deletions phalanx_customizer/config/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import dataclass, field
from .environment_config import EnvironmentConfig


@dataclass
class Environment:
"""
Represents an environment configuration.
"""

name: str
base_url: str
config: EnvironmentConfig = field(default_factory=EnvironmentConfig)
16 changes: 16 additions & 0 deletions phalanx_customizer/config/environment_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from dataclasses import dataclass


@dataclass
class EnvironmentConfig:
"""
Represents the configuration details of an environment.
"""

loadbalancerip: str = None
vault_path: str = None
nfs: str = None
gcs_bucket: str = None
gcs_bucket_url: str = None
qserv: str = None
github_oauth_client_id: str = None
Empty file.
71 changes: 71 additions & 0 deletions phalanx_customizer/core/environment_customizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from phalanx_customizer.config.environment import Environment
from phalanx_customizer.parsers.yaml_parser import EnvironmentYamlParser
from phalanx_customizer.file_operations.file_finder import FileFinder
from phalanx_customizer.file_operations.file_customizer import FileCustomizer
from phalanx_customizer.file_operations.config_updater import (
NginxConfigUpdater,
TapConfigUpdater,
)


class EnvironmentCustomizer:
"""
A class to customize environment configuration files for the Phalanx project.
"""

def __init__(self, repo_path: str):
self.repo_path = repo_path
self.file_finder = FileFinder(repo_path)
self.file_customizer = FileCustomizer()
self.nginx_updater = NginxConfigUpdater(repo_path)
self.tap_updater = TapConfigUpdater(repo_path)
self.yaml_parser = EnvironmentYamlParser()

def create_environment_from_yaml(
self, base_env_yaml: str, new_env_yaml: str
) -> None:
"""
Customize the environment configuration files using YAML files.
Args:
base_env_yaml (str): Path to the YAML file containing base environment configuration.
new_env_yaml (str): Path to the YAML file containing new environment configuration.
"""
base_env = self.yaml_parser.parse(base_env_yaml)
new_env = self.yaml_parser.parse(new_env_yaml)
self.create_environment(base_env, new_env)

def create_environment(
self, base_env: Environment, new_env: Environment
) -> None:
"""
Customize the environment configuration files.
Args:
base_env (Environment): The base environment.
new_env (Environment): The new environment.
"""
matching_files = self.file_finder.find_matching_files(base_env.name)

for file_path in matching_files:
new_file_path = self.file_customizer.create_custom_file(
file_path, base_env.name, new_env.name
)

for param in ("nfs", "github_oauth_client_id"):
new_config, base_config = new_env.config, base_env.config
self.file_customizer.replace_string_in_file(
new_file_path,
getattr(base_config, param),
getattr(new_config, param),
)

for param in ("name", "base_url"):
self.file_customizer.replace_string_in_file(
new_file_path,
getattr(base_env, param),
getattr(new_env, param),
)

self.nginx_updater.update_nginx_config(new_env)
self.tap_updater.update_tap_config(new_env)
Empty file.
92 changes: 92 additions & 0 deletions phalanx_customizer/file_operations/config_updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import os
import re
import fileinput
from phalanx_customizer.config.environment import Environment


class NginxConfigUpdater:
def __init__(self, repo_path: str):
"""
Initialize the NginxConfigUpdater with the repository path.
Args:
repo_path (str): Path to the repository.
"""
self.repo_path = repo_path

def update_nginx_config(self, new_env: Environment) -> None:
"""
Update the nginx-ingress config file with new settings.
Args:
new_env (Environment): The new environment.
"""
nginx_config_path = os.path.join(
f"{self.repo_path}/applications",
"ingress-nginx",
f"values-{new_env.name}.yaml",
)
self._update_file(
nginx_config_path, "loadBalancerIP", new_env.config.loadbalancerip
)

def _update_file(self, file_path: str, key: str, new_value: str) -> None:
"""
Update a specific key in a file with a new value.
Args:
file_path (str): Path of the file.
key (str): Key to be updated.
new_value (str): New value for the key.
"""
with fileinput.FileInput(file_path, inplace=True) as file:
for line in file:
if key in line:
line = re.sub(f"{key}: .*", f'{key}: "{new_value}"', line)
print(line, end="")


class TapConfigUpdater:
def __init__(self, repo_path: str):
"""
Initialize the TapConfigUpdater with the repository path.
Args:
repo_path (str): Path to the repository.
"""
self.repo_path = repo_path

def update_tap_config(self, new_env: Environment) -> None:
"""
Update the tap config file with new settings.
Args:
new_env (Environment): The new environment.
"""
tap_config_path = os.path.join(
f"{self.repo_path}/applications",
"tap",
f"values-{new_env.name}.yaml",
)
self._update_file(
tap_config_path, "gcsBucket", new_env.config.gcs_bucket
)
self._update_file(
tap_config_path, "gcsBucketUrl", new_env.config.gcs_bucket_url
)
self._update_file(tap_config_path, "host", new_env.config.qserv)

def _update_file(self, file_path: str, key: str, new_value: str) -> None:
"""
Update a specific key in a file with a new value.
Args:
file_path (str): Path of the file.
key (str): Key to be updated.
new_value (str): New value for the key.
"""
with fileinput.FileInput(file_path, inplace=True) as file:
for line in file:
if key in line:
line = re.sub(f"{key}: .*", f'{key}: "{new_value}"', line)
print(line, end="")
41 changes: 41 additions & 0 deletions phalanx_customizer/file_operations/file_customizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import shutil
import fileinput


class FileCustomizer:
@staticmethod
def create_custom_file(
file_path: str, environment_base: str, new_environment_name: str
) -> str:
"""
Create a customized copy of the configuration file.
Args:
file_path (str): Path of the original configuration file.
environment_base (str): The base environment name.
new_environment_name (str): The new environment name.
Returns:
str: Path of the newly created customized file.
"""
new_file_path = file_path.replace(
environment_base, new_environment_name
)
shutil.copy2(file_path, new_file_path)
return new_file_path

@staticmethod
def replace_string_in_file(
file_path: str, old_string: str, new_string: str
) -> None:
"""
Replace a string in a file.
Args:
file_path (str): Path of the file.
old_string (str): String to be replaced.
new_string (str): Replacement string.
"""
with fileinput.FileInput(file_path, inplace=True) as file:
for line in file:
print(line.replace(old_string, new_string), end="")
48 changes: 48 additions & 0 deletions phalanx_customizer/file_operations/file_finder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
from typing import List


class FileFinder:
def __init__(self, repo_path: str):
"""
Initialize the FileFinder with the repository path.
Args:
repo_path (str): Path to the repository.
"""
self.repo_path = repo_path

def find_matching_files(self, environment_base: str) -> List[str]:
"""
Find all configuration files that match the environment_base name.
Args:
environment_base (str): The base environment name.
Returns:
List[str]: List of file paths.
"""
return self._find_subpath(
"applications", environment_base
) + self._find_subpath("environments", environment_base)

def _find_subpath(
self, directory: str, environment_base: str
) -> List[str]:
"""
Find files within a subpath of the repository that match the env base
name.
Args:
directory (str): Subdirectory to search within.
environment_base (str): The base environment name.
Returns:
List[str]: List of file paths.
"""
files = []
for root, _, filenames in os.walk(f"{self.repo_path}/{directory}"):
for filename in filenames:
if environment_base in filename:
files.append(os.path.join(root, filename))
return files
31 changes: 31 additions & 0 deletions phalanx_customizer/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import argparse
from phalanx_customizer.core.environment_customizer import (
EnvironmentCustomizer,
)


def main():
parser = argparse.ArgumentParser(
description="Phalanx Environment Customizer"
)
parser.add_argument(
"phalanx_repo_path", help="Path to the Phalanx repository"
)
parser.add_argument(
"base_env_yaml",
help="Path to the YAML file containing base environment configuration",
)
parser.add_argument(
"new_env_yaml",
help="Path to the YAML file containing new environment configuration",
)
args = parser.parse_args()

customizer = EnvironmentCustomizer(args.phalanx_repo_path)
customizer.create_environment_from_yaml(
args.base_env_yaml, args.new_env_yaml
)


if __name__ == "__main__":
main()
Empty file.
Loading

0 comments on commit b484f96

Please sign in to comment.