Skip to content

Commit e008ab0

Browse files
Reworked the defaults for what file to use with cfengine run
Now default is use the remote if it exists in input, otherwise transfer the local file + rm it after Ticket: ENT-14304 Signed-off-by: Simon Halvorsen <simon.halvorsen@northern.tech>
1 parent 0e2f53e commit e008ab0

1 file changed

Lines changed: 47 additions & 37 deletions

File tree

src/cfengine_cli/cfengine_wrapper/cfengine_commands.py

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,65 +68,63 @@ def _remote_home_dir(location: str) -> str:
6868
return "/root" if user == "root" else f"/home/{user}"
6969

7070

71-
def _resolve_file_remote(location: str, command: str, file_arg: str) -> str:
72-
local_exists = os.path.isfile(file_arg)
73-
71+
def _remote_path_for(location: str, file_arg: str) -> str:
7472
local_home = os.path.expanduser("~")
7573
remote_home = _remote_home_dir(location)
7674

7775
if file_arg.startswith(local_home + os.sep):
78-
# e.g. "~/update.cf", shell expands to /home/{user}/update.cf
7976
rel = file_arg[len(local_home) + 1 :]
80-
remote_path = f"{remote_home}/{rel}"
81-
elif file_arg.startswith("/"):
82-
remote_path = file_arg
83-
else:
84-
remote_path = f"{_DEFAULT_CFENGINE_INPUTS_DIR}/{file_arg}"
77+
return f"{remote_home}/{rel}"
78+
if file_arg.startswith("/"):
79+
return file_arg
80+
return f"{_DEFAULT_CFENGINE_INPUTS_DIR}/{file_arg}"
8581

86-
remote_exists = _remote_path_exists(location, remote_path)
8782

88-
if not local_exists and not remote_exists:
89-
raise UserError(
90-
f"Could not find '{file_arg}' locally or on {location} (checked {remote_path})."
91-
)
83+
def _resolve_file_remote(
84+
location: str, command: str, file_arg: str
85+
) -> tuple[str, str | None]:
9286

93-
if local_exists and remote_exists:
94-
choice = prompt_two_options(
95-
f"'{file_arg}' exists both locally and on {location} (at {remote_path}).\nUse the:",
96-
"local copy (uploads it, possibly overwriting the remote copy)",
97-
f"copy already on {location}, unchanged",
98-
)
99-
elif local_exists:
100-
choice = "a"
101-
else:
102-
choice = "b"
87+
remote_path = _remote_path_for(location, file_arg)
88+
exists_in_inputs = _remote_path_exists(location, remote_path)
10389

104-
if choice == "b":
105-
return _replace_file_token(command, file_arg, remote_path)
90+
if not exists_in_inputs and os.path.isfile(file_arg):
91+
remote_home = _remote_home_dir(location)
92+
uploaded_path = f"{remote_home}/{os.path.basename(file_arg)}"
93+
logging.warning(f"Uploading {file_arg} to {location}:{uploaded_path}")
94+
transfer_file(location, file_arg)
95+
return _replace_file_token(command, file_arg, uploaded_path), uploaded_path
10696

107-
uploaded_path = f"{remote_home}/{os.path.basename(file_arg)}"
108-
logging.warning(f"Uploading {file_arg} to {location}:{uploaded_path}")
109-
transfer_file(location, file_arg)
110-
return _replace_file_token(command, file_arg, uploaded_path)
97+
if not exists_in_inputs:
98+
raise UserError(
99+
f"Could not find '{file_arg}' locally or on {location} (checked {remote_path})."
100+
)
101+
return _replace_file_token(command, file_arg, remote_path), None
111102

112103

113-
def _resolve_command_for_agent(agent: Executable, command: str) -> str:
104+
def _resolve_command_for_agent(
105+
agent: Executable, command: str
106+
) -> tuple[str, str | None]:
114107
if agent.name != "cf-agent":
115-
return command
108+
return command, None
116109

117110
command = ensure_default_agent_flags(command)
118111
file_arg = extract_agent_file(command)
119112
if not file_arg:
120-
return command
113+
return command, None
121114

122115
if agent.is_local:
123116
if "/" in file_arg:
124-
return command # no ambiguity for a local target
125-
return _resolve_bare_filename_local(command, file_arg)
117+
return command, None # no ambiguity for a local target
118+
return _resolve_bare_filename_local(command, file_arg), None
126119

127120
return _resolve_file_remote(agent.location, command, file_arg)
128121

129122

123+
def _remove_remote_file(location: str, path: str) -> None:
124+
if run_command(location, f"rm -f {path}", sudo=False) is None:
125+
logging.warning(f"Failed to remove uploaded file '{path}' from {location}")
126+
127+
130128
# ---------------------------------------------------------------------------
131129
# Commands
132130
# ---------------------------------------------------------------------------
@@ -206,8 +204,20 @@ def run(*args, target: str | None = None) -> int:
206204
agent = require_executable("cf-agent", target)
207205
if not args:
208206
return agent.run("-KIf update.cf", "-KI")
209-
resolved = [_resolve_command_for_agent(agent, command) for command in args]
210-
return agent.run(*resolved)
207+
208+
resolved = []
209+
cleanup_paths = []
210+
for command in args:
211+
resolved_command, cleanup_path = _resolve_command_for_agent(agent, command)
212+
resolved.append(resolved_command)
213+
if cleanup_path:
214+
cleanup_paths.append(cleanup_path)
215+
216+
try:
217+
return agent.run(*resolved)
218+
finally:
219+
for path in cleanup_paths:
220+
_remove_remote_file(agent.location, path)
211221

212222

213223
def destroy(groupname, del_all=False) -> int:

0 commit comments

Comments
 (0)