2018-06-28 17:29:26 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
capi "github.com/hashicorp/consul/api"
|
|
|
|
napi "github.com/hashicorp/nomad/api"
|
|
|
|
vapi "github.com/hashicorp/vault/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProvisionerOptions struct {
|
|
|
|
Name string
|
|
|
|
Servers int
|
|
|
|
Clients int
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClusterInfo struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Servers []string
|
|
|
|
Clients []string
|
|
|
|
NomadClient *napi.Client
|
|
|
|
ConsulClient *capi.Client
|
|
|
|
VaultClient *vapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner interface {
|
|
|
|
ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error)
|
|
|
|
DestroyCluster(clusterID string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultProvisioner Provisioner = new(singleClusterProvisioner)
|
|
|
|
|
|
|
|
type singleClusterProvisioner struct{}
|
|
|
|
|
|
|
|
func (p *singleClusterProvisioner) ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error) {
|
|
|
|
h := md5.New()
|
|
|
|
h.Write([]byte(opts.Name))
|
|
|
|
info := &ClusterInfo{
|
|
|
|
ID: hex.EncodeToString(h.Sum(nil))[:8],
|
|
|
|
Name: opts.Name,
|
|
|
|
}
|
2018-06-29 02:58:57 +00:00
|
|
|
|
|
|
|
if len(os.Getenv("NOMAD_ADDR")) == 0 {
|
2018-06-28 17:29:26 +00:00
|
|
|
return nil, fmt.Errorf("environment variable NOMAD_ADDR not set")
|
|
|
|
}
|
|
|
|
|
2018-06-29 02:58:57 +00:00
|
|
|
nomadClient, err := napi.NewClient(napi.DefaultConfig())
|
2018-06-28 17:29:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info.NomadClient = nomadClient
|
|
|
|
|
2018-06-29 02:58:57 +00:00
|
|
|
if len(os.Getenv(capi.HTTPAddrEnvName)) != 0 {
|
|
|
|
consulClient, err := capi.NewClient(capi.DefaultConfig())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info.ConsulClient = consulClient
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(os.Getenv(vapi.EnvVaultAddress)) != 0 {
|
|
|
|
vaultClient, err := vapi.NewClient(vapi.DefaultConfig())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info.VaultClient = vaultClient
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:29:26 +00:00
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *singleClusterProvisioner) DestroyCluster(_ string) error {
|
|
|
|
//Maybe try to GC things based on id?
|
|
|
|
return nil
|
|
|
|
}
|