用python給網(wǎng)易蜂巢寫一個(gè)命令行client

不久之前饱普,兵哥利用 golang 寫過一款管理 網(wǎng)易蜂巢 在線資源的 CLI 管理工具。受此啟發(fā)坊谁,正好也用python寫一個(gè)練練手费彼。同時(shí)也借鑒了一下iaas 層 openstack cli client的呈現(xiàn)方式。

主要完成了蜂巢資源的增刪改查生命周期操作口芍。

項(xiàng)目地址: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

使用

認(rèn)證

蜂巢 API 需要使用 API Token 來發(fā)起 API 請(qǐng)求箍铲。 請(qǐng)到 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        |
+-------+------------+--------+----------+

創(chuàng)建容器

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      |
+--------+---------+-------------+---------------+------------+

查詢?nèi)萜餍畔?/h3>
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

當(dāng)使用 comb 管理大量容器時(shí),可以通過shell循環(huán)實(shí)現(xiàn)容器資源的批量操作

批量創(chuàng)建

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 鏡像創(chuàng)建的容器

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      |
+--------+--------+-------------+---------------+------------+

面向開發(fā)者的 workflow

通過comb cli翘瓮,你可以直接在開發(fā)機(jī)器上保存鏡像贮折,推送到蜂巢,然后根據(jù)鏡像的image id
直接構(gòu)建蜂巢容器资盅。甚至不需要登錄到web端進(jìn)行操作调榄。

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 進(jìn)行鏡像查詢呵扛∶壳欤可見我們剛才推送的鏡像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        |
+-------+------------+--------------+

利用鏡像構(gòu)建容器。

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      |
+--------+-------------+-------------+---------------+------------+

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末今穿,一起剝皮案震驚了整個(gè)濱河市缤灵,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蓝晒,老刑警劉巖腮出,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異芝薇,居然都是意外死亡胚嘲,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門剩燥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來慢逾,“玉大人,你說我怎么就攤上這事灭红÷绿玻” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵变擒,是天一觀的道長君珠。 經(jīng)常有香客問我,道長娇斑,這世上最難降的妖魔是什么策添? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮毫缆,結(jié)果婚禮上唯竹,老公的妹妹穿的比我還像新娘。我一直安慰自己苦丁,他們只是感情好浸颓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般产上。 火紅的嫁衣襯著肌膚如雪棵磷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天晋涣,我揣著相機(jī)與錄音仪媒,去河邊找鬼。 笑死谢鹊,一個(gè)胖子當(dāng)著我的面吹牛算吩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播撇贺,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼赌莺,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了松嘶?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤挎扰,失蹤者是張志新(化名)和其女友劉穎翠订,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體遵倦,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡尽超,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了梧躺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片似谁。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖掠哥,靈堂內(nèi)的尸體忽然破棺而出巩踏,到底是詐尸還是另有隱情,我是刑警寧澤续搀,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布塞琼,位于F島的核電站,受9級(jí)特大地震影響禁舷,放射性物質(zhì)發(fā)生泄漏彪杉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一牵咙、第九天 我趴在偏房一處隱蔽的房頂上張望派近。 院中可真熱鬧,春花似錦洁桌、人聲如沸渴丸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽曙强。三九已至残拐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間碟嘴,已是汗流浹背溪食。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留娜扇,地道東北人错沃。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像雀瓢,于是被迫代替她去往敵國和親枢析。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容