Conf Oclib Package Lightest
This commit is contained in:
20
conf/conf.go
20
conf/conf.go
@@ -8,18 +8,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
MonitorPath string
|
MonitorPath string
|
||||||
MongoUrl string
|
Logs string
|
||||||
DBName string
|
Mode string
|
||||||
Logs string
|
KubeHost string
|
||||||
LokiUrl string
|
KubePort string
|
||||||
NatsUrl string
|
KubeCA string
|
||||||
Mode string
|
KubeCert string
|
||||||
KubeHost string
|
KubeData string
|
||||||
KubePort string
|
|
||||||
KubeCA string
|
|
||||||
KubeCert string
|
|
||||||
KubeData string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var instance *Config
|
var instance *Config
|
||||||
|
|||||||
@@ -8,29 +8,30 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"oc-schedulerd/conf"
|
"oc-schedulerd/conf"
|
||||||
|
|
||||||
|
oclib "cloud.o-forge.io/core/oc-lib"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ContainerMonitor struct {
|
type ContainerMonitor struct {
|
||||||
Monitor LocalMonitor
|
Monitor LocalMonitor
|
||||||
KubeCA string
|
KubeCA string
|
||||||
KubeCert string
|
KubeCert string
|
||||||
KubeData string
|
KubeData string
|
||||||
KubeHost string
|
KubeHost string
|
||||||
KubePort string
|
KubePort string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainerMonitor(UUID string, peerId string, duration int) (Executor){
|
func NewContainerMonitor(UUID string, peerId string, duration int) Executor {
|
||||||
return &ContainerMonitor{
|
return &ContainerMonitor{
|
||||||
Monitor: LocalMonitor{
|
Monitor: LocalMonitor{
|
||||||
ExecutionID: UUID,
|
ExecutionID: UUID,
|
||||||
PeerID: peerId,
|
PeerID: peerId,
|
||||||
Duration: duration,
|
Duration: duration,
|
||||||
LokiUrl: conf.GetConfig().LokiUrl,
|
LokiUrl: oclib.GetConfig().LokiUrl,
|
||||||
MongoUrl: conf.GetConfig().MongoUrl,
|
MongoUrl: oclib.GetConfig().MongoUrl,
|
||||||
DBName: conf.GetConfig().DBName,
|
DBName: oclib.GetConfig().MongoDatabase,
|
||||||
},
|
},
|
||||||
KubeCA: conf.GetConfig().KubeCA,
|
KubeCA: conf.GetConfig().KubeCA,
|
||||||
KubeCert: conf.GetConfig().KubeCert,
|
KubeCert: conf.GetConfig().KubeCert,
|
||||||
KubeData: conf.GetConfig().KubeData,
|
KubeData: conf.GetConfig().KubeData,
|
||||||
KubeHost: conf.GetConfig().KubeHost,
|
KubeHost: conf.GetConfig().KubeHost,
|
||||||
@@ -38,7 +39,7 @@ func NewContainerMonitor(UUID string, peerId string, duration int) (Executor){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ContainerMonitor) PrepareMonitorExec() []string {
|
func (cm *ContainerMonitor) PrepareMonitorExec() []string {
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-e", cm.Monitor.ExecutionID,
|
"-e", cm.Monitor.ExecutionID,
|
||||||
@@ -49,7 +50,7 @@ func (cm *ContainerMonitor) PrepareMonitorExec() []string {
|
|||||||
"-M", "kubernetes",
|
"-M", "kubernetes",
|
||||||
"-H", cm.KubeHost,
|
"-H", cm.KubeHost,
|
||||||
"-P", cm.KubePort,
|
"-P", cm.KubePort,
|
||||||
"-C", cm.KubeCert,
|
"-C", cm.KubeCert,
|
||||||
"-D", cm.KubeData,
|
"-D", cm.KubeData,
|
||||||
"-c", cm.KubeCA,
|
"-c", cm.KubeCA,
|
||||||
}
|
}
|
||||||
@@ -58,8 +59,8 @@ func (cm *ContainerMonitor) PrepareMonitorExec() []string {
|
|||||||
args = append(args, "-t", fmt.Sprintf("%d", cm.Monitor.Duration))
|
args = append(args, "-t", fmt.Sprintf("%d", cm.Monitor.Duration))
|
||||||
}
|
}
|
||||||
|
|
||||||
return args
|
return args
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contact the docker's API at the KubeHost's URL to :
|
// Contact the docker's API at the KubeHost's URL to :
|
||||||
@@ -67,8 +68,8 @@ func (cm *ContainerMonitor) PrepareMonitorExec() []string {
|
|||||||
// - Create the container
|
// - Create the container
|
||||||
// - Start the container
|
// - Start the container
|
||||||
func (cm *ContainerMonitor) LaunchMonitor(args []string, l zerolog.Logger) {
|
func (cm *ContainerMonitor) LaunchMonitor(args []string, l zerolog.Logger) {
|
||||||
|
|
||||||
var containerID string
|
var containerID string
|
||||||
imageName := "oc-monitord"
|
imageName := "oc-monitord"
|
||||||
url := "http://" + cm.KubeHost + ":2375"
|
url := "http://" + cm.KubeHost + ":2375"
|
||||||
|
|
||||||
@@ -81,15 +82,15 @@ func (cm *ContainerMonitor) LaunchMonitor(args []string, l zerolog.Logger) {
|
|||||||
d, _ := io.ReadAll(resp.Body)
|
d, _ := io.ReadAll(resp.Body)
|
||||||
l.Fatal().Msg("Couldn't find the oc-monitord image : " + string(d))
|
l.Fatal().Msg("Couldn't find the oc-monitord image : " + string(d))
|
||||||
}
|
}
|
||||||
|
|
||||||
dataCreation := map[string]interface{}{"Image": imageName, "Cmd" : args}
|
dataCreation := map[string]interface{}{"Image": imageName, "Cmd": args}
|
||||||
byteData, err := json.Marshal(dataCreation)
|
byteData, err := json.Marshal(dataCreation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Fatal().Msg("Error when contacting the creating request body : " + err.Error())
|
l.Fatal().Msg("Error when contacting the creating request body : " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
r, _ := http.NewRequest("POST",url + "/containers/create", bytes.NewBuffer(byteData))
|
r, _ := http.NewRequest("POST", url+"/containers/create", bytes.NewBuffer(byteData))
|
||||||
r.Header.Add("Content-Type","application/json")
|
r.Header.Add("Content-Type", "application/json")
|
||||||
resp, err = http.DefaultClient.Do(r)
|
resp, err = http.DefaultClient.Do(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error())
|
l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error())
|
||||||
@@ -116,9 +117,9 @@ func (cm *ContainerMonitor) LaunchMonitor(args []string, l zerolog.Logger) {
|
|||||||
|
|
||||||
networkName := "oc"
|
networkName := "oc"
|
||||||
|
|
||||||
dataNetwork, _ := json.Marshal(map[string]string{"Container" : containerID})
|
dataNetwork, _ := json.Marshal(map[string]string{"Container": containerID})
|
||||||
r, _ = http.NewRequest("POST",url + "/networks/" + networkName + "/connect", bytes.NewBuffer(dataNetwork))
|
r, _ = http.NewRequest("POST", url+"/networks/"+networkName+"/connect", bytes.NewBuffer(dataNetwork))
|
||||||
r.Header.Add("Content-Type","application/json")
|
r.Header.Add("Content-Type", "application/json")
|
||||||
resp, err = http.DefaultClient.Do(r)
|
resp, err = http.DefaultClient.Do(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error())
|
l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error())
|
||||||
@@ -129,7 +130,7 @@ func (cm *ContainerMonitor) LaunchMonitor(args []string, l zerolog.Logger) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err = http.Post( url + "/containers/" + containerID + "/start", "", nil)
|
resp, err = http.Post(url+"/containers/"+containerID+"/start", "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error())
|
l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -143,4 +144,4 @@ func (cm *ContainerMonitor) LaunchMonitor(args []string, l zerolog.Logger) {
|
|||||||
// we can add logging with GET /containers/id/logs?stdout=true&follow=true
|
// we can add logging with GET /containers/id/logs?stdout=true&follow=true
|
||||||
|
|
||||||
// logExecution(stdoutMonitord, l)
|
// logExecution(stdoutMonitord, l)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"oc-schedulerd/conf"
|
"oc-schedulerd/conf"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
|
oclib "cloud.o-forge.io/core/oc-lib"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -12,20 +13,19 @@ type LocalMonitor struct {
|
|||||||
ExecutionID string
|
ExecutionID string
|
||||||
PeerID string
|
PeerID string
|
||||||
Duration int
|
Duration int
|
||||||
LokiUrl string
|
LokiUrl string
|
||||||
MongoUrl string
|
MongoUrl string
|
||||||
DBName string
|
DBName string
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalMonitor(UUID string, peerId string, duration int) (Executor){
|
func NewLocalMonitor(UUID string, peerId string, duration int) Executor {
|
||||||
return &LocalMonitor{
|
return &LocalMonitor{
|
||||||
ExecutionID: UUID,
|
ExecutionID: UUID,
|
||||||
PeerID: peerId,
|
PeerID: peerId,
|
||||||
Duration: duration,
|
Duration: duration,
|
||||||
LokiUrl: conf.GetConfig().LokiUrl,
|
LokiUrl: oclib.GetConfig().LokiUrl,
|
||||||
MongoUrl: conf.GetConfig().MongoUrl,
|
MongoUrl: oclib.GetConfig().MongoUrl,
|
||||||
DBName: conf.GetConfig().DBName,
|
DBName: oclib.GetConfig().MongoDatabase,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +46,6 @@ func (lm *LocalMonitor) PrepareMonitorExec() []string {
|
|||||||
"-d", lm.DBName,
|
"-d", lm.DBName,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if lm.Duration > 0 {
|
if lm.Duration > 0 {
|
||||||
args = append(args, "-t", fmt.Sprintf("%d", lm.Duration))
|
args = append(args, "-t", fmt.Sprintf("%d", lm.Duration))
|
||||||
}
|
}
|
||||||
@@ -54,7 +53,7 @@ func (lm *LocalMonitor) PrepareMonitorExec() []string {
|
|||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lm *LocalMonitor) LaunchMonitor(args []string, l zerolog.Logger) {
|
func (lm *LocalMonitor) LaunchMonitor(args []string, l zerolog.Logger) {
|
||||||
cmd := exec.Command(conf.GetConfig().MonitorPath, args...)
|
cmd := exec.Command(conf.GetConfig().MonitorPath, args...)
|
||||||
fmt.Printf("Command : %v\n", cmd)
|
fmt.Printf("Command : %v\n", cmd)
|
||||||
|
|
||||||
@@ -70,4 +69,3 @@ func (lm *LocalMonitor) LaunchMonitor(args []string, l zerolog.Logger) {
|
|||||||
|
|
||||||
logExecution(stdoutMonitord, l)
|
logExecution(stdoutMonitord, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -14,18 +14,6 @@ func main() {
|
|||||||
l := oclib.GetLogger()
|
l := oclib.GetLogger()
|
||||||
o := oclib.GetConfLoader()
|
o := oclib.GetConfLoader()
|
||||||
|
|
||||||
c := oclib.SetConfig(
|
|
||||||
o.GetStringDefault("MONGO_URL", "mongodb://127.0.0.1:27017"),
|
|
||||||
o.GetStringDefault("MONGO_DATABASE", "DC_myDC"),
|
|
||||||
o.GetStringDefault("NATS_URL", "nats://localhost:4222"),
|
|
||||||
o.GetStringDefault("LOKI_URL", ""),
|
|
||||||
o.GetStringDefault("LOG_LEVEL", "info"),
|
|
||||||
)
|
|
||||||
|
|
||||||
conf.GetConfig().DBName = c.MongoDatabase
|
|
||||||
conf.GetConfig().MongoUrl = c.MongoUrl
|
|
||||||
conf.GetConfig().NatsUrl = c.NATSUrl
|
|
||||||
conf.GetConfig().LokiUrl = c.LokiUrl
|
|
||||||
conf.GetConfig().Mode = o.GetStringDefault("MODE", "")
|
conf.GetConfig().Mode = o.GetStringDefault("MODE", "")
|
||||||
|
|
||||||
if conf.GetConfig().Mode == "container" {
|
if conf.GetConfig().Mode == "container" {
|
||||||
|
|||||||
Reference in New Issue
Block a user