Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
.PHONY: all install build libcswsscommon clean
.PHONY: all install build libcswsscommon clean FORCE

GO := /usr/local/go/bin/go
export GOROOT=/usr/local/go
export GOPATH=$(HOME)/go
export GOBIN=$(GOPATH)/bin
export GO111MODULE=on
export GOFLAGS=-modcacherw
REDISCONN_GO := $(GOPATH)/src/swsscommon/redisconn/dial.go
RACE_OPTION := -race
ifeq ($(CONFIGURED_ARCH),armhf)
RACE_OPTION :=
Expand All @@ -19,17 +20,22 @@ install: build

build: $(GOPATH)/bin/go-server-server $(GOPATH)/bin/go-server-server.test

$(GOPATH)/bin/go-server-server: libcswsscommon $(GOPATH)/src/go-server-server/main.go
$(GOPATH)/bin/go-server-server: libcswsscommon $(REDISCONN_GO)
cd $(GOPATH)/src/go-server-server && $(GO) get -v && $(GO) build $(RACE_OPTION) -v -o $(GOPATH)/bin/go-server-server

$(GOPATH)/bin/go-server-server.test: libcswsscommon $(GOPATH)/src/go-server-server/main.go
$(GOPATH)/bin/go-server-server.test: libcswsscommon $(REDISCONN_GO)
cd $(GOPATH)/src/go-server-server && $(GO) get -v && $(GO) test $(RACE_OPTION) -c -covermode=atomic -coverpkg "go-server-server/go" -v -o $(GOPATH)/bin/go-server-server.test

$(GOPATH)/src/go-server-server/main.go:
mkdir -p $(GOPATH)/src
cp -r go-server-server $(GOPATH)/src/go-server-server
cp -r swsscommon $(GOPATH)/src/swsscommon

$(REDISCONN_GO): /usr/share/swss/go/redisconn/dial.go $(GOPATH)/src/go-server-server/main.go FORCE
cmp -s $< $@ || /usr/bin/install -D -m 0644 $< $@

FORCE:

libcswsscommon:
make -C libcswsscommon
sudo make -C libcswsscommon install
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ This is a configuration agent which exposes HTTPS endpoints to perform dynamic n
1. scp/copy over the generated archive(`rest-api-image.gz`) to your switch
2. `docker load < rest-api-image.gz`
3. `docker run -d -p=8090:8090/tcp -v /var/run/redis/redis.sock:/var/run/redis/redis.sock --name rest-api --cap-add NET_ADMIN --privileged -t rest-api-image:latest`

## Native SDK dependency

Builds require `libswsscommon-dev` and its matching runtime library, including the
Go adapter introduced by swss-common #1275. The Makefile copies
`/usr/share/swss/go/redisconn/dial.go` into the existing local `swsscommon` module
before building. A sibling swss-common source checkout is not required.

`build.sh` installs SDK packages downloaded by `copy.py`. Those artifacts must
include #1275; the normal SONiC build uses its selected SDK packages.
23 changes: 23 additions & 0 deletions azurepipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ pr:
include:
- "*"

