Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
f3e60fff71 | |||
f58a33b0d8 | |||
6ff9524210 | |||
c263b5890e | |||
5a69e2729d | |||
27dd2c353e | |||
cb43219908 | |||
d5a7c406f5 | |||
3f55152c29 |
15
README.md
15
README.md
@@ -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
|
||||
}
|
||||
|
@@ -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
51
ci/pipeline.yml
Normal 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
|
@@ -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
2
go.mod
@@ -1,3 +1,3 @@
|
||||
module github.com/cioplenu/concourse-nomad-resource
|
||||
module github.com/natto1784/concourse-nomad-resource
|
||||
|
||||
go 1.14
|
||||
|
@@ -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 {
|
||||
|
@@ -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 {
|
||||
|
75
out/main.go
75
out/main.go
@@ -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
|
||||
|
Reference in New Issue
Block a user