LoginSignup
1
0

More than 3 years have passed since last update.

アリババクラウドのDevOps機能を理解する

Last updated at Posted at 2020-11-30

この記事では、Alibaba Cloud上のDevOps機能を探り、理解するための簡単なDevOpsツールを構築していきます。

本ブログは英語版からの翻訳です。オリジナルはこちらからご確認いただけます。一部機械翻訳を使用しております。翻訳の間違いがありましたら、ご指摘いただけると幸いです。

Alibaba Cloud Tech Share執筆者、ジョン・ハンリー。Tech Shareは、クラウドコミュニティ内で技術的な知識やベストプラクティスを共有することを奨励するAlibaba Cloudのインセンティブプログラムです。

ここでは、Alibaba CloudにDevOpsを適用するところから始めます。私は、シンプルなものから始めてシンプルなツールを構築し、その後、より複雑な実世界のDevOpsツールを使ってフォローアップしていく予定です。基本的なことから始めて、低レベルの機能を理解することがポイントです。

今は、ロードバランスが取れていてオートスケーリングができるテストサイトができたので、Alibaba Cloud CLIPython SDKについてもっと勉強したいと思っています。開発中、ECSインスタンスで公開しているファイルに変更を加える必要があることがよくあります。自動スケーリンググループはイメージから構築されているので、イメージを変更するのは手間と時間がかかります。テスト時には、迅速に編集、デプロイ、デバッグ、改善を行いたいです。つまり、ECSインスタンスにファイルを一度にアップロードするための迅速な方法が必要になります。

その1. CLIでの作業

SSH GUI ベースのプログラムを使ってファイルをコピーするのは簡単です。しかし、自動スケーリングでは、どれだけのインスタンスが実行されているのか、そのIPアドレスはわかりません。これらの変数はいつでも変更できます。今日は、CLIだけから始めて、CLIとPythonを組み合わせて改良し、以下のようにプログラムを作ってみます。

1、ロードバランサー(SLB)に接続されたECSインスタンスのリストを取得します。
2、各ECSインスタンスについて、パブリックIPアドレスを取得します。
3、各ECSインスタンスのためにSFTPを使用してファイルをアップロードします。
これは簡単に聞こえますが、これにはいくつかの作業が必要になります。

SLBで作業するためには、SLB用のSDKをインストールする必要があります。c:Python27Scriptsがパスにある状態で以下のコマンドを実行します。

# pip install aliyun-python-sdk-slb

アカウントに入っているサーバーロードバランサーをリストアップしてみましょう。リージョンを自分のリージョンに置き換えてください。

# aliyuncli slb DescribeLoadBalancers

結果はこんな感じです。

{
    "LoadBalancers": {
        "LoadBalancer": [
            {
                "VpcId": "",
                "RegionIdAlias": "us-west-1",
                "Address": "47.254.99.119",
                "AddressType": "internet",
                "LoadBalancerStatus": "active",
                "LoadBalancerName": "NeoPrime-SLB",
                "PayType": "PayOnDemand",
                "SlaveZoneId": "us-west-1b",
                "ResourceGroupId": "rg-abcdefe6zn3hvwi",
                "CreateTimeStamp": 1526271303000,
                "VSwitchId": "",
                "LoadBalancerId": "lb-abcdef1a7bf644x6s1fzg",
                "InternetChargeType": "4",
                "MasterZoneId": "us-west-1a",
                "NetworkType": "classic",
                "CreateTime": "2018-05-14T12:15Z",
                "RegionId": "us-west-1"
            }
        ]
    },
    "TotalCount": 1,
    "PageNumber": 1,
    "RequestId": "3AB8086E-1111-2222-3333-B4BC43AB548E",
    "PageSize": 1
}

LoadBalancerId:

"LoadBalancerId" "lb-abcdef1a7bf644x6s1fzg”,

次に、SLBの下で実行しているECSインスタンスを表示したいと思います。

