Skip to content

Commit

Permalink
Add slug to nomad and database, nomad now actually deploys correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkanwisher committed Nov 2, 2017
1 parent 70dc654 commit 19dd009
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/helpers/nomad_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

func main() {
controllers.SendNomadJob("a4200d60-8817-44a5-92a0-5fe6de44e563.zip", "slug")
controllers.SendNomadJob("a4200d60-8817-44a5-92a0-5fe6de44e563.zip", "blockssh")
}
55 changes: 41 additions & 14 deletions controllers/upload.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -48,6 +49,10 @@ func genObjectName(c *gin.Context) string {

//TODO set NOMAD_ADDR
func SendNomadJob(filename, slug string) error {
if slug == "" {
return errors.New("slug is blank won't send to nomad")
}

ncfg := api.DefaultConfig()
nomadClient, err := api.NewClient(ncfg)
if err != nil {
Expand All @@ -73,28 +78,28 @@ func SendNomadJob(filename, slug string) error {
Delay: helper.TimeToPtr(25 * time.Second),
Mode: helper.StringToPtr("delay"),
},
EphemeralDisk: &api.EphemeralDisk{
SizeMB: helper.IntToPtr(300),
},
// EphemeralDisk: &api.EphemeralDisk{
// SizeMB: helper.IntToPtr(300),
// },
Tasks: []*api.Task{
{
Name: name,
Driver: "docker",
Config: map[string]interface{}{
"image": "loomnetwork/rpc_gateway:f0d1db9", //TODO make this a config option
"image": "loomnetwork/rpc_gateway:70dc654", //TODO make this a config option
"port_map": []map[string]int{{
"web": 8080,
"web": 8081,
}},
},
Env: map[string]string{
"SPAWN_NETWORK": "node /src/build/cli.node.js",
"APP_ZIP_FILE": fmt.Sprintf("do://%s", filename),
"APP_ZIP_FILE": fmt.Sprintf("do://uploads/%s", filename),
"DEMO_MODE": "false",
"PRIVATE_KEY_JSON_PATH": "data.json",
},
Resources: &api.Resources{
CPU: helper.IntToPtr(500),
MemoryMB: helper.IntToPtr(256),
MemoryMB: helper.IntToPtr(500),
Networks: []*api.NetworkResource{
{
MBits: helper.IntToPtr(10),
Expand Down Expand Up @@ -136,38 +141,60 @@ func SendNomadJob(filename, slug string) error {

func UploadApplication(c *gin.Context) {
r := c.Request
w := c.Writer
cfg := config.Default(c)
db := dbpkg.DBInstance(c)

slugId := models.NormalizeSlug(c.PostForm("application_slug"))
autoCreate := c.PostForm("auto_create")

app := models.Application{}
if err := db.Where("slug = ?", slugId).Find(&app).Error; err != nil {
if autoCreate == "true" {
log.WithField("slug", slugId).Warn("Creating new application on upload")
application := models.Application{LastDeployed: time.Now(), Name: slugId, Slug: slugId}

if err := db.Create(&application).Error; err != nil {
log.WithField("error", err).Warn("Failed creating application in db")

c.JSON(http.StatusBadRequest, gin.H{"Error": "failed creating application"})
return
}
} else {
log.WithField("error", err).Warn("Failed retrieving application slug from db")

c.JSON(http.StatusBadRequest, gin.H{"Error": "duplicate application and/or error"})
return
}
}
fmt.Printf("----%v----%v\n", app.Slug, app)

r.ParseMultipartForm(32 << 20)
file, handler, err := r.FormFile("uploadfile")
if err != nil {
fmt.Println(err)
log.WithField("error", err).Warn("Failed retrieving zipfile from form")

c.JSON(http.StatusBadRequest, gin.H{"Error": "unable able to parse the upload"})
return
}
defer file.Close()
fmt.Fprintf(w, "%v", handler.Header)

uniqueFilename := genObjectName(c)
err = uploadS3CompatibleFile(cfg, uniqueFilename, file)
if err != nil {
fmt.Println(err)
log.WithField("error", err).Warn("upload to s3 failed")

c.JSON(http.StatusBadRequest, gin.H{"Error": "storage of data failed"})
return
}

err = SendNomadJob(uniqueFilename, "slug") //TODO get slug from database
err = SendNomadJob(uniqueFilename, app.Slug) //TODO get slug from database
if err != nil {
fmt.Println(err) //TODO log
log.WithField("error", err).Warn("sendnomadjob failed")

c.JSON(http.StatusBadRequest, gin.H{"Error": "Could not create test network"})
return
}
// create new version
db := dbpkg.DBInstance(c)
deployHistory := models.DeployHistory{
BundleName: handler.Filename, //uploaded name
UniqueFileName: uniqueFilename,
Expand Down
10 changes: 10 additions & 0 deletions models/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package models

import (
"strings"
"time"

"github.com/jinzhu/gorm"
Expand All @@ -16,3 +17,12 @@ type Application struct {
LastDeployed time.Time `json:"lastdeployed" form:"lastdeployed"`
DefaultChain string `json:"default_chain" form:"default_chain" gorm:"default:'loom'"`
}

func NormalizeSlug(slug string) string {
return strings.Replace(slug, "_", "-", -1)
}
func (a *Application) BeforeSave() error {
a.Slug = NormalizeSlug(a.Slug) //No dashes in nomad name
//TODO validate other failures in the name
return nil
}
14 changes: 10 additions & 4 deletions nomad_rpc_proxy.nomad
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

job "example" {
datacenters = ["dc1"]

type = "service"

update {
Expand Down Expand Up @@ -31,19 +33,23 @@ job "example" {
}

task "rpc_gateway" {
driver = "docker"
config {
image = "loomnetwork/rpc_gateway:8ad8cae"
image = "loomnetwork/rpc_gateway:70dc654"
port_map {
rpc = 8080
rpc = 8081
}
}
env {
SPAWN_NETWORK = "node /src/build/cli.node.js --acctKeys data.json"
"SPAWN_NETWORK" = "node /src/build/cli.node.js"
"APP_ZIP_FILE" = "do://uploads/a4200d60-8817-44a5-92a0-5fe6de44e563.zip"
"DEMO_MODE" = "false"
"PRIVATE_KEY_JSON_PATH" = "data.json"
}

resources {
cpu = 500 # 500 MHz
memory = 256 # 256MB
memory = 500 # 256MB
network {
mbits = 10
port "rpc" {}
Expand Down
1 change: 1 addition & 0 deletions templates/dashboard/_deployment.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="row">
<div class="col-md-6 overview-sidebar">
<form action="/upload" class="dropzone" id="DApp-uploader">
<input type="hidden" id="application_slug" value="{{.app.Slug}}">
</form>
</div>
<div class="col-md-6 content">
Expand Down

0 comments on commit 19dd009

Please sign in to comment.