First example using a JSON blob as the configuration for a job

This commit is contained in:
Renan DelValle 2016-08-19 15:25:15 -07:00
parent f4ac7abe1b
commit 073faabc21
2 changed files with 136 additions and 0 deletions

21
examples/job.json Normal file
View file

@ -0,0 +1,21 @@
{
"name": "sample",
"cpu": 1.4,
"ram_mb": 128,
"disk_mb": 64,
"executor": "docker-compose-executor",
"service": false,
"ports": 1,
"instances": 1,
"uris": [
{
"uri": "https://github.com/mesos/docker-compose-executor/releases/download/0.1.0/sample-app.tar.gz",
"extract": true,
"cache": true
}
],
"labels":{
"fileName":"sample-app/docker-compose.yml"
}
}

115
examples/jsonClient.go Normal file
View file

@ -0,0 +1,115 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"github.com/rdelval/gorealis"
)
type URIJson struct {
URI string `json:"uri"`
Extract bool `json:"extract"`
Cache bool `json:"cache"`
}
type JobJson struct {
Name string `json:"name"`
CPU float64 `json:"cpu"`
RAM int64 `json:"ram_mb"`
Disk int64 `json:"disk_mb"`
Executor string `json:"executor"`
Instances int32 `json:"instances"`
URIs []URIJson`json:"uris"`
Labels map[string]string `json:"labels"`
Service bool `json:"service"`
Ports int `json:"ports"`
}
func (j *JobJson) Validate() bool {
if j.Name == "" {
return false
}
if j.CPU == 0.0 {
return false
}
if j.RAM == 0 {
return false
}
if j.Disk == 0 {
return false
}
return true
}
func main() {
jsonFile := flag.String("file", "", "JSON file containing job definition")
flag.Parse()
if *jsonFile == "" {
flag.Usage()
os.Exit(1)
}
file, err := os.Open(*jsonFile)
if err != nil {
fmt.Println("Error opening file ", err)
os.Exit(1)
}
jsonJob := new(JobJson)
err = json.NewDecoder(file).Decode(jsonJob)
if err != nil {
fmt.Println("Error parsing file ", err)
os.Exit(1)
}
jsonJob.Validate()
//Create new configuration with default transport layer
config, err := realis.NewDefaultConfig("http://192.168.33.7:8081")
if err != nil {
fmt.Print(err)
os.Exit(1)
}
realis.AddBasicAuth(&config, "aurora", "secret")
r := realis.NewClient(config)
auroraJob := realis.NewJob().
Environment("prod").
Role("vagrant").
Name(jsonJob.Name).
CPU(jsonJob.CPU).
RAM(jsonJob.RAM).
Disk(jsonJob.Disk).
ExecutorName(jsonJob.Executor).
InstanceCount(jsonJob.Instances).
IsService(jsonJob.Service).
AddPorts(jsonJob.Ports)
for _, uri := range jsonJob.URIs {
auroraJob.AddURIs(uri.Extract, uri.Cache, uri.URI)
}
for k, v := range jsonJob.Labels {
auroraJob.AddLabel(k, v)
}
resp, err := r.CreateJob(auroraJob)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(resp)
}