# aliyuncli slb DescribeLoadBalancersRelatedEcs --LoadBalancerId lb-abcdef1a7bf644x6s1fzg

結果はこんな感じです。

{
    "LoadBalancers": {
        "LoadBalancer": [
            {
                "Count": 2,
                "MasterSlaveVServerGroups": {
                    "MasterSlaveVServerGroup": []
                },
                "VServerGroups": {
                    "VServerGroup": []
                },
                "BackendServers": {
                    "BackendServer": [
                        {
                            "NetworkType": "vpc",
                            "VmName": "i-0123451kxyhfs89rykzg",
                            "Weight": 50
                        },
                        {
                            "NetworkType": "vpc",
                            "VmName": "i-01234538slefk99bps9h",
                            "Weight": 50
                        }
                    ]
                },
                "LoadBalancerId": "lb-abcdef1a7bf644x6s1fzg"
            }
        ]
    },
    "Message": "successful",
    "RequestId": "D3FA6102-B103-4CC5-9482-2F688C38DC7D",
    "Success": true
}

各インスタンスのVmName:

            "VmName": "i-0123451kxyhfs89rykzg",

            "VmName": "i-01234538slefk99bps9h",

次に、最初のECSインスタンスのIPアドレスを表示したいと思います。注意:このコマンドは1行です。

# aliyuncli ecs DescribeInstanceAttribute --InstanceId i-0123451kxyhfs89rykzg --filter PublicIpAddress.IpAddress[0]

結果は以下のようになります。

”47.254.99.120"

次に、他のECSインスタンスのIPアドレスを表示したいと思います。注意:このコマンドは1行です。

# aliyuncli ecs DescribeInstanceAttribute --InstanceId i-01234538slefk99bps9h --filter PublicIpAddress.IpAddress[0]

結果は以下のようになります。

”47.254.99.121"

それは簡単でした。あとはSSHを使って、SFTPプロトコルを使ってECSインスタンスにファイルをアップロードするだけです。私はBitviseのSSHプログラムを使用しているので、ファイルのアップロードにはBitviseのSFTPクライアントを使用しています。以下のコマンドはWindows用です。自動スケーリンググループを作成したときに指定したキーペアが必要です。

set CMD="put -o c:/tmp/hello.txt /var/www/html/hello.txt" 

sftpc -keypairfile=Test-KeyPair.pem root@47.254.99.120 -cmd=%CMD% 
sftpc -keypairfile=Test-KeyPair.pem root@47.254.99.121 -cmd=%CMD%

繰り返しになりますが、これは簡単です。しかし、明日や来月になると、稼働しているインスタンスが違うかもしれないし、IPアドレスも違うかもしれません。これは、ECSインスタンスIDとパブリックIPアドレスを計算するバッチスクリプトを編集しなければならないことを意味します。解決策は、これを自動化するためにCLIとPythonを組み合わせることです。

その2. CLIをPythonで自動化する

このパートで言及したファイルのzipパッケージをダウンロードすることができます。slb_upload.zipのダウンロード

pythonのコードはPython 3.xです。

slb_upload.pyがメインプログラムです。このプログラムは3つまたは4つの引数をとります。サーバーロードバランサが1つしかない場合は、4つ目の引数を指定する必要はありません。複数の SLB がある場合は、SLB ID を指定します。

使用方法: slb_upload local_path remote_path [slb_id]

例のコマンドです。

python slb_upload c:\work\websites\neoprime.xyz\hello.html /var/www/html/hello.html

myslb.py ファイルには、SLB および ECS とのインタフェースをとるためのルーチンが含まれています。このファイルには、以下の関数が含まれています。

1、ECS_UploadFIle - SFTP を使用してファイルを ECS インスタンスにアップロードします。
2、ECS_GetPublicIp - SLB から VmName を取得し、ECS インスタンスのパブリック IP アドレスを取得します。
3、SLB_GetServerLoadBalancers - Sever ロードバランサーの配列を返します。
4、SLB_GetBackendInstances - SLB にアタッチされた ECS インスタンスの配列を返します。
ソースファイル: myslb.py

