Using command-line tool UCloud CLI, lightweight operating cloud resource management API

Up to now, UCloud API has been provided in different languages Python / Java / Golang etc. SDK. To further reduce the user's operation and maintenance manpower, has introduced a command-line invocation API based Golang SDK command-line tool CLI (Command Line Interface), provides a lightweight and open source on GitHub ( https://github.com/ ucloud / ucloud-cli). CLI command line interaction more in line with research and development operation and maintenance of operating practices, and some typical usage scenarios by CLI is also easier to maintain and codes of precipitation .

Here is the actual scene of some users encountered with the CLI can better address, in this summary and gives examples of use.

Scene One: create and delete large quantities of host

PingCAP As a professional supplier of distributed database, create a one-time batch of 300 cloud hosting use UCloud, distributed service testing. The UCloud console allows you to create a maximum of 10 cloud host, thus requiring the user to fool operate 30 times. However, the actual host API can support up to 1,000 concurrent creation. For a one-time API operations, to spend time with the SDK to create a script written input-output ratio is very low. Such scenes PingCAP is resolved through the CLI.

The example command scenario is as follows:

Ucloud uhost the Create -cpu 1 $ - MEMORY-2 gb the -image-the above mentioned id uImage-xxx -password test123 -count 300
Using command-line tool UCloud CLI, lightweight operating cloud resource management API
(Figure: Host console page to create a one-time up to 10 concurrent)

Scene 2: Centralized management / cleanup resources, is not easy to miss

Users often purchase a variety of products, varying numbers of each, should be part of a temporary demand created resources, use often forgotten after the end promptly removed, because the distribution of scattered difficult to manage, unknowingly paid for. Such as XD GLOBAL and other users, the good use of CLI to quickly create and promptly clean up unused resources.

Example command is as follows:

$ ucloud uhost delete --uhost-id ucloud uhost list --uhost-id-only --page-off
Using command-line tool UCloud CLI, lightweight operating cloud resource management API

(Figure: Console scattered resources products)

Scene Three: global dynamic acceleration PathX numerous examples of port management

Overseas game publishers in order to improve the player experience, often used UCloud global dynamic acceleration service PathX achieve regional players nearest access, effectively circumvent the slow international response to network congestion caused by packet loss and other issues.

A game company also uses PathX, but the number of ports that need to manage a single line of more than 60. Methods First, the product must first support 60 ports of non-standard management console, one by one and then add the user to configure all ports of the page. Method two is to write a script architect, port users adjust the parameter update. However, with the product iteration, API adjusted upgrade script becomes a problem, the two sides with strong dependence.

Using command-line tool UCloud CLI, lightweight operating cloud resource management API
(Figure: 9 console port supports PathX management protocol)

CLI precipitated such a scene, directly supported PathX multi-port management, the following sample code: # PathX specified line to add a resource instance TCP port 80

$ Ucloud THX also add port --uga-en-kjkxxx also --protocol tcp --port 3000-3079,8080

Scene Four: frequent switching batch EIP

Because a user information industry business scenarios need, frequently trigger switch its services outside the network IP. R & D manpower to implement the script needs, is a very common practice in the industry. UCloud CLI is used to complete the goal with a lightweight manner.

Sample code is as follows: # eip create and bind, and then the solution before putting eip release the bound

Switch-EXT uhost ucloud $ EIP --create-EIP-bandwidth-mb-2 --uhost the above mentioned id xxx --unband-All-All --release
Using command-line tool UCloud CLI, lightweight operating cloud resource management API
(Figure: unbundling console first and then bind the new EIP)

Another convenience of UCloud CLI

1. Multi-dimensional support command completion function

