diff --git a/src/pyinfra/facts/server.py b/src/pyinfra/facts/server.py index 6ca2a6ed8..95cc89b74 100644 --- a/src/pyinfra/facts/server.py +++ b/src/pyinfra/facts/server.py @@ -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 empty string """ @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 "" +fi + """.strip() class Hostname(FactBase): diff --git a/tests/facts/server.TmpDir/default_fallback.json b/tests/facts/server.TmpDir/default_fallback.json new file mode 100644 index 000000000..4e673789d --- /dev/null +++ b/tests/facts/server.TmpDir/default_fallback.json @@ -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 \"\"\nfi", + "output": ["/tmp"], + "fact": "/tmp" +} diff --git a/tests/facts/server.TmpDir/temp_set.json b/tests/facts/server.TmpDir/temp_set.json new file mode 100644 index 000000000..a157244fc --- /dev/null +++ b/tests/facts/server.TmpDir/temp_set.json @@ -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 \"\"\nfi", + "output": ["/temp"], + "fact": "/temp" +} diff --git a/tests/facts/server.TmpDir/tmp_set.json b/tests/facts/server.TmpDir/tmp_set.json new file mode 100644 index 000000000..b8fbb7e46 --- /dev/null +++ b/tests/facts/server.TmpDir/tmp_set.json @@ -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 \"\"\nfi", + "output": ["/usr/tmp"], + "fact": "/usr/tmp" +} diff --git a/tests/facts/server.TmpDir/tmpdir_set.json b/tests/facts/server.TmpDir/tmpdir_set.json new file mode 100644 index 000000000..5561d140a --- /dev/null +++ b/tests/facts/server.TmpDir/tmpdir_set.json @@ -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 \"\"\nfi", + "output": ["/var/tmp"], + "fact": "/var/tmp" +}