Skip to content

Commit 10ca04f

Browse files
Added initial implementation of gNOI OS Install RPC.
1 parent e0d629d commit 10ca04f

File tree

11 files changed

+1381
-105
lines changed

11 files changed

+1381
-105
lines changed

common_utils/context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
GNMI_SET_FAIL
4545
GNOI_REBOOT
4646
GNOI_FACTORY_RESET
47+
GNOI_OS_INSTALL
4748
DBUS
4849
DBUS_FAIL
4950
DBUS_APPLY_PATCH_DB
@@ -80,6 +81,8 @@ func (c CounterType) String() string {
8081
return "GNOI reboot"
8182
case GNOI_FACTORY_RESET:
8283
return "GNOI Factory Reset"
84+
case GNOI_OS_INSTALL:
85+
return "GNOI OS Install"
8386
case DBUS:
8487
return "DBUS"
8588
case DBUS_FAIL:

gnmi_server/gnoi.go

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -5,115 +5,19 @@ import (
55
"encoding/json"
66
jwt "github.com/dgrijalva/jwt-go"
77
log "github.com/golang/glog"
8-
gnoi_os_pb "github.com/openconfig/gnoi/os"
98
spb "github.com/sonic-net/sonic-gnmi/proto/gnoi"
109
spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt"
11-
ssc "github.com/sonic-net/sonic-gnmi/sonic_service_client"
1210
transutil "github.com/sonic-net/sonic-gnmi/transl_utils"
1311
"google.golang.org/grpc/codes"
1412
"google.golang.org/grpc/status"
15-
"os"
1613
"os/user"
17-
"strings"
1814
"time"
1915
)
2016

2117
const (
2218
stateDB string = "STATE_DB"
2319
)
2420

25-
func (srv *OSServer) Verify(ctx context.Context, req *gnoi_os_pb.VerifyRequest) (*gnoi_os_pb.VerifyResponse, error) {
26-
_, err := authenticate(srv.config, ctx, "gnoi", false)
27-
if err != nil {
28-
log.V(2).Infof("Failed to authenticate: %v", err)
29-
return nil, err
30-
}
31-
32-
log.V(1).Info("gNOI: Verify")
33-
dbus, err := ssc.NewDbusClient()
34-
if err != nil {
35-
log.V(2).Infof("Failed to create dbus client: %v", err)
36-
return nil, err
37-
}
38-
defer dbus.Close()
39-
40-
image_json, err := dbus.ListImages()
41-
if err != nil {
42-
log.V(2).Infof("Failed to list images: %v", err)
43-
return nil, err
44-
}
45-
46-
images := make(map[string]interface{})
47-
err = json.Unmarshal([]byte(image_json), &images)
48-
if err != nil {
49-
log.V(2).Infof("Failed to unmarshal images: %v", err)
50-
return nil, err
51-
}
52-
53-
current, exists := images["current"]
54-
if !exists {
55-
return nil, status.Errorf(codes.Internal, "Key 'current' not found in images")
56-
}
57-
current_image, ok := current.(string)
58-
if !ok {
59-
return nil, status.Errorf(codes.Internal, "Failed to assert current image as string")
60-
}
61-
resp := &gnoi_os_pb.VerifyResponse{
62-
Version: current_image,
63-
}
64-
return resp, nil
65-
}
66-
67-
func (srv *OSServer) Activate(ctx context.Context, req *gnoi_os_pb.ActivateRequest) (*gnoi_os_pb.ActivateResponse, error) {
68-
_, err := authenticate(srv.config, ctx, "gnoi" /*writeAccess=*/, true)
69-
if err != nil {
70-
log.Errorf("Failed to authenticate: %v", err)
71-
return nil, err
72-
}
73-
74-
log.Infof("gNOI: Activate")
75-
image := req.GetVersion()
76-
log.Infof("Requested to activate image %s", image)
77-
78-
dbus, err := ssc.NewDbusClient()
79-
if err != nil {
80-
log.Errorf("Failed to create dbus client: %v", err)
81-
return nil, err
82-
}
83-
defer dbus.Close()
84-
85-
var resp gnoi_os_pb.ActivateResponse
86-
err = dbus.ActivateImage(image)
87-
if err != nil {
88-
log.Errorf("Failed to activate image %s: %v", image, err)
89-
image_not_exists := os.IsNotExist(err) ||
90-
(strings.Contains(strings.ToLower(err.Error()), "not") &&
91-
strings.Contains(strings.ToLower(err.Error()), "exist"))
92-
if image_not_exists {
93-
// Image does not exist.
94-
resp.Response = &gnoi_os_pb.ActivateResponse_ActivateError{
95-
ActivateError: &gnoi_os_pb.ActivateError{
96-
Type: gnoi_os_pb.ActivateError_NON_EXISTENT_VERSION,
97-
Detail: err.Error(),
98-
},
99-
}
100-
} else {
101-
// Other error.
102-
resp.Response = &gnoi_os_pb.ActivateResponse_ActivateError{
103-
ActivateError: &gnoi_os_pb.ActivateError{
104-
Type: gnoi_os_pb.ActivateError_UNSPECIFIED,
105-
Detail: err.Error(),
106-
},
107-
}
108-
}
109-
return &resp, nil
110-
}
111-
112-
log.Infof("Successfully activated image %s", image)
113-
resp.Response = &gnoi_os_pb.ActivateResponse_ActivateOk{}
114-
return &resp, nil
115-
}
116-
11721
func (srv *Server) Authenticate(ctx context.Context, req *spb_jwt.AuthenticateRequest) (*spb_jwt.AuthenticateResponse, error) {
11822
// Can't enforce normal authentication here.. maybe only enforce client cert auth if enabled?
11923
// ctx,err := authenticate(srv.config, ctx, false)

0 commit comments

Comments
 (0)