Skip to content

Commit

Permalink
Merge pull request #235 from jumppad-labs/b-nomad-dc
Browse files Browse the repository at this point in the history
Add datacenter attribute for nomad, fix data_with_permissions
  • Loading branch information
nicholasjackson authored Aug 25, 2023
2 parents c25e0ff + 62bbafa commit 3570895
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 22 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 17 additions & 17 deletions examples/nomad/app_config/example2.nomad
Original file line number Diff line number Diff line change
@@ -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
}
}
Expand All @@ -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
}
Expand All @@ -43,15 +43,15 @@ 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
}

env {
LISTEN_ADDR = ":19090"
NAME = "Example2"
NAME = "Example2"
}

config {
Expand Down
14 changes: 13 additions & 1 deletion examples/nomad/nomad.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions examples/nomad/test/nomad_cluster.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions examples/nomad/variables.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "client_nodes" {
default = 0
}

variable "datacenter" {
default = "dc1"
}
11 changes: 8 additions & 3 deletions pkg/config/resources/nomad/provider_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -864,7 +865,7 @@ func randomID() string {

var nomadConnectorDeployment = `
job "connector" {
datacenters = ["dc1"]
datacenters = ["%s"]
type = "service"
Expand Down Expand Up @@ -986,6 +987,8 @@ data_dir = "/var/lib/nomad"
`

const serverConfig = `
datacenter = "%s"
server {
enabled = true
bootstrap_expect = 1
Expand All @@ -1007,6 +1010,8 @@ plugin "raw_exec" {
`

const clientConfig = `
datacenter = "%s"
client {
enabled = true
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/resources/nomad/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion pkg/config/zz_hclparser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"fmt"
"os"
"strconv"

"github.com/jumppad-labs/hclconfig"
"github.com/jumppad-labs/hclconfig/types"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 3570895

Please sign in to comment.