Skip to content

Commit ba6d959

Browse files
authored
test-cloud-server: build and publish test-cloud-server image (#510)
* test-cloud-server: build and publish test-cloud-server image
1 parent c1e0134 commit ba6d959

7 files changed

Lines changed: 441 additions & 224 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
6+
name: Create and publish a test-cloud-server image
7+
8+
on:
9+
push:
10+
branches:
11+
- main
12+
tags:
13+
- '*'
14+
15+
env:
16+
REGISTRY: ghcr.io
17+
IMAGE_NAME: ${{ github.repository }}/test-cloud-server
18+
19+
jobs:
20+
build-and-push-image:
21+
runs-on: ubuntu-latest
22+
permissions:
23+
contents: read
24+
packages: write
25+
26+
steps:
27+
- name: Checkout repository
28+
uses: actions/checkout@v2
29+
30+
- name: Log in to the Container registry
31+
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
32+
with:
33+
registry: ${{ env.REGISTRY }}
34+
username: ${{ github.actor }}
35+
password: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Extract metadata (tags, labels) for test-cloud-server
38+
id: meta
39+
uses: docker/metadata-action@v3
40+
with:
41+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
42+
tags: |
43+
type=schedule
44+
type=ref,event=branch
45+
type=ref,event=tag
46+
type=ref,event=pr
47+
48+
- name: Build hub-build image
49+
run: make hub-build
50+
51+
- name: Build and push test-cloud-server docker image
52+
uses: docker/build-push-action@v2
53+
with:
54+
context: ./test/cloud-server
55+
push: true
56+
tags: ${{ steps.meta.outputs.tags }}
57+
labels: ${{ steps.meta.outputs.labels }}

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ env: clean certificates nats mongo privateKeys
8282
if [ "${TRAVIS_OS_NAME}" == "linux" ]; then \
8383
sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'; \
8484
fi
85+
mkdir -p $(WORKING_DIRECTORY)/.tmp/devsim
8586
docker run \
8687
-d \
8788
--privileged \
8889
--name=devsim \
8990
--network=host \
91+
-v $(WORKING_DIRECTORY)/.tmp/devsim:/tmp \
9092
ghcr.io/iotivity/iotivity-lite/cloud-server-debug:latest \
9193
devsim-$(SIMULATOR_NAME_SUFFIX)
9294

@@ -158,6 +160,7 @@ clean:
158160
docker rm -f nats || true
159161
docker rm -f nats-cloud-connector || true
160162
docker rm -f devsim || true
163+
sudo rm -rf ./.tmp/devsim
161164
sudo rm -rf ./.tmp/certs || true
162165
sudo rm -rf ./.tmp/mongo || true
163166
sudo rm -rf ./.tmp/home || true

bundle/client/ob/main.go

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
"flag"
88
"fmt"
99
"log"
10+
"net"
1011
"net/http"
12+
"strings"
1113
"time"
1214

1315
"github.com/plgd-dev/device/client"
16+
"github.com/plgd-dev/device/schema/device"
1417
capb "github.com/plgd-dev/hub/certificate-authority/pb"
1518
"github.com/plgd-dev/hub/grpc-gateway/pb"
1619
grpcCloud "github.com/plgd-dev/hub/pkg/net/grpc"
@@ -86,18 +89,31 @@ func ownAndOnboard(ctx context.Context, c *OcfClient, deviceID, apn, authCode st
8689

8790
func main() {
8891
addr := flag.String("addr", "localhost:443", "address")
89-
accessToken := flag.String("accessToken", "", "accessToken")
90-
authCode := flag.String("authCode", "test", "authCode")
91-
deviceID := flag.String("deviceId", "", "deviceId")
92-
discoverTimeout := flag.Duration("discoverTimeout", time.Second, "discoverTimeout")
93-
apn := flag.String("authorizationProvider", "plgd", "authorizationProvider")
92+
authAddr := flag.String("authAddr", "", "auth address to get access token from mock oauth server")
93+
accessToken := flag.String("accessToken", "", "use directly access token without contacting mock oauth server")
94+
authCode := flag.String("authCode", "test", "use authorization code for registration device to the cloud")
95+
deviceID := flag.String("deviceId", "", "onboard the device")
96+
listDevices := flag.Bool("listDevices", false, "list devices which can be onboard to the cloud")
97+
discoverDuration := flag.Duration("discoverDuration", time.Second, "discover devices for X seconds")
98+
apn := flag.String("authorizationProvider", "plgd", "use authorization provider for registration device to the cloud")
9499
flag.Parse()
95100

101+
if *authAddr == "" {
102+
*authAddr = *addr
103+
}
96104
if *accessToken == "" {
97105
var err error
98-
*accessToken, err = getServiceToken(*addr)
106+
*accessToken, err = getServiceToken(*authAddr)
99107
if err != nil {
100-
log.Fatalf("cannot get access token")
108+
log.Fatalf("cannot get access token: %v", err)
109+
}
110+
}
111+
112+
// check if port is part of address, otherwise append ":443"
113+
_, _, err := net.SplitHostPort(*addr)
114+
if err != nil {
115+
if strings.Contains(err.Error(), "missing port in address") {
116+
*addr = *addr + ":443"
101117
}
102118
}
103119

@@ -115,12 +131,17 @@ func main() {
115131
grpcClient := pb.NewGrpcGatewayClient(grpcConn)
116132

117133
caClient := capb.NewCertificateAuthorityClient(grpcConn)
118-
ctx, cancel := context.WithTimeout(context.Background(), *discoverTimeout+60*time.Second)
134+
ctx, cancel := context.WithTimeout(context.Background(), *discoverDuration+60*time.Second)
119135
defer cancel()
120136
ctx = grpcCloud.CtxWithToken(ctx, *accessToken)
121137

138+
hubConfiguration, err := grpcClient.GetHubConfiguration(ctx, &pb.HubConfigurationRequest{})
139+
if err != nil {
140+
log.Fatalf("cannot get hub configuration: %v", err)
141+
}
142+
122143
c := new(OcfClient)
123-
err = c.Initialize(ctx, grpcClient, caClient)
144+
err = c.Initialize(ctx, hubConfiguration, caClient)
124145
if err != nil {
125146
log.Fatalf("cannot initialize ocf client: %v", err)
126147
}
@@ -130,17 +151,29 @@ func main() {
130151
return
131152
}
132153

133-
devices, err := c.Discover(ctx, *discoverTimeout)
154+
devices, err := c.Discover(ctx, *discoverDuration)
134155
if err != nil {
135156
log.Fatalf("cannot device devices: %v", err)
136157
}
137-
fmt.Printf("found %v devices with discover timeout %v\n", len(devices), *discoverTimeout)
138-
158+
filteredDevices := make([]client.DeviceDetails, 0, len(devices))
139159
for _, d := range devices {
140160
if d.IsSecured && d.OwnershipStatus == client.OwnershipStatus_ReadyToBeOwned {
161+
filteredDevices = append(filteredDevices, d)
162+
}
163+
}
164+
fmt.Printf("found %v ready to be owned devices with discover duration %v\n", len(filteredDevices), *discoverDuration)
165+
for _, d := range filteredDevices {
166+
if !*listDevices {
141167
ownAndOnboard(ctx, c, d.ID, *apn, *authCode)
142168
return
143169
}
170+
name := "unknown"
171+
id := d.ID
172+
if d.Details != nil {
173+
if v, ok := d.Details.(*device.Device); ok {
174+
name = v.Name
175+
}
176+
}
177+
fmt.Printf("%v(%v)\n", name, id)
144178
}
145-
146179
}

bundle/client/ob/ocfclient.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

78
"github.com/plgd-dev/device/app"
@@ -11,7 +12,6 @@ import (
1112
capb "github.com/plgd-dev/hub/certificate-authority/pb"
1213
"github.com/plgd-dev/hub/certificate-authority/signer"
1314
"github.com/plgd-dev/hub/grpc-gateway/pb"
14-
"github.com/plgd-dev/kit/v2/codec/json"
1515
"github.com/plgd-dev/kit/v2/security"
1616
)
1717

@@ -21,24 +21,19 @@ type OcfClient struct {
2121
}
2222

2323
// Initialize creates and initializes new local client
24-
func (c *OcfClient) Initialize(ctx context.Context, grpcClient pb.GrpcGatewayClient, caClient capb.CertificateAuthorityClient) error {
25-
hubConfiguration, err := grpcClient.GetHubConfiguration(ctx, &pb.HubConfigurationRequest{})
26-
if err != nil {
27-
return err
28-
}
24+
func (c *OcfClient) Initialize(ctx context.Context, hubConfiguration *pb.HubConfigurationResponse, caClient capb.CertificateAuthorityClient) error {
2925
appCallback, err := app.NewApp(&app.AppConfig{
3026
RootCA: hubConfiguration.GetCertificateAuthorities(),
3127
})
3228
if err != nil {
33-
return err
29+
return fmt.Errorf("cannot create app callback: %w", err)
3430
}
3531

3632
signer := signer.NewIdentityCertificateSigner(caClient)
3733

3834
localClient, err := client.NewClientFromConfig(&client.Config{
39-
DisablePeerTCPSignalMessageCSMs: true,
40-
KeepAliveConnectionTimeoutSeconds: 10,
41-
ObserverPollingIntervalSeconds: 1,
35+
KeepAliveConnectionTimeoutSeconds: 30,
36+
ObserverPollingIntervalSeconds: 15,
4237
DeviceCacheExpirationSeconds: 3600,
4338
MaxMessageSize: 512 * 1024,
4439
DeviceOwnershipBackend: &client.DeviceOwnershipBackendConfig{
@@ -48,12 +43,12 @@ func (c *OcfClient) Initialize(ctx context.Context, grpcClient pb.GrpcGatewayCli
4843
}, appCallback, nil, func(err error) {})
4944

5045
if err != nil {
51-
return err
46+
return fmt.Errorf("cannot create client: %w", err)
5247
}
5348

5449
err = localClient.Initialization(ctx)
5550
if err != nil {
56-
return err
51+
return fmt.Errorf("cannot initialize client: %w", err)
5752
}
5853

5954
c.localClient = localClient
@@ -72,21 +67,6 @@ func (c *OcfClient) Discover(ctx context.Context, timeout time.Duration) (map[st
7267
return c.localClient.GetDevices(ctx)
7368
}
7469

75-
// GetResource retrieves, encodes and returns resource representation of specified resource
76-
func (c *OcfClient) GetResource(ctx context.Context, deviceID, resourceHref string) (string, error) {
77-
var data interface{}
78-
err := c.localClient.GetResource(ctx, deviceID, resourceHref, &data)
79-
if err != nil || data == nil {
80-
return "", err
81-
}
82-
83-
dataJSON, err := json.Encode(data)
84-
if err != nil {
85-
return "", err
86-
}
87-
return string(dataJSON), nil
88-
}
89-
9070
// OwnDevice transfers the ownersip of the device to user represented by the token
9171
func (c *OcfClient) OwnDevice(ctx context.Context, deviceID string) (string, error) {
9272
return c.localClient.OwnDevice(ctx, deviceID, client.WithOTM(client.OTMType_JustWorks))

test/cloud-server/Dockerfile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
FROM hub-build AS build
2+
ARG root_directory=$GOPATH/src/github.com/plgd-dev/hub
3+
4+
#grpc-gateway
5+
ARG service=grpc-gateway
6+
WORKDIR $root_directory/$service/service
7+
RUN go test -c -ldflags "-linkmode external -extldflags -static" -o /go/bin/grpc-gateway.test
8+
9+
#certificate-generator
10+
ARG service=kit
11+
WORKDIR /
12+
RUN cd $GOPATH/pkg/mod/github.com/plgd-dev/kit/v2* && go build -ldflags "-linkmode external -extldflags -static" -o /go/bin/certificate-generator ./cmd/certificate-generator
13+
14+
#nats
15+
WORKDIR $root_directory
16+
RUN curl -L https://github.com/nats-io/nats-server/releases/download/v2.3.1/nats-server-v2.3.1-linux-amd64.zip -o ./nats-server.zip
17+
RUN mkdir -p ./nats-server
18+
RUN unzip ./nats-server.zip -d ./nats-server
19+
RUN cp ./nats-server/*/nats-server /go/bin/nats-server
20+
21+
RUN curl -L https://github.com/nats-io/natscli/releases/download/0.0.24/nats-0.0.24-linux-amd64.zip -o ./nats.zip
22+
RUN mkdir -p ./nats
23+
RUN unzip ./nats.zip -d ./nats
24+
RUN cp ./nats/*/nats /go/bin/nats
25+
26+
FROM ubuntu:20.04 as service
27+
RUN apt update
28+
RUN apt install -y wget gnupg iproute2 systemctl openssl nginx ca-certificates netcat
29+
RUN wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
30+
RUN wget https://github.com/mikefarah/yq/releases/download/v4.6.3/yq_linux_amd64 -O /usr/bin/yq && chmod +x /usr/bin/yq
31+
RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list
32+
RUN apt update
33+
RUN apt-get install -y mongodb-org-server mongodb-org
34+
COPY --from=build /go/bin/certificate-generator /usr/local/bin/certificate-generator
35+
COPY --from=build /go/bin/grpc-gateway.test /usr/local/bin/grpc-gateway.test
36+
COPY --from=build /go/bin/nats-server /usr/local/bin/nats-server
37+
COPY --from=build /go/bin/nats /usr/local/bin/nats
38+
COPY run.sh /usr/local/bin/run.sh
39+
40+
ENV FQDN="localhost"
41+
42+
# ports
43+
ENV MONGO_PORT=27017
44+
ENV NATS_PORT=4222
45+
46+
47+
ENTRYPOINT ["/usr/local/bin/run.sh"]

0 commit comments

Comments
 (0)