示例#1
0
func (c *FastPushPlugin) FastPush(cliConnection plugin.CliConnection, appName string, dryRun bool) {
	// Please check what GetApp returns here
	// https://github.com/cloudfoundry/cli/blob/master/plugin/models/get_app.go

	authToken := c.GetAppVersionId(cliConnection, appName)

	if dryRun {
		// NEED TO HANDLE DRY RUN
		c.ui.Warn("warning: No changes will be applied, this is a dry run !!")
	}

	apiEndpoint := c.GetApiEndpoint(cliConnection, appName)
	request := gorequest.New()
	_, body, err := request.Get(apiEndpoint+"/_fastpush/files").Set("x-auth-token", authToken).End()
	if err != nil {
		panic(err)
	}
	remoteFiles := map[string]*lib.FileEntry{}
	json.Unmarshal([]byte(body), &remoteFiles)

	localFiles := lib.ListFiles()

	filesToUpload := c.ComputeFilesToUpload(localFiles, remoteFiles)
	payload, _ := json.Marshal(filesToUpload)
	_, body, err = request.Put(apiEndpoint+"/_fastpush/files").Set("x-auth-token", authToken).Send(string(payload)).End()
	if err != nil {
		panic(err)
	}
	status := lib.Status{}
	json.Unmarshal([]byte(body), &status)
	c.ui.Say(status.Health)
}
示例#2
0
func main() {

	viper.SetConfigName("cf-fastpush-controller")
	viper.AddConfigPath("/etc/")
	viper.AddConfigPath("$HOME/.config/")
	viper.AddConfigPath(".")
	viper.ReadInConfig()
	viper.AutomaticEnv()

	viper.SetDefault(lib.CONFIG_BIND_ADDRESS, "0.0.0.0")
	viper.SetDefault(lib.CONFIG_PORT, "9000")
	viper.SetDefault(lib.CONFIG_BACKEND_DIRS, "./")
	viper.SetDefault(lib.CONFIG_RESTART_REGEX, "^*.py$")
	viper.SetDefault(lib.CONFIG_IGNORE_REGEX, "")
	viper.SetDefault(lib.CONFIG_BACKEND_COMMAND, "python -m http.server")
	viper.SetDefault(lib.CONFIG_BACKEND_PORT, "8080")
	viper.SetDefault(lib.CONFIG_BASE_PATH, "/_fastpush")

	appCmd = viper.GetString(lib.CONFIG_BACKEND_COMMAND)
	listenOn = viper.GetString(lib.CONFIG_BIND_ADDRESS) + ":" + viper.GetString(lib.CONFIG_PORT)
	backendOn = "127.0.0.1:" + viper.GetString(lib.CONFIG_BACKEND_PORT)
	basePath := viper.GetString(lib.CONFIG_BASE_PATH)
	localAuthToken := GetLocalToken()

	log.Println("Controller listening to: " + listenOn)

	http.HandleFunc(basePath+"/files", func(w http.ResponseWriter, r *http.Request) {
		SetJsonContentType(w)
		if !IsAuthenticated(r, localAuthToken) {
			http.Error(w, "Invalid authentication token", http.StatusUnauthorized)
			return
		}
		if r.Method == "GET" {
			ListFiles(w, r)
		} else if r.Method == "PUT" {
			UploadFiles(w, r)
		} else {
			http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed)
		}
	})
	http.HandleFunc(basePath+"/restart", func(w http.ResponseWriter, r *http.Request) {
		SetJsonContentType(w)
		if !IsAuthenticated(r, localAuthToken) {
			http.Error(w, "Invalid authentication token", http.StatusUnauthorized)
			return
		}
		if r.Method == "POST" {
			RestartApp(w, r)
		} else {
			http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed)
		}
	})
	http.HandleFunc(basePath+"/status", func(w http.ResponseWriter, r *http.Request) {
		SetJsonContentType(w)
		if !IsAuthenticated(r, localAuthToken) {
			http.Error(w, "Invalid authentication token", http.StatusUnauthorized)
			return
		}
		if r.Method == "GET" {
			GetStatus(w, r)
		} else {
			http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed)
		}
	})
	reverseProxy := httputil.NewSingleHostReverseProxy(&url.URL{
		Scheme: "http",
		Host:   backendOn,
	})
	http.HandleFunc("/", ReverseProxyHandler(reverseProxy))

	go lib.RestartApp(appCmd)
	go lib.ListFiles()
	http.ListenAndServe(listenOn, nil)
}
示例#3
0
func ListFiles(w http.ResponseWriter, r *http.Request) {
	files := lib.ListFiles()
	json.NewEncoder(w).Encode(files)
}