|
15 | 15 | format_policy_fin_fout, |
16 | 16 | ) |
17 | 17 | from cfengine_cli.utils import UserError |
18 | | -from cfengine_cli.up import validate_config |
| 18 | +from cfengine_cli.up import validate_config, up_do, resolve_templates |
19 | 19 | from cfbs.commands import build_command |
20 | 20 | from cf_remote.commands import deploy as deploy_command |
| 21 | +from cf_remote.paths import cf_remote_dir |
| 22 | +from pydantic import ValidationError |
21 | 23 |
|
22 | 24 |
|
23 | 25 | def _require_cfagent(): |
@@ -178,12 +180,47 @@ def up(args) -> int: |
178 | 180 | with open(args.config, "r") as f: |
179 | 181 | content = yaml.safe_load(f) |
180 | 182 | except yaml.YAMLError: |
181 | | - raise UserError("'%s' is not a valid yaml config" % args.config) |
| 183 | + raise UserError("'%s' is not valid yaml" % args.config) |
182 | 184 | except FileNotFoundError: |
183 | 185 | raise UserError("'%s' doesn't exist" % args.config) |
184 | 186 |
|
185 | | - validate_config(content) |
| 187 | + g_new = resolve_templates(content) |
| 188 | + try: |
| 189 | + new_config = validate_config(g_new) |
| 190 | + except ValidationError as v: |
| 191 | + msgs = [] |
| 192 | + for err in v.errors(): |
| 193 | + msgs.append( |
| 194 | + f"{err['msg']}. Input '{err['input']}' at location '{err['loc']}'" |
| 195 | + ) |
| 196 | + raise UserError("\n".join(msgs)) |
| 197 | + |
186 | 198 | if args.validate: |
187 | 199 | return 0 |
188 | | - print("Starting VMs...") |
| 200 | + |
| 201 | + g_old = {} |
| 202 | + try: |
| 203 | + if not args.reset: |
| 204 | + with open(os.path.join(cf_remote_dir(), "old_groups.yaml"), "r") as f: |
| 205 | + g_old = yaml.safe_load(f) |
| 206 | + else: |
| 207 | + os.remove(os.path.join(cf_remote_dir(), "old_groups.yaml")) |
| 208 | + except: |
| 209 | + pass |
| 210 | + if g_old != {}: |
| 211 | + try: |
| 212 | + validate_config(g_old) |
| 213 | + except ValidationError as v: |
| 214 | + msgs = [] |
| 215 | + for err in v.errors(): |
| 216 | + msgs.append( |
| 217 | + f"{err['msg']}. Input '{err['input']}' at location '{err['loc']}'" |
| 218 | + ) |
| 219 | + raise UserError("\n".join(msgs)) |
| 220 | + |
| 221 | + is_ok = up_do(g_old, g_new, new_config) |
| 222 | + |
| 223 | + if is_ok: |
| 224 | + with open(os.path.join(cf_remote_dir(), "old_groups.yaml"), "w") as f: |
| 225 | + yaml.dump(g_new, f, default_flow_style=False, sort_keys=False) |
189 | 226 | return 0 |
0 commit comments