Skip to content

Commit a05a11a

Browse files
committed
support sudo for mkdir plugin
1 parent 455e77a commit a05a11a

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

docs/plugins/generated.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,6 +3823,11 @@ Create a directory with owner/group/mode parameters.
38233823
| Parameter | Required | Type | Default | Description |
38243824
|---|---:|---|---|---|
38253825
| `path` | yes | `path` | | Remote or local path, depending on the plugin. |
3826+
| `mode` | no | `string` | | POSIX file mode, for example 0644 or 0755. |
3827+
| `owner` | no | `string` | | Remote file owner. |
3828+
| `group` | no | `string` | | Primary group, file group owner or remote group name. |
3829+
| `cwd` | no | `path` | | Remote or local working directory for this operation. |
3830+
| `sudo` | no | `boolean` | `False` | Run the remote operation through sudo -n when supported. |
38263831

38273832
Result fields:
38283833

src/automax/plugins/fs_mkdir.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class FsMkdirPlugin(BasePlugin):
2020
name = "fs.mkdir"
2121
description = "Create a directory with owner/group/mode parameters."
2222
required_params = ("path",)
23+
optional_params = ("mode", "owner", "group", "cwd", "sudo")
2324
opens_remote_session = True
2425

2526
def _command(self, params: Dict[str, Any], context: ExecutionContext) -> str:
@@ -36,12 +37,13 @@ def _command(self, params: Dict[str, Any], context: ExecutionContext) -> str:
3637
if group:
3738
checks.append(f'test "$(stat -c %G {path})" = {quote(group)}')
3839

39-
commands = [f"mkdir -p {path}"]
40+
sudo = "sudo -n " if bool(params.get("sudo", False)) else ""
41+
commands = [f"{sudo}mkdir -p {path}"]
4042
if mode:
41-
commands.append(f"chmod {quote(mode)} {path}")
43+
commands.append(f"{sudo}chmod {quote(mode)} {path}")
4244
if owner or group:
4345
owner_group = f"{owner or ''}:{group or ''}"
44-
commands.append(f"chown {quote(owner_group)} {path}")
46+
commands.append(f"{sudo}chown {quote(owner_group)} {path}")
4547

4648
command = (
4749
" && ".join(checks)

tests/test_next_engine.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,47 @@ def test_builtin_filesystem_plugins_are_registered():
322322
assert alias not in output_names
323323

324324

325+
326+
327+
def test_fs_mkdir_manual_commands_render_sudo_owner_group_and_mode():
328+
from automax.plugins.fs_mkdir import FsMkdirPlugin
329+
330+
context = ExecutionContext(
331+
run_id="test",
332+
dry_run=True,
333+
job={},
334+
task={},
335+
step={},
336+
substep={},
337+
target=Target(name="node", host="host"),
338+
vars={},
339+
outputs={},
340+
secrets={},
341+
)
342+
343+
commands = FsMkdirPlugin().manual_commands(
344+
{
345+
"path": "/u01/app/grid",
346+
"owner": "grid",
347+
"group": "oinstall",
348+
"mode": "0775",
349+
"sudo": True,
350+
},
351+
context,
352+
)
353+
354+
assert commands == [
355+
'test -d /u01/app/grid && '
356+
'test "$(stat -c %a /u01/app/grid)" = 0775 && '
357+
'test "$(stat -c %U /u01/app/grid)" = grid && '
358+
'test "$(stat -c %G /u01/app/grid)" = oinstall || '
359+
'{ sudo -n mkdir -p /u01/app/grid && '
360+
'sudo -n chmod 0775 /u01/app/grid && '
361+
'sudo -n chown grid:oinstall /u01/app/grid; '
362+
'echo __AUTOMAX_CHANGED__; }'
363+
]
364+
365+
325366
def test_ssh_smoke_script_is_syntax_valid():
326367
script = Path("scripts/ssh-smoke.sh")
327368
assert script.exists()

0 commit comments

Comments
 (0)