用python给网易蜂巢写一个命令行client

不久之前,兵哥利用 golang 写过一款管理 网易蜂巢 在线资源的 CLI 管理工具。受此启发,正好也用python写一个练练手。同时也借鉴了一下iaas 层 openstack cli client的呈现方式。

主要完成了蜂巢资源的增删改查生命周期操作。

项目地址:https://github.com/163yun/comb-py

hzhuangzhexiao@debian:~/comb_client# ./comb.py   
Usage: comb.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  app-image-list
  container-create
  container-delete
  container-flow
  container-image-list
  container-list
  container-restart
  container-show
  container-to-image
  repositories-list
  repositories-show

安装

git clone https://github.com/163yun/comb-py.git
cd comb

依赖

click
tabulate

安装依赖

pip install click
pip install tabulate

使用

认证

蜂巢 API 需要使用 API Token 来发起 API 请求。 请到 https://c.163.com 页面登陆到你的账户,查看你的 Access Key 和 Access Secret。

cp auth.conf.sample auth.conf

然后编辑你的 auth.conf 文件,将ACCESS_KEYACCESS_SECRET 替换为你的即可。

[DEFAULT]
COMB_OPENAPI = https://open.c.163.com
ACCESS_KEY = 07ed767760f74d8a868071144d1048e8
ACCESS_SECRET = d965faa27f794e588c412ad90b6340fc

列出所有镜像

hzhuangzhexiao@debian:~/comb_client# ./comb.py app-image-list
+-------+------------+--------+----------+
| id    | name       | tag    | weight   |
|-------+------------+--------+----------|
| 22568 | minimal    | latest | 0        |
| 35630 | myss2-0528 | latest | 1000     |
| 30656 | myss       | latest | 0        |
| 26413 | nagios     | v1     | 0        |
| 22567 | aas        | latest | 0        |
| 1     | tomcat     | 7.0.62 | 1000     |
| 5     | php        | 5.5    | 1000     |
| 6     | nodejs     | 0.12.2 | 1000     |
| 7     | python     | 2.7    | 1000     |
| 9     | ruby       | 1.9    | 1000     |
| 26369 | tomcat_apm | latest | 0        |
+-------+------------+--------+----------+

创建容器

hzhuangzhexiao@debian:~/comb_client# ./comb.py container-create --charge_type 1 --spec_id 1 --image_type 1 --image_id 10005 --name testNew

{"charge_type": "1", "image_id": "10005", "bandwidth": "", "name": "testNew", "use_public_network": "", "image_type": "1", "network_charge_type": "", "spec_id": "1", "desc": ""}
{u'id': 631746}  

hzhuangzhexiao@debian:~/comb_client# ./comb.py   container-list
+--------+---------+-------------+---------------+------------+
| id     | name    | status      | public_ip     | image_id   |
|--------+---------+-------------+---------------+------------|
| 631746 | testNew | create_succ | 59.111.91.67  | 10005      |
| 628306 | test    | create_succ | 59.111.91.23  | 21697      |
| 193887 | myss2   | create_succ | 59.111.72.128 | 30656      |
+--------+---------+-------------+---------------+------------+

列出所有容器

hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-list
+--------+---------+-------------+---------------+------------+
| id     | name    | status      | public_ip     | image_id   |
|--------+---------+-------------+---------------+------------|
| 631746 | testNew | create_succ | 59.111.91.67  | 10005      |
| 628306 | test    | create_succ | 59.111.91.23  | 21697      |
| 193887 | myss2   | create_succ | 59.111.72.128 | 30656      |
+--------+---------+-------------+---------------+------------+

查询容器信息

hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-show 631746
+---------------------+----------------------+
| Field               | Value                |
|---------------------+----------------------|
| id                  | 631746               |
| bandwidth           | 100                  |
| charge_type         | 1                    |
| created_at          | 2016-09-02T06:41:48Z |
| desc                |                      |
| env_var             |                      |
| image_id            | 10005                |
| name                | testNew              |
| network_charge_type | 2                    |
| private_ip          | 10.173.32.82         |
| public_ip           | 59.111.91.67         |
| replicas            | 1                    |
| spec_id             | 1                    |
| ssh_key_ids         |                      |
| status              | create_succ          |
| updated_at          | 2016-09-02T06:42:22Z |
| use_public_network  | 1                    |
+---------------------+----------------------+

查询已用的流量

hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-flow  631746
+---------------------+---------+
| Field               | Value   |
|---------------------+---------|
| container_up_flow   | 0.00B   |
| container_down_flow | 0.00B   |
+---------------------+---------+

hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-flow 193887
+---------------------+---------+
| Field               | Value   |
|---------------------+---------|
| container_up_flow   | 18.61GB |
| container_down_flow | 30.66GB |
+---------------------+---------+

