1
- # coding: utf-8
2
-
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
3
import click
4
- import core_pb2 as pb
5
4
from grpc .beta import implementations
6
5
from grpc .framework .interfaces .face .face import AbortionError
7
6
7
+ import core_pb2 as pb
8
8
9
- def _get_stub (ctx ):
10
- try :
11
- channel = implementations .insecure_channel ('localhost' , 5000 )
12
- except Exception :
13
- click .echo (click .style ('error getting channel' , fg = 'red' , bold = True ))
14
- ctx .exit (- 1 )
15
9
10
+ @click .group ()
11
+ @click .option ('--grpc-host' , default = 'localhost' , show_default = True )
12
+ @click .option ('--grpc-port' , default = 5000 , show_default = True , type = int )
13
+ @click .pass_context
14
+ def cli (ctx , grpc_host , grpc_port ):
15
+ channel = implementations .insecure_channel (grpc_host , grpc_port )
16
16
if not channel :
17
17
click .echo (click .style ('error getting stub' , fg = 'red' , bold = True ))
18
18
ctx .exit (- 1 )
19
- return pb .beta_create_CoreRPC_stub (channel )
20
-
21
19
22
- @click .group ()
23
- @click .pass_context
24
- def cli (ctx ):
25
- pass
20
+ stub = pb .beta_create_CoreRPC_stub (channel )
21
+ ctx .obj ['stub' ] = stub
26
22
27
23
28
24
@cli .command ('pod:list' )
29
25
@click .pass_context
30
26
def list_pods (ctx ):
31
- stub = _get_stub (ctx )
32
27
try :
33
- r = stub .ListPods (pb .Empty (), 5 )
28
+ r = ctx . obj [ ' stub' ] .ListPods (pb .Empty (), 5 )
34
29
except AbortionError as e :
35
30
click .echo (click .style ('abortion error: %s' % e .details , fg = 'red' , bold = True ))
36
31
ctx .exit (- 1 )
@@ -44,11 +39,10 @@ def list_pods(ctx):
44
39
@click .argument ('desc' )
45
40
@click .pass_context
46
41
def create_pod (ctx , name , desc ):
47
- stub = _get_stub (ctx )
48
42
opts = pb .AddPodOptions (name = name , desc = desc )
49
43
50
44
try :
51
- pod = stub .AddPod (opts , 5 )
45
+ pod = ctx . obj [ ' stub' ] .AddPod (opts , 5 )
52
46
except AbortionError as e :
53
47
click .echo (click .style ('abortion error: %s' % e .details , fg = 'red' , bold = True ))
54
48
ctx .exit (- 1 )
@@ -64,11 +58,10 @@ def create_pod(ctx, name, desc):
64
58
@click .argument ('name' )
65
59
@click .pass_context
66
60
def get_pod (ctx , name ):
67
- stub = _get_stub (ctx )
68
61
opts = pb .GetPodOptions (name = name )
69
62
70
63
try :
71
- pod = stub .GetPod (opts , 5 )
64
+ pod = ctx . obj [ ' stub' ] .GetPod (opts , 5 )
72
65
except AbortionError as e :
73
66
click .echo (click .style ('abortion error: %s' % e .details , fg = 'red' , bold = True ))
74
67
ctx .exit (- 1 )
@@ -80,11 +73,10 @@ def get_pod(ctx, name):
80
73
@click .argument ('name' )
81
74
@click .pass_context
82
75
def get_pod_nodes (ctx , name ):
83
- stub = _get_stub (ctx )
84
76
opts = pb .ListNodesOptions (podname = name )
85
77
86
78
try :
87
- r = stub .ListPodNodes (opts , 5 )
79
+ r = ctx . obj [ ' stub' ] .ListPodNodes (opts , 5 )
88
80
except AbortionError as e :
89
81
click .echo (click .style ('abortion error: %s' % e .details , fg = 'red' , bold = True ))
90
82
ctx .exit (- 1 )
@@ -98,11 +90,10 @@ def get_pod_nodes(ctx, name):
98
90
@click .argument ('nodename' )
99
91
@click .pass_context
100
92
def get_node (ctx , podname , nodename ):
101
- stub = _get_stub (ctx )
102
93
opts = pb .GetNodeOptions (podname = podname , nodename = nodename )
103
94
104
95
try :
105
- node = stub .GetNode (opts , 5 )
96
+ node = ctx . obj [ ' stub' ] .GetNode (opts , 5 )
106
97
except AbortionError as e :
107
98
click .echo (click .style ('abortion error: %s' % e .details , fg = 'red' , bold = True ))
108
99
ctx .exit (- 1 )
@@ -117,14 +108,13 @@ def get_node(ctx, podname, nodename):
117
108
@click .option ('--public' , '-p' , is_flag = True )
118
109
@click .pass_context
119
110
def add_node (ctx , nodename , endpoint , podname , public ):
120
- stub = _get_stub (ctx )
121
111
opts = pb .AddNodeOptions (nodename = nodename ,
122
112
endpoint = endpoint ,
123
113
podname = podname ,
124
114
public = public )
125
115
126
116
try :
127
- node = stub .AddNode (opts , 5 )
117
+ node = ctx . obj [ ' stub' ] .AddNode (opts , 5 )
128
118
except AbortionError as e :
129
119
click .echo (click .style ('abortion error: %s' % e .details , fg = 'red' , bold = True ))
130
120
ctx .exit (- 1 )
@@ -138,11 +128,10 @@ def add_node(ctx, nodename, endpoint, podname, public):
138
128
@click .argument ('uid' )
139
129
@click .pass_context
140
130
def build_image (ctx , repo , version , uid ):
141
- stub = _get_stub (ctx )
142
131
opts = pb .BuildImageOptions (repo = repo , version = version , uid = uid )
143
132
144
133
try :
145
- for m in stub .BuildImage (opts , 3600 ):
134
+ for m in ctx . obj [ ' stub' ] .BuildImage (opts , 3600 ):
146
135
if m .error :
147
136
click .echo (click .style (m .error , fg = 'red' ), nl = False )
148
137
elif m .stream :
@@ -161,7 +150,6 @@ def build_image(ctx, repo, version, uid):
161
150
@cli .command ('deploy' )
162
151
@click .pass_context
163
152
def create_container (ctx ):
164
- stub = _get_stub (ctx )
165
153
specs = """appname: "test-ci"
166
154
entrypoints:
167
155
web:
@@ -191,7 +179,7 @@ def create_container(ctx):
191
179
env = ['ENV_A=1' , 'ENV_B=2' ])
192
180
193
181
try :
194
- for m in stub .CreateContainer (opts , 3600 ):
182
+ for m in ctx . obj [ ' stub' ] .CreateContainer (opts , 3600 ):
195
183
click .echo (m )
196
184
except AbortionError as e :
197
185
click .echo (click .style ('abortion error: %s' % e .details , fg = 'red' , bold = True ))
@@ -204,11 +192,10 @@ def create_container(ctx):
204
192
@click .argument ('ids' , nargs = - 1 )
205
193
@click .pass_context
206
194
def remove_container (ctx , ids ):
207
- stub = _get_stub (ctx )
208
195
ids = pb .ContainerIDs (ids = [pb .ContainerID (id = i ) for i in ids ])
209
196
210
197
try :
211
- for m in stub .RemoveContainer (ids , 3600 ):
198
+ for m in ctx . obj [ ' stub' ] .RemoveContainer (ids , 3600 ):
212
199
click .echo ('%s: success %s, message: %s' % (m .id , m .success , m .message ))
213
200
except AbortionError as e :
214
201
click .echo (click .style ('abortion error: %s' % e .details , fg = 'red' , bold = True ))
@@ -218,4 +205,4 @@ def remove_container(ctx, ids):
218
205
219
206
220
207
if __name__ == '__main__' :
221
- cli ()
208
+ cli (obj = {} )
0 commit comments