import json
import os
import pysftp
import subprocess
import sys
import myutils

#
# This value is the full path to the CLI
#
program_cli = 'C:\\Python27\\Scripts\\aliyuncli.exe'

#
# This function will take an IP Address and upload a file using SFTP
#
# ip - this is the Public IP address
# username - This is the username to use with the Key Pair. For Ubuntu this is "root"
# keypair - This is the SSK Key Pair (PEM file) to connect to the ECS instance
# local_path - This is the path on the Windows system of the file to be uploaded
# remote_path - This is the path on the ECS instance. This needs to be a full path.
#
# WARNING: There is a security issue with this code. This function does NOT verify the
# host fingerprint.
#

def ECS_UploadFile(ip, username, keypair, local_path, remote_path):
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None   
    try:
        with pysftp.Connection(
                    ip,
                    username=username,
                    private_key=keypair,
                    cnopts=cnopts) as sftp:

            myutils.printf("Uploading %s to %s:%s", local_path, ip, remote_path)
            sftp.put(local_path, remote_path)

    except Exception as ex:
        print("Exception: ", ex)
        sys.exit(-1)

#
# This function will take a VmName (from the SLB json) and return the Public IP Address
#
# vmname - This is the VmName value returned from "aliyuncli slb DescribeLoadBalancersRelatedEcs"
#

def ECS_GetPublicIp(vmname):
    args =    [
            program_cli,
            'ecs',
            'DescribeInstanceAttribute',
            '--InstanceId',
            vmname,
            '--filter',
            'PublicIpAddress.IpAddress[0]'
        ]

    process = subprocess.Popen(args, stdout=subprocess.PIPE)
    (output, err) = process.communicate()
    rc = process.wait()

    data = json.loads(output)

    return data

#
# This function will return a list of Server Load Balancers
#
# This CLI command will list the SLBs
# aliyuncli slb DescribeLoadBalancers
#

def SLB_GetServerLoadBalancers():
    args =    [
            program_cli,
            'slb',
            'DescribeLoadBalancers',
            '--filter',
            'LoadBalancers.LoadBalancer[].LoadBalancerId'
        ]

    process = subprocess.Popen(args, stdout=subprocess.PIPE)
    (output, err) = process.communicate()
    rc = process.wait()

    if rc != 0:
        return []

    data = json.loads(output)

    return data

#
# List the ECS instances running under this load balancer
# aliyuncli slb DescribeLoadBalancersRelatedEcs --LoadBalancerId lb-abcdef1a7bf644x6s1012
#

def SLB_GetBackendInstances(slb_id):
    args =    [
            program_cli,
            'slb',
            'DescribeLoadBalancersRelatedEcs',
            '--LoadBalancerId',
            slb_id
        ]

    try:
        print("Describing Load Balancer ECS Instances")
        process = subprocess.Popen(args, stdout=subprocess.PIPE)
        (output, err) = process.communicate()
        rc = process.wait()
        if rc != 0:
            return []

    except Exception as ex:
        print("Exception: ", ex)
        return []

    #print(output)

    data = json.loads(output)

    slbs = data['LoadBalancers']
    slb = slbs['LoadBalancer'][0]
    bes = slb['BackendServers']['BackendServer']

    return bes

ソースファイル: slb_upload.py

import json
import os
import pysftp
import subprocess
import sys
import myutils
import myslb

#
# This value is the full path to the CLI
#
program_cli = 'C:\\Python27\\Scripts\\aliyuncli.exe'

# This is the SSH login
username ="root"

# Key Pair for SSH to connect to the ECS instance
key_pair = "../Test-KeyPair.pem"

# Local file to upload to the ECS instance
# local_path = "c:/tmp/hello.txt"

