Compare commits

9 Commits

Author SHA1 Message Date
f3e60fff71 check for nil maps
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-05-08 23:37:08 +05:30
f58a33b0d8 change template delimiters
changed delimiters from "{{", "}}" to "{{+", "+}}", to avoid parsing the vault templates

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-05-08 22:31:09 +05:30
6ff9524210 update readme
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-03-29 08:21:05 +05:30
c263b5890e move Restart and Templating to config options
and run gofmt

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-03-29 08:17:35 +05:30
5a69e2729d move pipeline files to their own repo 2022-02-14 13:13:21 +05:30
27dd2c353e i know not a single thing about golang
also i hate tabs
2022-02-13 17:20:00 +05:30
cb43219908 added option to turn off templating 2022-02-06 23:48:18 +05:30
d5a7c406f5 added consul and vault tokens 2022-02-06 15:10:15 +05:30
3f55152c29 change resource to forked repo 2022-02-05 21:05:15 +05:30
8 changed files with 124 additions and 37 deletions

View File

@@ -8,7 +8,7 @@ resource_types:
- name: nomad
type: registry-image
source:
repository: cioplenu/concourse-nomad-resource
repository: natto1784/concourse-nomad-resource
tag: latest
resources:
@@ -43,11 +43,14 @@ like a version on the fly.
#### Parameters
* `job_path`: (required) Path of the HCL job file to run
* `vars`: { [key: string]: string } dictionary of variables to substitute in the job file. Each key
should be represented in the job file as `{{.key}}`
should be represented in the job file as `{{+.key+}}`
* `var_files`: { [key: string]: string } dictionary of paths to files to read to get variable
values. Each key should be represented in the job file as `{{.key}}` and the values should be path
values. Each key should be represented in the job file as `{{+.key+}}` and the values should be path
to text files which content will be used as the variable value. Whitespace and trailing newlines
will be trimmed from the value.
* `templating`: { bool }: Whether to use templating or not. `true` by default
* `restart`: { bool }: Whether to restart a job or not. `false` by default
> **NOTE:** Restart just runs `stop -purge` against the job before running `run` against it
## Example
@@ -56,7 +59,7 @@ resource_types:
- name: nomad
type: registry-image
source:
repository: cioplenu/concourse-nomad-resource
repository: natto1784/concourse-nomad-resource
tag: latest
resources:
@@ -100,10 +103,10 @@ job "sample" {
driver = "docker"
config {
image = "cioplenu/sample:{{.version}}"
image = "cioplenu/sample:{{+.version+}}"
auth = {
username = "some-username"
password = "{{.registry_token}}"
password = "{{+.registry_token+}}"
}
force_pull = true
}

View File

@@ -5,8 +5,8 @@ import (
"os"
"sort"
resource "github.com/cioplenu/concourse-nomad-resource"
"github.com/cioplenu/concourse-nomad-resource/common"
resource "github.com/natto1784/concourse-nomad-resource"
"github.com/natto1784/concourse-nomad-resource/common"
)
type Request struct {

51
ci/pipeline.yml Normal file
View File

@@ -0,0 +1,51 @@
resources:
- name: resource-repo
type: git
icon: git
source:
uri: https://github.com/natto1784/concourse-nomad-resource.git
branch: master
- name: target-image
type: registry-image
icon: docker
source:
repository: ((docker.user))/concourse-nomad-resource
tag: latest
username: ((docker.user))
password: ((docker.pass))
jobs:
- name: configure-self
public: true
plan:
- get: resource-repo
trigger: true
- set_pipeline: self
file: resource-repo/ci/pipeline.yml
- name: build-and-push
plan:
- get: resource-repo
trigger: true
passed: [configure-self]
- task: build-image
privileged: true
config:
caches:
- path: cache
platform: linux
image_resource:
type: registry-image
source:
repository: rdclda/concourse-oci-build-task
inputs:
- name: resource-repo
outputs:
- name: image
params:
CONTEXT: resource-repo
run:
path: build
- put: target-image
params:
image: image/image.tar

View File

@@ -7,7 +7,7 @@ import (
"os"
"os/exec"
resource "github.com/cioplenu/concourse-nomad-resource"
resource "github.com/natto1784/concourse-nomad-resource"
)
func Check(err error, msg string) {

2
go.mod
View File

@@ -1,3 +1,3 @@
module github.com/cioplenu/concourse-nomad-resource
module github.com/natto1784/concourse-nomad-resource
go 1.14

View File

@@ -4,8 +4,8 @@ import (
"encoding/json"
"os"
resource "github.com/cioplenu/concourse-nomad-resource"
"github.com/cioplenu/concourse-nomad-resource/common"
resource "github.com/natto1784/concourse-nomad-resource"
"github.com/natto1784/concourse-nomad-resource/common"
)
type Request struct {

View File

@@ -1,9 +1,11 @@
package resource
type Source struct {
URL string `json:"url"`
Name string `json:"name"`
Token string `json:"token"`
URL string `json:"url"`
Name string `json:"name"`
Token string `json:"token"`
ConsulToken string `json:"consul_token"`
VaultToken string `json:"vault_token"`
}
type JobVersion struct {

View File

@@ -11,14 +11,16 @@ import (
"strings"
"text/template"
resource "github.com/cioplenu/concourse-nomad-resource"
"github.com/cioplenu/concourse-nomad-resource/common"
resource "github.com/natto1784/concourse-nomad-resource"
"github.com/natto1784/concourse-nomad-resource/common"
)
type Params struct {
JobPath string `json:"job_path"`
Vars map[string]string `json:"vars"`
VarFiles map[string]string `json:"var_files"`
JobPath string `json:"job_path"`
Vars map[string]string `json:"vars"`
VarFiles map[string]string `json:"var_files"`
Templating bool `json:"templating"`
Restart bool `json:"restart"`
}
type OutConfig struct {
@@ -45,26 +47,53 @@ func main() {
templPath := filepath.Join(sourceDir, config.Params.JobPath)
templFile, err := ioutil.ReadFile(templPath)
common.Check(err, "Could not read input file "+templPath)
tmpl, err := template.New("job").Parse(string(templFile))
common.Check(err, "Error parsing template")
if config.Params.Templating != false {
tmpl, err := template.New("job").Delims("{{+", "+}}").Parse(string(templFile))
common.Check(err, "Error parsing template")
for name, path := range config.Params.VarFiles {
varPath := filepath.Join(sourceDir, path)
varFile, err := ioutil.ReadFile(varPath)
common.Check(err, "Error reading var file")
config.Params.Vars[name] = strings.TrimSpace(string(varFile))
if config.Params.Vars == nil {
config.Params.Vars = map[string]string{}
}
if config.Params.VarFiles == nil {
config.Params.VarFiles = map[string]string{}
}
for name, path := range config.Params.VarFiles {
varPath := filepath.Join(sourceDir, path)
varFile, err := ioutil.ReadFile(varPath)
common.Check(err, "Error reading var file")
config.Params.Vars[name] = strings.TrimSpace(string(varFile))
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, config.Params.Vars)
common.Check(err, "Error executing template")
outFile, err := os.Create(templPath)
common.Check(err, "Error creating output file")
defer outFile.Close()
_, err = outFile.Write(buf.Bytes())
common.Check(err, "Error writing output file")
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, config.Params.Vars)
common.Check(err, "Error executing template")
outFile, err := os.Create(templPath)
common.Check(err, "Error creating output file")
defer outFile.Close()
_, err = outFile.Write(buf.Bytes())
common.Check(err, "Error writing output file")
if config.Params.Restart != false {
cmd := exec.Command(
"nomad",
"job",
"stop",
"-purge",
"-address="+config.Source.URL,
"-token="+config.Source.Token,
config.Source.Name,
)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
_ = cmd.Run()
//Ignore even if the job fails to purge
}
cmd := exec.Command(
"nomad",
@@ -72,6 +101,8 @@ func main() {
"run",
"-address="+config.Source.URL,
"-token="+config.Source.Token,
"-consul-token="+config.Source.ConsulToken,
"-vault-token="+config.Source.VaultToken,
templPath,
)
var out bytes.Buffer