-
Notifications
You must be signed in to change notification settings - Fork 20
Implemented spawn config validation #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
victormlg
wants to merge
4
commits into
cfengine:master
Choose a base branch
from
victormlg:spawn-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,226 @@ | ||
| from pydantic import BaseModel, model_validator, ValidationError, Field | ||
| from typing import Union, Literal, Optional, List, Annotated | ||
| from functools import reduce | ||
|
|
||
| from cf_remote.utils import CFRUserError | ||
| from cf_remote import log | ||
|
|
||
| import cf_remote.validate as validate | ||
|
|
||
|
|
||
| # Forces pydantic to throw validation error if config contains unknown keys | ||
| class NoExtra(BaseModel, extra="forbid"): | ||
| pass | ||
|
|
||
|
|
||
| class Config(NoExtra): | ||
| pass | ||
|
|
||
|
|
||
| class AWSConfig(Config): | ||
| image: str | ||
| size: Literal["micro", "xlarge"] = "micro" | ||
|
|
||
| @model_validator(mode="after") | ||
| def check_aws_config(self): | ||
| validate.validate_aws_image(self.image) | ||
| return self | ||
|
|
||
|
|
||
| class VagrantConfig(Config): | ||
| box: str | ||
| memory: int = 512 | ||
| cpus: int = 1 | ||
| sync_folder: Optional[str] = None | ||
| provision: Optional[str] = None | ||
|
|
||
| @model_validator(mode="after") | ||
| def check_vagrant_config(self): | ||
| if self.memory < 512: | ||
| raise CFRUserError("Cannot allocate less than 512MB to a Vagrant VM") | ||
| if self.cpus < 1: | ||
| raise CFRUserError("Cannot use less than 1 cpu per Vagrant VM") | ||
|
|
||
| validate.validate_vagrant_box(self.box) | ||
|
|
||
| return self | ||
|
|
||
|
|
||
| class GCPConfig(Config): | ||
| image: str # There is no list of avalaible GCP platforms to validate against yet | ||
| network: Optional[str] = None | ||
| public_ip: bool = True | ||
| size: str = "n1-standard-1" | ||
|
|
||
|
|
||
| class AWSProvider(Config): | ||
| provider: Literal["aws"] | ||
| aws: AWSConfig | ||
|
|
||
| @model_validator(mode="after") | ||
| def check_aws_provider(self): | ||
| validate.validate_aws_credentials() | ||
| return self | ||
|
|
||
|
|
||
| class GCPProvider(Config): | ||
| provider: Literal["gcp"] | ||
| gcp: GCPConfig | ||
|
|
||
| @model_validator(mode="after") | ||
| def check_gcp_provider(self): | ||
| validate.validate_gcp_credentials() | ||
| return self | ||
|
|
||
|
|
||
| class VagrantProvider(Config): | ||
| provider: Literal["vagrant"] | ||
| vagrant: VagrantConfig | ||
|
|
||
|
|
||
| class SaveMode(Config): | ||
| mode: Literal["save"] | ||
| hosts: List[str] | ||
|
|
||
|
|
||
| class SpawnMode(Config): | ||
| mode: Literal["spawn"] | ||
| # "Field" forces pydantic to report errors on the branch defined by the field "provider" | ||
| spawn: Annotated[ | ||
| Union[VagrantProvider, AWSProvider, GCPProvider], | ||
| Field(discriminator="provider"), | ||
| ] | ||
| count: int | ||
|
|
||
| @model_validator(mode="after") | ||
| def check_spawn_config(self): | ||
| if self.count < 1: | ||
| raise CFRUserError("Cannot spawn less than 1 instance") | ||
| return self | ||
|
|
||
|
|
||
| class CFEngineConfig(Config): | ||
| version: Optional[str] = None | ||
| bootstrap: Optional[str] = None | ||
| edition: Literal["community", "enterprise"] = "enterprise" | ||
| remote_download: bool = False | ||
| hub_package: Optional[str] = None | ||
| client_package: Optional[str] = None | ||
| package: Optional[str] = None | ||
| demo: bool = False | ||
|
|
||
| @model_validator(mode="after") | ||
| def check_cfengine_config(self): | ||
| packages = [self.package, self.hub_package, self.client_package] | ||
| for p in packages: | ||
| validate.validate_package(p, self.remote_download) | ||
|
|
||
| if self.version and any(packages): | ||
| log.warning("Specifying package overrides cfengine version") | ||
|
|
||
| validate.validate_version(self.version, self.edition) | ||
| validate.validate_state_bootstrap(self.bootstrap) | ||
|
|
||
| return self | ||
|
|
||
|
|
||
| class GroupConfig(Config): | ||
| role: Literal["client", "hub"] | ||
| # "Field" forces pydantic to report errors on the branch defined by the field "provider" | ||
| source: Annotated[Union[SaveMode, SpawnMode], Field(discriminator="mode")] | ||
| cfengine: Optional[CFEngineConfig] = None | ||
| scripts: Optional[List[str]] = None | ||
|
|
||
| @model_validator(mode="after") | ||
| def check_group_config(self): | ||
| if ( | ||
| self.role == "hub" | ||
| and self.source.mode == "spawn" | ||
| and self.source.count != 1 | ||
| ): | ||
| raise CFRUserError("A hub can only have one host") | ||
|
|
||
| return self | ||
|
|
||
|
|
||
| def rgetattr(obj, attr, *args): | ||
| def _getattr(obj, attr): | ||
| return getattr(obj, attr, *args) | ||
|
|
||
| return reduce(_getattr, [obj] + attr.split(".")) | ||
|
|
||
|
|
||
| class Group: | ||
| """ | ||
| All group-specific data: | ||
| - Vagrantfile | ||
| Config that declares it: | ||
| - provider, count, cfengine version, role, ... | ||
| """ | ||
|
|
||
| def __init__(self, config: GroupConfig): | ||
| self.config = config | ||
| self.hosts = [] | ||
|
|
||
|
|
||
| class Host: | ||
| """ | ||
| All host-specific data: | ||
| - user, ip, ssh config, OS, uuid, ... | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| pass | ||
|
|
||
|
|
||
| def _resolve_templates(parent, templates): | ||
| if not parent: | ||
| return | ||
| if isinstance(parent, dict): | ||
| for key, value in parent.items(): | ||
| if isinstance(value, str) and value in templates: | ||
| parent[key] = templates[value] | ||
| else: | ||
| _resolve_templates(value, templates) | ||
| if isinstance(parent, list): | ||
| for value in parent: | ||
| _resolve_templates(value, templates) | ||
|
|
||
|
|
||
| def validate_config(content): | ||
| if not content: | ||
| raise CFRUserError("Empty spawn config") | ||
|
|
||
| if "groups" not in content: | ||
| raise CFRUserError("Missing 'groups' key in spawn config") | ||
|
|
||
| groups = content["groups"] | ||
| templates = content.get("templates") | ||
| if templates: | ||
| _resolve_templates(groups, templates) | ||
|
|
||
| if not isinstance(groups, list): | ||
| groups = [groups] | ||
|
|
||
| state = {} | ||
| try: | ||
| for g in groups: | ||
| if len(g) != 1: | ||
| raise CFRUserError( | ||
| "Too many keys in group definition: {}".format( | ||
| ", ".join(list(g.keys())) | ||
| ) | ||
| ) | ||
|
|
||
| for k, v in g.items(): | ||
| state[k] = Group(GroupConfig(**v)) | ||
|
|
||
| except ValidationError as v: | ||
| msgs = [] | ||
| for err in v.errors(): | ||
| msgs.append( | ||
| "{}. Input '{}' at location '{}'".format( | ||
| err["msg"], err["input"], err["loc"] | ||
| ) | ||
| ) | ||
| raise CFRUserError("\n".join(msgs)) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you are adding 2 dependencies, to be installed in GH Actions only. These changes would cause ImportErrors for anyone who installs and runs the new version of cf-remote.
To add a dependency, you need to update setup.py and requirements.txt.
With that said, and also considering the fact that your code does not work on older Pythons, it might be better if we move this new functionality to CFEngine CLI.