# Remote file that will be uploaded to the ECS instance
# remote_path = "/var/www/html/hello.txt"

# This is the Alibaba Cloud SLB that we want to work with
slb_id = ''

# Array of Backend Servers (ECS instances attached to a server load balancer
bes = []

if len(sys.argv) < 3:
    print("Error: Missing command line arguments")
    print("Usage: slb_upload local_path remote_path [slb_id]")
    sys.exit(-1)

local_path = sys.argv[1]
remote_path = sys.argv[2]

# Verify that the CLI exists
if os.path.exists(sys.argv[0]) == False:
    print("Error: Missing aliyun CLI: ", args[0])
    sys.exit(-1)

# Get the list of Server Load Balancers
slbs = myslb.SLB_GetServerLoadBalancers()

if len(slbs) == 0:
    print("Error: You do not have any server load balancers")
    sys.exit(1)

# if there is only one SLB, use that one
if len(slbs) == 1:
    slb_id = slbs[0]
else:
    # if there is more than one SLB, then use the one specified on the command line
    if len(sys.argv) < 4:
        # The user did not specify a Server Load Balancer
        print("Error: You have more than one server load balancer")
        print("Please specify the server load balancer ID on the command line")
        sys.exit(1)

    slb_id = sys.argv[3]

# Make sure that we have an SLB ID (not empty test)
if slb_id == '':
    print("Error: Missing server load balancer ID")
    print("Usage: slb_upload local_path remote_path [slb_id]")
    sys.exit(1)

# Get the list of ECS instances attached to this SLB
bes = myslb.SLB_GetBackendInstances(slb_id)

if len(bes) == 0:
    print("Error: Your SLB does not have any ECS instances attached")
    sys.exit(1)

print("Total Backend ECS Servers: ", len(bes))
#print(slb['BackendServers'])

# We have everything, upload files
print('Processing:')
print("------------------------------------------------------------")
for index, vm in enumerate(bes):
    vmname = vm['VmName']
    #print("VmName: ", vmname)

    ip = myslb.ECS_GetPublicIp(vmname)
    #print("Public IP:", ip)

    myslb.ECS_UploadFile(ip, username, key_pair, local_path, remote_path)
print("------------------------------------------------------------")

sys.exit(0)

その3:PythonとSDKを使った自動化

このパートで紹介したファイルのzipパッケージをダウンロードすることができます。slb_upload_python.zipのダウンロード

その2のコードでは、PythonとCLIを使っていかに迅速にタスクを自動化できるかを示しています。Alibaba Cloud ToolsとSDKを本当に理解し始めるために、次の論理的なステップは、CLIを使用する代わりにプログラムを100%のSDKベースに変換することです。これは次のようになります。

Python SDKを使用する場合、最初のステップは資格情報を設定することです。Alibaba CLIは、HOMEディレクトリに.aliyuncliというディレクトリを作成します。このディレクトリには2つのファイルがあります。

1、configure - このファイルには、地域、jsonなどの優先出力などの項目が含まれています。
2、credentials - このファイルには、アクセスキーとアクセスキーシークレットが含まれています。
CMD プロンプトでこのコマンドを実行します。

aliyuncli configure

CLIでは、いくつかの項目についてプロンプトが表示されます。

1、アリユンアクセスキーID
2、アリユンシークレットアクセスキー
3、デフォルトのリージョン ID
4、デフォルトの出力フォーマット
最初の 2 つの項目は、新しいユーザーを作成したときに保存したキーです。地域については、空白のままにするか、お好みの地域を指定してください。私は us-west-1 を使用しました。出力形式は json を指定します。

次に、myslb.pyモジュールを移植して、CLIコマンドラインの呼び出しをPython SDKの呼び出しに置き換えます。

ソースファイル: myslb.py

############################################################
# Version 1.01
# Date Created: 2018-05-15
# Last Update:  2018-05-16
# https://www.neoprime.io
# Copyright (c) 2018, NeoPrime, LLC
############################################################