重启容器

hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-restart 631746
hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-list
+--------+---------+--------------+---------------+------------+
| id     | name    | status       | public_ip     | image_id   |
|--------+---------+--------------+---------------+------------|
| 631746 | testNew | restart_succ | 59.111.91.67  | 10005      |
| 628306 | test    | create_succ  | 59.111.91.23  | 21697      |
| 193887 | myss2   | create_succ  | 59.111.72.128 | 30656      |
+--------+---------+--------------+---------------+------------+

删除容器

hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-delete 631746
hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-list
+--------+--------+-------------+---------------+------------+
| id     | name   | status      | public_ip     | image_id   |
|--------+--------+-------------+---------------+------------|
| 628306 | test   | create_succ | 59.111.91.23  | 21697      |
| 193887 | myss2  | create_succ | 59.111.72.128 | 30656      |
+--------+--------+-------------+---------------+------------+

容器镜像列表

hzhuangzhexiao@debian:~/comb_client# ./comb.py   container-image-list
+-------+------------+--------------+
|    id | name       | tag          |
|-------+------------+--------------|
| 22568 | minimal    | latest       |
| 30656 | myss       | latest       |
| 26413 | nagios     | v1           |
| 21640 | mongodb    | 3.2.0        |
| 10005 | centos     | 6.7          |
| 20837 | tomcat     | 7.0.28       |
| 21651 | nodejs     | 5.7.0        |
| 10029 | debian     | 7.8          |
| 20838 | django     | 1.9.1        |
| 20836 | jdk        | 1.7.0_03     |
| 20834 | jenkins    | 1.642.1      |
| 20175 | LAMP       | latest       |
| 10037 | mysql      | 5.6          |
| 20835 | nginx      | 1.2.1        |
| 10036 | redis      | 2.8.4        |
| 1003  | ubuntu     | 14.04        |
| 38664 | wordpress  | 4.5.2        |
+-------+------------+--------------+

镜像列表

hzhuangzhexiao@debian:~/comb_client# ./comb.py repositories-list

+-----------+-------------+-------------+--------------+-------------+----------------------+
| repo_id   | user_name   | repo_name   | open_level   | tag_count   | updated_at           |
|-----------+-------------+-------------+--------------+-------------+----------------------|
| 2095      | fcyiqiao    | minimal     | 1            | 1           | 2016-06-03T09:59:04Z |
| 18312     | fcyiqiao    | myss2-0528  | 0            | 1           | 2016-05-28T14:43:05Z |
| 10671     | fcyiqiao    | myss        | 0            | 1           | 2016-05-06T13:23:21Z |
| 5331      | fcyiqiao    | nagios      | 1            | 1           | 2016-04-15T04:47:44Z |
| 2093      | fcyiqiao    | aas         | 0            | 1           | 2016-03-18T08:57:53Z |
+-----------+-------------+-------------+--------------+-------------+----------------------+

查询镜像详情

hzhuangzhexiao@debian:~/comb_client# ./comb.py repositories-show 5331
+-------------+----------------------+
| Field       | Value                |
|-------------+----------------------|
| repo_id     | 5331                 |
| user_name   | fcyiqiao             |
| repo_name   | nagios               |
| open_level  | 1                    |
| base_desc   |                      |
| detail_desc |                      |
| tag_count   | 1                    |
| created_at  | 2016-04-15T03:09:27Z |
| updated_at  | 2016-04-15T04:47:44Z |
+-------------+----------------------+

tips

当使用 comb 管理大量容器时,可以通过shell循环实现容器资源的批量操作

批量创建

hzhuangzhexiao@debian:~/comb_client# for i in $(seq 1 5); do ./comb.py container-create --charge_type 1 --spec_id 1 --image_type 1 --image_id 10005 --name testCentos$i ; done


hzhuangzhexiao@debian:~/comb_client# ./comb.py container-list
+--------+-------------+-------------+---------------+------------+
| id     | name        | status      | public_ip     | image_id   |
|--------+-------------+-------------+---------------+------------|
| 635717 | testCentos5 | create_succ | 59.111.91.75  | 10005      |
| 635716 | testCentos4 | create_succ | 59.111.91.74  | 10005      |
| 635715 | testCentos3 | create_succ | 59.111.91.73  | 10005      |
| 635714 | testCentos2 | create_succ | 59.111.91.72  | 10005      |
| 635713 | testCentos1 | create_succ | 59.111.91.71  | 10005      |
| 634664 | testNew2    | create_succ | 59.111.91.69  | 10005      |
| 634663 | testNew1    | create_succ | 59.111.91.68  | 10005      |
| 628306 | test        | create_succ | 59.111.91.23  | 21697      |
| 193887 | myss2       | create_succ | 59.111.72.128 | 30656      |
+--------+-------------+-------------+---------------+------------+

