用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閱讀 230,182評論 6 543
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,489評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,290評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,776評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,510評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,866評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,860評論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,036評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,585評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,331評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,536評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,058評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,754評論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,154評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,469評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,273評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,505評論 2 379

推薦閱讀更多精彩內容