Skip to content

Latest commit

 

History

History
921 lines (637 loc) · 14.9 KB

File metadata and controls

921 lines (637 loc) · 14.9 KB

PyColonies API Reference

Complete API reference for the PyColonies SDK.

Client Initialization

from pycolonies import Colonies

client = Colonies(host, port, tls=False, native_crypto=True)
Parameter Type Description
host str Server hostname
port int Server port
tls bool Enable TLS (default: False)
native_crypto bool Use native crypto library (default: True)

Colony Management

list_colonies

List all colonies on the server.

colonies = client.list_colonies(server_prvkey)
Parameter Type Description
server_prvkey str Server private key

Returns: List of colonies


add_colony

Create a new colony.

colony = client.add_colony(colonyid, colonyname, server_prvkey)
Parameter Type Description
colonyid str Colony ID (derived from colony private key)
colonyname str Colony name
server_prvkey str Server private key

get_colony

Get colony details.

colony = client.get_colony(colonyname, prvkey)

del_colony

Delete a colony.

client.del_colony(colonyname, server_prvkey)

stats

Get colony statistics.

stats = client.stats(colonyname, prvkey)

Returns: Colony statistics (process counts, etc.)


Executor Management

add_executor

Register a new executor.

executor = {
    "executorname": "my-executor",
    "executorid": executorid,
    "colonyname": colonyname,
    "executortype": "python-executor"
}
client.add_executor(executor, colony_prvkey)

approve_executor

Approve a registered executor.

client.approve_executor(colonyname, executorname, colony_prvkey)

reject_executor

Reject a registered executor.

client.reject_executor(colonyname, executorname, colony_prvkey)

remove_executor

Remove an executor.

client.remove_executor(colonyname, executorname, colony_prvkey)

list_executors

List all executors in a colony.

executors = client.list_executors(colonyname, prvkey)

Returns: List of executor objects


get_executor

Get details about a specific executor.

executor = client.get_executor(colonyname, executorname, prvkey)

Process Management

submit_func_spec

Submit a function specification for execution.

from pycolonies import func_spec

spec = func_spec(
    func="my_function",
    args=["arg1", "arg2"],
    colonyname="my_colony",
    executortype="python-executor",
    maxexectime=60,
    maxwaittime=60,
    maxretries=3
)

process = client.submit_func_spec(spec, prvkey)

Returns: Process object with processid


assign

Assign a waiting process to an executor.

process = client.assign(colonyname, timeout, executor_prvkey)
Parameter Type Description
colonyname str Colony name
timeout int Timeout in seconds (blocks until process available)
executor_prvkey str Executor private key

Returns: Process object or None if timeout


get_process

Get process details by ID.

process = client.get_process(processid, prvkey)

list_processes

List processes by state.

processes = client.list_processes(colonyname, count, state, prvkey)
Parameter Type Description
state int 0=waiting, 1=running, 2=success, 3=failed

close

Close a process as successful.

client.close(processid, output, prvkey)
Parameter Type Description
output list Output values

fail

Close a process as failed.

client.fail(processid, errors, prvkey)
Parameter Type Description
errors list Error messages

set_output

Set process output without closing.

client.set_output(processid, output, prvkey)

wait

Wait for a process to complete.

completed_process = client.wait(process, timeout, prvkey)

remove_process

Remove a process.

client.remove_process(processid, prvkey)

remove_all_processes

Remove all processes in a colony.

client.remove_all_processes(colonyname, prvkey, state=-1)
Parameter Type Description
state int -1=all, 0=waiting, 1=running, 2=success, 3=failed

Workflow Management

submit_workflow

Submit a workflow (process graph).

from pycolonies import Workflow, func_spec

wf = Workflow(colonyname="my_colony")

f1 = func_spec(func="step1", args=[], colonyname="my_colony",
               executortype="python-executor", maxexectime=60, maxwaittime=60)
wf.functionspecs.append(f1)

f2 = func_spec(func="step2", args=[], colonyname="my_colony",
               executortype="python-executor", maxexectime=60, maxwaittime=60)
f2.conditions.dependencies.append("step1")
wf.functionspecs.append(f2)

processgraph = client.submit_workflow(wf, prvkey)

