Python API

CycleCloud Python API 可讓您與 CycleCloud REST API 互動,而不需要手動執行 HTTP 要求。 若要取得 API 來源發佈,請流覽至 CycleCloud 安裝上的 /about ,然後按一下 [ 下載 Python API ] 連結。 當您擁有來源散發之後,就可以 pip install 將其放入 Python 環境,並開始使用。

用戶端物件

您可以使用或不使用指定的組態來建構 Client 物件。 如果您未指定組態字典,它會自動嘗試從預設 CycleCloud CLI ini 檔案提取設定, (~/.cycle/config.ini)

組態可以當作具有下列索引鍵/值組的聽寫提供:

  • url - 必要,CycleCloud 安裝的 Web 介面 URL
  • username - 必填
  • password - 必要,使用者的純文字密碼
  • timeout - 嘗試連線/與系統 (60 進行連線/通訊時,預設會發生逾時錯誤的時間,以秒為單位)
  • verify_certificates - 布林值,指出憑證檢查是否應該在預設啟用 (True)

或者,您可以將這些值指定為建構函式的關鍵字引數。

from cyclecloud.client import Client

# configuration read from ~/.cycle/config.ini
cl1 = Client() 

# config provided as dictionary
config = {"url": "http://127.0.0.1:8443",
          "username": "admin",
          "password": "password",
          "timeout": 60,
          "verify_certificates": False}
cl2 = Client(config)

# config provided as keyword arguments
cl3 = Client(url="http://127.0.0.1:8443", username="admin", password="password")

用戶端屬性

  • session- Session 物件- 僅用於呼叫直接 API

  • clusters - 系統中叢集物件的對應,以叢集名稱為索引鍵

叢集物件

Cluster 物件允許控制 CycleCloud 安裝中的特定叢集。

from cyclecloud.client import Client
cl1 = Client()

# gets a Cluster object for a cluster named "test-cluster-1" from the client cl1
cluster_obj = cl1.clusters["test-cluster-1"]

# prints the current state of the cluster
print(cluster_obj.get_status().state)

# start up to 5 new cores
cluster_obj.scale_by_cores("execute", 5)

叢集屬性

  • name - 這個物件所參考的叢集名稱

  • nodes - 可反覆運算組成此叢集的節點記錄清單

叢集函式

  • get_status(nodes=False) - 取得叢集的 [叢集狀態 ] 物件,並選擇性地填入節點清單。

  • scale_by_cores(node_array, total_core_count) - 將系統設定為將指定的節點陣列調整為所需的核心計數總數。 如果節點陣列已經包含超過 total_core_count 核心,則呼叫將不會有任何作用。

  • scale_by_nodes(node_array, total_node_count) - 將系統設定為將指定的節點陣列調整為所需的節點總數。 如果節點陣列已經包含超過 total_node_count 節點,則呼叫將不會有任何作用。

直接 API

其餘 API 可以使用 位於 cyclecloud.apicyclecloud.model API,直接從 REST API產生,以更直接的方式存取。 若要這樣做,您只需建構 Client 物件,並使用 session 它上所提供的 屬性進行呼叫。

from cyclecloud.client import Client
from cyclecloud.api import clusters

cl1 = Client()

# prints the current state of the cluster
response_status, cluster_status = clusters.get_cluster_status(cl1.session, "test-cluster-1", nodes=False)
print(cluster_status.state)