Skip to content

Commit 7c12991

Browse files
committed
implement a jsonrpc api for all zos endpoint followin net/rpc pkg
protocol.
1 parent 8b64fae commit 7c12991

File tree

18 files changed

+886
-6
lines changed

18 files changed

+886
-6
lines changed

cmds/modules/api_gateway/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
1313
"github.com/threefoldtech/zbus"
1414
"github.com/threefoldtech/zos/pkg/environment"
15+
"github.com/threefoldtech/zos/pkg/rpc"
1516
"github.com/threefoldtech/zos/pkg/stubs"
1617
substrategw "github.com/threefoldtech/zos/pkg/substrate_gateway"
1718
"github.com/threefoldtech/zos/pkg/utils"
@@ -36,6 +37,11 @@ var Module cli.Command = cli.Command{
3637
Usage: "number of workers `N`",
3738
Value: 1,
3839
},
40+
&cli.UintFlag{
41+
Name: "port",
42+
Usage: "rpc server port",
43+
Value: 3000,
44+
},
3945
},
4046
Action: action,
4147
}
@@ -44,6 +50,7 @@ func action(cli *cli.Context) error {
4450
var (
4551
msgBrokerCon string = cli.String("broker")
4652
workerNr uint = cli.Uint("workers")
53+
port uint = cli.Uint("port")
4754
)
4855

4956
server, err := zbus.NewRedisServer(module, msgBrokerCon, workerNr)
@@ -98,6 +105,13 @@ func action(cli *cli.Context) error {
98105
}
99106
api.SetupRoutes(router)
100107

108+
go func() {
109+
if err := rpc.Run(ctx, port, manager, redis, msgBrokerCon); err != nil {
110+
log.Error().Err(err).Msg("failed to run rpc server")
111+
return
112+
}
113+
}()
114+
101115
pair, err := id.KeyPair()
102116
if err != nil {
103117
return err

openrpc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@
297297
}
298298
},
299299
{
300-
"name": "zos.DeploymentCreate",
300+
"name": "zos.DeploymentDeploy",
301301
"params": [
302302
{
303303
"name": "deployment",

pkg/rpc/admin.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package rpc
2+
3+
func (s *Service) AdminPublicNICSet(arg string, reply *any) error {
4+
return s.networkerStub.SetPublicExitDevice(s.ctx, arg)
5+
}
6+
7+
func (s *Service) AdminPublicNICGet(arg any, reply *ExitDevice) error {
8+
ed, err := s.networkerStub.GetPublicExitDevice(s.ctx)
9+
if err != nil {
10+
return err
11+
}
12+
13+
reply.AsDualInterface = ed.AsDualInterface
14+
reply.IsDual = ed.IsDual
15+
reply.IsSingle = ed.IsSingle
16+
return nil
17+
}
18+
19+
func (s *Service) AdminInterfaces(arg any, reply *Interfaces) error {
20+
interfaces, err := s.networkerStub.Interfaces(s.ctx, "", "")
21+
if err != nil {
22+
return err
23+
}
24+
25+
for name, inf := range interfaces.Interfaces {
26+
reply.Interfaces = append(reply.Interfaces, Interface{
27+
Name: name,
28+
Mac: inf.Mac,
29+
Ips: func() []string {
30+
var ips []string
31+
for _, ip := range inf.IPs {
32+
ips = append(ips, ip.String())
33+
}
34+
return ips
35+
}(),
36+
})
37+
}
38+
39+
return nil
40+
}

pkg/rpc/converter.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package rpc
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/threefoldtech/zos/pkg/capacity/dmi"
7+
)
8+
9+
// This function heavily used and it depends on matching json tags on both types
10+
func convert(input any, output any) error {
11+
data, err := json.Marshal(input)
12+
if err != nil {
13+
return err
14+
}
15+
16+
if err := json.Unmarshal(data, &output); err != nil {
17+
return err
18+
}
19+
20+
return nil
21+
}
22+
23+
func convertDmi(input *dmi.DMI, output *DMI) {
24+
dmi := *input
25+
26+
*output = DMI{
27+
Tooling: Tooling{
28+
Aggregator: dmi.Tooling.Aggregator,
29+
Decoder: dmi.Tooling.Decoder,
30+
},
31+
Sections: func() []Section {
32+
var sections []Section
33+
for _, sec := range dmi.Sections {
34+
section := Section{
35+
HandleLine: sec.HandleLine,
36+
TypeStr: sec.TypeStr,
37+
Type: uint64(sec.Type),
38+
SubSections: func() []SubSection {
39+
var subsections []SubSection
40+
for _, subsec := range sec.SubSections {
41+
subsection := SubSection{
42+
Title: subsec.Title,
43+
Properties: func() []PropertyData {
44+
var properties []PropertyData
45+
for key, prop := range subsec.Properties {
46+
property := PropertyData{
47+
Name: key,
48+
Val: prop.Val,
49+
Items: prop.Items,
50+
}
51+
properties = append(properties, property)
52+
}
53+
return properties
54+
}(),
55+
}
56+
subsections = append(subsections, subsection)
57+
}
58+
return subsections
59+
}(),
60+
}
61+
sections = append(sections, section)
62+
}
63+
return sections
64+
}(),
65+
}
66+
}

pkg/rpc/deployment.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package rpc
2+
3+
import (
4+
"context"
5+
6+
"github.com/threefoldtech/zos/pkg/gridtypes"
7+
)
8+
9+
func (s *Service) DeploymentChanges(arg uint64, reply *Workloads) error {
10+
wls, err := s.provisionStub.Changes(context.Background(), GetTwinID(s.ctx), arg)
11+
if err != nil {
12+
return err
13+
}
14+
15+
for _, wl := range wls {
16+
var workload Workload
17+
if err := convert(wl, &workload); err != nil {
18+
return err
19+
}
20+
reply.Workloads = append(reply.Workloads, workload)
21+
}
22+
23+
return nil
24+
}
25+
26+
func (s *Service) DeploymentList(arg any, reply *Deployments) error {
27+
deps, err := s.provisionStub.List(s.ctx, GetTwinID(s.ctx))
28+
if err != nil {
29+
return err
30+
}
31+
32+
for _, dep := range deps {
33+
var deployment Deployment
34+
if err := convert(dep, &deployment); err != nil {
35+
return err
36+
}
37+
reply.Deployments = append(reply.Deployments, deployment)
38+
}
39+
40+
return nil
41+
}
42+
43+
func (s *Service) DeploymentGet(arg uint64, reply *Deployment) error {
44+
dep, err := s.provisionStub.Get(s.ctx, GetTwinID(s.ctx), arg)
45+
if err != nil {
46+
return err
47+
}
48+
49+
return convert(dep, reply)
50+
}
51+
52+
func (s *Service) DeploymentUpdate(arg Deployment, reply *any) error {
53+
var deployment gridtypes.Deployment
54+
if err := convert(arg, &deployment); err != nil {
55+
return err
56+
}
57+
return s.provisionStub.CreateOrUpdate(s.ctx, GetTwinID(s.ctx), deployment, true)
58+
}
59+
60+
func (s *Service) DeploymentDeploy(arg Deployment, reply *any) error {
61+
var deployment gridtypes.Deployment
62+
if err := convert(arg, &deployment); err != nil {
63+
return err
64+
}
65+
return s.provisionStub.CreateOrUpdate(s.ctx, GetTwinID(s.ctx), deployment, false)
66+
}

pkg/rpc/gpu.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package rpc
2+
3+
func (s *Service) GpuList(arg any, reply *GPUs) error {
4+
gpus, err := s.statisticsStub.ListGPUs(s.ctx)
5+
if err != nil {
6+
return err
7+
}
8+
9+
for _, gpu := range gpus {
10+
reply.GPUs = append(reply.GPUs, GPU{
11+
ID: gpu.ID,
12+
Vendor: gpu.Vendor,
13+
Device: gpu.Device,
14+
Contract: gpu.Contract,
15+
})
16+
}
17+
18+
return nil
19+
}

pkg/rpc/network.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package rpc
2+
3+
import (
4+
"fmt"
5+
"net"
6+
7+
"github.com/threefoldtech/zos/pkg/gridtypes"
8+
)
9+
10+
func (s *Service) NetworkPublicIps(arg any, reply *Ips) error {
11+
ips, err := s.provisionStub.ListPublicIPs(s.ctx)
12+
if err != nil {
13+
return err
14+
}
15+
16+
reply.Ips = append(reply.Ips, ips...)
17+
return nil
18+
}
19+
20+
func (s *Service) NetworkHasIpv6(arg any, reply *bool) error {
21+
ipData, err := s.networkerStub.GetPublicIPv6Subnet(s.ctx)
22+
hasIP := ipData.IP != nil && err == nil
23+
*reply = hasIP
24+
return nil
25+
}
26+
27+
func (s *Service) NetworkInterfaces(arg any, reply *Interfaces) error {
28+
type q struct {
29+
inf string
30+
ns string
31+
rename string
32+
}
33+
34+
for _, i := range []q{
35+
{"zos", "", "zos"},
36+
{"nygg6", "ndmz", "ygg"},
37+
} {
38+
ips, _, err := s.networkerStub.Addrs(s.ctx, i.inf, i.ns)
39+
if err != nil {
40+
return fmt.Errorf("failed to get ips for '%s' interface: %w", i, err)
41+
}
42+
43+
reply.Interfaces = append(reply.Interfaces, Interface{
44+
Name: i.rename,
45+
Ips: func() []string {
46+
var list []string
47+
for _, item := range ips {
48+
ip := net.IP(item)
49+
list = append(list, ip.String())
50+
}
51+
52+
return list
53+
}(),
54+
})
55+
}
56+
57+
return nil
58+
}
59+
60+
func (s *Service) NetworkPublicConfig(arg any, reply *PublicConfig) error {
61+
config, err := s.networkerStub.GetPublicConfig(s.ctx)
62+
if err != nil {
63+
return err
64+
}
65+
66+
reply.Domain = config.Domain
67+
reply.IPv4 = config.IPv4.String()
68+
reply.IPv6 = config.IPv6.String()
69+
reply.GW4 = config.GW4.String()
70+
reply.GW6 = config.GW6.String()
71+
reply.Type = string(config.Type)
72+
return nil
73+
}
74+
75+
func (s *Service) NetworkWGPorts(arg any, reply *WGPorts) error {
76+
ports, err := s.networkerStub.WireguardPorts(s.ctx)
77+
if err != nil {
78+
return err
79+
}
80+
81+
for _, port := range ports {
82+
reply.Ports = append(reply.Ports, uint64(port))
83+
}
84+
85+
return nil
86+
}
87+
88+
func (s *Service) NetworkPrivateIps(arg string, reply *Ips) error {
89+
ips, err := s.provisionStub.ListPrivateIPs(s.ctx, GetTwinID(s.ctx), gridtypes.Name(arg))
90+
if err != nil {
91+
return err
92+
}
93+
94+
reply.Ips = append(reply.Ips, ips...)
95+
return nil
96+
}

pkg/rpc/perf.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package rpc
2+
3+
func (s *Service) PerfGetCpuBench(arg any, reply *CpuBenchTaskResult) error {
4+
r, err := s.performanceMonitorStub.GetCpuBenchTaskResult(s.ctx)
5+
if err != nil {
6+
return err
7+
}
8+
9+
return convert(r, reply)
10+
}
11+
12+
func (s *Service) PerfGetHealth(arg any, reply *HealthTaskResult) error {
13+
r, err := s.performanceMonitorStub.GetHealthTaskResult(s.ctx)
14+
if err != nil {
15+
return err
16+
}
17+
18+
return convert(r, reply)
19+
}
20+
21+
func (s *Service) PerfGetIperf(arg any, reply *IperfTaskResult) error {
22+
r, err := s.performanceMonitorStub.GetIperfTaskResult(s.ctx)
23+
if err != nil {
24+
return err
25+
}
26+
27+
return convert(r, reply)
28+
}
29+
30+
func (s *Service) PerfGetPublicIP(arg any, reply *PublicIpTaskResult) error {
31+
r, err := s.performanceMonitorStub.GetPublicIpTaskResult(s.ctx)
32+
if err != nil {
33+
return err
34+
}
35+
36+
return convert(r, reply)
37+
}
38+
39+
func (s *Service) PerfGetAll(arg any, reply *AllTaskResult) error {
40+
r, err := s.performanceMonitorStub.GetAllTaskResult(s.ctx)
41+
if err != nil {
42+
return err
43+
}
44+
45+
return convert(r, reply)
46+
}

0 commit comments

Comments
 (0)