Skip to content

Commit f665354

Browse files
committed
Added idempotent spawning from config
Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 72a8bac commit f665354

7 files changed

Lines changed: 397 additions & 141 deletions

File tree

src/cfengine_cli/commands.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
format_policy_fin_fout,
1616
)
1717
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
1919
from cfbs.commands import build_command
2020
from cf_remote.commands import deploy as deploy_command
21+
from cf_remote.paths import cf_remote_dir
22+
from pydantic import ValidationError
2123

2224

2325
def _require_cfagent():
@@ -179,12 +181,47 @@ def up(args) -> int:
179181
with open(args.config, "r") as f:
180182
content = yaml.safe_load(f)
181183
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)
183185
except FileNotFoundError:
184186
raise UserError("'%s' doesn't exist" % args.config)
185187

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+
187199
if args.validate:
188200
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)
190227
return 0

src/cfengine_cli/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def _get_arg_parser():
126126
up_parser.add_argument(
127127
"--validate", action="store_true", help="Validate the given config"
128128
)
129+
up_parser.add_argument(
130+
"--reset", action="store_true", help="Create a fresh new environment"
131+
)
129132
parser = dev_subparsers.add_parser(
130133
"generate-changelog",
131134
description="""Changelog generator for CFEngine repositories.

0 commit comments

Comments
 (0)