Skip to content

Commit 445bbbb

Browse files
committed
Added idempotent spawning from config
Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 0f859bf commit 445bbbb

7 files changed

Lines changed: 352 additions & 142 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():
@@ -178,12 +180,47 @@ def up(args) -> int:
178180
with open(args.config, "r") as f:
179181
content = yaml.safe_load(f)
180182
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)
182184
except FileNotFoundError:
183185
raise UserError("'%s' doesn't exist" % args.config)
184186

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+
186198
if args.validate:
187199
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)
189226
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
return ap
130133

131134

0 commit comments

Comments
 (0)