diff --git a/ChangeLog.md b/ChangeLog.md index 7ab16d94..30256e86 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,9 @@ # Change Log +## version v0.5.43 +* Add ability to set datacenter for Nomad clusters +* Fix permissions to use octet value when using data_with_permissions + ## version v0.5.41 * Add automatic push for build images to Kubernetes * Add automatic refresh for Kubernetes jobs diff --git a/examples/nomad/app_config/example2.nomad b/examples/nomad/app_config/example2.nomad index cb5de151..1e315fe6 100644 --- a/examples/nomad/app_config/example2.nomad +++ b/examples/nomad/app_config/example2.nomad @@ -1,28 +1,28 @@ job "example_2" { - datacenters = ["dc1"] - type = "service" + datacenters = ["{{datacenter}}"] + type = "service" update { - max_parallel = 1 - min_healthy_time = "10s" - healthy_deadline = "3m" + max_parallel = 1 + min_healthy_time = "10s" + healthy_deadline = "3m" progress_deadline = "10m" - auto_revert = false - canary = 0 + auto_revert = false + canary = 0 } - + migrate { - max_parallel = 1 - health_check = "checks" + max_parallel = 1 + health_check = "checks" min_healthy_time = "10s" healthy_deadline = "5m" } - + group "fake_service" { count = 1 network { - port "http" { + port "http" { to = 19090 # Dynamic port allocation } } @@ -31,10 +31,10 @@ job "example_2" { # The number of attempts to run the job within the specified interval. attempts = 2 interval = "30m" - delay = "15s" - mode = "fail" + delay = "15s" + mode = "fail" } - + ephemeral_disk { size = 30 } @@ -43,7 +43,7 @@ job "example_2" { # The "driver" parameter specifies the task driver that should be used to # run the task. driver = "docker" - + logs { max_files = 2 max_file_size = 10 @@ -51,7 +51,7 @@ job "example_2" { env { LISTEN_ADDR = ":19090" - NAME = "Example2" + NAME = "Example2" } config { diff --git a/examples/nomad/nomad.hcl b/examples/nomad/nomad.hcl index 4902b4da..466d167d 100644 --- a/examples/nomad/nomad.hcl +++ b/examples/nomad/nomad.hcl @@ -21,6 +21,8 @@ resource "nomad_cluster" "dev" { client_config = resource.template.nomad_config.destination consul_config = "./consul_config/agent.hcl" + datacenter = variable.datacenter + network { id = resource.network.cloud.id } @@ -35,10 +37,20 @@ resource "nomad_cluster" "dev" { } } +resource "template" "example_2" { + source = file("./app_config/example2.nomad") + + variables = { + datacenter = variable.datacenter + } + + destination = "${data("jobs")}/example2.nomad" +} + resource "nomad_job" "example_2" { cluster = resource.nomad_cluster.dev - paths = ["./app_config/example2.nomad"] + paths = [resource.template.example_2.destination] health_check { timeout = "60s" diff --git a/examples/nomad/test/nomad_cluster.feature b/examples/nomad/test/nomad_cluster.feature index dc6459e4..25c98e35 100644 --- a/examples/nomad/test/nomad_cluster.feature +++ b/examples/nomad/test/nomad_cluster.feature @@ -14,6 +14,20 @@ Feature: Nomad Cluster | resource.container.consul | And a HTTP call to "http://localhost:18500/v1/status/leader" should result in status 200 And a HTTP call to "http://localhost:19091" should result in status 200 + + @datacenter + Scenario: Nomad Cluster + Given the following jumppad variables are set + | key | value | + | datacenter | dc2 | + And I have a running blueprint + Then the following resources should be running + | name | + | resource.network.cloud | + | resource.nomad_cluster.dev | + | resource.container.consul | + And a HTTP call to "http://localhost:18500/v1/status/leader" should result in status 200 + And a HTTP call to "http://localhost:19091" should result in status 200 @multi-node Scenario: Nomad Multi-Node Cluster diff --git a/examples/nomad/variables.hcl b/examples/nomad/variables.hcl new file mode 100644 index 00000000..8ff7c983 --- /dev/null +++ b/examples/nomad/variables.hcl @@ -0,0 +1,7 @@ +variable "client_nodes" { + default = 0 +} + +variable "datacenter" { + default = "dc1" +} \ No newline at end of file diff --git a/pkg/config/resources/nomad/provider_cluster.go b/pkg/config/resources/nomad/provider_cluster.go index 2dca36d9..f77fef07 100644 --- a/pkg/config/resources/nomad/provider_cluster.go +++ b/pkg/config/resources/nomad/provider_cluster.go @@ -423,7 +423,7 @@ func (p *ClusterProvider) createServerNode(img ctypes.Image, volumeID string, is } // generate the server config - sc := dataDir + "\n" + fmt.Sprintf(serverConfig, cpu) + sc := dataDir + "\n" + fmt.Sprintf(serverConfig, p.config.Datacenter, cpu) // write the nomad config to a file os.MkdirAll(p.config.ConfigDir, os.ModePerm) @@ -540,7 +540,7 @@ func (p *ClusterProvider) createServerNode(img ctypes.Image, volumeID string, is // returns the fqdn, docker id, and an error if unsuccessful func (p *ClusterProvider) createClientNode(id string, image, volumeID, serverID string) (string, string, error) { // generate the client config - sc := dataDir + "\n" + fmt.Sprintf(clientConfig, serverID) + sc := dataDir + "\n" + fmt.Sprintf(clientConfig, p.config.Datacenter, serverID) // write the default config to a file clientConfigPath := path.Join(p.config.ConfigDir, "client_config.hcl") @@ -697,6 +697,7 @@ func (p *ClusterProvider) deployConnector() error { config := fmt.Sprintf( nomadConnectorDeployment, + p.config.Datacenter, p.config.ConnectorPort, p.config.ConnectorPort+1, string(cert), @@ -864,7 +865,7 @@ func randomID() string { var nomadConnectorDeployment = ` job "connector" { - datacenters = ["dc1"] + datacenters = ["%s"] type = "service" @@ -986,6 +987,8 @@ data_dir = "/var/lib/nomad" ` const serverConfig = ` +datacenter = "%s" + server { enabled = true bootstrap_expect = 1 @@ -1007,6 +1010,8 @@ plugin "raw_exec" { ` const clientConfig = ` +datacenter = "%s" + client { enabled = true diff --git a/pkg/config/resources/nomad/resource_cluster.go b/pkg/config/resources/nomad/resource_cluster.go index 2afb3c0e..69e74c59 100644 --- a/pkg/config/resources/nomad/resource_cluster.go +++ b/pkg/config/resources/nomad/resource_cluster.go @@ -27,6 +27,8 @@ type NomadCluster struct { 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"` @@ -76,6 +78,10 @@ func (n *NomadCluster) Process() error { 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 { diff --git a/pkg/config/zz_hclparser.go b/pkg/config/zz_hclparser.go index ea2a40c4..36b09043 100644 --- a/pkg/config/zz_hclparser.go +++ b/pkg/config/zz_hclparser.go @@ -1,7 +1,9 @@ package config import ( + "fmt" "os" + "strconv" "github.com/jumppad-labs/hclconfig" "github.com/jumppad-labs/hclconfig/types" @@ -70,7 +72,15 @@ func customHCLFuncDockerHost() (string, error) { } func customHCLFuncDataFolderWithPermissions(name string, permissions int) (string, error) { - perms := os.FileMode(permissions) + if permissions > 0 && permissions < 778 { + return "", fmt.Errorf("permissions must be a three digit number less than 777") + } + + // convert the permissions to an octal e.g. 777 to 0777 + strInt := fmt.Sprintf("0%d", permissions) + oInt, _ := strconv.ParseInt(strInt, 8, 64) + + perms := os.FileMode(oInt) return utils.GetDataFolder(name, perms), nil }