Skip to content

Commit 54d5f52

Browse files
committed
A couple of basic commands implemented
Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
1 parent 13b45ae commit 54d5f52

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

cfengine_cli/commands.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
from cfengine_cli.shell import user_command
2+
from cfengine_cli.paths import bin
3+
4+
15
def run() -> int:
2-
print("RUN")
6+
user_command(f"{bin('cf-agent')} -KIf update.cf && {bin('cf-agent')} -KI")
37
return 0
48

59

6-
def update() -> int:
7-
print("UPDATE")
10+
def report() -> int:
11+
user_command(f"{bin('cf-agent')} -KIf update.cf && {bin('cf-agent')} -KI")
12+
user_command(f"{bin('cf-hub')} --query rebase -H 127.0.0.1")
13+
user_command(f"{bin('cf-hub')} --query delta -H 127.0.0.1")
814
return 0
915

1016

1117
def help() -> int:
12-
print("HELP")
18+
print("Example usage:")
19+
print("cfengine run")
1320
return 0

cfengine_cli/main.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ def _get_arg_parser():
4242
subp.add_parser("help", help="Print help information")
4343

4444
subp.add_parser(
45-
"run", help="Run the CFEngine agent, evaluating and enforcing policy"
45+
"run", help="Run the CFEngine agent, fetching, evaluating, and enforcing policy"
4646
)
4747

4848
subp.add_parser(
49-
"update", help="Update the policy, downloading it from the policy server"
49+
"report",
50+
help="Run the agent and hub commands necessary to get new reporting data",
5051
)
5152

5253
return ap
@@ -59,12 +60,12 @@ def get_args():
5960

6061

6162
def run_command_with_args(command, _) -> int:
62-
if command == "info":
63-
return commands.info()
6463
if command == "run":
6564
return commands.run()
66-
if command == "update":
67-
return commands.update()
65+
if command == "report":
66+
return commands.report()
67+
if command == "help":
68+
return commands.help()
6869
raise UserError("Unknown command: '{}'".format(command))
6970

7071

cfengine_cli/paths.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1+
import re
12
import os
2-
from cfengine_cli.utils import user_error
3-
4-
5-
def path_append(dir, subdir):
6-
dir = os.path.abspath(os.path.expanduser(dir))
7-
return dir if not subdir else os.path.join(dir, subdir)
3+
from cfengine_cli.utils import UserError
4+
from cf_remote.paths import path_append
85

96

107
def cfengine_dir(subdir=None):
11-
override_dir = os.getenv("CF_REMOTE_DIR")
8+
"""
9+
Returns the directory used by the Python tools for temporary files,
10+
global config, downloads, etc.
11+
12+
Defaults to ~/.cfengine/, but can be overridden via the CFENGINE_DIR
13+
environment variable.
14+
"""
15+
override_dir = os.getenv("CFENGINE_DIR")
1216

1317
if override_dir:
1418
override_dir = os.path.normpath(override_dir)
1519
parent = os.path.dirname(override_dir)
1620

1721
if not os.path.exists(parent):
18-
user_error(
22+
raise UserError(
1923
"'{}' doesn't exist. Make sure this path is correct and exists.".format(
2024
parent
2125
)
@@ -26,19 +30,15 @@ def cfengine_dir(subdir=None):
2630
return path_append("~/.cfengine/", subdir)
2731

2832

29-
def cf_remote_dir(subdir=None):
30-
return path_append(cfengine_dir("cf-remote"), subdir)
31-
32-
33-
def cf_remote_file(fname=None):
34-
return path_append(cfengine_dir("cf-remote"), fname)
35-
36-
37-
def cf_remote_packages_dir(subdir=None):
38-
return path_append(cf_remote_dir("packages"), subdir)
33+
def bin(component: str) -> str:
34+
"""
35+
Get the path to a binary for use in a command.
3936
37+
For example: "cf-agent" -> "/var/cfengine/bin/cf-agent"
4038
41-
CLOUD_CONFIG_FNAME = "cloud_config.json"
42-
CLOUD_CONFIG_FPATH = cf_remote_file(CLOUD_CONFIG_FNAME)
43-
CLOUD_STATE_FNAME = "cloud_state.json"
44-
CLOUD_STATE_FPATH = cf_remote_file(CLOUD_STATE_FNAME)
39+
# Warning: Don't use this function with untrusted input.
40+
"""
41+
# Stop things like semicolons, slashes, spaces, etc.
42+
# Only letters and dashes allowed currently.
43+
assert re.fullmatch(r"[-a-zA-Z]+", component)
44+
return f"/var/cfengine/bin/{component}"

cfengine_cli/shell.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
3+
4+
def user_command(cmd):
5+
"""Run a command which was requested by the user.
6+
7+
For example if the user typed cfengine run, we
8+
run the shell command `/var/cfengine/bin/cf-agent -KI`.
9+
10+
Be transparent about the command, printing it first,
11+
so the user has visibility of what is happening and
12+
can learn the underlying tools over time.
13+
14+
We might expand this in the future with more logging,
15+
confirmation prompts, etc.
16+
"""
17+
18+
print(f"Running command: '{cmd}'")
19+
os.system(cmd)
20+
21+
22+
def silent_command(cmd):
23+
"""Under-the-hood shell commands which the user does not
24+
need to know about.
25+
"""
26+
27+
os.system(cmd)

0 commit comments

Comments
 (0)