Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic Management of Cloud Assistant

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve automatic management of cloud assistant. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Use Cloud Assistant to automate the management of instances

The purpose of OPS ECS instances is to maintain the best state of ECS instances and ensure the efficiency of troubleshooting, but manual maintenance will cost you a lot of time and effort, so Aliyun has developed a cloud assistant to solve how to automate and batch deal with daily maintenance tasks. This article gives an example of how to use cloud helper API to execute commands for ECS instances to achieve the purpose of automating operation and maintenance ECS instances.

Introduction to command types

Currently, the Cloud Assistant supports the following three command types.

prerequisite

You need to ensure that the network type of the target ECS instance is a proprietary network (VPC).

The status of the target ECS instance must be Running.

The target ECS instance must be pre-installed with the Cloud Assistant client. You can refer to Aliyun Assistant to install and use the Cloud Assistant client.

When executing a command of type PowerShell, you need to ensure that the target Windows instance has been configured with the PowerShell module.

The following example is done in the command line tool, and you need to make sure that you have installed the Ali Cloud command line tool CLI (Command-Line Interface).

For an example of Windows, see installing command line tools and SDK online.

For an example of Linux, see installing command line tools and SDK online.

You need to upgrade SDK.

Modify the CLI configuration:

Download the file aliyunOpenApiData.py.

Replace the file aliyunOpenApiData.py in% python_install_path%\ Lib\ site-packages\ aliyuncli in the path with the downloaded file.

For information about how to configure Ali Cloud CLI, see the documentation to configure the command line tool and SDK.

Operation steps

The following example shows how to use Cloud Assistant in Aliyun CLI to execute commands for ECS instances through API. Take, for example, the execution of an echo 123 command.

Run the aliyuncli ecs CreateCommand-- CommandContent ZWNobyAxMjM=-- Type RunShellScript-- Name test-- Description test creation command (CreateCommand) in CMD, PowerShell, or Shell on your local computer.

Run aliyuncli ecs InvokeCommand-- InstanceIds your-vm-instance-id1 instance-id2-- CommandId your-command-id-- Timed false execute the command (InvokeCommand).

Note:

InstanceIds is your ECS instance ID. Multiple ECS instances are supported, up to a maximum of 100.

Timed indicates whether it is a periodic task, Timed True indicates a periodic task, and Timed False indicates that it is not a periodic task.

When your task is a periodic task, that is, when the parameter Timed is True, you need to specify a period with the parameter Frequency. For example, 0 * / 20 * means the period is every 20 minutes. For more information about Cron expressions, see the description for the values of Cron expressions.

The result returns a common InvokeId for all target ECS instances. You can use this InvokeId to query the execution of the command.

(optional) run aliyuncli ecs DescribeInvocations-- InstanceId your-vm-instance-id-- InvokeId your-invoke-id to view the command execution status (DescribeInvocations). Where InvokeId is the execution ID returned when the command is executed for the ECS instance in step 2.

If the returned parameter InvokeStatus is Finished, it only means that the execution of the command process is completed, which does not necessarily mean the expected command effect. You need to view the actual execution result through the parameter Output in DescribeInvocationResults.

(optional) run aliyuncli ecs DescribeInvocationResults-- InstanceId your-vm-instance-id-- InvokeId your-invoke-id to view the actual execution result (DescribeInvocationResults) of the command for the specified ECS instance. Where InvokeId is the execution ID returned when the command is executed for the ECS instance in step 2.

When creating a command (CreateCommand), you can also set the following request parameters for the command.

Complete code example for using Cloud Assistant through Python SDK

You can also use Cloud Assistant through Aliyun SDK. For information about how to configure Ali Cloud SDK, see the documentation to configure the command line tool and SDK. The following is a complete code example of using the Cloud Assistant through Python SDK.

# coding=utf-8# if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs'# if the python sdk is install using' sudo pip install-- upgrade aliyun-python-sdk-ecs'# make sure the sdk version is 2.1.2, you can use command 'pip show aliyun-python-sdk-ecs' to checkimport jsonimport loggingimport osimport timeimport datetimeimport base64from aliyunsdkcore import clientfrom aliyunsdkecs.request.v20140526.CreateCommandRequest import CreateCommandRequestfrom aliyunsdkecs.request.v20140526.InvokeCommandRequest import InvokeCommandRequestfrom aliyunsdkecs.request.v20140526.DescribeInvocationResultsRequest import DescribeInvocationResultsRequest# configuration the log output formatter, if you want to save the output to file # append ", filename='ecs_invoke.log'" after datefmt.logging.basicConfig (level=logging.INFO, format='% (asctime) s% (filename) s [line:% (lineno) d]% (levelname) s% (message) slots, datefmt='%a,% d% b% Y% Hpurs% Mizuza% filenametrics Filemode='w') # access_key = 'Your Access Key Id'#acess_key_secrect =' Your Access Key Secrect'#region_name = 'cn-shanghai'#zone_id =' cn-shanghai-b'access_key = 'LTAIXXXXXXXXXXXX'acess_key_secrect =' 4dZXXXXXXXXXXXXXXXXXXXXXXXX'region_name = 'cn-hangzhou'zone_id =' cn-hangzhou-f'clt = client.AcsClient (access_key, acess_key_secrect, region_name) def create_command (command_content, type, name Description): request = CreateCommandRequest () request.set_CommandContent (command_content) request.set_Type (type) request.set_Name (name) request.set_Description (description) response = _ send_request (request) if response is None: return None command_id = response.get ('CommandId') return command_id Def invoke_command (instance_id, command_id, timed, cronat): request = InvokeCommandRequest () request.set_Timed (timed) InstanceIds = [instance_id] request.set_InstanceIds (InstanceIds) request.set_CommandId (command_id) request.set_Frequency (cronat) response = _ send_request (request) invoke_id = response.get ('InvokeId') return invoke_id Def get_task_output_by_id (instance_id, invoke_id): logging.info ("Check instance% s invoke_id is% s", instance_id Invoke_id) request = DescribeInvocationResultsRequest () request.set_InstanceId (instance_id) request.set_InvokeId (invoke_id) response = _ send_request (request) invoke_detail = None output = None if response is not None: result_list = response.get ('Invocation'). Get (' InvocationResults'). Get ('InvocationResult') for item in result_list: invoke_detail = item Output = base64.b64decode (item.get ('Output')) break Return output Def execute_command (instance_id): command_str = 'yum check-update' command_id = create_command (base64.b64encode (command_str),' RunShellScript', 'test',' test') if (command_id is None): logging.info ('create command failed') return invoke_id = invoke_command (instance_id, command_id,' false') '') if (invoke_id is None): logging.info ('invoke command failed') return time.sleep (15) output = get_task_output_by_id (instance_id, invoke_id) if (output is None): logging.info (' get result failed') return logging.info ("output:% s is\ n" Output) # send open api requestdef _ send_request (request): request.set_accept_format ('json') try: response_str = clt.do_action (request) logging.info (response_str) response_detail = json.loads (response_str) return response_detail except Exception as e: logging.error (e) if _ _ name__ = =' _ main__': execute_ Command ('iMurbp17zhpbXXXXXXXXXXXXXXX') about how to automate the management of cloud assistant, that's all. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report