Example #1
1
func main() {
	sourceDir, destDir, destSubfolder, fileType, mode := "", "", "", "", ""

	log.SetPrefix(fmt.Sprintf("cache-monitor (PID %d) ", os.Getpid()))

	var debug bool

	flag.StringVar(&sourceDir, "sourcedir", "", "The source directory where this cache-monitor should pull files from. The cache manager automatically watches files in base/, mobile/, and desktop/, calling the handler for each modified file.")
	flag.StringVar(&destDir, "destdir", "", "The cache directory where this cache-monitor should place files in. If this directory does not exist, it is created. Subfolders for each different layout are generated automatically.")
	flag.StringVar(&destSubfolder, "destsubfolder", "", "Do you want the files to end up in a subdirectory? This is different from changing the destdir in that the subfolder is applied to the path after the layout. E.g. setting the subfolder to 'events' would make the files end up in $sourcedir/$layout/$events/$file for each layout.")
	flag.StringVar(&fileType, "filetype", "", "What filetype is this cache-monitor monitoring? This generally, but not always, corresponds to the file extension. For example, 'js', 'css', 'img', and 'tmpl' are all valid types.")
	flag.StringVar(&mode, "mode", "sync", "What mode should the monitor run in? Valid options include:\n\tsync: Runs once, performing a one-way sync of all files.\n\tdeamon: Runs in the background, updating files as they are changed. Currently only supported on Linux.")
	flag.BoolVar(&debug, "debug", false, "Run in debug mode? Outputs a lot more garbage.")
	flag.Parse()

	if debug {
		dlog = log.New(os.Stderr, "DEBUG: ", log.Ltime|log.Lshortfile)
	} else {
		nul, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
		if err != nil {
			log.Fatal("Could not open null device? Wtf?")
		}
		dlog = log.New(nul, "", 0)
	}

	if mode == "deamon" {
		dlog.Println("Warning: Deamon mode is still beta. Currently it only works on linux systems with inotify support.")
	}

	var v Visitor
	v.source = new(Path)
	v.dest = new(Path)
	v.source.root = sourceDir
	v.dest.root = destDir
	v.dest.subfolder = destSubfolder
	v.fileType = fileType

	var err error

	if mode == "deamon" {
		v.deamon = true
		v.watcher, err = inotify.NewWatcher()
		if err != nil {
			log.Fatal("Error creating inotify instance; deamon mode cannot possibly work.")
		}
	}

	if !osutil.FileExists(sourceDir) {
		dlog.Printf("Warning: Given source directory of %s does not exist, exiting...", sourceDir)
		os.Exit(0)
	}
	filepath.Walk(sourceDir, filepath.WalkFunc(func(pat string, fi os.FileInfo, err error) error { return v.Visit(pat, fi, err) }))

	if v.deamon {
		v.InotifyLoop()
	}
}
Example #2
0
func main() {
	// Kinda hackish :/
	wd, _ := os.Getwd()
	os.Setenv("PATH",
		path.Join(wd, "framework/bin")+":"+
			path.Join(wd, "framework/sh")+":"+
			os.Getenv("PATH"))

	os.MkdirAll("cache", 0755)

	var inpipe, outpipe, context string

	flag.StringVar(&inpipe, "inpipe", "cache/wfdr-deamon-pipe-in", "Name of a file that should be used for a input IPC pipe. Should not exist.")
	flag.StringVar(&outpipe, "outpipe", "cache/wfdr-deamon-pipe-out", "Name of a file that should be used for an output IPC pipe. Should not exist.")
	flag.StringVar(&context, "context", "debug", "Context to run the daemon and child processes from. Valid choices are 'debug', 'test' and 'prod'.")

	flag.Parse()

	if context != "debug" && context != "test" && context != "prod" {
		log.Println("Invalid context argument provided!")
		log.Fatal(flag.Lookup("context").Usage)
	}

	os.Setenv("WFDR_CONTEXT", context)

	if osutil.FileExists(inpipe) || osutil.FileExists(outpipe) {
		log.Fatal("Pipe files already exist, the daemon is likely already running. However, it is also possible that the daemon was not cleanly shut down on its last run, and the files linger. If you suspect this to be the case, remove cache/wfdr-deamon-pipe-in and cache/wfdr-deamon-pipe-out, then try starting the daemon again.")
	}

	infile, err := moduled.OpenPipe(inpipe)
	if err != nil {
		log.Fatal(err)
	}
	outfile, err := moduled.OpenPipe(outpipe)
	if err != nil {
		log.Fatal(err)
	}

	rwc := &moduled.PipeReadWriteCloser{Input: infile, Output: outfile}

	go monitorPipe(rwc)

	sigc := make(chan os.Signal, 2)
	signal.Notify(sigc, syscall.Signal(0x02), syscall.Signal(0x09), syscall.Signal(0x0f))

	for {
		sig := <-sigc
		switch sig.(syscall.Signal) {
		// SIGINT, SIGKILL, SIGTERM
		case 0x02, 0x09, 0xf:
			Exit(0)
		// SIGCHLD
		case 0x11:
			// Do nothing
		default:
			log.Println(sig)
			break
		}
	}
}
Example #3
0
func StartModule(name string) (*Module, error) {
	if ModuleRunning(name) {
		return nil, errors.New(fmt.Sprint("The module seems to be already started..."))
	}

	// Handle special/internal modules
	switch name {
	case "internal:shared":
		return StartSharedSync()
	}

	cwd, _ := os.Getwd()
	jaildir := path.Join(cwd, "jails/"+name)
	moddir := path.Join(cwd, "modules/"+name)

	err := JailInit(moddir, jaildir, name)
	if err != nil {
		return nil, err
	}

	path := jaildir + "/sh:" + jaildir + "/bin:" + os.Getenv("PATH")

	if !osutil.FileExists(jaildir + "/sh/run") {
		cp, err := osutil.WaitRun("cp", []string{"framework/sh/jail-run", jaildir + "/sh/run"})
		if err != nil {
			return nil, errors.New(fmt.Sprint("Error copying default run file, cannot continue:", err))
		}
		cp.Wait()
	}

	modulep, err := osutil.RunWithEnvAndWd("run", []string{name}, []string{"PATH=" + path}, jaildir)
	if err != nil {
		return nil, errors.New(fmt.Sprint("Could not start module "+name+"!:", err))
	}
	pid := modulep.Process.Pid

	module := new(Module)
	module.Name = name
	module.MainProcess = modulep.Process
	// wtf? [sic]
	defer func() {
		modules[name] = module
	}()

	log.Println("Started Module "+name+"! PID:", pid)

	// Start the sync deamon, syncronizes css, js, and templates in the background
	syncproc, err := StartSync(moddir, jaildir, name)
	if err != nil {
		return module, err
	}

	module.SyncProcess = syncproc

	return module, nil
}
Example #4
0
// Looks through all the possible command paths for the given node, and resolves to the most specific one.
func calculateCommandPath(nodePath []string) string {
	path := "suds/" + strings.Join(nodePath, "/") + "/main"
	if osutil.FileExists(path) {
		return path
	}
	if len(nodePath) < 2 {
		return nodePath[0]
	}
	return calculateCommandPath(nodePath[1:])
}
Example #5
0
// Creates the pipe if it does not exist, and then opens it. If the file exists, it ensures that the existing file is a pipe. Returns the file if it could be successfully opened.
func OpenPipe(pipename string) (*os.File, error) {
	if !osutil.FileExists(pipename) {
		syscall.Mkfifo(pipename, 0644)
	}

	finfo, err := os.Stat(pipename)
	if err != nil {
		return nil, errors.New(fmt.Sprint("Error stating pipe ", pipename, ": ", err))
	}

	if finfo.Mode()&os.ModeNamedPipe != os.ModeNamedPipe {
		return nil, errors.New(fmt.Sprint("Specified pipe file ", pipename, " is not a named pipe type file! Remove it to have a pipe file created in it's place."))
	}

	file, err := os.OpenFile(pipename, os.O_RDWR, 0644)
	if err != nil {
		return nil, errors.New(fmt.Sprint("Error opening pipe ", pipename, ": ", err))
	}
	return file, nil
}