Harbor客戶端工具,命令行對Harbor進行管理
一、Harborclient工具簡介
項目地址: https://github.com/int32bit/harborclient
Harborclient是Harbor的第三方擴展開源工具,正彌補Harbor不足,它適合開發(fā)和運維人員管理鏡像倉庫、項目等資源,包含的特性如下:
- harborclient參考了OpenStack命令行工具的優(yōu)秀架構(gòu)和設(shè)計模式,使用也和OpenStack命令行非常類似。
- harborclient通過子命令劃分不同的功能,并且所有功能是可擴展的,增加功能只需要在client下增加do_xxx方法即可。主模塊會自動發(fā)現(xiàn)并注冊子命令。
- 相比OpenStack的命令行工具,精簡了部分復(fù)雜功能,重新設(shè)計了大多數(shù)接口,暴露的API更直觀和易用。
- 支持DEBUG模式查看Harbor API調(diào)用過程,便于調(diào)試追蹤。
- 支持timings選項,能夠報告API請求響應(yīng)時間,便于測試Harbor API性能。
- 支持https。
二、安裝Harborclient(docker)
2.1 鏡像拉取/制作
harborclient托管在docker hub中,可以直接拉取已經(jīng)構(gòu)建的鏡像到本地:
$ docker pull krystism/harborclient
docker hub中的鏡像不一定是最新的,建議從源碼中build鏡像,在項目根下運行:
$ git clone https://github.com/int32bit/harborclient
$ docker build -t yourname/harborclient .
2.2 啟動容器
$ docker run -itd \
-e HARBOR_URL="https://192.168.2.19:443" \
-e HARBOR_USERNAME="admin" \
-e HARBOR_PASSWORD="Harbor12345" \
-e HARBOR_PROJECT=1 \
--rm krystism/harborclient
+---------+----------+----------+----------------------+--------------+---------------+
| user_id | username | is_admin | email | realname | comment |
+---------+----------+----------+----------------------+--------------+---------------+
| 1 | admin | 1 | admin@example.com | system admin | admin user |
| 11 | int32bit | 0 | int32bit@example.com | int32bit | for int32bit |
+---------+----------+----------+----------------------+--------------+---------------+
HARBOR_URL: 指定Harbor地址;
HARBOR_USERNAME: 指定Harbor用戶名;
HARBOR_PASSWORD: 指定Harbor用戶密碼;
HARBOR_PROJECT: 指定項目ID號;
2.3 設(shè)置命令自動補全
harborclient支持命令補全,以bash為例,需要首先安裝bash-completion,然后執(zhí)行以下命令配置自動補全:
$ sudo yum install -y bash-completion
$ complete -W $(harbor bash-completion) harbor
此時可以通過tab鍵自動補全參數(shù)以及子命令了。
三、使用
3.1 子命令集參數(shù)說明
- 子命令
get-cert:
get-conf: 查看harbor信息;
info: 查看harbor信息;
job-list:
job-log:
- 參數(shù)
--debug: 詳細打印調(diào)用的harbor API以及參數(shù),并顯示response信息;
--timings: 參數(shù)將打印命令執(zhí)行時調(diào)用的所有API列表并報告響應(yīng)時間;
--timeout: 參數(shù)可以設(shè)置允許的最長響應(yīng)時間(單位: 秒),支持浮點數(shù),超過這個時間未響應(yīng)將導(dǎo)致請求超時異常;
3.2 使用https
harborclient支持https,對應(yīng)使用https的Harbor可以通過--os-cacert選項指定CA證書,也可以通過--insecure跳過認證校驗,相當于curl -k命令。
$ harbor list #使用https的harbor會出現(xiàn)該報錯
CommandError: Unable to authorize user 'admin': Certificate verify failed, please use '--os-cacert' option to specify a CA bundle file to use in verifying a TLS (https) server certificate or use '--insecure' option to explicitly allow client to perform insecure TLS (https) requests.
$ harbor --insecure list
+-----------------------+------------+-----------+------------+------------+------------+----------------------+
| name | project_id | size | tags_count | star_count | pull_count | update_time |
+-----------------------+------------+-----------+------------+------------+------------+----------------------+
| int32bit/busybox | 2 | 715181 | 1 | 0 | 0 | 2017-11-01T07:06:36Z |
| int32bit/golang | 2 | 257883053 | 2 | 0 | 0 | 2017-11-01T12:59:05Z |
| int32bit/golang:1.7.3 | 2 | 257883053 | 2 | 0 | 0 | 2017-11-01T12:59:05Z |
| int32bit/hello-world | 2 | 974 | 1 | 0 | 0 | 2017-11-01T13:22:46Z |
+-----------------------+------------+-----------+------------+------------+------------+----------------------+
3.3 使用案例
(1) 創(chuàng)建用戶
$ harbor --insecure user-create \
--username lidabai \
--password lidabai666 \
--email lidabai@example.com \
--realname newuser \
--comment "I am a new user"
Create user 'lidabai' successfully. #提升創(chuàng)建成功
--username: 指定要創(chuàng)建的用戶名(必須);
--password: 指定創(chuàng)建用戶的用戶密碼(必須);
--email:指定郵箱;
--comment:描述信息;
(2) 刪除用戶
$ harbor --insecure user-delete new-user
Delete user 'new-user' sucessfully.
List repositories and images
(3) 查看鏡像列表
$ harbor list
+-----------------------+------------+-----------+------------+------------+------------+----------------------+
| name | project_id | size | tags_count | star_count | pull_count | update_time |
+-----------------------+------------+-----------+------------+------------+------------+----------------------+
| int32bit/busybox | 2 | 715181 | 1 | 0 | 0 | 2017-11-01T07:06:36Z |
| int32bit/golang:1.7.3 | 2 | 257883053 | 2 | 0 | 0 | 2017-11-01T12:59:05Z |
| int32bit/hello-world | 2 | 974 | 1 | 0 | 0 | 2017-11-01T13:22:46Z |
+-----------------------+------------+-----------+------------+------------+------------+----------------------+
(4) 查看鏡像詳細信息
$ harbor show int32bit/golang:1.7.3
+--------------------+-------------------------------------------------------------------------+
| Property | Value |
+--------------------+-------------------------------------------------------------------------+
| creation_time | 2017-11-01T12:59:05Z |
| description | |
| id | 2 |
| name | int32bit/golang |
| project_id | 2 |
| pull_count | 0 |
| star_count | 0 |
| tag_architecture | amd64 |
| tag_author | |
| tag_created | 2016-11-08T19:32:39.908048617Z |
| tag_digest | sha256:37d263ccd240e113a752c46306ad004e36532ce118eb3131d9f76f43cc606d5d |
| tag_docker_version | 1.12.3 |
| tag_name | 1.7.3 |
| tag_os | linux |
| tag_signature | - |
| tags_count | 2 |
| update_time | 2017-11-01T12:59:05Z |
+--------------------+-------------------------------------------------------------------------+
(5) 查看最熱門鏡像
$ harbor top
+----------------------+------------+------------+
| name | pull_count | star_count |
+----------------------+------------+------------+
| int32bit/busybox | 10 | 0 |
| int32bit/golang | 8 | 0 |
| int32bit/hello-world | 1 | 0 |
+----------------------+------------+------------+
(6) 查看用戶角色
$ harbor member-list
+----------+--------------+---------+---------+
| username | role_name | user_id | role_id |
+----------+--------------+---------+---------+
| admin | projectAdmin | 1 | 1 |
| foo | developer | 5 | 2 |
| test | guest | 6 | 3 |
+----------+--------------+---------+---------+
(7) 查看日志
$ harbor logs
+--------+----------------------+----------+------------+-----------+-----------------------------+
| log_id | op_time | username | project_id | operation | repository |
+--------+----------------------+----------+------------+-----------+-----------------------------+
| 1 | 2017-11-01T06:56:07Z | admin | 2 | create | int32bit/ |
| 2 | 2017-11-01T07:06:36Z | admin | 2 | push | int32bit/busybox:latest |
| 3 | 2017-11-01T12:59:05Z | admin | 2 | push | int32bit/golang:1.7.3 |
| 4 | 2017-11-01T13:22:46Z | admin | 2 | push | int32bit/hello-world:latest |
| 5 | 2017-11-01T14:21:49Z | admin | 2 | push | int32bit/golang:latest |
| 6 | 2017-11-03T20:39:04Z | admin | 3 | create | test/ |
| 7 | 2017-11-03T20:39:22Z | admin | 3 | delete | test/ |
| 8 | 2017-11-03T20:39:38Z | admin | 4 | create | test/ |
| 9 | 2017-11-03T20:49:33Z | admin | 4 | delete | test/ |
+--------+----------------------+----------+------------+-----------+-----------------------------+
(8) 搜索
$ harbor search int32bit
Find 1 Projects:
+------------+----------+--------+------------+----------------------+
| project_id | name | public | repo_count | creation_time |
+------------+----------+--------+------------+----------------------+
| 2 | int32bit | 1 | 3 | 2017-11-01T06:56:07Z |
+------------+----------+--------+------------+----------------------+
Find 3 Repositories:
+----------------------+--------------+------------+----------------+
| repository_name | project_name | project_id | project_public |
+----------------------+--------------+------------+----------------+
| int32bit/busybox | int32bit | 2 | 1 |
| int32bit/golang | int32bit | 2 | 1 |
| int32bit/hello-world | int32bit | 2 | 1 |
+----------------------+--------------+------------+----------------+
(9) 查看復(fù)制目標
$ harbor target-list
+----+----------------------+-------------------------------------+----------+----------+----------------------+
| id | name | endpoint | username | password | creation_time |
+----+----------------------+-------------------------------------+----------+----------+----------------------+
| 1 | test-target | http://192.168.99.101:8888 | admin | - | 2017-11-02T01:35:30Z |
| 2 | test-target-2 | http://192.168.99.101:9999 | admin | - | 2017-11-02T13:43:07Z |
| 3 | int32bit-test-target | http://192.168.99.101:8888/int32bit | admin | - | 2017-11-02T14:28:54Z |
+----+----------------------+-------------------------------------+----------+----------+----------------------+
(10) ping復(fù)制目標倉庫
檢測目標倉庫網(wǎng)絡(luò)是否通暢。
$ harbor target-ping 1
OK
(11) 查看復(fù)制任務(wù)
$ harbor job-list 1
+----+----------------------+-----------+----------+----------------------+
| id | repository | operation | status | update_time |
+----+----------------------+-----------+----------+----------------------+
| 1 | int32bit/busybox | transfer | finished | 2017-11-02T01:35:31Z |
| 2 | int32bit/golang | transfer | finished | 2017-11-02T01:35:31Z |
| 3 | int32bit/hello-world | transfer | finished | 2017-11-02T01:35:31Z |
+----+----------------------+-----------+----------+----------------------+
(12) 查看復(fù)制任務(wù)日志
$ harbor job-log 1
2017-11-02T01:35:30Z [INFO] initializing: repository: int32bit/busybox, tags: [], source URL: http://registry:5000, destination URL: http://192.168.99.101:8888, insecure: false, destination user: admin
2017-11-02T01:35:30Z [INFO] initialization completed: project: int32bit, repository: int32bit/busybox, tags: [latest], source URL: http://registry:5000, destination URL: http://192.168.99.101:8888, insecure: false, destination user: admin
2017-11-02T01:35:30Z [WARNING] the status code is 409 when creating project int32bit on http://192.168.99.101:8888 with user admin, try to do next step
2017-11-02T01:35:30Z [INFO] manifest of int32bit/busybox:latest pulled successfully from http://registry:5000: sha256:030fcb92e1487b18c974784dcc110a93147c9fc402188370fbfd17efabffc6af
2017-11-02T01:35:30Z [INFO] all blobs of int32bit/busybox:latest from http://registry:5000: [sha256:54511612f1c4d97e93430fc3d5dc2f05dfbe8fb7e6259b7351deeca95eaf2971 sha256:03b1be98f3f9b05cb57782a3a71a44aaf6ec695de5f4f8e6c1058cd42f04953e]
2017-11-02T01:35:31Z [INFO] blob sha256:54511612f1c4d97e93430fc3d5dc2f05dfbe8fb7e6259b7351deeca95eaf2971 of int32bit/busybox:latest already exists in http://192.168.99.101:8888
2017-11-02T01:35:31Z [INFO] blob sha256:03b1be98f3f9b05cb57782a3a71a44aaf6ec695de5f4f8e6c1058cd42f04953e of int32bit/busybox:latest already exists in http://192.168.99.101:8888
2017-11-02T01:35:31Z [INFO] blobs of int32bit/busybox:latest need to be transferred to http://192.168.99.101:8888: []
2017-11-02T01:35:31Z [INFO] manifest of int32bit/busybox:latest exists on source registry http://registry:5000, continue manifest pushing
2017-11-02T01:35:31Z [INFO] manifest of int32bit/busybox:latest exists on destination registry http://192.168.99.101:8888, skip manifest pushing
2017-11-02T01:35:31Z [INFO] no tag needs to be replicated, next state is "finished"
(13) 查看資源統(tǒng)計
$ harbor usage
+-----------------------+-------+
| Property | Value |
+-----------------------+-------+
| private_project_count | 0 |
| private_repo_count | 0 |
| public_project_count | 2 |
| public_repo_count | 3 |
| total_project_count | 2 |
| total_repo_count | 3 |
+-----------------------+-------+
(14) 修改用戶密碼
$ harbor change-password lidabai
Old password: ***** #輸入舊密碼
New Password: ***** #輸入新密碼
Retype new Password: ***** #再次輸出新密碼
Update password successfully.
(15) 設(shè)置用戶為管理員
$ harbor promote int32bit
Promote user 'int32bit' as administrator successfully.
3.4 增加自定義子命令
以增加一個echo子命令為例,首先在harborclient/v2目錄下新創(chuàng)建一個模塊文件tests.py,實現(xiàn)echo方法,如下:
from harborclient import base
class TestManager(base.Manager):
def echo(self, message):
return message
在harborclient/v2/client中注冊TestManager:
... # 省略其它import
from harborclient.v2 import tests
class Client(object):
def __init__(self,
username=None,
password=None,
project=None,
baseurl=None,
insecure=False,
cacert=None,
api_version=None,
*argv,
**kwargs):
... # 省略其它Manager
self.tests = tests.TestManager(self)
最后在harborclient/v2/shell.py中注冊新的子命令,注意在shell.py中所有的do_xx方法都會對應(yīng)一條子命令,轉(zhuǎn)化規(guī)則為:
do_a_b_c => a-b-c
比如:
do_user_list => user-list
do_project_show => project-show
方法的doc文檔將轉(zhuǎn)化為echo子命令的幫助信息。因此新增echo子命令,只需要在shell.py中新增do_echo方法:
@utils.arg(
'--message',
metavar='<message>',
dest='message',
required=True,
help='The message to print.')
def do_echo(cs, args):
"""Print a message."""
message = cs.tests.echo(args.message)
print(message)
此時echo子命令就實現(xiàn)了。查看幫助信息:
$ harbor help echo
usage: harbor echo --message <message>
Print a message.
Optional arguments:
--message <message> The message to print.