-
Notifications
You must be signed in to change notification settings - Fork 24
/
resource_cluster.go
131 lines (103 loc) · 4.75 KB
/
resource_cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package nomad
import (
"fmt"
"github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/config"
ctypes "github.com/jumppad-labs/jumppad/pkg/config/resources/container"
"github.com/jumppad-labs/jumppad/pkg/utils"
)
// TypeCluster is the resource string for a Cluster resource
const TypeNomadCluster string = "nomad_cluster"
// Cluster is a config stanza which defines a Kubernetes or a Nomad cluster
type NomadCluster struct {
// embedded type holding name, etc
types.ResourceMetadata `hcl:",remain"`
Networks ctypes.NetworkAttachments `hcl:"network,block" json:"networks,omitempty"` // Attach to the correct network // only when Image is specified
Image *ctypes.Image `hcl:"image,block" json:"images,omitempty"` // optional image to use for the cluster
ClientNodes int `hcl:"client_nodes,optional" json:"client_nodes,omitempty"`
Environment map[string]string `hcl:"environment,optional" json:"environment,omitempty"`
ServerConfig string `hcl:"server_config,optional" json:"server_config,omitempty"`
ClientConfig string `hcl:"client_config,optional" json:"client_config,omitempty"`
ConsulConfig string `hcl:"consul_config,optional" json:"consul_config,omitempty"`
Volumes ctypes.Volumes `hcl:"volume,block" json:"volumes,omitempty"` // volumes to attach to the cluster
OpenInBrowser bool `hcl:"open_in_browser,optional" json:"open_in_browser,omitempty"` // open the UI in the browser after creation
Datacenter string `hcl:"datacenter,optional" json:"datacenter"` // Nomad datacenter, defaults dc1
// Images that will be copied from the local docker cache to the cluster
CopyImages ctypes.Images `hcl:"copy_image,block" json:"copy_images,omitempty"`
// Additional ports to expose on the nomad sever node
Ports ctypes.Ports `hcl:"port,block" json:"ports,omitempty"` // ports to expose
PortRanges ctypes.PortRanges `hcl:"port_range,block" json:"port_ranges,omitempty"` // range of ports to expose
// Output Parameters
// The APIPort the server is running on
APIPort int `hcl:"api_port,optional" json:"api_port,omitempty"`
// The Port where the connector is running
ConnectorPort int `hcl:"connector_port,optional" json:"connector_port,omitempty"`
// The directory where the server and client config is written to
ConfigDir string `hcl:"config_dir,optional" json:"config_dir,omitempty"`
// The fully qualified docker address for the server
ServerContainerName string `hcl:"server_container_name,optional" json:"server_container_name,omitempty"`
// The fully qualified docker address for the client nodes
ClientContainerName []string `hcl:"client_container_name,optional" json:"client_container_name,omitempty"`
// ExternalIP is the ip address of the cluster, this generally resolves
// to the docker ip
ExternalIP string `hcl:"external_ip,optional" json:"external_ip,omitempty"`
}
const nomadBaseImage = "shipyardrun/nomad"
const nomadBaseVersion = "1.6.1"
func (n *NomadCluster) Process() error {
if n.Image == nil {
n.Image = &ctypes.Image{Name: fmt.Sprintf("%s:%s", nomadBaseImage, nomadBaseVersion)}
}
if n.ServerConfig != "" {
n.ServerConfig = utils.EnsureAbsolute(n.ServerConfig, n.File)
}
if n.ClientConfig != "" {
n.ClientConfig = utils.EnsureAbsolute(n.ClientConfig, n.File)
}
if n.ConsulConfig != "" {
n.ConsulConfig = utils.EnsureAbsolute(n.ConsulConfig, n.File)
}
if n.Datacenter == "" {
n.Datacenter = "dc1"
}
// Process volumes
// make sure mount paths are absolute
for i, v := range n.Volumes {
n.Volumes[i].Source = utils.EnsureAbsolute(v.Source, n.File)
}
// do we have an existing resource in the state?
// if so we need to set any computed resources for dependents
c, err := config.LoadState()
if err == nil {
// try and find the resource in the state
r, _ := c.FindResource(n.ID)
if r != nil {
state := r.(*NomadCluster)
n.ExternalIP = state.ExternalIP
n.ConfigDir = state.ConfigDir
n.ServerContainerName = state.ServerContainerName
n.ClientContainerName = state.ClientContainerName
n.APIPort = state.APIPort
n.ConnectorPort = state.ConnectorPort
// add the image ids from the state, this allows the tracking of
// pushed images so that they can be automatically updated
// add the image id from state
for x, img := range n.CopyImages {
for _, sImg := range state.CopyImages {
if img.Name == sImg.Name && img.Username == sImg.Username {
n.CopyImages[x].ID = sImg.ID
}
}
}
// the network name is set
for x, net := range state.Networks {
n.Networks[x] = net
}
}
}
// set the default port if not set
if n.APIPort == 0 {
n.APIPort = 4646
}
return nil
}