func FindPackagesById(c *gin.Context, d *FindPackagesByIdCommand, q *NuGetQuery, logger *log.Entry) { logger.Info("FindPackagesById") var match *rekt.Entry for _, pkg := range registry.Packages { if pkg.Title == d.Id && (q.Filter != "IsLatestVersion" || pkg.IsLatestVersion) { match = pkg } } feed := &rekt.Feed{ Id: "https://www.example.com/api/v2/FindPackagesById", Title: "FindPackagesById", Updated: time.Now(), Entries: []*rekt.Entry{match}, } atom, err := feed.ToAtom() if err != nil { c.Status(500) return } c.Data(200, AtomMimeType, atom) return }
func (r RouteTableManagerEC2) checkRemoteHealthCheck(contextLogger *log.Entry, route *ec2.Route, rs ManageRoutesSpec) bool { contextLogger = contextLogger.WithFields(log.Fields{ "remote_healthcheck": rs.RemoteHealthcheckName, "current_eni": *(route.NetworkInterfaceId), }) contextLogger.Info("Has remote healthcheck ") if ip, ok := eniToIP[*route.NetworkInterfaceId]; ok { contextLogger = contextLogger.WithFields(log.Fields{"current_ip": ip}) if hc, ok := rs.remotehealthchecks[ip]; ok { contextLogger = contextLogger.WithFields(log.Fields{ "healthcheck_healthy": hc.IsHealthy(), "healthcheck_ready": hc.CanPassYet(), }) contextLogger.Info("Has remote healthcheck instance") if hc.CanPassYet() { if hc.IsHealthy() { contextLogger.Debug("Not replacing route, as current route is healthy") return false } else { contextLogger.Debug("Replacing route as remote healthcheck is unhealthy") } } else { contextLogger.Debug("Not replacing route as remote healthcheck cannot pass yet") return false } } else { contextLogger.Error("Cannot find healthcheck") return false } } else { contextLogger.Error("Cannot find ip for ENI") return false } return true }
func (a *Access) InfoForGood(entry *logrus.Entry, req *http.Request) { if host := RemoteHost(req); !a.Seen(host) { entry.Data["comment"] = fmt.Sprintf( ";last info-logged successful request from %s", host) entry.Info("") } else { entry.Debug("") } }
func reclaimTask(client client.Queue, taskID string, runID int, log *logrus.Entry) (*queue.TaskReclaimResponse, *updateError) { log.Info("Reclaiming task") tcrsp, err := client.ReclaimTask(taskID, strconv.Itoa(runID)) // check if an error occurred... if err != nil { return nil, &updateError{err: err.Error()} } log.Info("Reclaimed task successfully") return tcrsp, nil }
func (a *addictedProxy) GetShowSubtitle(reqEpisode *polochon.ShowEpisode, log *logrus.Entry) (polochon.Subtitle, error) { // TODO: add year // TODO: handle release shows, err := a.client.GetTvShows() if err != nil { return nil, err } var guessID string guessDist := 1000 for showName, showID := range shows { dist := levenshtein.Distance(strings.ToLower(showName), strings.ToLower(reqEpisode.ShowTitle)) if dist < guessDist { guessDist = dist guessID = showID } } subtitles, err := a.client.GetSubtitles(guessID, reqEpisode.Season, reqEpisode.Episode) if err != nil { return nil, err } filteredSubs := subtitles.FilterByLang(a.language) if len(filteredSubs) == 0 { return nil, polochon.ErrNoSubtitleFound } sort.Sort(addicted.ByDownloads(filteredSubs)) if reqEpisode.ReleaseGroup == "" { log.Info("No release group specified get the most downloaded subtitle") return &filteredSubs[0], err } subDist := 1000 var subtitle polochon.Subtitle var release string for _, sub := range filteredSubs { dist := levenshtein.Distance(strings.ToLower(reqEpisode.ReleaseGroup), strings.ToLower(sub.Release)) if dist < subDist { subDist = dist subtitle = &sub release = sub.Release } } log.Info("Subtitle chosen ", release, " whit distance ", subDist) return subtitle, err }
func claimTask(client client.Queue, taskID string, runID int, workerID string, workerGroup string, log *logrus.Entry) (*taskClaim, error) { log.Info("Claiming task") payload := queue.TaskClaimRequest{ WorkerGroup: workerGroup, WorkerID: workerID, } tcrsp, err := client.ClaimTask(taskID, strconv.Itoa(runID), &payload) // check if an error occurred... if err != nil { switch err := err.(type) { case httpbackoff.BadHttpResponseCode: e := &updateError{ err: err.Error(), statusCode: err.HttpResponseCode, } var errorMessage string switch { case err.HttpResponseCode == 401 || err.HttpResponseCode == 403: errorMessage = fmt.Sprintf("Not authorized to claim task %s (status code %v).", taskID, err.HttpResponseCode) case err.HttpResponseCode >= 500: errorMessage = fmt.Sprintf("Server error (status code %v) when attempting to claim task %v.", err.HttpResponseCode, taskID) case err.HttpResponseCode != 200: errorMessage = fmt.Sprintf("Received http status code %v when claiming task %v.", err.HttpResponseCode, taskID) } log.WithFields(logrus.Fields{ "error": err, "statusCode": err.HttpResponseCode, }).Error(errorMessage) return nil, e default: log.WithFields(logrus.Fields{ "error": err, }).Error(fmt.Sprintf("Unexpected error occurred when claiming task %s", taskID)) return nil, err } } return &taskClaim{ taskID: taskID, runID: runID, taskClaim: tcrsp, definition: &tcrsp.Task, }, nil }
func Search(c *gin.Context, d *SearchCommand, q *NuGetQuery, logger *log.Entry) { logger.Info("Search") feed := &rekt.Feed{ Id: "https://www.example.com/api/v2/Search", Title: "Search", Updated: time.Now(), Entries: registry.Packages, // just return all packages for every search } atom, err := feed.ToAtom() if err != nil { c.Status(500) return } c.Data(200, AtomMimeType, atom) }
func Packages(c *gin.Context, d *PackagesCommand, q *NuGetQuery, logger *log.Entry) { logger.Info("Packages") for _, pkg := range registry.Packages { if pkg.Title == d.Id && pkg.Version == d.Version { atom, err := pkg.ToAtom() if err != nil { c.Status(500) return } c.Data(200, AtomMimeType, atom) return } } c.Data(404, XmlMimeType, []byte(NuGetPackageNotFound)) }
func buildImage( log *logrus.Entry, inputFile, outputFile string, fromImage, novnc bool, boot, cdrom string, size int, ) error { // Find absolute outputFile outputFile, err := filepath.Abs(outputFile) if err != nil { log.Error("Failed to resolve output file, error: ", err) return err } // Create temp folder for the image tempFolder, err := ioutil.TempDir("", "taskcluster-worker-build-image-") if err != nil { log.Error("Failed to create temporary folder, error: ", err) return err } defer os.RemoveAll(tempFolder) var img *image.MutableImage if !fromImage { // Read machine definition machine, err2 := vm.LoadMachine(inputFile) if err2 != nil { log.Error("Failed to load machine file from ", inputFile, " error: ", err2) return err2 } // Construct MutableImage log.Info("Creating MutableImage") img, err2 = image.NewMutableImage(tempFolder, int(size), machine) if err2 != nil { log.Error("Failed to create image, error: ", err2) return err2 } } else { img, err = image.NewMutableImageFromFile(inputFile, tempFolder) if err != nil { log.Error("Failed to load image, error: ", err) return err } } // Create temp folder for sockets socketFolder, err := ioutil.TempDir("", "taskcluster-worker-sockets-") if err != nil { log.Error("Failed to create temporary folder, error: ", err) return err } defer os.RemoveAll(socketFolder) // Setup a user-space network log.Info("Creating user-space network") net, err := network.NewUserNetwork(tempFolder) if err != nil { log.Error("Failed to create user-space network, error: ", err) return err } // Setup logService so that logs can be posted to meta-service at: // http://169.254.169.254/v1/log net.SetHandler(&logService{Destination: os.Stdout}) // Create virtual machine log.Info("Creating virtual machine") vm, err := vm.NewVirtualMachine(img.Machine().Options(), img, net, socketFolder, boot, cdrom, log.WithField("component", "vm")) if err != nil { log.Error("Failed to recreated virtual-machine, error: ", err) return err } // Start the virtual machine log.Info("Starting virtual machine") vm.Start() // Open VNC display if !novnc { go qemurun.StartVNCViewer(vm.VNCSocket(), vm.Done) } // Wait for interrupt to gracefully kill everything interrupted := make(chan os.Signal, 1) signal.Notify(interrupted, os.Interrupt) // Wait for virtual machine to be done, or we get interrupted select { case <-interrupted: vm.Kill() err = errors.New("SIGINT received, aborting virtual machine") case <-vm.Done: err = vm.Error } <-vm.Done signal.Stop(interrupted) defer img.Dispose() if err != nil { if e, ok := err.(*exec.ExitError); ok { log.Error("QEMU error: ", string(e.Stderr)) } log.Info("Error running virtual machine: ", err) return err } // Package up the finished image log.Info("Package virtual machine image") err = img.Package(outputFile) if err != nil { log.Error("Failed to package finished image, error: ", err) return err } return nil }
// httpServer returns an http server func (s *Server) httpServer(log *logrus.Entry) *http.Server { addr := fmt.Sprintf("%s:%d", s.config.HTTPServer.Host, s.config.HTTPServer.Port) log.Debugf("http server will listen on: %s", addr) mux := mux.NewRouter() for _, route := range []struct { // name of the route name string // path of the route path string // allowed methods for this route methods string // handler is the http handler to run if the route matches handler func(http.ResponseWriter, *http.Request) // excluded tells if the route should be added to the router, // it's in the negative form so that the default behaviour is to add // the route to the router excluded bool }{ { name: "GetMovies", path: "/movies", methods: "GET", handler: s.movieIds, }, { name: "GetMovie", path: "/movies/{id}", methods: "GET", handler: s.getMovieDetails, }, { name: "DeleteMovie", path: "/movies/{id}", methods: "DELETE", handler: s.deleteMovie, }, { name: "DownloadMovie", path: "/movies/{id}/download", methods: "GET", handler: s.serveMovie, excluded: !s.config.HTTPServer.ServeFiles, }, { name: "GetShows", path: "/shows", methods: "GET", handler: s.showIds, }, { name: "GetShow", path: "/shows/{id}", methods: "GET", handler: s.getShowDetails, }, { name: "GetSeason", path: "/shows/{id}/seasons/{season:[0-9]+}", methods: "GET", handler: s.getSeasonDetails, }, { name: "GetEpisode", path: "/shows/{id}/seasons/{season:[0-9]+}/episodes/{episode:[0-9]+}", methods: "GET", handler: s.getShowEpisodeIDDetails, }, { name: "DeleteEpisode", path: "/shows/{id}/seasons/{season:[0-9]+}/episodes/{episode:[0-9]+}", methods: "DELETE", handler: s.deleteEpisode, }, { name: "DownloadEpisode", path: "/shows/{id}/seasons/{season:[0-9]+}/episodes/{episode:[0-9]+}/download", methods: "GET", handler: s.serveShow, excluded: !s.config.HTTPServer.ServeFiles, }, { name: "Wishlist", path: "/wishlist", methods: "GET", handler: s.wishlist, }, { name: "AddTorrent", path: "/torrents", methods: "POST", handler: s.addTorrent, }, } { if route.excluded { continue } // Register the route mux.HandleFunc(route.path, route.handler).Name(route.name).Methods(route.methods) } n := negroni.New() // Panic recovery n.Use(negroni.NewRecovery()) // Use logrus as logger n.Use(negronilogrus.NewMiddlewareFromLogger(s.log.Logger, "httpServer")) // gzip compression n.Use(gzip.Gzip(gzip.DefaultCompression)) // Add basic auth if configured if s.config.HTTPServer.BasicAuth { log.Info("server will require basic authentication") n.Use(NewBasicAuthMiddleware(s.config.HTTPServer.BasicAuthUser, s.config.HTTPServer.BasicAuthPassword)) } // Add token auth middleware if token configuration file specified if s.tokenManager != nil { n.Use(token.NewMiddleware(s.tokenManager, mux)) mux.HandleFunc("/tokens/allowed", s.tokenGetAllowed).Name("TokenGetAllowed") } // Wrap the router n.UseHandler(mux) return &http.Server{Addr: addr, Handler: n} }
func handleApiEvents(logContext *logrus.Entry, client string, events []*omaha.Event) error { for _, event := range events { evType, err := strconv.Atoi(event.Type) if err != nil { return err } evResult, err := strconv.Atoi(event.Result) if err != nil { return err } err = db.LogEvent(client, evType, evResult) if err != nil { logContext.Error(err) } switch evType { case eventTypeDownload: logContext.Info("Client is downloading new version.") case eventTypeArrive: logContext.Info("Client finished download.") case eventTypeApply: switch evResult { case eventResultOK: logContext.Info("Client applied package.") case eventResultError: logContext.Info("Client errored during update.") case eventResultDone: logContext.Info("Client upgraded to current version.") } case eventTypeSuccess: logContext.Info("Install success. Update completion prevented by instance.") default: logContext.Warn("Unknown event type %v.", evType) } } return nil }