Skip to content

Commit d428c15

Browse files
author
tonic
committed
Merge branch 'refactor/dev-cli' into 'master'
误以为是cli了 就简化了一下,把stub放进context里了 See merge request !3
2 parents 6252b73 + 6d81c88 commit d428c15

File tree

5 files changed

+74
-42
lines changed

5 files changed

+74
-42
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
core
22
core.yaml
3+
core.yml
4+
.ropeproject
5+
*.pyc

README.md

+42-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,50 @@
11
Core
22
====
33

4-
## INSTALL
4+
## setup dev environment
5+
6+
```shell
7+
git config --global url."git@gitlab.ricebook.net:".insteadOf "https://gitlab.ricebook.net/"
8+
go get gitlab.ricebook.net/platform/core.git
9+
mv $GOPATH/src/gitlab.ricebook.net/platform/core.git $GOPATH/src/gitlab.ricebook.net/platform/core
10+
cd $GOPATH/src/gitlab.ricebook.net/platform/core && go install
11+
ln -s $GOPATH/src/gitlab.ricebook.net/platform/core $MY_WORK_SPACE/eru-core2
12+
```
13+
14+
### GRPC
15+
16+
Generate golang & python code
17+
18+
```shell
19+
cd rpc/gen
20+
protoc --go_out=plugins=grpc:. core.proto
21+
protoc -I . --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` core.proto
22+
```
23+
24+
### deploy core on local environment
25+
26+
* create `core.yaml` like this
27+
528
```
6-
to generate golang code
29+
bind: ":5000"
30+
agent_port: "12345"
31+
permdir: "/mnt/mfs/permdirs"
32+
etcd:
33+
- "http://127.0.0.1:2379"
734
8-
$ cd rpc/gen
9-
$ protoc --go_out=plugins=grpc:. core.proto
35+
git:
36+
public_key: "[path_to_pub_key]"
37+
private_key: "[path_to_pri_key]"
1038
11-
to generate python code
39+
docker:
40+
log_driver: "json-file"
41+
network_mode: "bridge"
42+
cert_path: "[cert_file_dir]"
43+
hub: "hub.ricebook.net"
44+
```
45+
46+
* start eru core
1247

13-
$ cd rpc/gen
14-
$ protoc -I . --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` core.proto
48+
```
49+
core --config core.yaml
1550
```

cluster/calcium/build_image.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ func createDockerfile(buildDir, uid, reponame string, specs types.Specs) error {
270270
if err != nil {
271271
return err
272272
}
273-
parsedTemplate.Execute(f, specs)
273+
err = parsedTemplate.Execute(f, specs)
274+
if err != nil {
275+
return err
276+
}
274277

275278
if err := f.Sync(); err != nil {
276279
return err

devtools/client.py

100644100755
+21-34
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
1-
# coding: utf-8
2-
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
33
import click
4-
import core_pb2 as pb
54
from grpc.beta import implementations
65
from grpc.framework.interfaces.face.face import AbortionError
76

7+
import core_pb2 as pb
88

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)
159

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)
1616
if not channel:
1717
click.echo(click.style('error getting stub', fg='red', bold=True))
1818
ctx.exit(-1)
19-
return pb.beta_create_CoreRPC_stub(channel)
20-
2119

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
2622

2723

2824
@cli.command('pod:list')
2925
@click.pass_context
3026
def list_pods(ctx):
31-
stub = _get_stub(ctx)
3227
try:
33-
r = stub.ListPods(pb.Empty(), 5)
28+
r = ctx.obj['stub'].ListPods(pb.Empty(), 5)
3429
except AbortionError as e:
3530
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
3631
ctx.exit(-1)
@@ -44,11 +39,10 @@ def list_pods(ctx):
4439
@click.argument('desc')
4540
@click.pass_context
4641
def create_pod(ctx, name, desc):
47-
stub = _get_stub(ctx)
4842
opts = pb.AddPodOptions(name=name, desc=desc)
4943

5044
try:
51-
pod = stub.AddPod(opts, 5)
45+
pod = ctx.obj['stub'].AddPod(opts, 5)
5246
except AbortionError as e:
5347
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
5448
ctx.exit(-1)
@@ -64,11 +58,10 @@ def create_pod(ctx, name, desc):
6458
@click.argument('name')
6559
@click.pass_context
6660
def get_pod(ctx, name):
67-
stub = _get_stub(ctx)
6861
opts = pb.GetPodOptions(name=name)
6962