get_processgraph

Get a process graph by ID.

graph = client.get_processgraph(processgraphid, prvkey)

get_processgraphs

List process graphs in a colony.

graphs = client.get_processgraphs(colonyname, count, prvkey, state=None)

get_processes_for_workflow

Get all processes in a workflow.

processes = client.get_processes_for_workflow(processgraphid, colonyname, prvkey, count=100)

remove_processgraph

Remove a process graph.

client.remove_processgraph(processgraphid, prvkey)

remove_all_processgraphs

Remove all process graphs.

client.remove_all_processgraphs(colonyname, prvkey, state=None)

add_child

Dynamically add a child process to a workflow.

client.add_child(processgraphid, parentprocessid, childprocessid, funcspec, nodename, insert, prvkey)

find_process

Find a process by node name in a workflow.

process = client.find_process(nodename, processids, prvkey)

Channel Operations

channel_append

Append a message to a channel.

client.channel_append(
    processid,
    channel_name,
    sequence,
    payload,
    prvkey,
    in_reply_to=0,
    payload_type=""
)
Parameter Type Description
sequence int Client-assigned sequence number
payload str/bytes Message content
in_reply_to int Optional sequence being replied to
payload_type str "", "data", "end", or "error"

channel_read

Read messages from a channel.

entries = client.channel_read(processid, channel_name, after_seq, limit, prvkey)
Parameter Type Description
after_seq int Read messages after this sequence (0 for all)
limit int Max messages to return

Returns: List of message entries with sequence, payload, type, inreplyto


subscribe_channel

Subscribe to channel messages via WebSocket.

# Blocking mode - returns all messages
messages = client.subscribe_channel(processid, channel_name, prvkey, timeout=30)

# Callback mode - streaming
def on_message(entries):
    for entry in entries:
        print(entry['payload'].decode())
    return True  # False to stop

client.subscribe_channel(processid, channel_name, prvkey, timeout=30, callback=on_message)

Blueprint Management

add_blueprint_definition

Add a blueprint definition (requires colony owner key).

definition = {
    "kind": "MyResource",
    "metadata": {
        "name": "my-resource-def",
        "colonyname": colonyname
    },
    "spec": {
        "names": {"kind": "MyResource"}
    }
}
client.add_blueprint_definition(definition, colony_prvkey)

get_blueprint_definition

Get a blueprint definition by name.

definition = client.get_blueprint_definition(colonyname, name, prvkey)

get_blueprint_definitions

List all blueprint definitions.

definitions = client.get_blueprint_definitions(colonyname, prvkey)

remove_blueprint_definition

Remove a blueprint definition.

client.remove_blueprint_definition(colonyname, name, colony_prvkey)

add_blueprint

Add a blueprint instance.

blueprint = {
    "kind": "MyResource",
    "metadata": {
        "name": "my-instance",
        "colonyname": colonyname
    },
    "handler": {
        "executortype": "my-reconciler"
    },
    "spec": {
        "replicas": 3,
        "image": "nginx:latest"
    }
}
client.add_blueprint(blueprint, prvkey)

get_blueprint

Get a blueprint by name.

blueprint = client.get_blueprint(colonyname, name, prvkey)

get_blueprints

List blueprints, optionally filtered.

blueprints = client.get_blueprints(colonyname, prvkey, kind=None, location=None)

update_blueprint

Update a blueprint's spec.

blueprint['spec']['replicas'] = 5
client.update_blueprint(blueprint, prvkey, force_generation=False)

update_blueprint_status

Update a blueprint's status (called by reconciler).

status = {
    "replicas": 5,
    "ready": True,
    "lastSeen": "2024-01-01T12:00:00Z"
}
client.update_blueprint_status(colonyname, name, status, prvkey)

reconcile_blueprint

Trigger reconciliation for a blueprint.

process = client.reconcile_blueprint(colonyname, name, prvkey, force=False)

get_blueprint_history

Get change history for a blueprint.

history = client.get_blueprint_history(blueprintid, prvkey, limit=None)

remove_blueprint

Remove a blueprint.

client.remove_blueprint(colonyname, name, prvkey)

Cron Management

add_cron

Add a cron job.

from pycolonies import Workflow

