Environment requirements
obshell-sdk-python requires Python 3.0 or later.
OceanBase Shell (obshell) in the environment must be running.
Procedure
Note
This section demonstrates how to use obshell-sdk-python to deploy an OceanBase cluster. Perform the following steps on any OBServer node.
Install obshell-sdk-python.
pip install pythonCreate a client instance.
from obshell import ClientSet from obshell.auth import PasswordAuth def main(): client = ClientSet("10.10.10.1", 2886, PasswordAuth("****", "v1"))In this example,
10.10.10.1and2886are respectively the IP address and port number of the target obshell node. You can modify them as needed.PasswordAuthis the security authentication method used for requests to obshell. Configure the password of theroot@sysuser of the cluster where the target obshell node belongs and an authentication method version. If you do not specify an authentication method version, the client instance chooses one based on the version of obshell on the target node.Note
If the identity of the target obshell node is Single Agent,
PasswordAuthis not required.You can also create a client instance of the specified version as follows:
from obshell import ClientV1 from obshell.auth import PasswordAuth def main(): client = ClientV1("10.10.10.1", 2886, PasswordAuth("****", "v1"))Deploy an OceanBase cluster.
obshell-sdk-python allows you to deploy an OceanBase cluster in two ways: asynchronous task execution and synchronous task execution. For asynchronous task execution, a response is returned immediately after an API request is successfully sent to obshell. For synchronous task execution, a response is returned only after an API request is successfully sent to obshell and the obshell task is completed.
The following sample code deploys a cluster that contains three zones with one server in each zone:
Asynchronous task executionSynchronous task executionfrom obshell import ClientSet from obshell.auth import PasswordAuth def main(): client = ClientSet("10.10.10.1", 2886, PasswordAuth("****")) # Join the current node to the cluster as Master Agent. dag = client.v1.join("10.10.10.1", 2886, "zone1") client.v1.wait_dag_succeed(dag.generic_id) # Join other nodes to the cluster as Follower Agent. dag = client.v1.join("10.10.10.2", 2886, "zone2") client.v1.wait_dag_succeed(dag.generic_id) dag = client.v1.join("10.10.10.3", 2886, "zone3") client.v1.wait_dag_succeed(dag.generic_id) # Set configurations for each OBServer node. configs = { "datafile_size": "24G", "log_disk_size": "24G", "cpu_count": "16", "memory_limit": "16G", "system_memory": "8G", "enable_syslog_recycle": "true", "enable_syslog_wf": "true"} dag = client.v1.config_observer(configs, "GLOBAL", []) client.v1.wait_dag_succeed(dag.generic_id) # Set configurations for the OceanBase cluster. dag = client.v1.config_obcluster("test-sdk", 11, "****") client.v1.wait_dag_succeed(dag.generic_id) # Initialize the cluster. dag = client.v1.init() client.v1.wait_dag_succeed(dag.generic_id) # Obtain the status of the cluster. status = client.v1.get_status() print(status)from obshell import ClientSet from obshell.auth import PasswordAuth def main(): client = ClientSet("10.10.10.1", 2886, PasswordAuth("****")) # Join the current node to the cluster as Master Agent. client.v1.join_sync("10.10.10.1", 2886, "zone1") # Join other nodes to the cluster as Follower Agent. client.v1.join_sync("10.10.10.2", 2886, "zone2") client.v1.join_sync("10.10.10.3", 2886, "zone3") # Set configurations for each OBServer node. configs = { "datafile_size": "24G", "log_disk_size": "24G", "cpu_count": "16", "memory_limit": "16G", "system_memory": "8G", "enable_syslog_recycle": "true", "enable_syslog_wf": "true"} client.v1.config_observer_sync(configs, "GLOBAL", []) # Set configurations for the OceanBase cluster. client.v1.config_obcluster_sync("test-sdk", 11, "****") # Initialize the cluster. client.v1.init_sync() # Obtain the status of the cluster. status = client.v1.get_status() print(status)