Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Network code #49

Merged
merged 9 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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: 12 additions & 0 deletions api/proto/v1/network.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/*
MIT License
Copyright(c) 2022 Futurewei Cloud
Permission is hereby granted,
free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
syntax="proto3";
package merak;
option go_package = "merak/";
Expand Down
104 changes: 104 additions & 0 deletions deployments/kubernetes/network.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# MIT License
# Copyright(c) 2022 Futurewei Cloud
# Permission is hereby granted,
# free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons
# to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

apiVersion: v1
kind: Service
metadata:
name: merak-network-service
namespace: merak
spec:
selector:
app: merak-network
ports:
- protocol: TCP
name: grpc
port: 40053
targetPort: network-grpc
type: ClusterIP
---
# Merak Network Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: merak-network
namespace: merak
spec:
replicas: 1
selector:
matchLabels:
app: merak-network
template:
metadata:
labels:
app: merak-network
spec:
tolerations:
- key: "node-role.kubernetes.io/master"
operator: "Exists"
effect: "NoSchedule"
containers:
- name: merak-network
image: yanmo96/merak-network:dev
ports:
- containerPort: 40053
name: network-grpc
---
apiVersion: v1
kind: Service
metadata:
name: network-redis-main
namespace: merak
labels:
app: redis
role: main
tier: backend
spec:
ports:
- protocol: TCP
port: 30053
targetPort: network-redis
selector:
app: redis
role: main
tier: backend
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-main
namespace: merak
labels:
app: redis
role: main
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: main
tier: backend
spec:
containers:
- name: main
image: "redis:7.0.0"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
name: network-redis
18 changes: 18 additions & 0 deletions docker/network.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#MIT License
#Copyright(c) 2022 Futurewei Cloud
# Permission is hereby granted,
# free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons
# to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


FROM golang:1.18-alpine

WORKDIR /
yanmo96 marked this conversation as resolved.
Show resolved Hide resolved
RUN mkdir -p /merak-bin
COPY services/merak-network/merak-network /merak-bin/merak-network
CMD [ "/merak-bin/merak-network" ]
2 changes: 1 addition & 1 deletion services/merak-compute/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {

log.Printf("Starting gRPC server. Listening at %v", lis.Addr())
if err := gRPCServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
log.Printf("failed to serve: %v", err)
}
yanmo96 marked this conversation as resolved.
Show resolved Hide resolved

defer service.TemporalClient.Close()
Expand Down
56 changes: 56 additions & 0 deletions services/merak-network/activities/node_register_activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
MIT License
Copyright(c) 2022 Futurewei Cloud
Permission is hereby granted,
free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package activities

import (
"context"
"encoding/json"
pb "github.com/futurewei-cloud/merak/api/proto/v1/merak"
"github.com/futurewei-cloud/merak/services/merak-network/entities"
"github.com/futurewei-cloud/merak/services/merak-network/http"
"github.com/futurewei-cloud/merak/services/merak-network/utils"
"log"
"sync"
)

func RegisterNode(ctx context.Context, compute []*pb.InternalComputeInfo, wg *sync.WaitGroup, projectId string) (string, error) {
log.Println("RegisterNode")
//defer wg.Done()
log.Printf("compute %s", compute)
nodeInfo := entities.NodeStruct{}

for _, host := range compute {
log.Printf("host %s", host)
nodeBody := entities.NodeBody{
LocalIP: host.Ip,
MacAddress: host.Mac,
NodeID: host.Id,
NodeName: host.Name,
ServerPort: 50001,
Veth: host.Veth,
}
nodeInfo.Hosts = append(nodeInfo.Hosts, nodeBody)
}
log.Printf("nodeInfo: %s", nodeInfo)
returnMessage, returnErr := http.RequestCall("http://"+utils.ALCORURL+":30007/nodes/bulk", "POST", nodeInfo, nil)
if returnErr != nil {
log.Printf("returnErr %s", returnErr)
}
yanmo96 marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("returnMessage %s", returnMessage)
var returnJson entities.NodeReturn
json.Unmarshal([]byte(returnMessage), &returnJson)
log.Printf("returnJson : %+v", returnJson)
log.Println("RegisterNode done")
return "", nil
}
106 changes: 106 additions & 0 deletions services/merak-network/activities/services_activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
MIT License
Copyright(c) 2022 Futurewei Cloud
Permission is hereby granted,
free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package activities

import (
"context"
"fmt"
pb "github.com/futurewei-cloud/merak/api/proto/v1/merak"
"github.com/futurewei-cloud/merak/services/merak-network/http"
"log"
"os/exec"
"strings"
"sync"
)

func DoServices(ctx context.Context, services []*pb.InternalServiceInfo, wg *sync.WaitGroup, projectId string) (string, error) {
log.Println("DoServices")
//defer wg.Done()
log.Printf("DoServices %s", services)
idServiceMap := make(map[string]*pb.InternalServiceInfo)
runSequenceMap := make(map[string]string)
var runIds []string
numberOfService := 0
for _, service := range services {
if service.WhereToRun == "network" {
idServiceMap[service.Name] = service
if service.WhenToRun != "INIT" {
log.Printf("WhenToRun %s", service.WhenToRun)
if strings.ReplaceAll(strings.Split(service.WhenToRun, ":")[0], " ", "") == "AFTER" {
runSequenceMap[strings.Split(service.WhenToRun, ":")[1]] = service.Name
log.Printf("numberOfService %s", numberOfService)
}
if strings.ReplaceAll(strings.Split(service.WhenToRun, ":")[0], " ", "") == "BEFORE" {
runSequenceMap[service.Name] = strings.Split(service.WhenToRun, ":")[1]
log.Printf("numberOfService %s", numberOfService)
}
}
if service.WhenToRun == "INIT" {
runIds = append(runIds, service.Name)
}
numberOfService++
}
}
log.Printf("runSequenceMap %s", runSequenceMap)

for _, eachRunId := range runIds {
currentId := eachRunId
for {
if idServiceMap[currentId].Cmd == "curl" {
log.Println("ssh service")
var headers []string
var payload string
for _, parameter := range idServiceMap[currentId].Parameters {
if strings.Split(parameter, " ")[0] == "-H" {
headers = append(headers, strings.ReplaceAll(strings.Split(parameter, " ")[1], "'", ""))
}
if strings.Split(parameter, " ")[0] == "-d" {
payload = strings.ReplaceAll(strings.Split(parameter, " ")[1], "'", "")
}
}
returnMessage, returnErr := http.RequestCall(idServiceMap[currentId].Url, strings.Split(idServiceMap[currentId].Parameters[0], " ")[0], payload, headers)
if returnErr != nil {
log.Printf("returnErr %s", returnErr)
}
yanmo96 marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("returnMessage %s", returnMessage)

}
if idServiceMap[currentId].Cmd == "ssh" {
var shellCommand string
for _, command := range idServiceMap[currentId].Parameters {
shellCommand = shellCommand + " " + command
}
cmd := exec.Command(shellCommand)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return "", nil
}
log.Printf("shellCommand out: %s", string(stdout))
log.Println("curl service")
}

nextKey, ok := runSequenceMap[currentId]
log.Printf("nextKey %s", nextKey)
if ok {
fmt.Println("value: ", nextKey)
currentId = nextKey
} else {
fmt.Println("key not found")
break
}
}
}
return "", nil
}
Loading