Esempio n. 1
0
func BuildExecutable(root string) error {
	tempDir := root
	log.Println("[build] building into " + tempDir)

	buildDir := root
	log.Println("[build] building from " + buildDir)

	// build binary
	registry, err := MakeRegistry(buildDir)
	if err != nil {
		return err
	}

	err = WriteRegistry(registry, buildDir, BASE_URL)
	if err != nil {
		return err
	}

	// invoke go build to make binary
	cmdArgs := []string{"build", filepath.Join(BASE_URL, MAIN_EXE)}

	log.Println("[builder] will run go " + strings.Join(cmdArgs, " "))
	cmd := exec.Command("go", cmdArgs...)
	cmdEnv := make([]string, len(os.Environ()))
	copy(cmdEnv, os.Environ())

	// find GOPATH and replace
	var gopathFound bool

	for idx, vr := range cmdEnv {
		if strings.HasPrefix(vr, "GOPATH=") {
			cmdEnv[idx] = "GOPATH=" + tempDir + ":" + vr[7:]
			gopathFound = true
			break
		}
	}
	if !gopathFound {
		cmdEnv = append(cmdEnv, "GOPATH="+tempDir)
	}

	cmd.Env = cmdEnv

	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout

	err = cmd.Start()
	if err != nil {
		return err
	}

	err = cmd.Wait()
	if err != nil {
		return err
	}

	sourceBinary := os.Args[0]

	seedName := filepath.Base(MAIN_EXE) + "." + "seed"
	seedPath := filepath.Join(tempDir, seedName)

	seedFile, err := os.Create(seedPath)
	if err != nil {
		return err
	}
	os.Chmod(seedPath, 0755)

	sourceFile, err := os.Open(sourceBinary)
	if err != nil {
		return err
	}

	io.Copy(seedFile, sourceFile)

	emfs, err := fs.OpenEmbedFs(seedFile)
	if err != nil {
		return err
	}

	err = emfs.EmbedDirectory(filepath.Join(root, ".git"))
	if err != nil {
		return err
	}

	emfs.Close()

	// move to RELEASE_DIR
	if _, err := os.Stat(RELEASE_DIR); os.IsNotExist(err) {
		os.Mkdir(RELEASE_DIR, 0777)
	}

	os.Rename(seedPath, filepath.Join(RELEASE_DIR, seedName))
	return nil
}
Esempio n. 2
0
func main() {
	args, _ := docopt.Parse(USAGE, nil, true, VERSION, false)

	if args["build"].(bool) {
		err := buildGyard()
		if err != nil {
			fmt.Println("error while building binary: " + err.Error())
		}

		return
	}

	if args["extract"].(bool) {
		// just testing
		self, err := os.Open(os.Args[0])
		if err != nil {
			panic(err)
		}
		emfs, err := fs.OpenEmbedFs(self)
		if err != nil {
			panic(err)
		}
		repo, err := git.OpenFsRepo(emfs)
		if err != nil {
			panic(err)
		}
		head, err := repo.ReadSymbolicRef("HEAD")
		if err != nil {
			panic(err)
		}
		fmt.Println("HEAD is", head)
		return
	}

	if args["rape"].(bool) {
		fmt.Println("action: rape")
		fmt.Println(args)

		fmt.Println("RUNNING v." + args["<version>"].(string))

		nodesList := readNodesCache(args["<nodescache>"].(string))
		hostname, _ := os.Hostname()
		ver, _ := strconv.Atoi(args["<version>"].(string))
		gossipPort, _ := strconv.Atoi(args["--gossip-port"].(string))
		webPort, _ := strconv.Atoi(args["--web-port"].(string))

		api := httpapi.Start(webPort)

		api.UploadImage(ver, os.Args[0])

		conf := gossip.Config{
			RootNodes:    nodesList,
			LocalPort:    gossipPort,
			LocalVersion: int64(ver),
			Name:         fmt.Sprintf("%s:%d", hostname, gossipPort),
		}

		//if !args["-p"].(bool) {
		//    yard := getYard(args["-c"].(string))
		//    yardMap := map[string]string{
		//        "Hostname": yard.Hostname,
		//        "Port": yard.Port,
		//    }

		//    for _, grape := range yard.Runlist {
		//        // @TODO config
		//        registry.Registry[grape]().Ensure(yardMap)
		//    }

		//    return
		//}

		net := gossip.NewGossipNetwork(conf, &gossip.ImmediateExecutor{args})
		net.SendUpdateMsg(int64(ver), api.GetImageURI(), args["--extract-repo"].(int64))

		for {
			for _, m := range net.GetMembers() {
				fmt.Printf("[node] %s\n", m.Name)
			}

			if args["-x"].(bool) {
				return
			}

			time.Sleep(5 * time.Second)
		}
	}

	return
}