Send archive status request before sending the transfer status event

This commit is contained in:
Matthew Penner 2020-12-26 11:42:44 -07:00
parent 1ba3631cc1
commit 5be6e20b03
2 changed files with 21 additions and 13 deletions

View File

@ -233,7 +233,7 @@ func deleteServer(c *gin.Context) {
} }
}(s.Filesystem().Path()) }(s.Filesystem().Path())
var uuid = s.Id() uuid := s.Id()
server.GetServers().Remove(func(s2 *server.Server) bool { server.GetServers().Remove(func(s2 *server.Server) bool {
return s2.Id() == uuid return s2.Id() == uuid
}) })

View File

@ -48,7 +48,7 @@ type serverTransferRequest struct {
Server json.RawMessage `json:"server"` Server json.RawMessage `json:"server"`
} }
// Returns the archive for a server so that it can be transfered to a new node. // Returns the archive for a server so that it can be transferred to a new node.
func getServerArchive(c *gin.Context) { func getServerArchive(c *gin.Context) {
auth := strings.SplitN(c.GetHeader("Authorization"), " ", 2) auth := strings.SplitN(c.GetHeader("Authorization"), " ", 2)
@ -302,14 +302,12 @@ func postTransfer(c *gin.Context) {
data.log().Info("handling incoming server transfer request") data.log().Info("handling incoming server transfer request")
go func(data *serverTransferRequest) { go func(data *serverTransferRequest) {
hasError := true hasError := true
defer func() {
_ = data.sendTransferStatus(!hasError)
}()
// Create a new server installer. This will only configure the environment and not // Create a new server installer. This will only configure the environment and not
// run the installer scripts. // run the installer scripts.
i, err := installer.New(data.Server) i, err := installer.New(data.Server)
if err != nil { if err != nil {
_ = data.sendTransferStatus(false)
data.log().WithField("error", err).Error("failed to validate received server data") data.log().WithField("error", err).Error("failed to validate received server data")
return return
} }
@ -328,15 +326,23 @@ func postTransfer(c *gin.Context) {
defer func(s *server.Server) { defer func(s *server.Server) {
// In the event that this transfer call fails, remove the server from the global // In the event that this transfer call fails, remove the server from the global
// server tracking so that we don't have a dangling instance. // server tracking so that we don't have a dangling instance.
if hasError { if err := data.sendTransferStatus(!hasError); hasError || err != nil {
sendTransferLog("Server transfer failed, check Wings logs for additional information.") sendTransferLog("Server transfer failed, check Wings logs for additional information.")
s.Events().Publish(server.TransferStatusEvent, "failure") s.Events().Publish(server.TransferStatusEvent, "failure")
server.GetServers().Remove(func(s2 *server.Server) bool { server.GetServers().Remove(func(s2 *server.Server) bool {
return s.Id() == s2.Id() return s.Id() == s2.Id()
}) })
// If the transfer status was successful but the request failed, act like the transfer failed.
if !hasError && err != nil {
// Delete all extracted files.
if err := os.RemoveAll(s.Filesystem().Path()); err != nil && !os.IsNotExist(err) {
data.log().WithField("error", err).Warn("failed to delete local server files directory")
}
}
} else { } else {
i.Server().SetTransferring(false) s.SetTransferring(false)
i.Server().Events().Publish(server.TransferStatusEvent, "success") s.Events().Publish(server.TransferStatusEvent, "success")
sendTransferLog("Transfer completed.") sendTransferLog("Transfer completed.")
} }
}(i.Server()) }(i.Server())
@ -357,7 +363,7 @@ func postTransfer(c *gin.Context) {
size := res.ContentLength size := res.ContentLength
if size == 0 { if size == 0 {
data.log().WithField("error", err).Error("recieved an archive response with Content-Length of 0") data.log().WithField("error", err).Error("received an archive response with Content-Length of 0")
return return
} }
sendTransferLog("Got server archive response from remote node. (Content-Length: " + strconv.Itoa(int(size)) + ")") sendTransferLog("Got server archive response from remote node. (Content-Length: " + strconv.Itoa(int(size)) + ")")
@ -401,6 +407,8 @@ func postTransfer(c *gin.Context) {
buf := make([]byte, 1024*4) buf := make([]byte, 1024*4)
if _, err := io.CopyBuffer(file, io.TeeReader(reader, progress), buf); err != nil { if _, err := io.CopyBuffer(file, io.TeeReader(reader, progress), buf); err != nil {
ticker.Stop() ticker.Stop()
_ = file.Close()
sendTransferLog("Failed while writing archive file to disk: " + err.Error()) sendTransferLog("Failed while writing archive file to disk: " + err.Error())
data.log().WithField("error", err).Error("failed to copy archive file to disk") data.log().WithField("error", err).Error("failed to copy archive file to disk")
return return
@ -447,7 +455,7 @@ func postTransfer(c *gin.Context) {
sendTransferLog("Server environment has been created, extracting transfer archive..") sendTransferLog("Server environment has been created, extracting transfer archive..")
data.log().Info("server environment configured, extracting transfer archive") data.log().Info("server environment configured, extracting transfer archive")
if err := archiver.NewTarGz().Unarchive(data.path(), i.Server().Filesystem().Path()); err != nil { if err := archiver.NewTarGz().Unarchive(data.path(), i.Server().Filesystem().Path()); err != nil {
// Unarchiving failed, delete the server's data directory. // Un-archiving failed, delete the server's data directory.
if err := os.RemoveAll(i.Server().Filesystem().Path()); err != nil && !os.IsNotExist(err) { if err := os.RemoveAll(i.Server().Filesystem().Path()); err != nil && !os.IsNotExist(err) {
data.log().WithField("error", err).Warn("failed to delete local server files directory") data.log().WithField("error", err).Warn("failed to delete local server files directory")
} }
@ -462,8 +470,8 @@ func postTransfer(c *gin.Context) {
// hiccup or the fix of whatever error causing the success request to fail. // hiccup or the fix of whatever error causing the success request to fail.
hasError = false hasError = false
data.log().Info("archive transferred successfully, notifying panel of status") data.log().Info("archive extracted successfully, notifying Panel of status")
sendTransferLog("Archive transferred successfully.") sendTransferLog("Archive extracted successfully.")
}(&data) }(&data)
c.Status(http.StatusAccepted) c.Status(http.StatusAccepted)