Commit 65611dea authored by zauberstuhl's avatar zauberstuhl
Browse files

Use byteexec to restore/run ganggo binary from memory

parent 031a8867
......@@ -18,16 +18,15 @@ package main
//
import (
"io/ioutil"
"time"
"fmt"
"flag"
"github.com/jpillora/overseer"
"os"
"os/exec"
"github.com/revel/config"
"runtime"
"gopkg.in/AlecAivazis/survey.v1"
"github.com/getlantern/byteexec"
)
var (
......@@ -177,13 +176,22 @@ func main() {
interval = 1 * time.Hour
}
// Delete old files
// Restore assets and delete old files
os.RemoveAll("src")
// Extract assets and golang libraries
err := RestoreAssets("", "src")
if err != nil {
panic(err)
}
bin, err := ganggoBytes()
if err != nil {
panic(err)
}
// Restore ganggo binary file
bexec, err := byteexec.New(bin, fmt.Sprintf("ganggo.%s-%s.%s",
runtime.GOOS, runtime.GOARCH, version))
if err != nil {
panic(err)
}
// if app.conf is not in the current
// directory start the wizard and create it
......@@ -219,7 +227,33 @@ func main() {
}
overseer.Run(overseer.Config{
Program: prog,
Program: func(state overseer.State) {
// read config file from wizard and copy it to src folder
config, err := config.ReadDefault(userConfigPath)
if err != nil {
panic(err)
}
err = config.WriteFile(configPath, 0644, "AUTO-GENERATED BY GANGGO WIZARD")
if err != nil {
panic(err)
}
cmd := bexec.Command("-importPath", packageName,
"-srcPath", "src", "-runMode", mode)
if mode != "prod" {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
fmt.Printf("app#%s %s listening...\n", version, state.ID)
if err := cmd.Start(); err != nil {
panic(err)
}
// wait for overseer to restart or terminate
<-state.GracefulShutdown
fmt.Printf("app#%s %s exiting...\n", version, state.ID)
},
Fetcher: &Bintray{
User: "ganggo",
Repo: "ganggo",
......@@ -229,57 +263,3 @@ func main() {
Debug: true,
})
}
func prog(state overseer.State) {
buildID := int32(time.Now().Unix())
binFile := fmt.Sprintf("./ganggo.%s.%d", version, buildID)
if runtime.GOOS == "windows" {
binFile = fmt.Sprintf("ganggo.%s.%d.exe", version, buildID)
}
// clean-up binaries
defer func() {
os.Remove(binFile)
}()
// restore app binary
asset, err := ganggoBytes()
if err != nil {
panic(err)
}
err = ioutil.WriteFile(binFile, asset, 0755)
if err != nil {
panic(err)
}
// read config file from wizard and copy it to src folder
config, err := config.ReadDefault(userConfigPath)
if err != nil {
panic(err)
}
err = config.WriteFile(configPath, 0644, "AUTO-GENERATED BY GANGGO WIZARD")
if err != nil {
panic(err)
}
cmd := exec.Command(binFile, "-importPath", packageName,
"-srcPath", "src", "-runMode", mode)
if mode != "prod" {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
fmt.Printf("app#%s %s listening...\n", version, state.ID)
if err := cmd.Start(); err != nil {
panic(err)
}
// wait for overseer to restart or terminate
<-state.GracefulShutdown
// send kill signal to old process
cmd.Process.Kill()
fmt.Printf("app#%s %s exiting...\n", version, state.ID)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment