Esempio n. 1
0
func Errplane_main(defaults econfig.Consts) {
	fmt.Printf("ERRPlane Local Agent starting, Version %s \n", defaults.Current_build)

	goopt.Description = func() string {
		return "ERRPlane Local Agent."
	}
	goopt.Version = defaults.Current_build
	goopt.Summary = "ErrPlane Log and System Monitor"
	goopt.Parse(nil)

	var fconfig_file string
	fconfig_file = *config_file

	fmt.Printf("Loading config file %s.\n", fconfig_file)

	c, err := config.ReadDefault(fconfig_file)
	if err != nil {
		log.Fatal("Can not find the Errplane Config file, please install it in /etc/ranger/ranger.conf.")
	}

	api_key, _ := c.String("DEFAULT", "api_key")

	if len(*install_api_key) > 1 {
		fmt.Printf("Saving new Config!\n")
		c.AddOption("DEFAULT", "api_key", *install_api_key)
		c.WriteFile(fconfig_file, 0644, "")

		c, err := config.ReadDefault(fconfig_file)
		if err != nil {
			log.Fatal("Can not find the Errplane Config file, please install it in /etc/ranger/ranger.conf.")
		}
		api_key, _ = c.String("DEFAULT", "api_key")
	}

	if len(api_key) < 1 {
		log.Fatal("No api key found. Please rerun this with --install-api-key <api_key_here> ")
		os.Exit(1)
	}

	if !*amForeground {
		//Daemonizing requires root
		if os.Getuid() == 0 {
			daemonize.Do_fork(os.Args[0], fconfig_file)
			l4g.Fine("Exiting parent process-%s\n", os.Args[0])
			os.Exit(0)
		} else {
			fmt.Printf("Daemoning requires root \n")
			os.Exit(1)
		}
	}

	setup_logger()

	api_url, _ := c.String("DEFAULT", "api_host")
	config_url, _ := c.String("DEFAULT", "config_host")
	output_dir, _ := c.String("DEFAULT", "agent_path")
	if len(output_dir) < 1 {
		output_dir = "/usr/local/ranger/"
	}
	agent_bin, _ := c.String("DEFAULT", "agent_bin")
	if len(agent_bin) < 1 {
		agent_bin = "/usr/local/bin/ranger-local-agent"
	}
	pid_location, _ := c.String("DEFAULT", "pid_file")
	if len(pid_location) < 1 {
		pid_location = "/var/run/ranger/ranger.pid"
	}
	auto_update, _ := c.String("DEFAULT", "auto_upgrade")

	write_pid(pid_location)

	config_data := econfig.ParseJsonFromHttp(config_url, api_key)

	l4g.Debug("Expected agent version-%s\n", config_data.Version)

	if auto_update == "true" && config_data.Version != defaults.Current_build {
		econfig.Upgrade_version(config_data.Version, config_data.Sha256, output_dir, agent_bin, defaults)
		os.Exit(1)
	} else {
		l4g.Debug("Don't need to upgrade versions\n")
	}

	_, err = exec.LookPath("tail")
	if err != nil {
		log.Fatal("installing tail is in your future")
		//        exit(1)
	}

	configChan := make(chan *econfig.AgentConfigType)

	go theBrain(configChan, api_key, api_url)

	go econfig.CheckForUpdatedConfigs(auto_update, config_url, api_key, output_dir, agent_bin, configChan, defaults)

	if err != nil {
		log.Fatal(err)
	}

	err = nil
	for err == nil {
		//TODO monitor go routines, if one exists reload it
		time.Sleep(0)
		runtime.Gosched()
	}
}
Esempio n. 2
0
func Upgrade_version(new_version string, valid_hash string, out_dir string, agent_bin string, defaults Consts) {
	l4g.Debug("Upgrading to current version %s from version %s.\n", new_version, defaults.Current_build)

	download_file_url := fmt.Sprintf(defaults.Download_file_location, new_version, runtime.GOARCH)
	l4g.Debug("download_file %s\n", download_file_url)
	resp, err := http.Get(download_file_url)
	if err != nil {
		// handle error
		l4g.Error("error getting config data-%s\n", err)
		return
	}
	if resp.StatusCode != 200 {
		// handle error
		l4g.Error("Recieved a bad http code downloading %d-\n", resp.StatusCode)
		return
	}

	defer resp.Body.Close()
	download_file, err := ioutil.ReadAll(resp.Body)
	var h hash.Hash = sha256.New()
	h.Write(download_file)
	hash_code := fmt.Sprintf("%x", h.Sum([]byte{}))
	fmt.Printf("downloaded file with hash of %s\n", hash_code)

	if hash_code == valid_hash {
		l4g.Debug("Sweet valid file downloaded!")
	} else {
		l4g.Error("invalid hash!")
		return
	}

	out_file := fmt.Sprintf(defaults.Output_file_formatter, new_version)
	out_location := out_dir + out_file

	err = ioutil.WriteFile(out_location, download_file, 0744)
	if err != nil {
		l4g.Error(err)
		return
	}

	fmt.Printf("Finished writing file!\n")

	//ignore errors
	os.Remove(agent_bin)

	fmt.Printf("symlinking %s to %s\n", out_location, agent_bin)
	err = os.Symlink(out_location, agent_bin)
	if err != nil {
		l4g.Error("Failed symlinking!--%s\n", err)
		return
	}
	//Not entirely sure how to use filemode
	//    err = os.Chmod(agent_bin, FileMode.)
	cmd = exec.Command("chmod", "+x", agent_bin)
	err = cmd.Start()
	if err != nil {
		l4g.Error("Failed chmoding!--%s\n", err)
		return
	}

	fmt.Printf("Trying new version !\n")
	//    agent_bin  = "/Users/kanwisher/projects/ranger/local_agent/local_agent"
	//    cmd = exec.Command(agent_bin, "-c", "/Users/kanwisher/projects/ranger/local_agent/config/prod_ranger2.conf" )
	//    err = cmd.Start()
	//argv := []string {"local_agent"} //, "-c", "/Users/kanwisher/projects/ranger/local_agent/config/prod_ranger2.conf"}
	//var proca syscall.ProcAttr
	//proca.Env = os.Environ()
	//proca.Files =  []uintptr{uintptr(syscall.Stdout), uintptr(syscall.Stderr)}
	//     _, err = syscall.ForkExec(agent_bin, argv, &proca)//agent_bin)
	//     err = syscall.Exec("/Users/kanwisher/projects/ranger/local_agent/local_agent", argv, os.Environ())//agent_bin)
	//TODO for now just launch the daemon script instead of some insane fork/exec stufff
	if err != nil {
		l4g.Error("Failed running new version!--%s\n", err)
		return
	} else {
		l4g.Debug("Upgrading! Please wait! \n")
		daemonize.Do_fork("", "")
		time.Sleep(10 * time.Second)
		l4g.Debug("Upgraded! Now Extiing! \n")
		os.Exit(0)
	}

}