Skip to content

Commit 065a8ef

Browse files
committed
docs(grpc): add Python (grpcio) client examples
1 parent d32ffad commit 065a8ef

1 file changed

Lines changed: 90 additions & 12 deletions

File tree

docs/grpc.md

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,35 @@ With `Hyper` running, you can create a new `gRPC` client:
1515
```python
1616
```
1717

18-
## The contract
18+
### Creating VMs
1919

20-
The service is `hyper.grpc.v0.Hyper`, defined in
21-
[`proto/hyper/grpc/v0/hyper.proto`](https://github.com/harmont-dev/hyper/blob/main/proto/hyper/grpc/v0/hyper.proto):
20+
You can create new VMs with the `CreateVm` RPC:
2221

23-
| RPC | Purpose | Errors |
24-
| -------------- | -------------------------------------------------- | ------ |
25-
| `CreateVm`| Boot a microVM from an image; returns its `vm_id`. | `INVALID_ARGUMENT` (bad/missing image or enum), `RESOURCE_EXHAUSTED` (no capacity), `UNAVAILABLE` (host lost mid-create) |
26-
| `StopVm` | Tear down a running microVM. | `NOT_FOUND` (unknown id), `UNAVAILABLE` (host down) |
27-
| `GetVm` | Which node a microVM runs on. | `NOT_FOUND` |
28-
| `ListVms` | Every microVM known to the cluster. | -- |
22+
```python
23+
```
24+
25+
### Listing VMs
26+
27+
You can list running virtual machines with:
28+
29+
```python
30+
```
31+
32+
33+
### Getting VM Info
34+
35+
You can query the current status of any VM with:
36+
37+
```python
38+
```
39+
40+
### Stopping a VM
41+
42+
You can stop a running VM with:
43+
44+
```python
45+
```
2946

30-
A machine is addressed by its **`vm_id`** -- a URL-safe base64 string the server
31-
mints at creation. The server is stateless and identical on every node;
32-
placement and routing are cluster-wide, so any node can serve any request.
3347

3448
## Configuring the server
3549

@@ -88,6 +102,70 @@ Generate a client stub from `proto/hyper/grpc/v0/hyper.proto` with your
88102
language's gRPC tooling (`protoc`, `buf`, etc.) and call the `Hyper` service.
89103
The `.proto` ships in the published package as well as the repo.
90104

105+
### From Python (grpcio)
106+
107+
Generate the bindings with `grpcio-tools`:
108+
109+
```sh
110+
pip install grpcio grpcio-tools
111+
python -m grpc_tools.protoc -I proto \
112+
--python_out=. --grpc_python_out=. --pyi_out=. \
113+
proto/hyper/grpc/v0/hyper.proto
114+
```
115+
116+
That writes `hyper/grpc/v0/hyper_pb2.py`, `hyper_pb2_grpc.py`, and the
117+
`hyper_pb2.pyi` type stubs. Then drive the cluster:
118+
119+
```python
120+
import grpc
121+
from google.protobuf import empty_pb2
122+
from hyper.grpc.v0 import hyper_pb2, hyper_pb2_grpc
123+
124+
# Plaintext:
125+
channel = grpc.insecure_channel("localhost:50051")
126+
127+
# Or TLS, verifying the server against a CA bundle:
128+
# with open("/etc/hyper/ca.pem", "rb") as f:
129+
# creds = grpc.ssl_channel_credentials(f.read())
130+
# channel = grpc.secure_channel("hyper.example.com:50051", creds)
131+
132+
stub = hyper_pb2_grpc.HyperStub(channel)
133+
134+
# Create and boot a VM. instance_type and arch are required by the contract;
135+
# set them explicitly (an unset enum field decodes to its zero value).
136+
created = stub.CreateVm(
137+
hyper_pb2.CreateVmRequest(
138+
img_id="img-abc",
139+
instance_type=hyper_pb2.INSTANCE_TYPE_DECI,
140+
arch=hyper_pb2.ARCHITECTURE_X86_64,
141+
)
142+
)
143+
print(created.vm_id, created.node)
144+
145+
# Locate it.
146+
located = stub.GetVm(hyper_pb2.GetVmRequest(vm_id=created.vm_id))
147+
print(located.node)
148+
149+
# List every VM. ListVms takes google.protobuf.Empty.
150+
for vm in stub.ListVms(empty_pb2.Empty()).vms:
151+
print(vm.vm_id, vm.node)
152+
153+
# Stop it. StopVm returns google.protobuf.Empty.
154+
stub.StopVm(hyper_pb2.StopVmRequest(vm_id=created.vm_id))
155+
156+
channel.close()
157+
```
158+
159+
Errors arrive as `grpc.RpcError` carrying a status code:
160+
161+
```python
162+
try:
163+
stub.GetVm(hyper_pb2.GetVmRequest(vm_id="does-not-exist"))
164+
except grpc.RpcError as err:
165+
assert err.code() == grpc.StatusCode.NOT_FOUND
166+
print(err.details()) # "no such VM"
167+
```
168+
91169
### From the BEAM
92170

93171
Use the generated stub with the `connect/2` helper (which defaults to the Mint

0 commit comments

Comments
 (0)