|
| 1 | +from pydantic import BaseModel, model_validator, ValidationError, Field |
| 2 | +from typing import Union, Literal, Optional, List, Annotated |
| 3 | +from functools import reduce |
| 4 | +from cf_remote import log |
| 5 | + |
| 6 | +import cfengine_cli.validate as validate |
| 7 | +from cfengine_cli.utils import UserError |
| 8 | + |
| 9 | + |
| 10 | +# Forces pydantic to throw validation error if config contains unknown keys |
| 11 | +class NoExtra(BaseModel, extra="forbid"): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +class Config(NoExtra): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +class AWSConfig(Config): |
| 20 | + image: str |
| 21 | + size: Literal["micro", "xlarge"] = "micro" |
| 22 | + |
| 23 | + @model_validator(mode="after") |
| 24 | + def check_aws_config(self): |
| 25 | + validate.validate_aws_image(self.image) |
| 26 | + return self |
| 27 | + |
| 28 | + |
| 29 | +class VagrantConfig(Config): |
| 30 | + box: str |
| 31 | + memory: int = 512 |
| 32 | + cpus: int = 1 |
| 33 | + sync_folder: Optional[str] = None |
| 34 | + provision: Optional[str] = None |
| 35 | + |
| 36 | + @model_validator(mode="after") |
| 37 | + def check_vagrant_config(self): |
| 38 | + if self.memory < 512: |
| 39 | + raise UserError("Cannot allocate less than 512MB to a Vagrant VM") |
| 40 | + if self.cpus < 1: |
| 41 | + raise UserError("Cannot use less than 1 cpu per Vagrant VM") |
| 42 | + |
| 43 | + validate.validate_vagrant_box(self.box) |
| 44 | + |
| 45 | + return self |
| 46 | + |
| 47 | + |
| 48 | +class GCPConfig(Config): |
| 49 | + image: str # There is no list of avalaible GCP platforms to validate against yet |
| 50 | + network: Optional[str] = None |
| 51 | + public_ip: bool = True |
| 52 | + size: str = "n1-standard-1" |
| 53 | + |
| 54 | + |
| 55 | +class AWSProvider(Config): |
| 56 | + provider: Literal["aws"] |
| 57 | + aws: AWSConfig |
| 58 | + |
| 59 | + @model_validator(mode="after") |
| 60 | + def check_aws_provider(self): |
| 61 | + validate.validate_aws_credentials() |
| 62 | + return self |
| 63 | + |
| 64 | + |
| 65 | +class GCPProvider(Config): |
| 66 | + provider: Literal["gcp"] |
| 67 | + gcp: GCPConfig |
| 68 | + |
| 69 | + @model_validator(mode="after") |
| 70 | + def check_gcp_provider(self): |
| 71 | + validate.validate_gcp_credentials() |
| 72 | + return self |
| 73 | + |
| 74 | + |
| 75 | +class VagrantProvider(Config): |
| 76 | + provider: Literal["vagrant"] |
| 77 | + vagrant: VagrantConfig |
| 78 | + |
| 79 | + |
| 80 | +class SaveMode(Config): |
| 81 | + mode: Literal["save"] |
| 82 | + hosts: List[str] |
| 83 | + |
| 84 | + |
| 85 | +class SpawnMode(Config): |
| 86 | + mode: Literal["spawn"] |
| 87 | + # "Field" forces pydantic to report errors on the branch defined by the field "provider" |
| 88 | + spawn: Annotated[ |
| 89 | + Union[VagrantProvider, AWSProvider, GCPProvider], |
| 90 | + Field(discriminator="provider"), |
| 91 | + ] |
| 92 | + count: int |
| 93 | + |
| 94 | + @model_validator(mode="after") |
| 95 | + def check_spawn_config(self): |
| 96 | + if self.count < 1: |
| 97 | + raise UserError("Cannot spawn less than 1 instance") |
| 98 | + return self |
| 99 | + |
| 100 | + |
| 101 | +class CFEngineConfig(Config): |
| 102 | + version: Optional[str] = None |
| 103 | + bootstrap: Optional[str] = None |
| 104 | + edition: Literal["community", "enterprise"] = "enterprise" |
| 105 | + remote_download: bool = False |
| 106 | + hub_package: Optional[str] = None |
| 107 | + client_package: Optional[str] = None |
| 108 | + package: Optional[str] = None |
| 109 | + demo: bool = False |
| 110 | + |
| 111 | + @model_validator(mode="after") |
| 112 | + def check_cfengine_config(self): |
| 113 | + packages = [self.package, self.hub_package, self.client_package] |
| 114 | + for p in packages: |
| 115 | + validate.validate_package(p, self.remote_download) |
| 116 | + |
| 117 | + if self.version and any(packages): |
| 118 | + log.warning("Specifying package overrides cfengine version") |
| 119 | + |
| 120 | + validate.validate_version(self.version, self.edition) |
| 121 | + validate.validate_state_bootstrap(self.bootstrap) |
| 122 | + |
| 123 | + return self |
| 124 | + |
| 125 | + |
| 126 | +class GroupConfig(Config): |
| 127 | + role: Literal["client", "hub"] |
| 128 | + # "Field" forces pydantic to report errors on the branch defined by the field "provider" |
| 129 | + source: Annotated[Union[SaveMode, SpawnMode], Field(discriminator="mode")] |
| 130 | + cfengine: Optional[CFEngineConfig] = None |
| 131 | + scripts: Optional[List[str]] = None |
| 132 | + |
| 133 | + @model_validator(mode="after") |
| 134 | + def check_group_config(self): |
| 135 | + if ( |
| 136 | + self.role == "hub" |
| 137 | + and self.source.mode == "spawn" |
| 138 | + and self.source.count != 1 |
| 139 | + ): |
| 140 | + raise UserError("A hub can only have one host") |
| 141 | + |
| 142 | + return self |
| 143 | + |
| 144 | + |
| 145 | +def rgetattr(obj, attr, *args): |
| 146 | + def _getattr(obj, attr): |
| 147 | + return getattr(obj, attr, *args) |
| 148 | + |
| 149 | + return reduce(_getattr, [obj] + attr.split(".")) |
| 150 | + |
| 151 | + |
| 152 | +class Group: |
| 153 | + """ |
| 154 | + All group-specific data: |
| 155 | + - Vagrantfile |
| 156 | + Config that declares it: |
| 157 | + - provider, count, cfengine version, role, ... |
| 158 | + """ |
| 159 | + |
| 160 | + def __init__(self, config: GroupConfig): |
| 161 | + self.config = config |
| 162 | + self.hosts = [] |
| 163 | + |
| 164 | + |
| 165 | +class Host: |
| 166 | + """ |
| 167 | + All host-specific data: |
| 168 | + - user, ip, ssh config, OS, uuid, ... |
| 169 | + """ |
| 170 | + |
| 171 | + def __init__(self): |
| 172 | + pass |
| 173 | + |
| 174 | + |
| 175 | +def _resolve_templates(parent, templates): |
| 176 | + if not parent: |
| 177 | + return |
| 178 | + if isinstance(parent, dict): |
| 179 | + for key, value in parent.items(): |
| 180 | + if isinstance(value, str) and value in templates: |
| 181 | + parent[key] = templates[value] |
| 182 | + else: |
| 183 | + _resolve_templates(value, templates) |
| 184 | + if isinstance(parent, list): |
| 185 | + for value in parent: |
| 186 | + _resolve_templates(value, templates) |
| 187 | + |
| 188 | + |
| 189 | +def validate_config(content): |
| 190 | + if not content: |
| 191 | + raise UserError("Empty spawn config") |
| 192 | + |
| 193 | + if "groups" not in content: |
| 194 | + raise UserError("Missing 'groups' key in spawn config") |
| 195 | + |
| 196 | + groups = content["groups"] |
| 197 | + templates = content.get("templates") |
| 198 | + if templates: |
| 199 | + _resolve_templates(groups, templates) |
| 200 | + |
| 201 | + if not isinstance(groups, list): |
| 202 | + groups = [groups] |
| 203 | + |
| 204 | + state = {} |
| 205 | + try: |
| 206 | + for g in groups: |
| 207 | + if len(g) != 1: |
| 208 | + raise UserError( |
| 209 | + "Too many keys in group definition: {}".format( |
| 210 | + ", ".join(list(g.keys())) |
| 211 | + ) |
| 212 | + ) |
| 213 | + |
| 214 | + for k, v in g.items(): |
| 215 | + state[k] = Group(GroupConfig(**v)) |
| 216 | + |
| 217 | + except ValidationError as v: |
| 218 | + msgs = [] |
| 219 | + for err in v.errors(): |
| 220 | + msgs.append( |
| 221 | + "{}. Input '{}' at location '{}'".format( |
| 222 | + err["msg"], err["input"], err["loc"] |
| 223 | + ) |
| 224 | + ) |
| 225 | + raise UserError("\n".join(msgs)) |
0 commit comments