wf = Workflow(colonyname=colonyname)
# ... add function specs to workflow

client.add_cron(cronname, cronexpr, wait, wf, colonyname, prvkey)
Parameter Type Description
cronexpr str Cron expression (e.g., "0 * * * *")
wait bool Wait for previous run to complete

get_cron

Get a cron job by ID.

cron = client.get_cron(cronid, prvkey)

get_crons

List cron jobs.

crons = client.get_crons(colonyname, count, prvkey)

run_cron

Manually trigger a cron job.

process = client.run_cron(cronid, prvkey)

del_cron

Delete a cron job.

client.del_cron(cronid, prvkey)

Generator Management

add_generator

Add a generator.

client.add_generator(generator, prvkey)

get_generator

Get a generator by ID.

generator = client.get_generator(generatorid, prvkey)

get_generators

List generators.

generators = client.get_generators(colonyname, prvkey, count=100)

remove_generator

Remove a generator.

client.remove_generator(generatorid, prvkey)

User Management

add_user

Add a user to a colony.

user = {
    "colonyname": colonyname,
    "userid": userid,
    "name": "username",
    "email": "user@example.com",
    "phone": ""
}
client.add_user(user, server_prvkey)

get_users

List users in a colony.

users = client.get_users(colonyname, server_prvkey)

remove_user

Remove a user.

client.remove_user(colonyname, name, server_prvkey)

Function Registry

add_function

Register a function for an executor.

client.add_function(colonyname, executorname, funcname, prvkey)

get_functions_by_colony

List all functions in a colony.

functions = client.get_functions_by_colony(colonyname, prvkey)

get_functions_by_executor

List functions for a specific executor.

functions = client.get_functions_by_executor(colonyname, executorname, prvkey)

Attribute Management

add_attribute

Add an attribute to a running process.

attr = client.add_attribute(processid, key, value, prvkey)

get_attribute

Get an attribute by ID.

attr = client.get_attribute(attributeid, prvkey)

Logging

add_log

Add a log message to a process.

client.add_log(processid, message, prvkey)

get_process_log

Get logs for a process.

logs = client.get_process_log(colonyname, processid, count, since, prvkey)
Parameter Type Description
count int Max log entries to return
since int Timestamp to start from

get_executor_log

Get logs for an executor.

logs = client.get_executor_log(colonyname, executorid, count, since, prvkey)

File Storage

upload_file

Upload a file to storage.

client.upload_file(colonyname, filepath, label, keeplocal, prvkey)

upload_data

Upload data directly to storage.

client.upload_data(colonyname, data, filename, label, prvkey)

download_file

Download a file from storage.

client.download_file(colonyname, fileid, localpath, prvkey)

download_data

Download data from storage.

data = client.download_data(colonyname, fileid, prvkey)

get_file

Get file metadata.

file = client.get_file(colonyname, fileid, prvkey)

get_files

List files by label.

files = client.get_files(colonyname, label, prvkey)

get_file_labels

List file labels.

labels = client.get_file_labels(colonyname, prvkey, name="", exact=False)

delete_file

Delete a file from storage.

client.delete_file(colonyname, fileid, prvkey)

sync

Sync files between local and remote storage.

client.sync(colonyname, label, dir, keeplocal, prvkey)

Snapshots

create_snapshot

Create a snapshot of files.

client.create_snapshot(colonyname, label, name, prvkey)

get_snapshots

List snapshots.

snapshots = client.get_snapshots(colonyname, prvkey)

get_snapshot_by_name

Get a snapshot by name.

snapshot = client.get_snapshot_by_name(colonyname, name, prvkey)

get_snapshot_by_id

Get a snapshot by ID.

snapshot = client.get_snapshot_by_id(colonyname, snapshotid, prvkey)

Process States

State Value Description
WAITING 0 Process waiting for executor assignment
RUNNING 1 Process assigned and executing
SUCCESS 2 Process completed successfully
FAILED 3 Process failed

Error Handling

from pycolonies import ColoniesError, ColoniesConnectionError

try:
    process = client.assign(colonyname, 10, prvkey)
except ColoniesConnectionError as e:
    print(f"Connection error: {e}")
except ColoniesError as e:
    print(f"API error: {e}")