Aliyun - Swarm Cluster Client

1. 概述

Aliyun将于20191231停止SwarmPortal的技术支持,如何完成日后aliyun swarm cluster的维护,准备了一个脚本,可以完成template的更新。如果需要登录到docker container,可以登录到node完成;如果需要查看日志,可以通过logstore完成。

2. 参考文档

3. 使用说明

安装依赖

$ python3 aliyun_cs.py
Welcome to the aliyun cluster service shell. Type help or ? to list commands.
[malong clusters]# ?

Documented commands (type help <topic>):
========================================
bye  clusters  help  projects  select

[malong clusters]# ? clusters

        Get clusters list: clusters
        or get a cluster details: clusters <cluster_id>

[malong clusters]# ? select

        Select a cluster: select <cluster_id>

[malong clusters]# ? projects

        Get projects list: projects
        or get a project details: projects <project_name>
        or update a project template: projects <project_name> update <new_project_version> <new_project_template_filepath>

4. 代码奉上

import cmd, shlex, subprocess, json, requests
from functools import partial
from subprocess import check_call as _call


call = partial(_call, shell=True)


def exec_cmd(command_line):
    try:
        args = shlex.split(command_line)
        # res = subprocess.run(args, stdout=subprocess.PIPE)
        res = subprocess.check_output(args, encoding='utf8')
        data = json.loads(res)
    except Exception as e:
        print(e)
        return None  # todo should abort
    return data


class AliyunClusterInfo(object):
    name = None
    cluster_id = None
    region_id = None
    state = None
    master_url = None
    cluster_type = None
    created = None
    updated = None
    ca_pem_file = None
    cert_pem_file = None
    key_pem_file = None

    def __repr__(self):
        return f'{self.name:30} {self.cluster_id:33} {self.cluster_type:10} {self.state:10} {self.region_id:11} {self.updated:25}'

    @classmethod
    def from_dict(cls, data):
        info = cls()
        info.__dict__ = data
        return info


def get_clusters():
    data = exec_cmd('aliyun cs GET /clusters')
    clusters = [AliyunClusterInfo.from_dict(item) for item in data]
    clusters = sorted(clusters, key=lambda x: x.name)
    clusters = filter(lambda x: x.cluster_type == 'aliyun', clusters)  # ignore ManagedKubernetes
    return clusters


def get_a_cluster(cluster_id):
    return exec_cmd(f'aliyun cs GET /clusters/{cluster_id}')


def select_a_cluster(cluster_id):
    ca_pem_file = f'.certs/{cluster_id}.ca.pem'
    cert_pem_file = f'.certs/{cluster_id}.cert.pem'
    key_pem_file = f'.certs/{cluster_id}.key.pem'

    call(f'[ -d .certs ] || mkdir .certs')
    call(f'''[ -f {ca_pem_file} ] || aliyun cs GET /clusters/{cluster_id}/certs | jq '.ca' | sed 's/"//g' | sed 's/\\\\n/\\n/g' > {ca_pem_file}''')
    call(f'''[ -f {cert_pem_file} ] || aliyun cs GET /clusters/{cluster_id}/certs | jq '.cert' | sed 's/"//g' | sed 's/\\\\n/\\n/g' > {cert_pem_file}''')
    call(f'''[ -f {key_pem_file} ] || aliyun cs GET /clusters/{cluster_id}/certs | jq '.key' | sed 's/"//g' | sed 's/\\\\n/\\n/g' > {key_pem_file}''')

    data = get_a_cluster(cluster_id)
    cluster_info = AliyunClusterInfo.from_dict(data)
    cluster_info.ca_pem_file = ca_pem_file
    cluster_info.cert_pem_file = cert_pem_file
    cluster_info.key_pem_file = key_pem_file
    return cluster_info


def get_projects(cluster_info: AliyunClusterInfo, project_name=None):
    request_url = f'{cluster_info.master_url}/projects/?services=true&containers=true&q={project_name}' \
        if project_name else f'{cluster_info.master_url}/projects/?services=true&containers=true'
    res = requests.get(request_url,
                       verify=cluster_info.ca_pem_file,
                       cert=(cluster_info.cert_pem_file, cluster_info.key_pem_file))
    my_projects = list(filter(lambda x: not x['name'].startswith('acs'), res.json()))
    return my_projects


