Skip to content

Commit

Permalink
feat: introduce new service
Browse files Browse the repository at this point in the history
  • Loading branch information
c100k committed Mar 21, 2024
1 parent 57d237d commit 61e9ca6
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 4 deletions.
50 changes: 50 additions & 0 deletions data/servers.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"flavor": "medium",
"fqdn": "server01.mycompany.com",
"id": "123",
"ipv4": "192.168.0.26",
"name": "server01",
"scopes": {
"geo": {
"label": "Paris 01",
"value": "par-01"
},
"logical": {
"label": "Project 1",
"value": "project-1"
}
},
"ssh": {
"keyName": "keypair-01",
"port": 22,
"username": "admin"
},
"stack": "nodejs",
"status": "off"
},
{
"flavor": "medium",
"fqdn": "server02.mycompany.com",
"id": "456",
"ipv4": "192.168.0.27",
"name": "server02",
"scopes": {
"geo": {
"label": "Paris 01",
"value": "par-01"
},
"logical": {
"label": "Project 1",
"value": "project-1"
}
},
"ssh": {
"keyName": "keypair-01",
"port": 22,
"username": "admin"
},
"stack": "go",
"status": "off"
}
]
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ services:
- RBTX_API_KEY=NkCH56YZfP9psrycDLVk
- RBTX_PATH_PREFIX=cd5331ba
- RBTX_PORT=9001
- RBTX_SERVICE_IMPL=fileJson
- RBTX_SERVICE_FILE_JSON_FILE_PATH=/data/servers.example.json
ports:
- "9001:9001"
volumes:
- .docker/go:/go
- ./data:/data
- ./impl/http-server-go:/app
working_dir: "/app"
command: >
bash -c "go fmt
&& go run main.go config.go data.go errors.go handlers.go middlewares.go service.go service_noop.go service_self.go utils.go"
&& go run main.go config.go data.go errors.go handlers.go middlewares.go service.go service_file_json.go service_noop.go service_self.go utils.go"
swagger-ui:
image: swaggerapi/swagger-ui:v5.12.0
environment:
Expand Down
26 changes: 24 additions & 2 deletions impl/http-server-go/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Config struct {
runnableSSHUsername string
runnableStack string
serviceImpl string
serviceFileJsonFilePath *string
sysCmdPkg string
sysCmdReboot string
sysCmdStop string
Expand Down Expand Up @@ -56,13 +57,26 @@ func getConfig() *Config {
runnableSSHUsername: getEnvOr("RUNNABLE_SSH_USERNAME", "root"),
runnableStack: getEnvOr("RUNNABLE_STACK", "nodejs"),
serviceImpl: getEnvOr("SERVICE_IMPL", "self"),
serviceFileJsonFilePath: getNullableEnv("SERVICE_FILE_JSON_FILE_PATH"),
sysCmdPkg: getEnvOr("SYS_CMD_PKG", "syscall"),
sysCmdReboot: getEnvOr("SYS_CMD_REBOOT", "reboot"),
sysCmdStop: getEnvOr("SYS_CMD_STOP", "shutdown"),
}

if config.serviceImpl != "noop" && config.serviceImpl != "self" {
panic(fmt.Sprintf("Valid values for serviceImpl are : 'noop' and 'self'. Got '%s'", config.serviceImpl))
if config.serviceImpl != "fileJson" && config.serviceImpl != "noop" && config.serviceImpl != "self" {
panic(fmt.Sprintf("Valid values for serviceImpl are : 'fileJson' and 'noop' and 'self'. Got '%s'", config.serviceImpl))
}

if config.serviceImpl == "fileJson" {
if config.serviceFileJsonFilePath == nil {
panic(fmt.Sprintf("You must provide a json file path when serviceImpl is 'fileJson'"))
} else {
path := *config.serviceFileJsonFilePath
_, err := os.Stat(path)
if err != nil {
panic(fmt.Sprintf("The file %s does not exist", path))
}
}
}

if config.sysCmdPkg != "exec" && config.sysCmdPkg != "syscall" {
Expand Down Expand Up @@ -100,3 +114,11 @@ func getEnvAsIntOr(key string, fallback int32) int32 {
}
return *v
}

func getNullableEnv(key string) *string {
v := getEnvOr(key, "")
if len(v) == 0 {
return nil
}
return &v
}
4 changes: 3 additions & 1 deletion impl/http-server-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ func main() {

var service Service
switch config.serviceImpl {
case "fileJson":
service = ServiceFileJson{config: config, logger: logger}
case "noop":
service = ServiceNoop{logger: logger}
case "self":
service = ServiceSelf{config: config, logger: logger}
default:
panic("Invalid serviceImpl")
panic(fmt.Sprintf("Invalid serviceImpl : %s", config.serviceImpl))
}

router := mux.NewRouter()
Expand Down
50 changes: 50 additions & 0 deletions impl/http-server-go/service_file_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"encoding/json"
"io"
"log/slog"
"openapi"
"os"
)

type ServiceFileJson struct {
config *Config
logger *slog.Logger
}

func (service ServiceFileJson) list(params *openapi.ListRunnablesQueryParams) (*ServiceError, *openapi.ListResRunnable) {
config := service.config

file, err := os.Open(*config.serviceFileJsonFilePath)
if err != nil {
return &ServiceError{HttpStatus: 500, Message: err.Error()}, nil
}

defer file.Close()

content, err := io.ReadAll(file)
if err != nil {
return &ServiceError{HttpStatus: 500, Message: err.Error()}, nil
}

var items []openapi.Runnable
json.Unmarshal(content, &items)
if items == nil {
return &ServiceError{HttpStatus: 500, Message: "Fix your JSON file to respect the schema"}, nil
}

total := int32(len(items))

res := openapi.NewListResRunnable(items, total)

return nil, res
}

func (service ServiceFileJson) reboot(id string) (*ServiceError, *openapi.RunnableOperationRes) {
return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
}

func (service ServiceFileJson) stop(id string) (*ServiceError, *openapi.RunnableOperationRes) {
return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
}

0 comments on commit 61e9ca6

Please sign in to comment.