"""
Functions to support SLB and ECS
"""

import json
import subprocess
import sys
import myutils
import pysftp

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526 import DescribeInstanceAttributeRequest
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRequest
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRelatedEcsRequest

#
# This function will take an IP Address and upload a file using SFTP
#
# ip - this is the Public IP address
# username - This is the username to use with the Key Pair. For Ubuntu this is "root"
# keypair - This is the SSK Key Pair (PEM file) to connect to the ECS instance
# local_path - This is the path on the Windows system of the file to be uploaded
# remote_path - This is the path on the ECS instance. This needs to be a full path.
#
# WARNING: There is a security issue with this code. This function does NOT verify the
# host fingerprint.
#

def ECS_UploadFile(ip, username, keypair, local_path, remote_path):
    """ This function will take an IP Address and upload a file using SFTP """
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None
    try:
        with pysftp.Connection(
                    ip,
                    username=username,
                    private_key=keypair,
                    cnopts=cnopts) as sftp:

            myutils.printf("Uploading %s to %s:%s", local_path, ip, remote_path)
            sftp.put(local_path, remote_path)

    except Exception as ex:
        print("Exception: ", ex)
        sys.exit(-1)

#
# This function will take a VmName (from the SLB json) and return the Public IP Address
#
# cred - This is the dictionary of credentials (AccessKey, AccessKeyScret, Region)
# vmname - This is the VmName value returned from "aliyuncli slb DescribeLoadBalancersRelatedEcs"
#

def ECS_GetPublicIp(cred, vmname, debugFlag=False):
    """ This function will take a VmName (from the SLB json) and return the Public IP Address """

    client = AcsClient(cred['AccessKey'], cred['AccessKeySecret'], cred['Region'])

    request = DescribeInstanceAttributeRequest.DescribeInstanceAttributeRequest()

    request.set_InstanceId(vmname)

    try:
        response = client.do_action_with_exception(request)
        data = json.loads(response)

    except Exception as ex:
        print("Exception: ", ex)
        return False;

    ip = data['PublicIpAddress']['IpAddress']

    if len(ip) > 0:
        return ip[0]

    return False;

#
# This function will return a list of Server Load Balancers
#
# This CLI command will list the SLBs
# aliyuncli slb DescribeLoadBalancers
#

def SLB_GetServerLoadBalancers(cred, debugFlag=False):
    """ This function will return a list of Server Load Balancers """

    client = AcsClient(cred['AccessKey'], cred['AccessKeySecret'], cred['Region'])

    request = DescribeLoadBalancersRequest.DescribeLoadBalancersRequest()

    try:
        response = client.do_action_with_exception(request)
        data = json.loads(response)

    except Exception as ex:
        print("Exception: ", ex)
        return [];

    slbs = data['LoadBalancers']

    if len(slbs) == 0:
        return []

    ids = []

    for slb in slbs['LoadBalancer']:
        ids.append(slb['LoadBalancerId'])

    return ids;

#
# List the ECS instances running under this load balancer
# aliyuncli slb DescribeLoadBalancersRelatedEcs --LoadBalancerId lb-abcdef1a7bf644x6s1012
#

def SLB_GetBackendInstances(cred, slb_id, debugFlag=False):
    """ List the ECS instances running under this load balancer """

    client = AcsClient(cred['AccessKey'], cred['AccessKeySecret'], cred['Region'])

    request = DescribeLoadBalancersRelatedEcsRequest.DescribeLoadBalancersRelatedEcsRequest()

    request.set_LoadBalancerId(slb_id)

    try:
        response = client.do_action_with_exception(request)
        data = json.loads(response)

    except Exception as ex:
        print("Exception: ", ex)
        return [];

    slbs = data['LoadBalancers']

    if len(slbs['LoadBalancer']) == 0:
        return []

    slb = slbs['LoadBalancer'][0]
    bes = slb['BackendServers']['BackendServer']

    return bes

