Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions src/cfengine_cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
format_policy_fin_fout,
)
from cfengine_cli.utils import UserError
from cfengine_cli.up import validate_config
from cfengine_cli.up import validate_config, up_do, resolve_templates
from cfbs.commands import build_command
from cf_remote.commands import deploy as deploy_command
from cf_remote.paths import cf_remote_dir
from pydantic import ValidationError


def _require_cfagent():
Expand Down Expand Up @@ -178,12 +180,47 @@ def up(args) -> int:
with open(args.config, "r") as f:
content = yaml.safe_load(f)
except yaml.YAMLError:
raise UserError("'%s' is not a valid yaml config" % args.config)
raise UserError("'%s' is not valid yaml" % args.config)
except FileNotFoundError:
raise UserError("'%s' doesn't exist" % args.config)

validate_config(content)
g_new = resolve_templates(content)
try:
new_config = validate_config(g_new)
except ValidationError as v:
msgs = []
for err in v.errors():
msgs.append(
f"{err['msg']}. Input '{err['input']}' at location '{err['loc']}'"
)
raise UserError("\n".join(msgs))

if args.validate:
return 0
print("Starting VMs...")

g_old = {}
try:
if not args.reset:
with open(os.path.join(cf_remote_dir(), "old_groups.yaml"), "r") as f:
g_old = yaml.safe_load(f)
else:
os.remove(os.path.join(cf_remote_dir(), "old_groups.yaml"))
except:
pass
if g_old != {}:
try:
validate_config(g_old)
except ValidationError as v:
msgs = []
for err in v.errors():
msgs.append(
f"{err['msg']}. Input '{err['input']}' at location '{err['loc']}'"
)
raise UserError("\n".join(msgs))

is_ok = up_do(g_old, g_new, new_config)

if is_ok:
with open(os.path.join(cf_remote_dir(), "old_groups.yaml"), "w") as f:
yaml.dump(g_new, f, default_flow_style=False, sort_keys=False)
return 0
3 changes: 3 additions & 0 deletions src/cfengine_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def _get_arg_parser():
up_parser.add_argument(
"--validate", action="store_true", help="Validate the given config"
)
up_parser.add_argument(
"--reset", action="store_true", help="Create a fresh new environment"
)
return ap


Expand Down
Loading