7063
try:
71-
pod = stub.GetPod(opts, 5)
64+
pod = ctx.obj['stub'].GetPod(opts, 5)
7265
except AbortionError as e:
7366
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
7467
ctx.exit(-1)
@@ -80,11 +73,10 @@ def get_pod(ctx, name):
8073
@click.argument('name')
8174
@click.pass_context
8275
def get_pod_nodes(ctx, name):
83-
stub = _get_stub(ctx)
8476
opts = pb.ListNodesOptions(podname=name)
8577

8678
try:
87-
r = stub.ListPodNodes(opts, 5)
79+
r = ctx.obj['stub'].ListPodNodes(opts, 5)
8880
except AbortionError as e:
8981
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
9082
ctx.exit(-1)
@@ -98,11 +90,10 @@ def get_pod_nodes(ctx, name):
9890
@click.argument('nodename')
9991
@click.pass_context
10092
def get_node(ctx, podname, nodename):
101-
stub = _get_stub(ctx)
10293
opts = pb.GetNodeOptions(podname=podname, nodename=nodename)
10394

10495
try:
105-
node = stub.GetNode(opts, 5)
96+
node = ctx.obj['stub'].GetNode(opts, 5)
10697
except AbortionError as e:
10798
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
10899
ctx.exit(-1)
@@ -117,14 +108,13 @@ def get_node(ctx, podname, nodename):
117108
@click.option('--public', '-p', is_flag=True)
118109
@click.pass_context
119110
def add_node(ctx, nodename, endpoint, podname, public):
120-
stub = _get_stub(ctx)
121111
opts = pb.AddNodeOptions(nodename=nodename,
122112
endpoint=endpoint,
123113
podname=podname,
124114
public=public)
125115

126116
try:
127-
node = stub.AddNode(opts, 5)
117+
node = ctx.obj['stub'].AddNode(opts, 5)
128118
except AbortionError as e:
129119
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
130120
ctx.exit(-1)
@@ -138,11 +128,10 @@ def add_node(ctx, nodename, endpoint, podname, public):
138128
@click.argument('uid')
139129
@click.pass_context
140130
def build_image(ctx, repo, version, uid):
141-
stub = _get_stub(ctx)
142131
opts = pb.BuildImageOptions(repo=repo, version=version, uid=uid)
143132

144133
try:
145-
for m in stub.BuildImage(opts, 3600):
134+
for m in ctx.obj['stub'].BuildImage(opts, 3600):
146135
if m.error:
147136
click.echo(click.style(m.error, fg='red'), nl=False)
148137
elif m.stream:
@@ -161,7 +150,6 @@ def build_image(ctx, repo, version, uid):
161150
@cli.command('deploy')
162151
@click.pass_context
163152
def create_container(ctx):
164-
stub = _get_stub(ctx)
165153
specs = """appname: "test-ci"
166154
entrypoints:
167155
web:
@@ -191,7 +179,7 @@ def create_container(ctx):
191179
env=['ENV_A=1', 'ENV_B=2'])
192180

193181
try:
194-
for m in stub.CreateContainer(opts, 3600):
182+
for m in ctx.obj['stub'].CreateContainer(opts, 3600):
195183
click.echo(m)
196184
except AbortionError as e:
197185
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
@@ -204,11 +192,10 @@ def create_container(ctx):
204192
@click.argument('ids', nargs=-1)
205193
@click.pass_context
206194
def remove_container(ctx, ids):
207-
stub = _get_stub(ctx)
208195
ids = pb.ContainerIDs(ids=[pb.ContainerID(id=i) for i in ids])
209196

210197
try:
211-
for m in stub.RemoveContainer(ids, 3600):
198+
for m in ctx.obj['stub'].RemoveContainer(ids, 3600):
212199
click.echo('%s: success %s, message: %s' % (m.id, m.success, m.message))
213200
except AbortionError as e:
214201
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
@@ -218,4 +205,4 @@ def remove_container(ctx, ids):
218205

219206

220207
if __name__ == '__main__':
221-
cli()
208+
cli(obj={})

devtools/requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
click == 6.6
2+
six == 1.10.0
3+
protobuf == 3.0.0b3
4+
grpcio

0 commit comments

Comments
 (0)