The metadata of the cloud server is the data that describes the cloud server information, including the default metadata information in the eSurfing Cloud system and user-defined metadata information. Within the cloud server, this part of information can be easily viewed through the Intranet, and the cloud server can be configured or managed based on metadata information.
Default Metadata of Cloud Server
After the cloud server is created, eSurfing Cloud server contains the default metadata, and the following is the default metadata under the resource pool of multi-AZ type:
Metadata Description | Metadata Item |
Owner's eSurfing Cloud account ID | /meta-data/owner-account-id |
Cloud server ID. | /meta-data/instance-id |
Cloud Server Name | /meta-data/hostname |
Cloud Server Specification Name | /meta-data/flavor_name |
Resource pool ID of the cloud server | /meta-data/region-id |
Availability zone ID of the cloud server | /meta-data/zone-id |
Custom data of the cloud server | /meta-data/user_data |
Image ID used when creating the cloud server | /meta-data/image-id |
ID of the VPC to which the cloud server belongs | /meta-data/vpc-id |
Intranet IPv4 address of the cloud server's primary NIC | /meta-data/private-ipv4 |
MAC address list of the NIC | /meta-data/network/interfaces/macs/ |
Custom Metadata of Cloud Server
In addition to the above cloud server metadata items, users can also add corresponding custom metadata items. In the metadata management of cloud server OpenAPI, it is the content of that part of the metadata that the API operates on the cloud server.
Metadata Description | Metadata Item |
Get all custom metadata at once (return format is JSON) | /spec-meta-data |
Single custom metadata retrieval | /meta-data/instance/specs/[key] |
Use of Metadata
View the default metadata
On the console cloud server list page, select a cloud server for remote login.
The Intranet address for querying metadata is 169.254.169.254.
We can view the required metadata based on the table of default metadata above, we can view it using the curl command, which is composed os: curl 169.254.169.254 {metadata item}.
Sample shell script:
#!/usr/bin/bash
echo "======================instance metadata======================"
echo "instance owner account id: " $(curl -s 169.254.169.254/meta-data/owner-account-id)
echo "instance id: " $(curl -s 169.254.169.254/meta-data/instance-id)
echo "instance id: " $(curl -s 169.254.169.254/meta-data/instance-id)
Effect:
Use of Cloud Server Metadata OpenAPI
At present, the metadata OpenAPI provided by eSurfing Cloud allows to operate and view custom metadata for cloud servers, including create, update, query, and delete. With the python Sample Document and the document corresponding to OpenAPI, you can write the corresponding OpenAPI request.
1. Create custom metadata
First, let's create custom metadata via OpenAPI ({"test1": "123", "test2": "abc"}) according to OpenAPI Documentation for Creating Metadata.
# Fill in the parameters according to the OpenAPI documentation for creating cloud data
create_metadata_params = {
"regionID": huadong1_region_id,
"azName": huadong1_az1,
"instanceID": huadong1_instance,
"metadata": {
"test1": "123",
"test2": "abc"
}
}
# The definition of the post method is omitted here. For the implementation of this method, please refer to eSurfing Cloud official website documentation.
url = "https://ctecs-global.ctapi.ctyun.cn/v4/ecs/metadata/create"
res_metadata_create = post(url, params=create_metadata_params)
Result of Calling:
{
"returnObj": {
"instanceID": "c44de03e-300b-7237-1827-b51aeaff3ccd",
"metadata": {
"test1": "123",
"test2": "abc"
}
},
"details": "",
"message": "SUCCESS",
"description": "Successful",
"statusCode": 800
}
In the previous method, you can query the custom metadata information using the custom metadata item (test2.sh script is as follows) :
#!/usr/bin/bash
echo "======================instance metadata======================"
echo "user-defined metadata: " $(curl -s 169.254.169.254/spec-meta-data)
echo "test1: " $(curl -s 169.254.169.254/meta-data/instance/specs/test1)
echo "test2: " $(curl -s 169.254.169.254/meta-data/instance/specs/test2)
Result:
2. Query custom metadata
At the same time, we can also call the OpenAPI interface to query custom metadata.
res_metadata_details = get("https://ctecs-global.ctapi.ctyun.cn/v4/ecs/metadata/details", params=dict(regionID=huadong1_region_id, azName=huadong1_az1, instanceID=huadong1_instance))
Result of Calling:
{
"returnObj": {
"metadata": {
"test1": "123",
"test2": "abc"
}
},
"message": "SUCCESS",
"description": "Successful",
"statusCode": 800
}
3. Update custom metadata
Call the update metadata interface:
# Fill in the parameters according to the OpenAPI documentation for creating cloud data
update_metadata_params = {
"regionID": huadong1_region_id,
"azName": huadong1_az1,
"instanceID": huadong1_instance,
"metadata": {
"test1": "789"
}
}
# The definition of the post method is omitted here. For the implementation of this method, please refer to eSurfing Cloud official website documentation.
url = "https://ctecs-global.ctapi.ctyun.cn/v4/ecs/metadata/update"
res_metadata_update = post(url, params=update_metadata_params)
Curl check the situation inside the cloud server:
*Overwrite mode update (isForce=true)
# Fill in the parameters according to the OpenAPI documentation for creating cloud data
update_metadata_params = {
"regionID": huadong1_region_id,
"azName": huadong1_az1,
"instanceID": huadong1_instance,
"isForce": True,
"metadata": {
"test3": "IGs9"
}
}
# The definition of the post method is omitted here. For the implementation of this method, please refer to eSurfing Cloud official website documentation.
url = "https://ctecs-global.ctapi.ctyun.cn/v4/ecs/metadata/update"
res_metadata_update = post(url, params=update_metadata_params)
Curl check the situation inside the cloud server:
4. Delete custom metadata
Call the delete interface:
res_metadata_delete = post("https://ctecs-global.ctapi.ctyun.cn/v4/ecs/metadata/delete", params=dict(regionID=huadong1_region_id, azName=huadong1_az1, instanceID=huadong1_instance))
Curl check the situation inside the cloud server:
Example: Configure the python environment of a cloud server using metadata
Scenario: In the process of using Python, we often need to install many dependent packages, and each package has version restrictions on each other (for example, in deep learning frameworks, version correspondence is required between Keras and TensorFlow). At this point, we can record the version information of the python package environment required for this cloud server by creating metadata.
First, log in to the cloud server remotely on the console page. After entering the remote page, check the current python package information:
We need to write the version information of the key packages.
Here, we take some packages as an example and execute the OpenAPI to create custom metadata for cloud servers:
# Fill in the parameters according to the OpenAPI documentation for creating cloud data
create_metadata_params = {
"regionID": huadong1_region_id,
"azName": huadong1_az1,
"instanceID": huadong1_instance,
"metadta": {
"requeirements": {
"requests": "2.27.1",
"numpy": "1.16.6",
"matplotlib": "2.2.5"
}
}
}
# The definition of the post method is omitted here. For the implementation of this method, please refer to Python Sample Document in the eSurfing Cloud official website documentation (https://www.ctyun.cn/document/10026730/10044097)
url = "https://ctecs-global.ctapi.ctyun.cn/v4/ecs/metadata/create"
res_metadata_create = post(url, params=create_metadata_params)
Result of Execution:
{
"returnObj": {
"instanceID": "c44de03e-300b-7237-1827-b51aeaff3ccd",
"metadata": {
"requeirements": {
"requests": "2.27.1",
"numpy": "1.16.6",
"matplotlib": "2.2.5"
}
}
},
"details": "",
"message": "SUCCESS",
"description": "Successful",
"statusCode": 800
}
After creating metadata, we can use relevant scripts within the cloud server to configure python accordingly.
Sample python script (test.py):
import json
import subprocess
HOST = "169.254.169.254"
URL = HOST + "/spec-meta-data"
FILE_REQUEIRE = "requirements.txt"
def send_command(command):
command_ls = command.split()
process = subprocess.Popen(command_ls, stdout=subprocess.PIPE)
output, err = process.communicate()
return output.decode("utf-8")
if __name__ == "__main__":
res = send_command("curl -s %s" % URL)
metadata = json.loads(res)
requeire_info = metadata.get("requeirements")
with open(FILE_REQUEIRE, "w") as f:
for k, v in requeire_info.items():
write_info = "%s==%s" % (str(k), str(v))
f.writelines(write_line)
tsinghua_image = "https://pypi.tuna.tsinghua.edu.cn/simple" # Tsinghua Image Station
send_command("pip install -r %s -i %s" % (FILE_REQUEIRE, tsinghua_image))
Result of Execution:
You can see that the corresponding python package has been downloaded in the python environment of the cloud server. In the event of a problem with the cloud server, it is also possible to subsequently redeploy accordingly based on the python package information.