variables:
- name: BUILD_BRANCH
${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
value: $(System.PullRequest.TargetBranch)
${{ else }}:
value: $(Build.SourceBranchName)

jobs:
- job: Build
pool:
Expand All @@ -37,6 +44,20 @@ jobs:
df -h /
displayName: 'Free disk space'

- task: DownloadPipelineArtifact@2
inputs:
source: specific
project: build
pipeline: '9'
runVersion: specific
runId: '1288226'
artifact: sonic-swss-common-trixie
patterns: |
libswsscommon_*.deb
libswsscommon-dev_*.deb
targetPath: $(Pipeline.Workspace)/swss-common
displayName: 'Download swss-common Trixie packages'

- script: |
set -ex
# Only master and release branches have a sonic-slave container
Expand All @@ -52,6 +73,8 @@ jobs:
cp debs/*.deb $(Build.ArtifactStagingDirectory)/debs
cp debian/sonic-rest-api/usr/sbin/go-server-server $(Build.ArtifactStagingDirectory)
displayName: 'Build image'
env:
SWSSCOMMON_DEBS_DIR: $(Pipeline.Workspace)/swss-common

- script: |
set -ex
Expand Down
4 changes: 2 additions & 2 deletions build.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# How to build the Go MSEE server

## Prerequesites
1) libswsscommon built and the library available on `LD_LIBRARY_PATH`. Can be found at [sonic-swss-common](https://github.com/Azure/sonic-swss-common) - follow the build instructions there. Must also set `SWSSCOMMON_SRC` to the full path to the directory `sonic-swss-common`.
1) Install `libswsscommon-dev` and its matching runtime library, including the SDK Go adapter from [swss-common #1275](https://github.com/sonic-net/sonic-swss-common/pull/1275). A swss-common source checkout is not required.
2) libcswsscommon must be built, run make within `MSEE/libcswsscommon` after step 1.
2) Redis database on localhost:6379 - we use DB 0 (`APPL_DB`) and 4
3) Go version 1.8 (static binaries can be found at [https://golang.org/dl/](https://golang.org/dl/))
Expand All @@ -26,7 +26,7 @@ $GOPATH/src/mseethrifttest -> MSEE/mseethrifttest
$GOPATH/src/swsscommon -> MSEE/swsscommon
```

It should then be possible to run `go get` followed by `go build` within `$GOPATH/src/go-server-server` to build the `go-server-server` binary.
Run `make build` from the repository root to prepare the SDK Go adapter in the local `swsscommon` module and build the `go-server-server` binary.

## Building the mock Thrift server
The Go server requires that it can connect to the Thrift server, we have a mock server that will allow every request to suceed, this will allow the Go server to run successfully.
Expand Down
9 changes: 8 additions & 1 deletion copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import os
import shutil
import urllib.parse
from subprocess import Popen, PIPE

Expand All @@ -16,7 +17,13 @@ def main():
if not os.path.exists("./debs/"):
os.mkdir("./debs")

for dep in config['dependencies']:
swsscommon_debs_dir = os.environ.get("SWSSCOMMON_DEBS_DIR")
for dep in config['dependencies']:
if swsscommon_debs_dir and dep.startswith(("libswsscommon_", "libswsscommon-dev_")):
filename = f"{dep}_{config['arch']}.deb"
shutil.copyfile(os.path.join(swsscommon_debs_dir, filename), os.path.join("debs", filename))
continue

params = {
'branchName': config['branch'],
'platform': config['platform'],
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Source: sonic-rest-api-server
Maintainer: Pavel Shirshov <pavelsh@microsoft.com>
Section: net
Priority: optional
Build-Depends: dh-exec (>=0.3), debhelper (>= 9)
Build-Depends: libswsscommon-dev, dh-exec (>=0.3), debhelper (>= 9)
Standards-Version: 1.0.0

Package: sonic-rest-api
Expand Down
15 changes: 10 additions & 5 deletions go-server-server/go/persistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ func genVxlanTunnelInfo() {

func DBConnect() {
if *RunApiAsLocalTestDocker {
redisDB = redis.NewClient(&redis.Options{
redisDB = newRedisClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
})
} else {
redisDB = redis.NewClient(&redis.Options{
redisDB = newRedisClient(&redis.Options{
Network: "unix",
Addr: REDIS_SOCK,
Password: "",
Expand All @@ -205,9 +205,14 @@ func DBConnect() {
swssDB = swsscommon.NewDBConnector(APPL_DB, "localhost", 6379, SWSS_TIMEOUT)
swss_conf_DB = swsscommon.NewDBConnector(CONFIG_DB, "localhost", 6379, SWSS_TIMEOUT)
} else {
swssDB = swsscommon.NewDBConnector2(APPL_DB, REDIS_SOCK, SWSS_TIMEOUT)
swss_conf_DB = swsscommon.NewDBConnector2(CONFIG_DB, REDIS_SOCK, SWSS_TIMEOUT)
swss_ctr_DB = swsscommon.NewDBConnector2(COUNTER_DB, REDIS_SOCK, SWSS_TIMEOUT)
connect := func(name string) swsscommon.DBConnector {
db, err := swsscommon.NewDBConnectorNamed(name, SWSS_TIMEOUT, true)
if err != nil { log.Fatalf("Cannot connect to %s: %v", name, err) }
return db
}
swssDB = connect("APPL_DB")
swss_conf_DB = connect("CONFIG_DB")
swss_ctr_DB = connect("COUNTERS_DB")
}
app_db_ops = db_ops{separator: ":", swss_db: swssDB, db_num: APPL_DB}
conf_db_ops = db_ops{separator: "|", swss_db: swss_conf_DB, db_num: CONFIG_DB}
Expand Down
18 changes: 18 additions & 0 deletions go-server-server/go/redis_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package restapi

import (
"context"
"net"

"github.com/redis/go-redis/v9"
"swsscommon/redisconn"
)

func newRedisClient(options *redis.Options) *redis.Client {
if options.Network == "unix" && options.TLSConfig == nil {
options.Dialer = func(ctx context.Context, _ string, address string) (net.Conn, error) {
return redisconn.DialUnixContext(ctx, address, options.DialTimeout, true)
}
}
return redis.NewClient(options)
}
Loading