Skip to main content

CLI and SDK

CLI References

Please refer to CLI quickstart on setting up the Anyscale CLI.

anyscale service rollout

Manage the Service rollout.

Please reference Anyscale Service YAML Schema for the supported values in the YAML file.

Options:

  • -f, --service-config-file TEXT The path of the Service configuration file.
  • -n, --name TEXT Name of service. Overrides the name specified in the configuration file.
  • --canary-percent INTEGER The percentage of traffic to send to the canary version of the service. Overrides the canary percent specified in the configuration file.
  • --rollout-strategy [ROLLOUT|IN_PLACE] Strategy for rollout. Overrides the rollout strategy specified in the configuration file.
  • -h, --help Show this message and exit.

SDK Example

It's a common pattern to auto-deploy your Service as part of your CI/CD pipeline. To get you started, here is sample Python SDK code that rollouts a new version of your Service using your local working directory.

import requests
import time
from anyscale.sdk.anyscale_client.models import *
from anyscale import AnyscaleSDK

def wait_for_service_state(sdk, service_id: str, expected_state: ServiceEventCurrentState, timeout_s = 1200):
"""
This is a helper method to wait for the Service to enter an expected state.

It throws an error if the service enters an unexpected error state.
"""
start = time.time()
while time.time() - start < timeout_s:
service = sdk.get_service(service_id).result
curr_state = service.current_state
if curr_state == expected_state:
return
if expected_state != ServiceEventCurrentState.UNHEALTHY and curr_state == ServiceEventCurrentState.UNHEALTHY:
raise RuntimeError(
f"Service entered an unexpected state {curr_state}\n"
f"Please check the error for {service.name} in the web UI at https://console.anyscale.com/services/{service.id}."
)
time.sleep(1)
curr_state = sdk.get_service(service_id).result.current_state
raise TimeoutError(
f"Service did not enter {expected_state} after {timeout_s} seconds."
)

def main():
service_name = "<SERVICE_NAME>"
# IDs can be found on Anyscale Console under Configurations.
compute_config_id="<COMPUTE_CONFIG_ID>"
build_id="<CLUSTER_ENV_BUILD_ID>"
upload_path="<YOUR_REMOTE_BUCKET_PATH>" # example: "s3://anyscale-test-bucket/"

sdk = AnyscaleSDK()

services = sdk.list_services(name=service_name).results

service = services[0]

apply_model = ApplyProductionServiceV2Model(
name=service.name,
project_id=service.project_id,
compute_config_id=compute_config_id,
build_id=build_id,
ray_serve_config={
"applications": [
{
"name": "default",
"import_path": "serve_hello:entrypoint",
"runtime_env": {
"working_dir": ".",
"upload_path": upload_path,
},
}
]
},
)

print(f"Rolling out Service {service.name}. It may take a few minutes.")

service = sdk.rollout_service(apply_model).result

wait_for_service_state(sdk, service.id, ServiceEventCurrentState.RUNNING)

print(f"Service has entered the running state. View details at https://console.anyscale.com/services/{service.id}")

# Please note that Service query would succeed only if you are connected to the VPN.
token = service.auth_token
base_url = f"https://{service.hostname}"

# Requests config
path = "/"
full_url = f"{base_url}{path}"
headers = {"Authorization": f"Bearer {token}"}

resp = requests.get(full_url, headers=headers)

print(f"Service query returns: {resp.text}")

if __name__ == "__main__":
main()
info

The script specifies a local working directory ., which will automatically upload your directory to remote storage. Therefore, please ensure you exported credentials with write permissions to your bucket.

SDK References

Please refer to SDK quickstart on setting up the Anyscale SDK.

Below are the SDK methods for Services.

get_service

Get a Service

Parameters

NameTypeDescriptionNotes
service_idstrDefaults to null

Returns ServicemodelResponse

list_services

Parameters

NameTypeDescriptionNotes
project_idoptional strproject_id to filter byDefaults to null
nameoptional strname to filter byDefaults to null
state_filterList[ServiceEventCurrentState]A list of Service states to filter byDefaults to []
creator_idoptional strfilter by creator idDefaults to null
sort_fieldServiceSortFieldIf absent, the default sorting order is 1. status (active first).2. Last updated at (descending). 3. Name (ascending).Defaults to null
sort_orderSortOrderIf sort_field is absent, this field is ignored. If absent, this field defaults to ascending.Defaults to null
paging_tokenoptional strDefaults to null
countoptional intDefaults to null

Returns ServicemodelListResponse

rollback_service

Rollback a Service

Parameters

NameTypeDescriptionNotes
service_idstrDefaults to null

Returns ServicemodelResponse

rollout_service

Rollout a service

Parameters

NameTypeDescriptionNotes
apply_service_modelApplyServiceModel

Returns ServicemodelResponse

terminate_service

Terminate a Service

Parameters

NameTypeDescriptionNotes
service_idstrDefaults to null

Returns ServicemodelResponse