-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break out into smaller modules, make a bit more readable
- Loading branch information
Showing
20 changed files
with
419 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
from .phalanx_customizer import Environment, EnvironmentCustomizer | ||
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.