Skip to content

Commit 09d516f

Browse files
committed
Added --validate and updated README
Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent a226367 commit 09d516f

4 files changed

Lines changed: 51 additions & 21 deletions

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,33 @@ cfengine build
7777

7878
(This is equivalent to running `cfbs build`).
7979

80+
### Spawn and install cfengine from a config
81+
82+
**this feature is still in work in progress**
83+
84+
Given a yaml config:
85+
86+
```yaml
87+
templates:
88+
ubuntu:
89+
count: 1
90+
mode: spawn
91+
spawn:
92+
provider: vagrant
93+
vagrant:
94+
box: ubuntu/focal64
95+
96+
groups:
97+
myhub:
98+
role: hub
99+
source: ubuntu
100+
```
101+
102+
It up will spawn the necessary VMs and install cfengine using cf-remote
103+
```
104+
cfengine up config.yaml
105+
```
106+
80107
## Supported platforms and versions
81108

82109
This tool will only support a limited number of platforms, it is not intended to run everywhere CFEngine runs.

src/cfengine_cli/commands.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,18 @@ def profile(args) -> int:
152152
return 0
153153

154154

155-
def up(config) -> int:
155+
def up(args) -> int:
156156
content = None
157157
try:
158-
with open(config, "r") as f:
158+
with open(args.config, "r") as f:
159159
content = yaml.safe_load(f)
160160
except yaml.YAMLError:
161-
raise UserError("'%s' is not a valid yaml config" % config)
161+
raise UserError("'%s' is not a valid yaml config" % args.config)
162162
except FileNotFoundError:
163-
raise UserError("'%s' doesn't exist" % config)
163+
raise UserError("'%s' doesn't exist" % args.config)
164164

165-
print("Starting VMs...")
166165
validate_config(content)
166+
if args.validate:
167+
return 0
168+
print("Starting VMs...")
167169
return 0

src/cfengine_cli/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ def _get_arg_parser():
120120
up_parser.add_argument(
121121
"config", default="config.yaml", nargs="?", help="Path to yaml config"
122122
)
123+
up_parser.add_argument(
124+
"--validate", action="store_true", help="Validate the given config"
125+
)
123126
return ap
124127

125128

@@ -154,7 +157,7 @@ def run_command_with_args(args) -> int:
154157
if args.command == "profile":
155158
return commands.profile(args)
156159
if args.command == "up":
157-
return commands.up(args.config)
160+
return commands.up(args)
158161
raise UserError(f"Unknown command: '{args.command}'")
159162

160163

src/cfengine_cli/validate.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from cf_remote.paths import CLOUD_CONFIG_FPATH, CLOUD_STATE_FPATH
2-
from cf_remote.utils import read_json, CFRUserError, is_package_url
2+
from cf_remote.utils import read_json, is_package_url
33
from cf_remote.spawn import (
44
Providers,
55
get_cloud_driver,
@@ -10,6 +10,8 @@
1010
from cf_remote.cloud_data import aws_image_criteria
1111
from cf_remote.remote import Releases
1212

13+
from cfengine_cli.utils import UserError
14+
1315
import os
1416
import subprocess
1517

@@ -22,15 +24,15 @@ def validate_state_bootstrap(bootstrap):
2224

2325
# TODO: Change how to check this if cloud_state.json changes format
2426
if key in state and state[key].values()[1]["role"] != "hub":
25-
raise CFRUserError("Cannot bootstrap to an existing host that is not a hub")
27+
raise UserError("Cannot bootstrap to an existing host that is not a hub")
2628

2729

2830
def validate_package(package, remote_download=False):
2931
if package is None:
3032
return
3133

3234
if remote_download and not is_package_url(package):
33-
raise CFRUserError("Package '{}' is not a valid package URL")
35+
raise UserError("Package '{}' is not a valid package URL")
3436

3537

3638
def validate_version(version, edition):
@@ -39,7 +41,7 @@ def validate_version(version, edition):
3941
if version:
4042
release = releases.pick_version(version)
4143
if release is None:
42-
raise CFRUserError(
44+
raise UserError(
4345
"Could not find a release for version {}. The supported versions are {}".format(
4446
version, releases
4547
)
@@ -55,13 +57,13 @@ def validate_vagrant_box(box):
5557
]
5658

5759
if box not in box_list:
58-
raise CFRUserError("Box '{}' is not installed or doesn't exist".format(box))
60+
raise UserError("Box '{}' is not installed or doesn't exist".format(box))
5961

6062

6163
def validate_aws_image(platform):
6264
platform_name = platform.split("-")[0]
6365
if platform_name not in aws_image_criteria:
64-
raise CFRUserError(
66+
raise UserError(
6567
"Platform '%s' is not in our set of image criteria. (Available platforms: %s)"
6668
% (platform, ", ".join(aws_image_criteria.keys()))
6769
)
@@ -83,7 +85,7 @@ def validate_aws_credentials():
8385
creds_data = read_json(CLOUD_CONFIG_FPATH)
8486

8587
if not creds_data:
86-
raise CFRUserError("Cloud configuration not found at %s" % CLOUD_CONFIG_FPATH)
88+
raise UserError("Cloud configuration not found at %s" % CLOUD_CONFIG_FPATH)
8789
creds = None
8890
try:
8991
creds = _get_aws_creds_from_env() or AWSCredentials(
@@ -92,9 +94,7 @@ def validate_aws_credentials():
9294
creds_data["aws"].get("token", ""),
9395
)
9496
except KeyError:
95-
raise CFRUserError(
96-
"Incomplete AWS credential info"
97-
) # TODO: report missing keys
97+
raise UserError("Incomplete AWS credential info") # TODO: report missing keys
9898

9999
region = creds_data["aws"].get("region", "eu-west-1")
100100
sec_groups = creds_data["aws"]["security_groups"]
@@ -104,7 +104,7 @@ def validate_aws_credentials():
104104
try:
105105
get_cloud_driver(Providers.AWS, creds, region)
106106
except InvalidCredsError as error:
107-
raise CFRUserError(
107+
raise UserError(
108108
"Invalid credentials, check cloud_config.json (%s.)" % str(error)[1:-1]
109109
)
110110
return creds, region, sec_groups, key_pair
@@ -116,17 +116,15 @@ def validate_gcp_credentials():
116116
creds_data = read_json(CLOUD_CONFIG_FPATH)
117117

118118
if not creds_data:
119-
raise CFRUserError("Cloud configuration not found at %s" % CLOUD_CONFIG_FPATH)
119+
raise UserError("Cloud configuration not found at %s" % CLOUD_CONFIG_FPATH)
120120
try:
121121
creds = GCPCredentials(
122122
creds_data["gcp"]["project_id"],
123123
creds_data["gcp"]["service_account_id"],
124124
creds_data["gcp"]["key_path"],
125125
)
126126
except KeyError:
127-
raise CFRUserError(
128-
"Incomplete AWS credential info"
129-
) # TODO: report missing keys
127+
raise UserError("Incomplete AWS credential info") # TODO: report missing keys
130128

131129
region = creds_data["gcp"].get("region", "europe-west1-b")
132130

0 commit comments

Comments
 (0)