knot.space
The knot.space library provides space management functions for scripts.
Functions
| Function | Description |
|---|---|
create(name, template_name, description='', shell='bash', depends_on=None, stack='') |
Create a new space |
delete(name) |
Delete a space by name |
get(name) |
Get detailed space information |
start(name) |
Start a space |
stop(name) |
Stop a space |
restart(name) |
Restart a space |
list() |
List all spaces for the current user |
is_running(name) |
Check if a space is running |
get_description(name) |
Get the description of a space |
set_description(name, description) |
Set the description of a space |
get_dependencies(name) |
Get the dependency space IDs for a space |
set_dependencies(name, depends_on) |
Set the dependency spaces for a space |
get_stack(name) |
Get the stack name for a space |
set_stack(name, stack) |
Set the stack name for a space |
get_field(name, field) |
Get a custom field value |
set_field(name, field, value) |
Set a custom field value |
transfer(name, user_id) |
Transfer space ownership |
share(name, user_ids) |
Share space with one or more users |
unshare(name, user_id=None) |
Remove space share, optionally for a specific user |
run_script(space_name, script_name, *args) |
Execute a script in a space |
run(space_name, command, args=[], timeout=30, workdir='') |
Execute a command in a space |
read_file(space_name, file_path) |
Read file contents from a space |
write_file(space_name, file_path, content) |
Write content to a file in a space |
port_forward(source_space, local_port, remote_space, remote_port, persistent=False, force=False) |
Forward a port between spaces |
port_apply(source_space, forwards) |
Replace all port forwards with the given list |
port_list(space) |
List active port forwards |
port_stop(space, local_port) |
Stop a port forward |
Usage
import knot.space as space
# List all spaces
spaces = space.list()
for s in spaces:
print(f"{s['name']}: {'running' if s['is_running'] else 'stopped'}")
# Create a new space
space_id = space.create("my-space", "ubuntu", description="My dev environment")
# Start a space
space.start("my-space")
# Execute a command
output = space.run("my-space", "ls", args=["-la", "/tmp"])
print(output)
# Read a file
content = space.read_file("my-space", "/etc/hostname")
print(content)Function Details
create(name, template_name, description=’’, shell=‘bash’, depends_on=None, stack=’’)
Create a new space.
Parameters:
name(string): Name for the new spacetemplate_name(string): Name of the template to usedescription(string, optional): Description for the spaceshell(string, optional): Shell to use (default: “bash”)depends_on(list, optional): List of dependency space names or IDsstack(string, optional): Stack name to group this space under
Returns: string - The space ID of the newly created space
run(space_name, command, args=[], timeout=30, workdir=’')
Execute a command in a running space.
Parameters:
space_name(string): Name of the spacecommand(string): Command to executeargs(list, optional): Arguments for the commandtimeout(int, optional): Timeout in seconds (default: 30)workdir(string, optional): Working directory
Returns: string - Command output
read_file(space_name, file_path)
Read file contents from a running space.
Parameters:
space_name(string): Name of the spacefile_path(string): Path to the file
Returns: string - File contents
write_file(space_name, file_path, content)
Write content to a file in a running space.
Parameters:
space_name(string): Name of the spacefile_path(string): Path to the filecontent(string): Content to write
Returns: bool - True on success
get_dependencies(name)
Get the dependency space IDs for a space.
Parameters:
name(string): Name or ID of the space
Returns: list - List of dependency space IDs
set_dependencies(name, depends_on)
Set the dependency spaces for a space. Dependencies are required to be started before the space starts.
Parameters:
name(string): Name or ID of the spacedepends_on(list): List of dependency space names or IDs
Returns: bool - True on success
get(name)
Get detailed information about a space.
Parameters:
name(string): Name or ID of the space
Returns: dict containing:
id(string): Space IDname(string): Space namedescription(string): Space descriptiontemplate_id(string): Template IDtemplate_name(string): Template nameuser_id(string): Owner user IDusername(string): Owner usernameshares(list): List of shared user IDsdepends_on(list): List of dependency space IDsshell(string): Default shellplatform(string): Platform (e.g., “linux/amd64”)zone(string): Zone nameis_running(bool): Whether the space is runningis_pending(bool): Whether the space is pendingis_deleting(bool): Whether the space is being deletednode_hostname(string): Hostname of the node running the spacecreated_at(string): Creation timestampalt_names(list): Additional space namesicon_url(string): Icon URLcustom_fields(list): Custom field valuesstartup_script_id(string): Startup script IDstack(string): Stack name (empty if unstacked)
get_stack(name)
Get the stack name for a space.
Parameters:
name(string): Name or ID of the space
Returns: string - The stack name (empty string if unstacked)
set_stack(name, stack)
Set the stack name for a space. Spaces with the same stack name are grouped together.
Parameters:
name(string): Name or ID of the spacestack(string): Stack name (empty string to unstack)
Returns: bool - True on success
share(name, user_ids)
Share a space with one or more users.
Parameters:
name(string): Name or ID of the spaceuser_ids(string or list): User ID, username, or email to share with, or a list of those values
Returns: bool - True on success
unshare(name, user_id=None)
Remove a space share.
Parameters:
name(string): Name or ID of the spaceuser_id(string, optional): User ID, username, or email to remove sharing for. If omitted, owners stop all sharing and recipients leave their own share.
Returns: bool - True on success
transfer(name, user_id)
Transfer space ownership to another user.
Parameters:
name(string): Name or ID of the spaceuser_id(string): User ID, username, or email of the new owner
Returns: bool - True on success
port_apply(source_space, forwards)
Replace all port forwards for a space with the given list. Any existing forwards not in the list are stopped, and any new forwards in the list are started. Forwards that already exist with the same local port, space, and remote port are left unchanged.
Parameters:
source_space(string): Source space name or IDforwards(list): List of dicts, each containing:local_port(int): Local port numberspace(string): Remote space name or IDremote_port(int): Remote port numberpersistent(bool, optional): Persist the forward across restarts (default: False)force(bool, optional): Skip validation checks (default: False)
Returns: dict containing:
applied(list): List of forwards that were startedstopped(list): List of forwards that were stoppederrors(list): List of error messages (if any)