Core Functions¶
Core functionality provided by the fscm module.
Command Execution¶
run¶
Execute a shell command.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
cmd |
str | required | Command to execute |
check |
bool | True | Raise on non-zero exit |
sudo |
bool | False | Run with sudo |
quiet |
bool | False | Suppress output |
destructive |
bool | True | Track in changelist |
env |
dict | None | Environment variables |
cwd |
str | None | Working directory |
stdin |
str | None | Input to send |
timeout |
int | None | Timeout in seconds |
Returns: RunReturn object
Example:
run_ro¶
Execute a read-only (non-destructive) command quietly.
Equivalent to run(cmd, quiet=True, destructive=False).
Example:
getstdout¶
Execute a command and return stdout as a string.
Example:
fails¶
Check if a command fails (returns non-zero).
Example:
runmany¶
Execute multiple commands.
Parameters:
| Name | Type | Description |
|---|---|---|
cmds |
str or list | Commands (newline-separated string or list) |
Example:
File Operations¶
file¶
Create or update a file.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
path |
str/Path | required | File path |
content |
str/bytes/Path | required | File contents |
mode |
str | None | Permissions (e.g., "0644") |
owner |
str | None | Owner (e.g., "root:root") |
sudo |
bool | False | Use sudo for writing |
encoding |
str | "utf-8" | Text encoding |
Returns: ChangeList
Example:
mkdir¶
Create a directory.
Creates parent directories as needed.
Example:
chmod¶
Change file permissions.
Example:
chown¶
Change file ownership.
Example:
lineinfile¶
Ensure a line exists in a file.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
path |
str | required | File path |
line |
str | None | Line content to ensure |
regexp |
str | None | Pattern to match |
state |
str | "present" | "present" or "absent" |
Example:
make_executable¶
Make a file executable.
backup_file¶
Create a backup of a file.
Returns the backup path or None if backup disabled.
Templates¶
template¶
Render a Jinja2 template.
Example:
config = fscm.template(
"templates/nginx.conf.j2",
domain="example.com",
port=80
)
fscm.file("/etc/nginx/nginx.conf", config)
System Object¶
s¶
The global system object provides OS-specific operations.
s.pkgs_install¶
Install packages using the system package manager.
Example:
s.pkg_is_installed¶
Check if a package is installed.
s.pkg_get_installed_version¶
Get the installed version of a package.
Utility Functions¶
detect_system¶
Detect the current operating system.
Returns a Debian, Arch, or MacOS instance.
hostname¶
Get the system hostname.
this_file_path¶
Get the path of the calling script.
this_dir_path¶
Get the directory of the calling script.
p¶
Create a PathHelper for convenient path operations.
Example:
get_secrets¶
Load secrets from pass password manager.
download_and_check_sha¶
Download a file and verify its SHA256 checksum.
RunReturn¶
Object returned by run().
Properties¶
| Property | Type | Description |
|---|---|---|
ok |
bool | True if exit code is 0 |
returncode |
int | Exit code |
stdout |
str/bytes | Standard output |
stderr |
str/bytes | Standard error |
Methods¶
assert_ok¶
Raise an exception if the command failed.
to_change¶
Convert to a CmdRun change object.