Skip to content

Commit 2e7cac8

Browse files
[gNOI] Added initial implementation of gNOI.OS Install RPC.
1 parent db9afb6 commit 2e7cac8

File tree

7 files changed

+1028
-96
lines changed

7 files changed

+1028
-96
lines changed

common_utils/context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const (
4343
GNMI_SET
4444
GNMI_SET_FAIL
4545
GNOI_REBOOT
46+
GNOI_OS_ACTIVATE
47+
GNOI_OS_INSTALL
48+
GNOI_OS_VERIFY
4649
DBUS
4750
DBUS_FAIL
4851
DBUS_APPLY_PATCH_DB
@@ -77,6 +80,12 @@ func (c CounterType) String() string {
7780
return "GNMI set fail"
7881
case GNOI_REBOOT:
7982
return "GNOI reboot"
83+
case GNOI_OS_ACTIVATE:
84+
return "GNOI OS Activate"
85+
case GNOI_OS_INSTALL:
86+
return "GNOI OS Install"
87+
case GNOI_OS_VERIFY:
88+
return "GNOI OS VERIFY"
8089
case DBUS:
8190
return "DBUS"
8291
case DBUS_FAIL:

gnmi_server/gnoi.go

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import (
66
jwt "github.com/dgrijalva/jwt-go"
77
log "github.com/golang/glog"
88
gnoi_file_pb "github.com/openconfig/gnoi/file"
9-
gnoi_os_pb "github.com/openconfig/gnoi/os"
109
spb "github.com/sonic-net/sonic-gnmi/proto/gnoi"
1110
spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt"
1211
ssc "github.com/sonic-net/sonic-gnmi/sonic_service_client"
1312
transutil "github.com/sonic-net/sonic-gnmi/transl_utils"
1413
"google.golang.org/grpc/codes"
1514
"google.golang.org/grpc/status"
16-
"os"
1715
"os/user"
1816
"strconv"
1917
"strings"
@@ -96,98 +94,6 @@ func (srv *FileServer) Get(req *gnoi_file_pb.GetRequest, stream gnoi_file_pb.Fil
9694
return status.Errorf(codes.Unimplemented, "")
9795
}
9896

99-
func (srv *OSServer) Verify(ctx context.Context, req *gnoi_os_pb.VerifyRequest) (*gnoi_os_pb.VerifyResponse, error) {
100-
_, err := authenticate(srv.config, ctx, "gnoi", false)
101-
if err != nil {
102-
log.V(2).Infof("Failed to authenticate: %v", err)
103-
return nil, err
104-
}
105-
106-
log.V(1).Info("gNOI: Verify")
107-
dbus, err := ssc.NewDbusClient()
108-
if err != nil {
109-
log.V(2).Infof("Failed to create dbus client: %v", err)
110-
return nil, err
111-
}
112-
defer dbus.Close()
113-
114-
image_json, err := dbus.ListImages()
115-
if err != nil {
116-
log.V(2).Infof("Failed to list images: %v", err)
117-
return nil, err
118-
}
119-
120-
images := make(map[string]interface{})
121-
err = json.Unmarshal([]byte(image_json), &images)
122-
if err != nil {
123-
log.V(2).Infof("Failed to unmarshal images: %v", err)
124-
return nil, err
125-
}
126-
127-
current, exists := images["current"]
128-
if !exists {
129-
return nil, status.Errorf(codes.Internal, "Key 'current' not found in images")
130-
}
131-
current_image, ok := current.(string)
132-
if !ok {
133-
return nil, status.Errorf(codes.Internal, "Failed to assert current image as string")
134-
}
135-
resp := &gnoi_os_pb.VerifyResponse{
136-
Version: current_image,
137-
}
138-
return resp, nil
139-
}
140-
141-
func (srv *OSServer) Activate(ctx context.Context, req *gnoi_os_pb.ActivateRequest) (*gnoi_os_pb.ActivateResponse, error) {
142-
_, err := authenticate(srv.config, ctx, "gnoi" /*writeAccess=*/, true)
143-
if err != nil {
144-
log.Errorf("Failed to authenticate: %v", err)
145-
return nil, err
146-
}
147-
148-
log.Infof("gNOI: Activate")
149-
image := req.GetVersion()
150-
log.Infof("Requested to activate image %s", image)
151-
152-
dbus, err := ssc.NewDbusClient()
153-
if err != nil {
154-
log.Errorf("Failed to create dbus client: %v", err)
155-
return nil, err
156-
}
157-
defer dbus.Close()
158-
159-
var resp gnoi_os_pb.ActivateResponse
160-
err = dbus.ActivateImage(image)
161-
if err != nil {
162-
log.Errorf("Failed to activate image %s: %v", image, err)
163-
image_not_exists := os.IsNotExist(err) ||
164-
(strings.Contains(strings.ToLower(err.Error()), "not") &&
165-
strings.Contains(strings.ToLower(err.Error()), "exist"))
166-
if image_not_exists {
167-
// Image does not exist.
168-
resp.Response = &gnoi_os_pb.ActivateResponse_ActivateError{
169-
ActivateError: &gnoi_os_pb.ActivateError{
170-
Type: gnoi_os_pb.ActivateError_NON_EXISTENT_VERSION,
171-
Detail: err.Error(),
172-
},
173-
}
174-
} else {
175-
// Other error.
176-
resp.Response = &gnoi_os_pb.ActivateResponse_ActivateError{
177-
ActivateError: &gnoi_os_pb.ActivateError{
178-
Type: gnoi_os_pb.ActivateError_UNSPECIFIED,
179-
Detail: err.Error(),
180-
},
181-
}
182-
}
183-
return &resp, nil
184-
}
185-
186-
log.Infof("Successfully activated image %s", image)
187-
resp.Response = &gnoi_os_pb.ActivateResponse_ActivateOk{}
188-
return &resp, nil
189-
}
190-
19197
func (srv *Server) Authenticate(ctx context.Context, req *spb_jwt.AuthenticateRequest) (*spb_jwt.AuthenticateResponse, error) {
19298
// Can't enforce normal authentication here.. maybe only enforce client cert auth if enabled?
19399
// ctx,err := authenticate(srv.config, ctx, false)

0 commit comments

Comments
 (0)