//AnagramResponder returns previous user messages with text anagramed func AnagramResponder(e *irc.Event, con *irc.Connection, logger *log.Logger) { channel := e.Arguments[0] trimmed := strings.Trim(e.Arguments[1], " ") re := regexp.MustCompile("([0-9]*)*([!]+)anagram[s]*") if cmd := re.FindStringSubmatch(trimmed); len(cmd) == 3 { //Parse command back := -1 if cmd[2] != "" { back += len(cmd[2]) } if cmd[1] != "" { b, _ := strconv.Atoi(cmd[1]) back += b } hh := ChannelHistory[channel] //Pull previous msg from history if back >= hh.Len() && back < 20 { con.Privmsg(channel, "Sorry, sheave doesn't have history recorded that far back :(") return } else if back >= hh.Len() && back > 20 { con.Privmsg(channel, "sheave is limited to 20 previous messages per channel") return } logger.Printf("Anagraming %d back in channel: %#v\n%#v\n", back, channel, hh) x := hh.Hist(back) s := AM.AnagramSentence(x) logger.Printf("Anagraming: %#v -> %#v\n", x, s) con.Privmsg(channel, "Anagramed: "+strings.Join(s, " ")) } }
// Write an CLF formatted access log to 'logger' func AccessLog(logger *log.Logger) Pipe { return func(conn *Conn, req *http.Request) bool { var remoteHost = req.RemoteAddr // FIXME var ident string = "-" var authuser string = "-" var now *time.Time = time.UTC() var timestamp string = now.Format("[07/Jan/2006:15:04:05 -0700]") var request string = fmt.Sprintf("%s %s %s", req.Method, req.RawURL, req.Proto) var status int = conn.status var size int64 = conn.written var referer string = "-" var userAgent string = "-" if len(req.Referer()) > 0 { referer = req.Referer() } if len(req.UserAgent()) > 0 { userAgent = req.UserAgent() } // Spawn a new goroutine to perform the actual print to the logfile // instead of making the pipeline wait. go func() { logger.Printf("%s %s %s %s \"%s\" %d %d \"%s\" \"%s\"\n", remoteHost, ident, authuser, timestamp, request, status, size, referer, userAgent) }() return true } }
// NewTaskRunner is used to create a new task context func NewTaskRunner(logger *log.Logger, config *config.Config, updater TaskStateUpdater, ctx *driver.ExecContext, alloc *structs.Allocation, task *structs.Task, consulService *ConsulService) *TaskRunner { // Merge in the task resources task.Resources = alloc.TaskResources[task.Name] // Build the restart tracker. tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup) if tg == nil { logger.Printf("[ERR] client: alloc '%s' for missing task group '%s'", alloc.ID, alloc.TaskGroup) return nil } restartTracker := newRestartTracker(tg.RestartPolicy, alloc.Job.Type) tc := &TaskRunner{ config: config, updater: updater, logger: logger, restartTracker: restartTracker, consulService: consulService, ctx: ctx, alloc: alloc, task: task, updateCh: make(chan *structs.Allocation, 8), destroyCh: make(chan struct{}), waitCh: make(chan struct{}), } return tc }
func loadImageDataBase(baseDir string, objSrv objectserver.ObjectServer, logger *log.Logger) (*ImageDataBase, error) { fi, err := os.Stat(baseDir) if err != nil { return nil, errors.New( fmt.Sprintf("Cannot stat: %s\t%s\n", baseDir, err)) } if !fi.IsDir() { return nil, errors.New(fmt.Sprintf("%s is not a directory\n", baseDir)) } imdb := new(ImageDataBase) imdb.baseDir = baseDir imdb.imageMap = make(map[string]*image.Image) imdb.objectServer = objSrv startTime := time.Now() if err = imdb.scanDirectory(""); err != nil { return nil, err } if logger != nil { plural := "" if imdb.CountImages() != 1 { plural = "s" } logger.Printf("Loaded %d image%s in %s\n", imdb.CountImages(), plural, time.Since(startTime)) } return imdb, nil }
// ListenAndServe creates a context, registers all handlers // using the provided routes function, and starts listening on the port func ListenAndServe(l *log.Logger, port string, routes func(*context)) error { routes(&context{Logger: l}) l.Printf("Listening on http://0.0.0.0:%s\n", port) return http.ListenAndServe(":"+port, nil) }
func notify(config localConfig, hook hookEvent, ws, logDir string, notification grimNotification, logger *log.Logger) error { if hook.EventName != "push" && hook.EventName != "pull_request" { return nil } ghErr := setRefStatus(config.gitHubToken(), hook.Owner, hook.Repo, hook.StatusRef, notification.GithubRefStatus(), "", "") context := buildContext(hook, ws, logDir) message, color, err := notification.HipchatNotification(context, config) logger.Print(message) if config.hipChatToken() != "" && config.hipChatRoom() != "" { if err != nil { logger.Printf("Hipchat: Error while rendering message: %v", err) return err } err = sendMessageToRoom(config.hipChatToken(), config.hipChatRoom(), config.grimServerID(), message, color) if err != nil { logger.Printf("Hipchat: Error while sending message to room: %v", err) return err } } else { logger.Print("HipChat: config.hipChatToken and config.hitChatRoom not set") } return ghErr }
// invokeEventScript will execute the given event script with the given // event. Depending on the event, the semantics of how data are passed // are a bit different. For all events, the SERF_EVENT environmental // variable is the type of the event. For user events, the SERF_USER_EVENT // environmental variable is also set, containing the name of the user // event that was fired. // // In all events, data is passed in via stdin to faciliate piping. See // the various stdin functions below for more information. func invokeEventScript(logger *log.Logger, script string, self serf.Member, event serf.Event) error { defer metrics.MeasureSince([]string{"agent", "invoke", script}, time.Now()) var output bytes.Buffer // Determine the shell invocation based on OS var shell, flag string if runtime.GOOS == windows { shell = "cmd" flag = "/C" } else { shell = "/bin/sh" flag = "-c" } cmd := exec.Command(shell, flag, script) cmd.Args[0] = "serf-event" cmd.Env = append(os.Environ(), "SERF_EVENT="+event.EventType().String(), "SERF_SELF_NAME="+self.Name, "SERF_SELF_ROLE="+self.Tags["role"], ) cmd.Stderr = &output cmd.Stdout = &output // Add all the tags for name, val := range self.Tags { tag_env := fmt.Sprintf("SERF_TAG_%s=%s", strings.ToUpper(name), val) cmd.Env = append(cmd.Env, tag_env) } stdin, err := cmd.StdinPipe() if err != nil { return err } switch e := event.(type) { case serf.MemberEvent: go memberEventStdin(logger, stdin, &e) case serf.UserEvent: cmd.Env = append(cmd.Env, "SERF_USER_EVENT="+e.Name) cmd.Env = append(cmd.Env, fmt.Sprintf("SERF_USER_LTIME=%d", e.LTime)) go userEventStdin(logger, stdin, &e) default: return fmt.Errorf("Unknown event type: %s", event.EventType().String()) } if err := cmd.Start(); err != nil { return err } err = cmd.Wait() logger.Printf("[DEBUG] Event '%s' script output: %s", event.EventType().String(), output.String()) if err != nil { return err } return nil }
// gceProjectIDFromMetadata queries the metadata service on GCE to get the // project ID (name) of an instance. func gceProjectIDFromMetadata(logger *log.Logger) (string, error) { logger.Printf("[INFO] agent: Attempting to discover GCE project from metadata.") client := &http.Client{} req, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/project/project-id", nil) if err != nil { return "", err } req.Header.Add("Metadata-Flavor", "Google") resp, err := client.Do(req) if err != nil { return "", err } defer resp.Body.Close() project, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } logger.Printf("[INFO] agent: GCE project discovered as: %s", project) return string(project), nil }
// gceDiscoverZones discovers a list of zones from a supplied zone pattern, or // all of the zones available to a project. func gceDiscoverZones(logger *log.Logger, ctx context.Context, computeService *compute.Service, project, pattern string) ([]string, error) { var zones []string if pattern != "" { logger.Printf("[INFO] agent: Discovering zones for project %s matching pattern: %s", project, pattern) } else { logger.Printf("[INFO] agent: Discovering all zones available to project: %s", project) } call := computeService.Zones.List(project) if pattern != "" { call = call.Filter(fmt.Sprintf("name eq %s", pattern)) } if err := call.Pages(ctx, func(page *compute.ZoneList) error { for _, v := range page.Items { zones = append(zones, v.Name) } return nil }); err != nil { return zones, err } logger.Printf("[INFO] agent: Discovered GCE zones: %s", strings.Join(zones, ", ")) return zones, nil }
func loginWithNameAndPwd(r *http.Request, res http.ResponseWriter, params martini.Params, session sessions.Session, render render.Render, l *log.Logger) { err := r.ParseForm() if err != nil { l.Printf("ParseForm error: %v\n", err) render.HTML(200, "error", "登录失败!") return } username := r.PostFormValue("username") //password := r.PostFormValue("password") l.Printf("Start logging in, username = %s \n", username) //gitlabClient, err := gogitlab.NewGitlabByLogin(constant.GitLabHost, constant.GitLabApiPath, username, password) //if err != nil { // l.Printf("Login error: %v\n", err) // render.HTML(200, "error", "登录失败!") // return //} //gitUser, err := gitlabClient.CurrentUser() //if err != nil { // l.Printf("Get current error: %v\n", err) // render.HTML(200, "error", "登录失败!") // return //} uInfo := &UserInfo{} session.Set(constant.UserInfoKey, uInfo) render.HTML(200, "dashboard", uInfo) }
func (imdb *ImageDataBase) loadFile(filename string, logger *log.Logger) error { pathname := path.Join(imdb.baseDir, filename) file, err := os.Open(pathname) if err != nil { return err } defer file.Close() reader := fsutil.NewChecksumReader(file) decoder := gob.NewDecoder(reader) var image image.Image if err := decoder.Decode(&image); err != nil { return err } if err := reader.VerifyChecksum(); err != nil { if err == fsutil.ErrorChecksumMismatch { logger.Printf("Checksum mismatch for image: %s\n", filename) return nil } if err != io.EOF { return err } } image.FileSystem.RebuildInodePointers() if err := image.Verify(); err != nil { return err } imdb.Lock() defer imdb.Unlock() if imdb.scheduleExpiration(&image, filename) { imdb.logger.Printf("Deleting already expired image: %s\n", filename) return os.Remove(pathname) } imdb.imageMap[filename] = &image return nil }
func makeProber(testname string, c *testConfig, logger *log.Logger) libprober.RegisterProber { switch c.Testtype { case "dns": hostname := c.Specs.Hostname return dnsprober.New(testname, hostname) case "ldap": sssd := c.Specs.SssdConfig ch := fsutil.WatchFile(sssd, logger) file := <-ch fsutil.WatchFileStop() return ldapprober.Makeldapprober(testname, file, c.Probefreq) case "pid": pidpath := c.Specs.Pathname if pidpath == "" { return nil } return pidprober.Makepidprober(testname, pidpath) case "testprog": testprogpath := c.Specs.Pathname if testprogpath == "" { return nil } return testprogprober.Maketestprogprober(testname, testprogpath) case "url": urlpath := c.Specs.Urlpath urlport := c.Specs.Urlport return urlprober.Makeurlprober(testname, urlpath, urlport) default: logger.Printf("Test type %s not supported", c.Testtype) return nil } }
func scannerDaemon(rootDirectoryName string, cacheDirectoryName string, configuration *Configuration, fsChannel chan *FileSystem, logger *log.Logger) { if runtime.GOMAXPROCS(0) < 2 { runtime.GOMAXPROCS(2) } runtime.LockOSThread() loweredPriority := false var oldFS FileSystem for { fs, err := scanFileSystem(rootDirectoryName, cacheDirectoryName, configuration, &oldFS) if err != nil { logger.Printf("Error scanning\t%s\n", err) } else { oldFS.RegularInodeTable = fs.RegularInodeTable oldFS.SymlinkInodeTable = fs.SymlinkInodeTable oldFS.InodeTable = fs.InodeTable fsChannel <- fs if !loweredPriority { syscall.Setpriority(syscall.PRIO_PROCESS, 0, 10) loweredPriority = true } } } }
func loggingMiddleware(l *log.Logger, n http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() n.ServeHTTP(w, r) l.Printf("Req took %s to complete", time.Since(start)) }) }
func makeHardlinks(hardlinksToMake []sub.Hardlink, rootDirectoryName string, triggers *triggers.Triggers, tmpDir string, takeAction bool, logger *log.Logger) { tmpName := path.Join(tmpDir, "temporaryHardlink") for _, hardlink := range hardlinksToMake { triggers.Match(hardlink.NewLink) if takeAction { targetPathname := path.Join(rootDirectoryName, hardlink.Target) linkPathname := path.Join(rootDirectoryName, hardlink.NewLink) // A Link directly to linkPathname will fail if it exists, so do a // Link+Rename using a temporary filename. if err := fsutil.ForceLink(targetPathname, tmpName); err != nil { logger.Println(err) continue } if err := fsutil.ForceRename(tmpName, linkPathname); err != nil { logger.Println(err) if err := fsutil.ForceRemove(tmpName); err != nil { logger.Println(err) } } else { logger.Printf("Linked: %s => %s\n", linkPathname, targetPathname) } } } }
func NewContext( localStoragePath string, args []string, logger *log.Logger, fatal *log.Logger, ) (c G5CLIExecutionContext) { l := local.New(localStoragePath) if !l.IsPersisted() { logger.Println("No locally cached data. Enhance...") if err := g5hub.PopulateLocalStorage(l); err != nil { fatal.Printf(err.Error() + "\n") os.Exit(1) } l.Persist() } if l.ShouldNag() { l.LastNagged = time.Now() l.Persist() layout := "Mon, 01/02/06" s := l.LastUpdated.Format(layout) logger.Printf("Your last update was %v. You should consider running the update command. I'll nag you again soon.\n", s) } return G5CLIExecutionContext{ LocalStorage: l, Args: args, Logger: logger, Fatal: fatal, Heroku: CLIHerokuExecutor{SuccessLogger: logger, ErrorLogger: fatal}, } }
func NewSessionManager(logger *log.Logger) *SessionManager { manager := new(SessionManager) manager.sessionMap = make(map[string]*Session) manager.timeout = 300 go func(manager *SessionManager) { for { l := time.Now().Unix() for id, v := range (*manager).sessionMap { if v.expire < l { // expire if logger != nil { logger.Printf("Expired session(id:%s)", id) } f := (*manager).onEnd if f != nil { f((*manager).sessionMap[id]) } delete((*manager).sessionMap, id) } } time.Sleep(1000000000) } }(manager) return manager }
func newObjectServer(baseDir string, logger *log.Logger) ( *ObjectServer, error) { fi, err := os.Stat(baseDir) if err != nil { return nil, errors.New( fmt.Sprintf("Cannot stat: %s\t%s\n", baseDir, err)) } if !fi.IsDir() { return nil, errors.New(fmt.Sprintf("%s is not a directory\n", baseDir)) } var objSrv ObjectServer objSrv.baseDir = baseDir objSrv.sizesMap = make(map[hash.Hash]uint64) if logger == nil { logger = log.New(os.Stdout, "", log.LstdFlags) } objSrv.logger = logger startTime := time.Now() if err = scanDirectory(&objSrv, baseDir, ""); err != nil { return nil, err } plural := "" if len(objSrv.sizesMap) != 1 { plural = "s" } logger.Printf("Scanned %d object%s in %s\n", len(objSrv.sizesMap), plural, time.Since(startTime)) return &objSrv, nil }
func GetArtifact(destDir, source, checksum string, logger *log.Logger) (string, error) { if source == "" { return "", fmt.Errorf("Source url is empty in Artifact Getter") } u, err := url.Parse(source) if err != nil { return "", err } // if checksum is seperate, apply to source if checksum != "" { source = strings.Join([]string{source, fmt.Sprintf("checksum=%s", checksum)}, "?") logger.Printf("[DEBUG] client.getter: Applying checksum to Artifact Source URL, new url: %s", source) } artifactFile := filepath.Join(destDir, path.Base(u.Path)) if err := gg.GetFile(artifactFile, source); err != nil { return "", fmt.Errorf("Error downloading artifact: %s", err) } // Add execution permissions to the newly downloaded artifact if runtime.GOOS != "windows" { if err := syscall.Chmod(artifactFile, 0755); err != nil { logger.Printf("[ERR] driver.raw_exec: Error making artifact executable: %s", err) } } return artifactFile, nil }
func addObjects(conn *srpc.Conn, adder ObjectAdder, logger *log.Logger) error { defer conn.Flush() decoder := gob.NewDecoder(conn) encoder := gob.NewEncoder(conn) numAdded := 0 numObj := 0 for ; ; numObj++ { var request objectserver.AddObjectRequest var response objectserver.AddObjectResponse if err := decoder.Decode(&request); err != nil { if err == io.EOF || err == io.ErrUnexpectedEOF { break } return err } if request.Length < 1 { break } response.Hash, response.Added, response.Error = adder.AddObject(conn, request.Length, request.ExpectedHash) if response.Added { numAdded++ } if err := encoder.Encode(response); err != nil { return err } if response.Error != nil { logger.Printf("AddObjects(): failed, %d of %d are new objects %s", numAdded, numObj, response.Error.Error()) return nil } } logger.Printf("AddObjects(): %d of %d are new objects", numAdded, numObj) return nil }
func (e *subscriptionEntity) addV2(name string, connected bool, statusChannel chan<- *PNStatus, messageChannel chan<- *PNMessageResult, presenceChannel chan<- *PNPresenceEventResult, logger *log.Logger) { logger.Printf("INFO: ITEMS: Adding item '%s', connected: %t", name, connected) e.Lock() defer e.Unlock() if _, ok := e.items[name]; ok { logger.Printf("INFO: ITEMS: Item '%s' is not added since it's already exists", name) return } item := &subscriptionItem{ Name: name, StatusChannel: statusChannel, MessageChannel: messageChannel, PresenceChannel: presenceChannel, Connected: connected, IsV2: true, } e.items[name] = item }
func logPrintf(l *log.Logger, format string, v ...interface{}) { if l == nil { log.Printf(format, v...) } else { l.Printf(format, v...) } }
func runMain(logger *log.Logger) { parseFlags() var err error var wd string projectPath = srcPath + projectFlag + "/" if err = os.Chdir(projectPath); err != nil { logger.Printf("\n**invalid project path '%s'\n%s\n", projectFlag, err) help() os.Exit(1) } if debugFlag { wd, err = os.Getwd() if err != nil { fmt.Println(err) } logger.Println("Working DIR:", wd) } testFiles(logger) }
func waitForNotifyEvents(watcher *fsnotify.Watcher, pathname string, channel chan<- io.ReadCloser, logger *log.Logger) { if file, err := os.Open(pathname); err == nil { channel <- file } for { select { case event, ok := <-watcher.Event: if !ok { return } if event.Name != pathname { continue } if file, err := os.Open(pathname); err != nil { if os.IsNotExist(err) { continue } if logger != nil { logger.Printf("Error opening file: %s: %s\n", pathname, err) } } else { channel <- file } case err, ok := <-watcher.Error: if !ok { return } logger.Println("Error with watcher:", err) } } }
func watchDaemon(mdbFileName string, mdbChannel chan<- *mdb.Mdb, logger *log.Logger) { var lastStat syscall.Stat_t var lastMdb *mdb.Mdb for ; ; time.Sleep(time.Second) { var stat syscall.Stat_t if err := syscall.Stat(mdbFileName, &stat); err != nil { logger.Printf("Error stating file: %s\t%s\n", mdbFileName, err) continue } stat.Atim = lastStat.Atim if stat != lastStat { mdb := loadFile(mdbFileName, logger) if mdb == nil { continue } compareStartTime := time.Now() if lastMdb == nil || !compare(lastMdb, mdb) { if lastMdb != nil { mdbCompareTimeDistribution.Add(time.Since(compareStartTime)) } mdbChannel <- mdb lastMdb = mdb } lastStat = stat } } }
// Sends data on stdin for a user event. The stdin simply contains the // payload (if any) of the event. func userEventStdin(logger *log.Logger, stdin io.WriteCloser, e *serf.UserEvent) { defer stdin.Close() if _, err := stdin.Write(e.Payload); err != nil { logger.Printf("[ERR] Error writing user event payload: %s", err) return } }
func timeMe(logger *log.Logger, label string) func() { t0 := time.Now() logger.Printf("%s: start", label) return func() { logger.Printf("%s: took %v", label, time.Since(t0)) } }
func build(builder gin.Builder, runner gin.Runner, logger *log.Logger) { err := builder.Build() if err != nil { buildError = err logger.Println("ERROR! Build failed.") fmt.Println(builder.Errors()) } else { // print success only if there were errors before, otherwise tell when rebuilt var notificationMessage string if buildError != nil { notificationMessage = "Build Successful" logger.Println(notificationMessage) } else { var curDir1 string curDir, _ := os.Getwd() segments := strings.Split(curDir, "/") if len(segments) > 0 { curDir1 = segments[len(segments)-1] } notificationMessage = fmt.Sprintf("%v - Rebuilt at %v \n", curDir1, time.Now().Format("15:04:05.999999")) logger.Printf(notificationMessage) } fmt.Println(notificationsEnabled) if notificationMessage != "" && notificationsEnabled { mack.Notify(notificationMessage) } buildError = nil if immediate { runner.Run() } } time.Sleep(100 * time.Millisecond) }
func scannerDaemon(rootDirectoryName string, cacheDirectoryName string, configuration *Configuration, fsChannel chan<- *FileSystem, logger *log.Logger) { runtime.LockOSThread() loweredPriority := false var oldFS FileSystem for { fs, err := scanFileSystem(rootDirectoryName, cacheDirectoryName, configuration, &oldFS) if err != nil { if err.Error() == "DisableScan" { disableScanAcknowledge <- true <-disableScanAcknowledge continue } logger.Printf("Error scanning: %s\n", err) } else { oldFS.InodeTable = fs.InodeTable oldFS.DirectoryInode = fs.DirectoryInode fsChannel <- fs runtime.GC() if !loweredPriority { syscall.Setpriority(syscall.PRIO_PROCESS, 0, 15) loweredPriority = true } } } }
func Register(consulURL string, logger *log.Logger, service *Service) error { var httpresponse *http.Response var err error var buf []byte buf, err = json.Marshal(service) if err != nil { log.Println(err.Error()) return err } body := bytes.NewBuffer(buf) log.Println(body.String()) httpresponse, err = http.Post(consulURL+REGISTER, "application/json", body) if err != nil { logger.Println(err.Error()) return err } logger.Printf("register status code: %d\n", httpresponse.StatusCode) if httpresponse.StatusCode != 200 { return errors.New("register: invalid status code " + httpresponse.Status) } return err }