在命令补全方面,通常每次发布新版本都需要终端用户再次生成补全脚本,功能局限并且使用成本较高。因此,UCloud CLI扩展了spf13/cobra框架的功能(相关代码已开源https://github.com/lixiaojun629/cobra),把命令补全功能集成到命令行工具内部,用户只需要一次配置,日后版本升级都不必再重新生成补全脚本,使用方便。

补全功能包括:支持命令补全、命令参数补全、命令参数值静态补全以及命令参数值动态补全等。针对命令参数值动态补全,为了减少调用API次数、避免卡顿,UCloud CLI还添加了动态补全本地缓存特性。

命令参数值静态补全以及命令参数值动态补全的代码示例见下图所示,其中参数line的可选值BGP和International是固定在代码里的静态枚举值,参数eip-id的可选值是由Tab键触发的API请求得到的。

Using command-line tool UCloud CLI, lightweight operating cloud resource management API
(图:命令参数值静态补全)

Using command-line tool UCloud CLI, lightweight operating cloud resource management API
(图:命令参数动态补全)

2.命令编写更简单高效

UCloud CLI批量操作资源时原生支持并发、UI多线程渲染等,相比开启多进程执行命令行,占用系统资源更少,而且命令编写更加简单,降低运维代码成本。例如批量删除主机命令:

#命令1

$ ucloud uhost list --uhost-id-only | xargs -P 32 -I {} ucloud uhost delete --yes --uhost-id {}
#命令2

$ ucloud uhost delete --uhost-id ucloud uhost list --uhost-id-only --page-off
很明显命令2更加简单,可以有效减少运维人员的代码操作。

Using command-line tool UCloud CLI, lightweight operating cloud resource management API
(图:并发创建主机时,UI多线程渲染)

3.稳定可靠

由于UCloud CLI使用了spf13/cobra命令行开发框架,它也是Docker、Kubernetes和etcd等著名开源项目的CLI使用框架,久经考验,稳定可靠。

4.丰富的产品支持

在产品支持方面,目前UCloud CLI已经支持主机、网络、存储和数据库等常用云产品服务,基本能够满足用户大部分使用场景下的日常操作需求,支持产品列表见下图所示:

Using command-line tool UCloud CLI, lightweight operating cloud resource management API
(图:CLI支持产品)

在Docker中运行UCloud CLI

在Docker中也可以快速地使用UCloud CLI,按下面的操作步骤示例,就可以体验了。

1.首先安装Docker,拉取我们为你准备好的镜像:uhub.service.ucloud.cn/ucloudcli/ucloud-cli:0.1.20

$ docker pull uhub.service.ucloud.cn/ucloudcli/ucloud-cli:0.1.20
此镜像的构建脚本如下:

FROM ubuntu:18.04RUN apt-get update && apt-get install wget -yRUN wget https://github.com/ucloud/ucloud-cli/releases/download/0.1.20/ucloud-cli-linux-0.1.20-amd64.tgzRUN tar -zxf ucloud-cli-linux-0.1.20-amd64.tgz -C /usr/local/bin/RUN echo "complete -C $(which ucloud) ucloud" >> ~/.bashrc #配置自动补全
2.执行以下命令,以uhub.service.ucloud.cn/ucloudcli/ucloud-cli:0.1.20为镜像启动一个容器,容器名字为ucloud-cli:

RUN-Docker --name ucloud $ Expediting IT CLI -d uhub.service.ucloud.cn/ucloudcli/ucloud-cli:0.1.20
3. Run the following commands to connect to the container ucloud-cli, ready to use after a successful connection ucloud -cli, ucloud init command recommend that you perform the initial configuration.

$ docker exec -it ucloud-cli bash

to sum up

An important significance UCloud CLI is the ability to organic combination of API, affairs, scenes of the support needs of users, the cost of human resource management presented in a standardized form, to provide users with convenient and flexible product solutions. Future, UCloud CLI operation and maintenance will also help get rid of those operational challenges in more usage scenarios, welcome to click to read the description link to download the trial and give feedback. (User's Guide: https://docs.ucloud.cn/software/cli/intro ).

Using command-line tool UCloud CLI, lightweight operating cloud resource management API

Recommended activities: August 17 at the Shanghai Art Museum original state, [UCan afternoon tea - the development of cloud native Kubernetes of technology and operation and maintenance] Sharon will be invited six senior technical experts to conduct in-depth technical discussion and practical Case Studies. Welcome to scan the next Fanger Wei code to enroll!

Using command-line tool UCloud CLI, lightweight operating cloud resource management API

Guess you like

Origin blog.51cto.com/13832960/2430223