Skip to content

Commit

Permalink
feat: Move non-secret information into prd_config.yaml file (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
rehoumir authored Aug 19, 2024
1 parent 98c8035 commit c05281d
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 42 deletions.
2 changes: 2 additions & 0 deletions project_rossum_deploy/commands/download/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
should_write_object,
)
from project_rossum_deploy.commands.download.hooks import download_hooks
from project_rossum_deploy.common.migrate_config import migrate_config
from project_rossum_deploy.common.client import create_and_validate_client
from project_rossum_deploy.common.mapping import (
create_update_mapping,
Expand Down Expand Up @@ -73,6 +74,7 @@
async def download_project_wrapper(
destination: str, commit: bool = False, message: str = "", all: bool = False
):
await migrate_config()
await download_project(
destination=destination, commit_message=message, commit=commit, download_all=all
)
Expand Down
16 changes: 10 additions & 6 deletions project_rossum_deploy/commands/download/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,17 @@ async def check_schema_formula_fields_existence(remote_object: dict, path: Path)

async for formula_path in formula_directory_path.iterdir():
if formula_path.stem not in formula_field_ids:
print(
Panel(
f"Deleting a local formula field code file that no longer exists in Rossum: {path}",
style="yellow",
try:
os.remove(formula_path)
print(
Panel(
f"Deleted a local formula field code file that no longer exists in Rossum: {formula_path}",
style="yellow",
)
)
)
os.remove(formula_path)
# Permissions errors (e.g., hidden __pycache__ dirs could make the operation fail)
except Exception:
...


async def remove_local_nonexistent_object(
Expand Down
54 changes: 32 additions & 22 deletions project_rossum_deploy/commands/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,41 @@
The user is then expected to provide .env credentials and download Rossum objects.
""",
)
@click.argument(
"name",
default=".",
)
def init_project(name):
if os.path.exists(name) and name != ".":
print(Panel(f'Directory "{name}" already exists, skipping.'))
return

credentials_path = name + "/credentials.json"
if os.path.exists(credentials_path):
print(Panel(f'"{credentials_path}" already exists, skipping.'))
return

@click.argument("name", default=".", type=click.Path(path_type=Path))
def init_project(name: Path):
if not os.path.exists(name) and name != ".":
os.mkdir(name)

os.chdir(name)
subprocess.run(["git", "init"])
os.chdir("..")
# (re)initialize GIT
subprocess.run(["git", "init", name])

git_ignore_path = name / ".gitignore"
credentials_ignore_line = "\ncredentials.json"

git_ignore_contents = git_ignore_path.read_text()

if credentials_ignore_line not in git_ignore_contents:
with open(name + "/.gitignore", "a") as wf:
wf.write(credentials_ignore_line)

credentials_path = name / "credentials.json"
copy_dummy_credentials_file(credentials_path)

config_path = name / "prd_config.yaml"
copy_dummy_config_file(config_path)

print(Panel(f'Initialized a new PRD directory in "{name}" .'))


with open(name + "/.gitignore", "a") as wf:
wf.write("\ncredentials.json")
example_credentials_path = Path(__file__).parent.parent / "dummy_credentials.json"
def copy_dummy_credentials_file(destination: Path):
if not os.path.exists(destination):
example_credentials_path = (
Path(__file__).parent.parent / "dummy_credentials.json"
)
shutil.copyfile(example_credentials_path, destination)

shutil.copyfile(example_credentials_path, credentials_path)

print(Panel(f'Initialized a new directory in "{name}" .'))
def copy_dummy_config_file(destination: Path):
if not os.path.exists(destination):
example_config_path = Path(__file__).parent.parent / "dummy_config.yaml"
shutil.copyfile(example_config_path, destination)
2 changes: 2 additions & 0 deletions project_rossum_deploy/commands/migrate/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
download_project,
)
from project_rossum_deploy.commands.migrate.organization import migrate_organization
from project_rossum_deploy.common.migrate_config import migrate_config
from project_rossum_deploy.common.attribute_override import (
override_migrated_objects_attributes,
validate_override_migrated_objects_attributes,
Expand Down Expand Up @@ -77,6 +78,7 @@
async def migrate_project_wrapper(
plan_only: bool, force: bool, commit: bool, message: str
):
await migrate_config()
await migrate_project(
plan_only=plan_only, force=force, commit=commit, commit_message=message
)
Expand Down
2 changes: 2 additions & 0 deletions project_rossum_deploy/commands/purge/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rossum_api import ElisAPIClient

from project_rossum_deploy.commands.download.download import download_project
from project_rossum_deploy.common.migrate_config import migrate_config
from project_rossum_deploy.commands.migrate_mapping import migrate_mapping
from project_rossum_deploy.commands.purge.delete_objects import (
delete_all_objects_with_ids,
Expand Down Expand Up @@ -36,6 +37,7 @@
)
@coro
async def purge_project_wrapper(destination):
await migrate_config()
# To be able to run the command progammatically without the CLI decorators
await purge_project(
destination=destination,
Expand Down
2 changes: 2 additions & 0 deletions project_rossum_deploy/commands/upload/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from rossum_api import ElisAPIClient

from project_rossum_deploy.commands.download.download import download_project
from project_rossum_deploy.common.migrate_config import migrate_config
from project_rossum_deploy.commands.upload.dependencies import (
evaluate_create_dependencies,
merge_formula_changes,
Expand Down Expand Up @@ -82,6 +83,7 @@
async def upload_project_wrapper(
destination, all, force, indexed_only, commit, message
):
await migrate_config()
# To be able to run the command progammatically without the CLI decorators
await upload_project(
destination=destination,
Expand Down
25 changes: 25 additions & 0 deletions project_rossum_deploy/common/migrate_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from anyio import Path

from project_rossum_deploy.common.read_write import read_json, write_yaml
from project_rossum_deploy.utils.consts import settings


async def migrate_config():
config_path = Path("./") / settings.CONFIG_FILENAME

if await config_path.exists():
return

credentials_path = Path("./") / settings.CREDENTIALS_FILENAME
if not await credentials_path.exists():
return

credentials = await read_json(credentials_path)

config = {
"source_api_base": credentials.get("source", {}).get("api_base", ""),
"use_same_org_as_target": credentials.get("use_same_org_as_target", True),
"target_api_base": credentials.get("target", {}).get("api_base", ""),
}

await write_yaml(config_path, config)
2 changes: 2 additions & 0 deletions project_rossum_deploy/dummy_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source_api_base: ""
use_same_org_as_target: true
3 changes: 0 additions & 3 deletions project_rossum_deploy/dummy_credentials.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
{
"source": {
"api_base": "...",
"username": "...",
"password": "..."
},
"use_same_org_as_target": true,
"target": {
"api_base": "...",
"username": "...",
"password": "..."
}
Expand Down
30 changes: 19 additions & 11 deletions project_rossum_deploy/utils/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from rich.console import Console
from rich.panel import Panel
from rossum_api.api_client import Resource
import yaml


logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -81,9 +82,18 @@ def __init__(self):
f"{self.CREDENTIALS_FILENAME} is not a valid dictionary."
)

self.SOURCE_API_BASE = credentials.get(self.SOURCE_DIRNAME, {}).get(
"api_base", ""
)
config_path = Path("./") / self.CONFIG_FILENAME
if not config_path.exists():
return
config = yaml.safe_load(config_path.open("r"))

if not isinstance(config, dict):
raise click.ClickException(
f"{self.CONFIG_FILENAME} is not a valid dictionary."
)

self.SOURCE_API_BASE = config.get("source_api_base")

self.SOURCE_USERNAME = credentials.get(self.SOURCE_DIRNAME, {}).get(
"username", None
)
Expand All @@ -94,17 +104,14 @@ def __init__(self):
"token", None
)

if not credentials.get("use_same_org_as_target", False):
if not config.get("use_same_org_as_target", False):
self.IS_PROJECT_IN_SAME_ORG = False
if "target" not in credentials or not credentials.get(
self.TARGET_DIRNAME, {}
).get("api_base", ""):
if "target" not in credentials:
raise click.ClickException(
'Missing target credentials. If you are targetting the same org, set "use_same_org_as_target": true.'
f'Missing target credentials. If you are targetting the same org, set "use_same_org_as_target": true in {self.CONFIG_FILENAME}.'
)
self.TARGET_API_BASE = credentials.get(self.TARGET_DIRNAME, {}).get(
"api_base", ""
)
self.TARGET_API_BASE = config.get("target_api_base")

self.TARGET_USERNAME = credentials.get(self.TARGET_DIRNAME, {}).get(
"username", None
)
Expand All @@ -126,6 +133,7 @@ def __init__(self):
SOURCE_PASSWORD: str = ""

MAPPING_FILENAME: str = "mapping.yaml"
CONFIG_FILENAME: str = "prd_config.yaml"
CREDENTIALS_FILENAME: str = "credentials.json"
MAPPING_KEYS_ORDER: list = ["comment", "id", "name", "ignore", "targets"]

Expand Down

0 comments on commit c05281d

Please sign in to comment.