Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
20 changes: 18 additions & 2 deletions src/pyinfra/facts/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,28 @@ def command(self):

class TmpDir(FactBase):
"""
Returns the temporary directory of the current server, if configured.
Returns the temporary directory of the current server.

According to POSIX standards, checks environment variables in this order:
1. TMPDIR (if set and accessible)
2. TMP (if set and accessible)
3. TEMP (if set and accessible)
4. Falls back to /tmp
"""

@override
def command(self):
return "echo $TMPDIR"
return """
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ] && [ -w "$TMPDIR" ]; then
echo "$TMPDIR"
elif [ -n "$TMP" ] && [ -d "$TMP" ] && [ -w "$TMP" ]; then
echo "$TMP"
elif [ -n "$TEMP" ] && [ -d "$TEMP" ] && [ -w "$TEMP" ]; then
echo "$TEMP"
else
echo "/tmp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't provide a default here, the fallback is specified at the config level currently (https://github.com/pyinfra-dev/pyinfra/blob/3.x/src/pyinfra/api/config.py#L29).

To be honest I think this way is nicer, but we can't break the config for backwards compatibility so if no matching env vars the fact still needs to return a blank string.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the TMPDIR|TMP|TEMP are set in my arch/zsh terminal or in another ubuntu/bash PC I checked.

I had the this issue and the initial problem was server.script not working. The underlying issue was that this fact returning empty. I remember seeing something about setting a default in the pyinfra code, but did not check in details. Still the script operation was failing (v3.5).

So if this returns a blank, my guess is it still would fail here in my machine, unless the script was fixed in the meantime to use the proper default (if that is how it should work).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed the change, let me know @Fizzadar

fi
""".strip()


class Hostname(FactBase):
Expand Down
5 changes: 5 additions & 0 deletions tests/facts/server.TmpDir/default_fallback.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "if [ -n \"$TMPDIR\" ] && [ -d \"$TMPDIR\" ] && [ -w \"$TMPDIR\" ]; then\n echo \"$TMPDIR\"\nelif [ -n \"$TMP\" ] && [ -d \"$TMP\" ] && [ -w \"$TMP\" ]; then\n echo \"$TMP\"\nelif [ -n \"$TEMP\" ] && [ -d \"$TEMP\" ] && [ -w \"$TEMP\" ]; then\n echo \"$TEMP\"\nelse\n echo \"/tmp\"\nfi",
"output": ["/tmp"],
"fact": "/tmp"
}
5 changes: 5 additions & 0 deletions tests/facts/server.TmpDir/temp_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "if [ -n \"$TMPDIR\" ] && [ -d \"$TMPDIR\" ] && [ -w \"$TMPDIR\" ]; then\n echo \"$TMPDIR\"\nelif [ -n \"$TMP\" ] && [ -d \"$TMP\" ] && [ -w \"$TMP\" ]; then\n echo \"$TMP\"\nelif [ -n \"$TEMP\" ] && [ -d \"$TEMP\" ] && [ -w \"$TEMP\" ]; then\n echo \"$TEMP\"\nelse\n echo \"/tmp\"\nfi",
"output": ["/temp"],
"fact": "/temp"
}
5 changes: 5 additions & 0 deletions tests/facts/server.TmpDir/tmp_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "if [ -n \"$TMPDIR\" ] && [ -d \"$TMPDIR\" ] && [ -w \"$TMPDIR\" ]; then\n echo \"$TMPDIR\"\nelif [ -n \"$TMP\" ] && [ -d \"$TMP\" ] && [ -w \"$TMP\" ]; then\n echo \"$TMP\"\nelif [ -n \"$TEMP\" ] && [ -d \"$TEMP\" ] && [ -w \"$TEMP\" ]; then\n echo \"$TEMP\"\nelse\n echo \"/tmp\"\nfi",
"output": ["/usr/tmp"],
"fact": "/usr/tmp"
}
5 changes: 5 additions & 0 deletions tests/facts/server.TmpDir/tmpdir_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "if [ -n \"$TMPDIR\" ] && [ -d \"$TMPDIR\" ] && [ -w \"$TMPDIR\" ]; then\n echo \"$TMPDIR\"\nelif [ -n \"$TMP\" ] && [ -d \"$TMP\" ] && [ -w \"$TMP\" ]; then\n echo \"$TMP\"\nelif [ -n \"$TEMP\" ] && [ -d \"$TEMP\" ] && [ -w \"$TEMP\" ]; then\n echo \"$TEMP\"\nelse\n echo \"/tmp\"\nfi",
"output": ["/var/tmp"],
"fact": "/var/tmp"
}