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