Ejemplo n.º 1
0
func writeMdb(mdb *mdb.Mdb, mdbFileName string) error {
	tmpFileName := mdbFileName + "~"
	file, err := os.Create(tmpFileName)
	if err != nil {
		return errors.New("Error opening file " + err.Error())
	}
	defer os.Remove(tmpFileName)
	defer file.Close()
	writer := bufio.NewWriter(file)
	switch path.Ext(mdbFileName) {
	case ".gob":
		if err := gob.NewEncoder(writer).Encode(mdb.Machines); err != nil {
			return err
		}
	default:
		if err := json.WriteWithIndent(writer, "    ",
			mdb.Machines); err != nil {
			return err
		}
	}
	if err := writer.Flush(); err != nil {
		return err
	}
	return os.Rename(tmpFileName, mdbFileName)
}
Ejemplo n.º 2
0
func (jsonType) Generate(machine mdb.Machine, logger *log.Logger) (
	[]byte, time.Time, error) {
	buffer := new(bytes.Buffer)
	if err := json.WriteWithIndent(buffer, "    ", machine); err != nil {
		return nil, time.Time{}, err
	}
	buffer.WriteString("\n")
	return buffer.Bytes(), time.Time{}, nil
}
Ejemplo n.º 3
0
func (s state) listTriggersHandler(w http.ResponseWriter, req *http.Request) {
	writer := bufio.NewWriter(w)
	defer writer.Flush()
	imageName := req.URL.RawQuery
	fmt.Fprintf(writer, "<title>triggers %s</title>\n", imageName)
	fmt.Fprintln(writer, "<body>")
	fmt.Fprintln(writer, "<h3>")
	image := s.imageDataBase.GetImage(imageName)
	if image == nil {
		fmt.Fprintf(writer, "Image: %s UNKNOWN!\n", imageName)
	} else if image.Triggers == nil {
		fmt.Fprintln(writer, "NO TRIGGERS")
	} else {
		fmt.Fprintf(writer, "Triggers for image: %s\n", imageName)
		fmt.Fprintln(writer, "<pre>")
		json.WriteWithIndent(writer, "    ", image.Triggers.Triggers)
		fmt.Fprintln(writer, "</pre>")
	}
	fmt.Fprintln(writer, "</body>")
}
Ejemplo n.º 4
0
func (herd *Herd) showSubHandler(w io.Writer, req *http.Request) {
	subName := req.URL.RawQuery
	fmt.Fprintf(w, "<title>sub %s</title>", subName)
	if srpc.CheckTlsRequired() {
		fmt.Fprintln(w, "<body>")
	} else {
		fmt.Fprintln(w, "<body bgcolor=\"#ffb0b0\">")
		fmt.Fprintln(w,
			`<h1><center><font color="red">Running in insecure mode. You can get pwned!!!</center></font></h1>`)
	}
	if herd.updatesDisabledReason != "" {
		fmt.Fprintf(w, "<center>")
		herd.writeDisableStatus(w)
		fmt.Fprintln(w, "</center>")
	}
	fmt.Fprintln(w, "<h3>")
	sub := herd.getSub(subName)
	if sub == nil {
		fmt.Fprintf(w, "Sub: %s UNKNOWN!\n", subName)
		return
	}
	timeNow := time.Now()
	subURL := fmt.Sprintf("http://%s:%d/",
		strings.SplitN(sub.String(), "*", 2)[0], constants.SubPortNumber)
	fmt.Fprintf(w, "Information for sub: <a href=\"%s\">%s</a><br>\n",
		subURL, subName)
	fmt.Fprintln(w, "</h3>")
	fmt.Fprint(w, "<table border=\"0\">\n")
	newRow(w, "Required Image", true)
	sub.herd.showImage(w, sub.mdb.RequiredImage, true)
	newRow(w, "Planned Image", false)
	sub.herd.showImage(w, sub.mdb.PlannedImage, false)
	newRow(w, "Busy time", false)
	sub.showBusy(w)
	newRow(w, "Status", false)
	fmt.Fprintf(w, "    <td>%s</td>\n", sub.publishedStatus.html())
	newRow(w, "Uptime", false)
	showSince(w, sub.pollTime, sub.startTime)
	newRow(w, "Last scan duration", false)
	showDuration(w, sub.lastScanDuration, false)
	newRow(w, "Time since last successful poll", false)
	showSince(w, timeNow, sub.lastPollSucceededTime)
	newRow(w, "Time since last update", false)
	showSince(w, timeNow, sub.lastUpdateTime)
	newRow(w, "Time since last sync", false)
	showSince(w, timeNow, sub.lastSyncTime)
	newRow(w, "Last connection duration", false)
	showDuration(w, sub.lastConnectDuration, false)
	newRow(w, "Last short poll duration", false)
	showDuration(w, sub.lastShortPollDuration, !sub.lastPollWasFull)
	newRow(w, "Last full poll duration", false)
	showDuration(w, sub.lastFullPollDuration, sub.lastPollWasFull)
	newRow(w, "Last compute duration", false)
	showDuration(w, sub.lastComputeUpdateCpuDuration, false)
	fmt.Fprint(w, "  </tr>\n")
	fmt.Fprint(w, "</table>\n")
	fmt.Fprintln(w, "MDB Data:")
	fmt.Fprintln(w, "<pre>")
	json.WriteWithIndent(w, "    ", sub.mdb)
	fmt.Fprintln(w, "</pre>")
}
Ejemplo n.º 5
0
func (mdb *Mdb) debugWrite(w io.Writer) error {
	return json.WriteWithIndent(w, "    ", mdb)
}
Ejemplo n.º 6
0
func serverWatchDaemon(mdbServerHostname string, mdbServerPortNum uint,
	mdbFileName string, mdbChannel chan<- *mdb.Mdb, logger *log.Logger) {
	var lastMdb *mdb.Mdb
	if file, err := os.Open(mdbFileName); err == nil {
		lastMdb = loadFile(file, mdbFileName, logger)
		file.Close()
		if lastMdb != nil {
			sort.Sort(lastMdb)
			mdbChannel <- lastMdb
		}
	}
	address := fmt.Sprintf("%s:%d", mdbServerHostname, mdbServerPortNum)
	for ; ; time.Sleep(time.Second) {
		client, err := srpc.DialHTTP("tcp", address, time.Second*15)
		if err != nil {
			logger.Println(err)
			continue
		}
		conn, err := client.Call("MdbServer.GetMdbUpdates")
		if err != nil {
			logger.Println(err)
			client.Close()
			continue
		}
		decoder := gob.NewDecoder(conn)
		firstUpdate := true
		for {
			var mdbUpdate mdbserver.MdbUpdate
			if err := decoder.Decode(&mdbUpdate); err != nil {
				logger.Println(err)
				break
			} else {
				if firstUpdate {
					lastMdb = &mdb.Mdb{}
					firstUpdate = false
				}
				lastMdb = processUpdate(lastMdb, mdbUpdate)
				sort.Sort(lastMdb)
				mdbChannel <- lastMdb
				if file, err := os.Create(mdbFileName + "~"); err != nil {
					logger.Println(err)
				} else {
					writer := bufio.NewWriter(file)
					var err error
					if isGob(mdbFileName) {
						encoder := gob.NewEncoder(writer)
						err = encoder.Encode(lastMdb.Machines)
					} else {
						err = jsonwriter.WriteWithIndent(writer, "    ",
							lastMdb.Machines)
					}
					if err != nil {
						logger.Println(err)
						os.Remove(mdbFileName + "~")
					} else {
						writer.Flush()
						file.Close()
						os.Rename(mdbFileName+"~", mdbFileName)
					}
				}
			}
		}
		conn.Close()
		client.Close()
	}
}
Ejemplo n.º 7
0
func (t *rpcType) updateAndUnlock(request sub.UpdateRequest,
	rootDirectoryName string) error {
	defer t.clearUpdateInProgress()
	t.disableScannerFunc(true)
	defer t.disableScannerFunc(false)
	startTime := time.Now()
	if request.Triggers == nil {
		request.Triggers = triggers.New()
	}
	var oldTriggers triggers.Triggers
	file, err := os.Open(t.oldTriggersFilename)
	if err == nil {
		decoder := json.NewDecoder(file)
		var trig triggers.Triggers
		err = decoder.Decode(&trig.Triggers)
		file.Close()
		if err == nil {
			oldTriggers = trig
		} else {
			t.logger.Printf("Error decoding old triggers: %s", err.Error())
		}
	}
	t.copyFilesToCache(request.FilesToCopyToCache, rootDirectoryName)
	t.makeObjectCopies(request.MultiplyUsedObjects)
	t.lastUpdateHadTriggerFailures = false
	if len(oldTriggers.Triggers) > 0 {
		t.makeDirectories(request.DirectoriesToMake, rootDirectoryName,
			&oldTriggers, false)
		t.makeInodes(request.InodesToMake, rootDirectoryName,
			request.MultiplyUsedObjects, &oldTriggers, false)
		t.makeHardlinks(request.HardlinksToMake, rootDirectoryName,
			&oldTriggers, "", false)
		t.doDeletes(request.PathsToDelete, rootDirectoryName, &oldTriggers,
			false)
		t.changeInodes(request.InodesToChange, rootDirectoryName, &oldTriggers,
			false)
		matchedOldTriggers := oldTriggers.GetMatchedTriggers()
		if runTriggers(matchedOldTriggers, "stop", t.logger) {
			t.lastUpdateHadTriggerFailures = true
		}
	}
	fsChangeStartTime := time.Now()
	t.makeDirectories(request.DirectoriesToMake, rootDirectoryName,
		request.Triggers, true)
	t.makeInodes(request.InodesToMake, rootDirectoryName,
		request.MultiplyUsedObjects, request.Triggers, true)
	t.makeHardlinks(request.HardlinksToMake, rootDirectoryName,
		request.Triggers, t.objectsDir, true)
	t.doDeletes(request.PathsToDelete, rootDirectoryName, request.Triggers,
		true)
	t.changeInodes(request.InodesToChange, rootDirectoryName, request.Triggers,
		true)
	fsChangeDuration := time.Since(fsChangeStartTime)
	matchedNewTriggers := request.Triggers.GetMatchedTriggers()
	file, err = os.Create(t.oldTriggersFilename)
	if err == nil {
		writer := bufio.NewWriter(file)
		if err := jsonlib.WriteWithIndent(writer, "    ",
			request.Triggers.Triggers); err != nil {
			t.logger.Printf("Error marshaling triggers: %s", err)
		}
		writer.Flush()
		file.Close()
	}
	if runTriggers(matchedNewTriggers, "start", t.logger) {
		t.lastUpdateHadTriggerFailures = true
	}
	timeTaken := time.Since(startTime)
	if t.lastUpdateError != nil {
		t.logger.Printf("Update(): last error: %s\n", t.lastUpdateError)
	} else {
		t.rwLock.Lock()
		t.lastSuccessfulImageName = request.ImageName
		t.rwLock.Unlock()
	}
	t.logger.Printf("Update() completed in %s (change window: %s)\n",
		timeTaken, fsChangeDuration)
	return t.lastUpdateError
}