Skip to content

Commit cc1fd7d

Browse files
author
欧林宝
authored
cli 0.7.1 (#21)
1 parent 9471806 commit cc1fd7d

18 files changed

Lines changed: 456 additions & 339 deletions

File tree

Dockerfile

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
FROM ubuntu
2-
3-
RUN apt-get update
4-
RUN apt-get install -y python-pip
5-
RUN pip install -U jdcloud_cli
6-
1+
FROM alpine:3.6
2+
RUN apk -v --update add \
3+
python \
4+
py-pip \
5+
groff \
6+
less \
7+
mailcap \
8+
wget \
9+
jq \
10+
openssl \
11+
ca-certificates \
12+
py-setuptools \
13+
&& \
14+
pip install -U jdcloud_cli
15+
RUN update-ca-certificates
16+
RUN wget -q -c -O kubectl https://storage.googleapis.com/kubernetes-release/release/v1.8.12/bin/linux/amd64/kubectl \
17+
&& chmod +x kubectl \
18+
&& mv kubectl /usr/local/bin
19+
RUN apk -v --purge del py-pip && \
20+
rm /var/cache/apk/*
21+
722
RUN echo 'eval "$(register-python-argcomplete jdc)"' >> /root/.bashrc
823
RUN echo 'export COLUMNS=100' >> /root/.bashrc
24+
25+
VOLUME /root/.jdc
26+
WORKDIR /root
27+
ENTRYPOINT ["jdc"]
28+

jdcloud_cli/client_factory.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from jdcloud_sdk.services.nc.client.NcClient import NcClient
3535
from jdcloud_sdk.services.clouddnsservice.client.ClouddnsserviceClient import ClouddnsserviceClient
3636
from jdcloud_sdk.services.mps.client.MpsClient import MpsClient
37-
from jdcloud_sdk.services.jmr.client.JmrClient import JmrClient
3837
from jdcloud_sdk.services.streambus.client.StreambusClient import StreambusClient
3938
from jdcloud_sdk.services.oss.client.OssClient import OssClient
4039
from jdcloud_sdk.services.baseanti.client.BaseantiClient import BaseantiClient
@@ -68,7 +67,6 @@ def get(self, app):
6867
'nc': NcClient,
6968
'clouddnsservice': ClouddnsserviceClient,
7069
'mps': MpsClient,
71-
'jmr': JmrClient,
7270
'streambus': StreambusClient,
7371
'oss': OssClient,
7472
'baseanti': BaseantiClient,

jdcloud_cli/controllers/services/cr.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def describe_quotas(self):
141141
@expose(
142142
arguments=[
143143
(['--region-id'], dict(help="""(string) Region ID """, dest='regionId', required=False)),
144-
(['--registry-name'], dict(help="""(string) 用户定义的registry名称。<br> DNS兼容registry名称规则如下:; <br> 不可为空,且不能超过32字符 <br> 以小写字母开始和结尾,支持使用小写字母、数字、中划线(-); """, dest='registryName', required=False)),
144+
(['--registry-name'], dict(help="""(string) 用户定义的registry名称。<br> DNS兼容registry名称规则如下:; <br> 不可为空,且不能超过32字符 <br> 以小写字母开始和结尾,支持使用小写字母、数字、中划线(-); """, dest='registryName', required=True)),
145145
(['--description'], dict(help="""(string) 注册表描述,<a href="https://www.jdcloud.com/help/detail/3870/isCatalog/1">参考公共参数规范</a>。; """, dest='description', required=False)),
146146
(['--input-json'], dict(help='(json) 以json字符串或文件绝对路径形式作为输入参数。\n字符串方式举例:--input-json \'{"field":"value"}\';\n文件格式举例:--input-json file:///xxxx.json', dest='input_json', required=False)),
147147
(['--headers'], dict(help="""(json) 用户自定义Header,举例:'{"x-jdcloud-security-token":"abc","test":"123"}'""", dest='headers', required=False)),
@@ -151,7 +151,7 @@ def describe_quotas(self):
151151
description='''
152152
通过参数创建注册表。; 。
153153
154-
示例: jdc cr create-registry
154+
示例: jdc cr create-registry --registry-name xxx
155155
''',
156156
)
157157
def create_registry(self):
@@ -172,11 +172,44 @@ def create_registry(self):
172172
except Exception as e:
173173
print e.message
174174

175+
@expose(
176+
arguments=[
177+
(['--region-id'], dict(help="""(string) Region ID """, dest='regionId', required=False)),
178+
(['--registry-name'], dict(help="""(string) 待验证的注册表名。 """, dest='registryName', required=True)),
179+
(['--input-json'], dict(help='(json) 以json字符串或文件绝对路径形式作为输入参数。\n字符串方式举例:--input-json \'{"field":"value"}\';\n文件格式举例:--input-json file:///xxxx.json', dest='input_json', required=False)),
180+
(['--headers'], dict(help="""(json) 用户自定义Header,举例:'{"x-jdcloud-security-token":"abc","test":"123"}'""", dest='headers', required=False)),
181+
],
182+
formatter_class=RawTextHelpFormatter,
183+
help=''' 查询指定注册表名称是否已经存在以及是否符合命名规范。; ''',
184+
description='''
185+
查询指定注册表名称是否已经存在以及是否符合命名规范。; 。
186+
187+
示例: jdc cr check-registry-name --registry-name xxx
188+
''',
189+
)
190+
def check_registry_name(self):
191+
client_factory = ClientFactory('cr')
192+
client = client_factory.get(self.app)
193+
if client is None:
194+
return
195+
196+
try:
197+
from jdcloud_sdk.services.cr.apis.CheckRegistryNameRequest import CheckRegistryNameRequest
198+
params_dict = collect_user_args(self.app)
199+
headers = collect_user_headers(self.app)
200+
req = CheckRegistryNameRequest(params_dict, headers)
201+
resp = client.send(req)
202+
Printer.print_result(resp)
203+
except ImportError:
204+
print '{"error":"This api is not supported, please use the newer version"}'
205+
except Exception as e:
206+
print e.message
207+
175208
@expose(
176209
arguments=[
177210
(['--region-id'], dict(help="""(string) Region ID """, dest='regionId', required=False)),
178211
(['--registry-name'], dict(help="""(string) 注册表名称 """, dest='registryName', required=True)),
179-
(['--repository-name'], dict(help="""(string) 镜像仓库名称。; 可以专有模式如默认命名空间nginx-web-app;或者和命名空间一起将多个仓库聚集在一起如 project-a/nginx-web-app。; """, dest='repositoryName', required=False)),
212+
(['--repository-name'], dict(help="""(string) 镜像仓库名称。; 可以专有模式如默认命名空间nginx-web-app;或者和命名空间一起将多个仓库聚集在一起如 project-a/nginx-web-app。; """, dest='repositoryName', required=True)),
180213
(['--description'], dict(help="""(string) 注册表描述,<a href="https://www.jdcloud.com/help/detail/3870/isCatalog/1">参考公共参数规范</a>。; """, dest='description', required=False)),
181214
(['--input-json'], dict(help='(json) 以json字符串或文件绝对路径形式作为输入参数。\n字符串方式举例:--input-json \'{"field":"value"}\';\n文件格式举例:--input-json file:///xxxx.json', dest='input_json', required=False)),
182215
(['--headers'], dict(help="""(json) 用户自定义Header,举例:'{"x-jdcloud-security-token":"abc","test":"123"}'""", dest='headers', required=False)),
@@ -186,7 +219,7 @@ def create_registry(self):
186219
description='''
187220
通过参数创建镜像仓库。; 仓库名称可以分解为多个路径名,每个名称必须至少包含一个小写字母数字,考虑URL规范。; 支持包含段划线或者下划线进行分割,但不允许点'.',多个路径名之间通过("/")连接,总长度不超过256个字符,当前只支持二级目录。; 。
188221
189-
示例: jdc cr create-repository --registry-name xxx
222+
示例: jdc cr create-repository --registry-name xxx --repository-name xxx
190223
''',
191224
)
192225
def create_repository(self):
@@ -209,7 +242,7 @@ def create_repository(self):
209242

210243
@expose(
211244
arguments=[
212-
(['--api'], dict(help="""(string) api name """, choices=['get-authorization-token','describe-images','describe-quotas','create-registry','create-repository',], required=True)),
245+
(['--api'], dict(help="""(string) api name """, choices=['get-authorization-token','describe-images','describe-quotas','create-registry','check-registry-name','create-repository',], required=True)),
213246
],
214247
formatter_class=RawTextHelpFormatter,
215248
help=''' 生成单个API接口的json骨架空字符串 ''',

jdcloud_cli/controllers/services/jke.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#
1717
# NOTE: This class is auto generated by the jdcloud code generator program.
1818

19+
import os
1920
from argparse import RawTextHelpFormatter
2021
from cement.ext.ext_argparse import expose
22+
from jinja2 import Template
2123
from jdcloud_cli.controllers.base_controller import BaseController
2224
from jdcloud_cli.client_factory import ClientFactory
2325
from jdcloud_cli.parameter_builder import collect_user_args, collect_user_headers
@@ -83,3 +85,67 @@ def describe_quotas(self):
8385
def generate_skeleton(self):
8486
skeleton = Skeleton('jke', self.app.pargs.api)
8587
skeleton.show()
88+
89+
@expose(
90+
arguments=[
91+
(['--region-id'], dict(help="""(string) 地域 ID """, dest='regionId', required=False)),
92+
(['--cluster-id'], dict(help="""(string) 集群 ID """, dest='clusterId', required=True)),
93+
(['--headers'], dict(help="""(json) 用户自定义Header,举例:'{"x-jdcloud-security-token":"abc","test":"123"}'""", dest='headers', required=False)),
94+
],
95+
formatter_class=RawTextHelpFormatter,
96+
help=''' 获取密钥凭证 ''',
97+
description='''
98+
获取密钥凭证。
99+
100+
示例: jdc jke get-credential --cluster-id xxx
101+
''',
102+
)
103+
def get_credential(self):
104+
client_factory = ClientFactory('jke')
105+
client = client_factory.get(self.app)
106+
if client is None:
107+
return
108+
109+
try:
110+
from jdcloud_sdk.services.jke.apis.DescribeClusterRequest import DescribeClusterRequest
111+
params_dict = collect_user_args(self.app)
112+
headers = collect_user_headers(self.app)
113+
req = DescribeClusterRequest(params_dict, headers)
114+
resp = client.send(req)
115+
if resp.error is not None:
116+
Printer.print_result(resp)
117+
else:
118+
current_dir = os.path.dirname(os.path.abspath(__file__))
119+
with open('%s/../../resources/jke/config.jinja' % current_dir) as f:
120+
content = f.read()
121+
122+
template = Template(content)
123+
result = template.render(self._build_credential_data(resp.result['cluster']))
124+
self._write_credential_file(result)
125+
Printer.print_text('Generate cluster credential successfully.')
126+
127+
except ImportError:
128+
print '{"error":"This api is not supported, please use the newer version"}'
129+
except Exception as e:
130+
print e
131+
132+
def _build_credential_data(self, cluster):
133+
masterAuth = cluster['masterAuth']
134+
return {
135+
'certificate_authority_data': masterAuth['clusterCaCertificate'],
136+
'server': 'https://%s:%s' % (cluster['endpoint'], cluster['endpointPort']),
137+
'username': masterAuth['user'],
138+
'password': masterAuth['password'],
139+
'client_certificate_data': masterAuth['clientCertificate'],
140+
'client_key_data': masterAuth['clientKey'],
141+
'context_name': 'cert-authentication@kubernetes' if cluster['clientCertificate'] else 'basic-authentication@kubernetes'
142+
}
143+
144+
def _write_credential_file(self, content):
145+
home = os.getenv('HOME')
146+
kube_dir = '%s/.kube' % home
147+
if not os.path.exists(kube_dir):
148+
os.mkdir(kube_dir)
149+
150+
with open('%s/config' % kube_dir, 'w') as f:
151+
f.write(content)

0 commit comments

Comments
 (0)