2023-04-17 09:25:26 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
|
|
|
nomad "github.com/hashicorp/nomad/api"
|
2023-05-03 08:52:34 +00:00
|
|
|
"log"
|
2023-04-17 09:25:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Cluster struct {
|
2023-05-03 08:52:34 +00:00
|
|
|
Nomad *nomad.Client
|
|
|
|
Consul *consul.Client
|
2023-04-17 09:25:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 08:52:34 +00:00
|
|
|
func NewCluster(conf *Config) Cluster {
|
2023-04-17 09:25:26 +00:00
|
|
|
cluster := Cluster{}
|
|
|
|
|
|
|
|
// Init Nomad
|
|
|
|
nomadConfig := nomad.DefaultConfig()
|
|
|
|
nomadConfig.Namespace = "ci"
|
|
|
|
nomadConfig.Address = conf.NomadAddr
|
|
|
|
nomadConfig.TLSConfig.CACert = conf.NomadCACert
|
|
|
|
nomadConfig.TLSConfig.ClientCert = conf.NomadClientCert
|
|
|
|
nomadConfig.TLSConfig.ClientKey = conf.NomadClientKey
|
|
|
|
|
|
|
|
ncli, err := nomad.NewClient(nomadConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to connect to Nomad, check your config and setup")
|
|
|
|
}
|
|
|
|
cluster.Nomad = ncli
|
|
|
|
|
|
|
|
// Init Consul
|
|
|
|
consulConfig := consul.DefaultConfig()
|
|
|
|
consulConfig.Address = conf.ConsulAddr
|
|
|
|
consulConfig.TLSConfig.CAFile = conf.ConsulCACert
|
|
|
|
consulConfig.TLSConfig.CertFile = conf.ConsulClientCert
|
|
|
|
consulConfig.TLSConfig.KeyFile = conf.ConsulClientKey
|
|
|
|
|
|
|
|
ccli, err := consul.NewClient(consulConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to connect to Consul, check your config and setup")
|
|
|
|
}
|
|
|
|
cluster.Consul = ccli
|
|
|
|
|
|
|
|
return cluster
|
|
|
|
}
|