mycred_acs.py ファイルには、上記のconfigureファイルとcredentialsファイルから資格情報をロードするための非常に単純なルーチンが含まれています。

ソースファイル: mycred_acs.py

############################################################
# Version 1.01
# Date Created: 2018-05-15
# Last Update:  2018-05-16
# https://www.neoprime.io
# Copyright (c) 2018, NeoPrime, LLC
############################################################

""" Automating with Python and the Alibaba Cloud SDKs """

import os
import sys
import platform
import configparser

def getHomeDir():
    dir = ""

    if platform.system() == "Windows":
        dir = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
        pass
    else:
        dir = os.environ['HOME']
        pass

    return dir

def getCredentialsPath():
    dir = getHomeDir()

    dir = dir + "\\.aliyuncli/credentials"

    return dir

def getConfigurePath():
    dir = getHomeDir()

    dir = dir + "\\.aliyuncli/configure"

    return dir

def loadConfigureFile(path, cred, profileName='default'):
    if os.path.exists(path) is False:
        print("Error: Cannot load configure file from file")
        print("File: ", path)
        return False;

    config = configparser.ConfigParser()
    config.read(path)

    try:
        cred['Region'] = config[profileName]['region']

    except Exception as ex:
        print("Exception: ", ex)
        print("Error: Cannot load configure profile name: ", profileName)
        return False;

    return cred;


def loadCredentialsFile(path, profileName='default'):
    cred = {
        'AccessKey': '',
        'AccessKeySecret': '',
        'Region': ''
        }

    if os.path.exists(path) is False:
        print("Error: Cannot load credentials file from file")
        print("File: ", path)
        return False;

    config = configparser.ConfigParser()
    config.read(path)

    try:
        cred['AccessKey'] = config[profileName]['aliyun_access_key_id']
        cred['AccessKeySecret'] = config[profileName]['aliyun_access_key_secret']

    except Exception as ex:
        print("Exception: ", ex)
        print("Error: Cannot load credential profile name: ", profileName)
        return False;

    return cred;

def LoadCredentials(profileName='default'):
    cred_file = getCredentialsPath()
    conf_file = getConfigurePath()

    cred = loadCredentialsFile(cred_file, profileName)

    if cred is False:
        return False;

    loadConfigureFile(conf_file, cred, profileName)

    return cred;

そして、メインプログラム slb_upload.py です。これは、ECS と SLB サポート関数への認証情報の受け渡しをサポートするために少し修正されました。

ソースファイル: slb_upload.py

############################################################
# Version 1.01
# Date Created: 2018-05-15
# Last Update:  2018-05-16
# https://www.neoprime.io
# Copyright (c) 2018, NeoPrime, LLC
############################################################

""" Automating with Python and the Alibaba Cloud SDKs """

import os
import sys
import platform

# Begin: Verify that we are running under Python version 3.6 or greater

if platform.sys.version_info.major < 3:
    print("This software requires Python 3.6 or greater")
    sys.exit(-1)

if platform.sys.version_info.major == 3 and platform.sys.version_info.minor < 6:
    print("This software requires Python 3.6 or greater")
    sys.exit(-1)

if sys.version_info[0] < 3:
    print("This software requires Python 3.6 or greater")
    sys.exit(-1)

# End: Verify that we are running under Python version 3.6 or greater

import myslb
import optparse
from slb_cmdline import process_cmdline, usage
# My library for processing Alibaba Cloud Services (ACS) credentials
import mycred_acs

# This is the SSH login
username = "root"

# Key Pair for SSH to connect to the ECS instance
key_pair = "../Test-KeyPair.pem"

# Local file to upload to the ECS instance
# local_path = "c:/tmp/hello.txt"

# Remote file that will be uploaded to the ECS instance
# remote_path = "/var/www/html/hello.txt"