批量删除名字中含有testNew的容器

hzhuangzhexiao@debian:~/comb_client# ./comb.py container-list |grep testNew |awk '{print $2}' | xargs -n 1 ./comb.py container-delete 

hzhuangzhexiao@debian:~/comb_client# ./comb.py container-list
+--------+-------------+-------------+---------------+------------+
| id     | name        | status      | public_ip     | image_id   |
|--------+-------------+-------------+---------------+------------|
| 635717 | testCentos5 | create_succ | 59.111.91.75  | 10005      |
| 635716 | testCentos4 | create_succ | 59.111.91.74  | 10005      |
| 635715 | testCentos3 | create_succ | 59.111.91.73  | 10005      |
| 635714 | testCentos2 | create_succ | 59.111.91.72  | 10005      |
| 635713 | testCentos1 | create_succ | 59.111.91.71  | 10005      |
| 628306 | test        | create_succ | 59.111.91.23  | 21697      |
| 193887 | myss2       | create_succ | 59.111.72.128 | 30656      |
+--------+-------------+-------------+---------------+------------+

批量删除所有使用centos 6.7 镜像创建的容器

hzhuangzhexiao@debian:~/comb_client# ./comb.py container-list |grep 10005  |awk '{print $2}' | xargs -n 1 ./comb.py container-delete 
hzhuangzhexiao@debian:~/comb_client# ./comb.py container-list
+--------+--------+-------------+---------------+------------+
| id     | name   | status      | public_ip     | image_id   |
|--------+--------+-------------+---------------+------------|
| 628306 | test   | create_succ | 59.111.91.23  | 21697      |
| 193887 | myss2  | create_succ | 59.111.72.128 | 30656      |
+--------+--------+-------------+---------------+------------+

面向开发者的 workflow

通过comb cli,你可以直接在开发机器上保存镜像,推送到蜂巢,然后根据镜像的image id
直接构建蜂巢容器。甚至不需要登录到web端进行操作。

docker login -u YOUR_ACCOUNT -p YOUR_PASSWD hub.c.163.com

列出本地的docker images。

hzhuangzhexiao@debian:~/comb_client# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
jenserat/seafile                                  latest              95d48661ce75        1 months ago        362 MB
ubuntu                                            14.04               e17b56e5200a        2 months ago        188 MB
ubuntu                                            latest              e17b56e5200a        2 months ago        188 MB
hub.c.163.com/fconline/shadowsocks               new                 c0ff81cd9b9c        3 months ago        363.2 MB


hzhuangzhexiao@debian:~/comb_client# docker tag e17b56e5200a  hub.c.163.com/fcyiqiao/seafile:v1
hzhuangzhexiao@debian:~/comb_client# docker push hub.c.163.com/fcyiqiao/seafile:v1
The push refers to a repository [hub.c.163.com/fcyiqiao/seafile]
5f70bf18a086: Pushed 
d3492de15d7c: Pushed 
01fbb4b5fa1b: Pushed 
2a4049cf895d: Pushed 
v1: digest: sha256:209272e2bd2678634c96999633c39e0ad7303f7d72793791f289f3c0b3cedf40 size: 4099
hzhuangzhexiao@debian:~/comb_client# 

利用 comb client 进行镜像查询。可见我们刚才推送的镜像id为49475.

hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-image-list
+-------+------------+--------------+
| id    | name       | tag          |
|-------+------------+--------------|
| 49475 | seafile    | v1           |
| 22568 | minimal    | latest       |
| 35630 | myss2-0528 | latest       |
| 30656 | myss       | latest       |
| 26413 | nagios     | v1           |
| 22567 | aas        | latest       |
| 21640 | mongodb    | 3.2.0        |
| 21699 | postgres   | 9.5.1        |
| 21697 | centos     | 6.5          |
| 20769 | ubuntu     | 15.04        |
| 20770 | ubuntu     | 16.04        |
| 38664 | wordpress  | 4.5.2        |
+-------+------------+--------------+

利用镜像构建容器。

hzhuangzhexiao@debian:~/comb_client#  ./comb.py container-create --charge_type 1 --spec_id 1 --image_type 1 --image_id  49475  --name  testSeafile


hzhuangzhexiao@debian:~/comb_client# ./comb.py  container-list
+--------+-------------+-------------+---------------+------------+
| id     | name        | status      | public_ip     | image_id   |
|--------+-------------+-------------+---------------+------------|
| 637165 | testSeafile | create_succ | 59.111.91.82  | 49475      |
| 628306 | test        | create_succ | 59.111.91.23  | 21697      |
+--------+-------------+-------------+---------------+------------+

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

推荐阅读更多精彩内容