def update_a_project_template(cluster_info: AliyunClusterInfo, project_name, new_version, new_template):
    body = {'template': new_template, 'version': new_version}
    request_url = f'{cluster_info.master_url}/projects/{project_name}/update'
    resp = requests.post(
        request_url, json=body, verify=cluster_info.ca_pem_file,
        cert=(cluster_info.cert_pem_file, cluster_info.key_pem_file))

    if resp.status_code == 202:
        print(f'Project {project_name} in {cluster_info.name}({cluster_info.cluster_id}) updated to version {new_version} success.')
    else:
        print(f'ERROR: Fail to update project {project_name} in {cluster_info.name}({cluster_info.cluster_id}) due to:')
        print(resp.status_code)
        print(resp.text)


class AliyunCsShell(cmd.Cmd):
    intro = 'Welcome to the aliyun cluster service shell. Type help or ? to list commands.'
    prompt = '[malong clusters]# '

    def __init__(self):
        super().__init__()
        self.cluster_info: AliyunClusterInfo = None

    def do_select(self, arg):
        """
        Select a cluster: select <cluster_id>
        """
        if len(arg) != 33:
            print(f'{arg} error, should be a valid cluster_id')
            return

        self.cluster_info = select_a_cluster(arg)
        print('select cluster_id success')

    def do_clusters(self, arg):
        """
        Get clusters list: clusters
        or get a cluster details: clusters <cluster_id>
        """
        if not arg:
            print(f"{'='*40}Clusters Table{'='*40}")
            clusters = get_clusters()
            for item in clusters:
                print(item)
        else:
            if len(arg) != 33:  # the length of cluster_id should be 33
                print(f'cluster_id {arg} error')
                return
            print(f"{'='*40}Cluster {arg} Details{'='*40}")
            data = get_a_cluster(arg)
            print(json.dumps(data, indent=4))

    def do_projects(self, arg):
        """
        Get projects list: projects
        or get a project details: projects <project_name>
        or update a project template: projects <project_name> update <new_project_version> <new_project_template_filepath>
        """
        if not self.cluster_info:
            print(f'Please select cluster_id first')
            return

        args = arg.split() if arg else []
        if not args:
            my_projects = get_projects(self.cluster_info)

            print(f"{'='*40}Projects Details{'='*40}")
            print(json.dumps(my_projects, indent=4))

            print(f"{'='*40}Projects Summary{'='*40}")
            print(f'my projects num: {len(my_projects)}')
            print(f'my projects:')
            for proj in my_projects:
                print(f"{proj['name']:40} {proj['version']:10} {proj['current_state']:10} {proj['updated']}")
        elif len(args) == 1:
            project_name = args[0]
            print(f"{'='*40}Project {project_name} Template{'='*40}")
            projects = get_projects(self.cluster_info, project_name=project_name)
            if not projects:
                print(f'not exists project: {project_name}')
                return
            for proj in projects:
                print(f"{'=' * 20}cluster_id: {self.cluster_info.cluster_id}, cluster_name: {self.cluster_info.name}")
                print(f"{'=' * 20}project_name: {proj['name']}, project_version: {proj['version']}, project_template: \n{proj['template']}")
        elif len(args) == 4:
            if args[1] != 'update':
                print('only support update operation')
                return
            project_name = args[0]
            new_proj_version = args[2]
            new_proj_template = args[3]
            with open(new_proj_template, 'r') as f:
                new_proj_template = f.read()
            update_a_project_template(self.cluster_info, project_name, new_proj_version, new_proj_template)
        else:
            print('not support command, please type "help projects" for help')

    def do_bye(self, arg):
        """
        Bye bye
        """
        exit()


if __name__ == '__main__':
    AliyunCsShell().cmdloop()
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,163评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,301评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,089评论 0 352
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,093评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,110评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,079评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,005评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,840评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,278评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,497评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,394评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,980评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,628评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,649评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,548评论 2 352

推荐阅读更多精彩内容