# This is the Alibaba Cloud SLB that we want to work with
slb_id = ''

# This is the Alibaba Cloud Region that we want to work with
region_id = ''

# Array of Backend Servers (ECS instances attached to a server load balancer
bes = []

# We need a minimum of 2 command line parameters (argv >= 3)
if len(sys.argv) < 3:
    print("Error: Missing command line arguments")
    usage();
    sys.exit(-1)

# Process the command line
(options, args) = process_cmdline()

if len(args) < 2:
    print("Error: Not enough command line arguments")
    usage();
    sys.exit(-1)

if len(args) > 2:
    print("Error: Too many command line arguments")
    usage();
    sys.exit(-1)

local_path = args[0]
remote_path = args[1]

# Load the Alibaba Cloud Credentials (AccessKey)
cred = mycred_acs.LoadCredentials(profileName = options.profile)

if cred is False:
    sys.exit(-1)

if options.debug:
    print(cred)

# Verify that the CLI exists
if os.path.exists(sys.argv[0]) is False:
    print("Error: Missing aliyun CLI: ", sys.argv[0])
    sys.exit(-1)

if options.slb_id:
    slb_id = options.slb_id

if options.slb_id is False:
    # Get the list of Server Load Balancers
    slbs = myslb.SLB_GetServerLoadBalancers(cred, options.debug)

    if options.debug:
        print("Server Load Balancers")
        print(slbs)

    #if len(slbs) == 0:
    n = len(slbs)
    if n == 0:
        print("Error: You do not have any server load balancers")
        sys.exit(1)

    # if there is only one SLB, use that one
    if len(slbs) == 1:
        slb_id = slbs[0]
    else:
        # if there is more than one SLB, then use the one specified on the command line
        # The user did not specify a Server Load Balancer
        print("Error: You have more than one server load balancer")
        print("Please specify the server load balancer ID on the command line")
        sys.exit(1)
else:
    if options.debug:
        print("SLB ID: ", slb_id)

# Make sure that we have an SLB ID (not empty test)
if slb_id == '':
    print("Error: Missing server load balancer ID")
    usage();
    sys.exit(1)

# Get the list of ECS instances attached to this SLB
bes = myslb.SLB_GetBackendInstances(cred, slb_id, options.debug)

#if len(bes) == 0:
n = len(bes)
if n == 0:
    print("Error: Your SLB does not have any ECS instances attached")
    sys.exit(1)

print("Total Backend ECS Servers: ", len(bes))
#print(slb['BackendServers'])

if options.debug:
    print("Backend Servers")
    print(bes)

# We have everything, upload files
print('Processing:')
print("------------------------------------------------------------")
for index, vm in enumerate(bes):
    vmname = vm['VmName']
    #print("VmName: ", vmname)

    ip = myslb.ECS_GetPublicIp(cred, vmname, options.debug)

    if ip is False:
        print("Error: Cannnot determine IP address for instance: ", vmname)
        continue;

    print("Public IP:", ip)

    myslb.ECS_UploadFile(ip, username, key_pair, local_path, remote_path)
print("------------------------------------------------------------")

sys.exit(0)

続きを読む Alibaba Cloud DevOps Cookbook Part 2

開発者向けドキュメント

コマンドラインインターフェイス(CLI):
1、アリババクラウドCLI

サーバーロードバランサー(SLB):
1、サーバーロードバランサーのドキュメントページ
2、サーバーロードバランサー開発者ガイド

Elastic Compute Service(ECS):
1、Elastic Compute Service ドキュメントページ
2、Elastic Compute Service Developer Guide

アリババクラウドは日本に2つのデータセンターを有し、世界で60を超えるアベラビリティーゾーンを有するアジア太平洋地域No.1(2019ガートナー)のクラウドインフラ事業者です。
アリババクラウドの詳細は、こちらからご覧ください。
アリババクラウドジャパン公式ページ

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0