Example #1
0
// deleteTemplate
func deleteTemplate(rw http.ResponseWriter, req *http.Request) {

	// create a template and unregister it (remove it from the database)
	t := models.Template{ID: req.URL.Query().Get(":id")}
	if err := t.Unregister(); err != nil {
		fmt.Println("BONK!!!", err.Error())
	}

	fmt.Println("Unregistered template", t.ID)
}
Example #2
0
// postTemplate
func postTemplate(rw http.ResponseWriter, req *http.Request) {

	dump, err := httputil.DumpRequestOut(req, true)
	if err != nil {
		fmt.Println("BOINK!", err.Error())
	}

	fmt.Println(`
Request:
--------------------------------------------------------------------------------
` + string(dump))

	// read the request body
	b, err := ioutil.ReadAll(req.Body)
	if err != nil {
		fmt.Println("READ ALL ERR!", err.Error())
		return
	}
	defer req.Body.Close()

	fmt.Println("BODY?", string(b))

	// create a new template
	t := models.Template{}
	if err := json.Unmarshal(b, &t); err != nil {
		fmt.Println("MARSHALL ERR!", err.Error())
		return
	}

	fmt.Printf("TEMPLATE?? %#v\n", t)

	// register the template (write it to the database)
	id, err := t.Register()
	if err != nil {
		fmt.Println("BONK!", err.Error())
		return
	}

	fmt.Println("Registered template", id)
}
Example #3
0
func postBuild(rw http.ResponseWriter, req *http.Request) {

	dump, err := httputil.DumpRequestOut(req, true)
	if err != nil {
		fmt.Println("BOINK!", err.Error())
	}

	fmt.Println(`
Request:
--------------------------------------------------------------------------------
` + string(dump))

	// read the request body
	b, err := ioutil.ReadAll(req.Body)
	if err != nil {
		fmt.Println("READ ALL ERR!", err.Error())
		return
	}
	defer req.Body.Close()

	// create a new build
	build := models.Build{}
	if err := json.Unmarshal(b, &build); err != nil {
		fmt.Println("MARSHALL ERR!", err.Error())
		return
	}

	fmt.Printf("BUILD! %#v\n", build)

	// Create a build in the database
	db, err := sql.Open("postgres", config.Options.DBConn)
	if err != nil {
		fmt.Println("BONK!", err)
	}

	// 1. Create entry in DB that represents the build
	if err := db.QueryRow(`INSERT INTO builds(created_at, meta_data, template_id, transform_payload, updated_at) VALUES($1, $2, $3, $4, $5) RETURNING build_id;`, time.Now(), build.MetaData, build.TemplateID, build.TransformPayload, time.Now()).Scan(&build.ID); err != nil {
		fmt.Println("BINK!", err)
	}

	// once the build is done send on this channel to close anything
	done := make(chan bool)

	// 1.5 At a short interval, update last_updated field (goroutine) because packer run might take time
	go func() {
		for {
			select {

			//
			case <-time.After(time.Second * 1):
				if err := db.QueryRow(`UPDATE builds SET updated_at=$1 WHERE build_id=$2;`, time.Now(), build.ID); err != nil {
					fmt.Println("BINK! %#v\n", err)
				}

			//
			case <-done:
				fmt.Println("DONE!!")
				return
			}
		}
	}()

	// fetch the template; we fetch the template because we MIGHT need its `download`, but we WILL needs its `transform_script`
	template := models.Template{}
	if err := db.QueryRow(`SELECT * FROM templates WHERE template_id=$1`, build.TemplateID).Scan(&template.CreatedAt, &template.Download, &template.ID, &template.TransformScript); err != nil {
		fmt.Println("ERR!", err.Error())
	}

	fmt.Printf("TEMPLATE! %#v\n", template)

	// 2. Install template on local node if not already available ([/var/db/sherpa]/templates/:id)
	templateDir := filepath.Join(config.Options.DataDir, "templates", template.ID)
	if _, err := os.Stat(templateDir); err != nil {

		//
		template.Fetch()
	}

	// 3. Copy template into a tmp build folder for duration of build ([/var/db/sherpa]/builds/:build_id)
	buildDir := filepath.Join(config.Options.DataDir, "builds", build.ID)
	if err := os.Mkdir(buildDir, 0755); err != nil {
		fmt.Println("BOONK!", err.Error())
	}

	fmt.Println("THINGS", templateDir, buildDir)

	//
	done <- true

	return

	// 4. Run transform script (builds/:build_id/{{transform_script}}) if present, passing transform_payload
	if template.TransformScript != "" {
		out, err := exec.Command(template.TransformScript, build.TransformPayload).CombinedOutput()
		if err != nil {
			fmt.Println("BUNK!", string(out))
		}
	}

	// 5. Packer run (all output (\n) is a new build_log for that build); update last_updated/state of build throughout process

	// BuildLog struct {
	// 	BuildID   string    `json:"build_id"`     //
	// 	CreatedAt time.Time `json:"created_at"`   //
	// 	ID        string    `json:"build_log_id"` //
	// 	Message   string    `json:"message"`      //
	// }

	// 6. Record - take packer info and dump it into meta_data

	// 7. Cleanup - delete build folder
	if err := os.Remove(buildDir); err != nil {
		fmt.Println("BOONK!", err.Error())
	}
}