Add code to notify panel when install is completed

This commit is contained in:
Dane Everitt 2020-01-19 13:30:54 -08:00
parent 7533e38543
commit 59299d3cda
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 73 additions and 13 deletions

View File

@ -97,3 +97,28 @@ func (r *PanelRequest) GetInstallationScript(uuid string) (InstallationScript, *
return res, nil, nil
}
type installRequest struct {
Successful bool `json:"successful"`
}
// Marks a server as being installed successfully or unsuccessfully on the panel.
func (r *PanelRequest) SendInstallationStatus(uuid string, successful bool) (*RequestError, error) {
b, err := json.Marshal(installRequest{Successful: successful})
if err != nil {
return nil, errors.WithStack(err)
}
resp, err := r.Post(fmt.Sprintf("/servers/%s/install", uuid), b)
if err != nil {
return nil, errors.WithStack(err)
}
defer resp.Body.Close()
r.Response = resp
if r.HasError() {
return r.Error(), nil
}
return nil, nil
}

View File

@ -424,12 +424,11 @@ func (rt *Router) routeServerInstall(w http.ResponseWriter, r *http.Request, ps
s := rt.GetServer(ps.ByName("server"))
defer r.Body.Close()
if err := s.Install(); err != nil {
zap.S().Errorw("failed to install server", zap.String("server", s.Uuid), zap.Error(err))
http.Error(w, "failed to install server", http.StatusInternalServerError)
return
go func (serv *server.Server) {
if err := serv.Install(); err != nil {
zap.S().Errorw("failed to execute server installation process", zap.String("server", s.Uuid), zap.Error(err))
}
}(s)
w.WriteHeader(http.StatusAccepted)
}

View File

@ -18,7 +18,26 @@ import (
"sync"
)
// Executes the installation stack for a server process. Bubbles any errors up to the calling
// function which should handle contacting the panel to notify it of the server state.
func (s *Server) Install() error {
err := s.internalInstall()
zap.S().Debugw("notifying panel of server install state", zap.String("server", s.Uuid))
if serr := s.SyncInstallState(err == nil); serr != nil {
zap.S().Warnw(
"failed to notify panel of server install state",
zap.String("server", s.Uuid),
zap.Bool("was_successful", err == nil),
zap.Error(serr),
)
}
return err
}
// Internal installation function used to simplify reporting back to the Panel.
func (s *Server) internalInstall() error {
script, rerr, err := api.NewRequester().GetInstallationScript(s.Uuid)
if err != nil || rerr != nil {
if err != nil {
@ -33,15 +52,13 @@ func (s *Server) Install() error {
return errors.WithStack(err)
}
go func() {
zap.S().Infow("beginning installation process for server", zap.String("server", s.Uuid))
if err := p.Run(); err != nil {
zap.S().Errorw("failed to complete installation process for server", zap.String("server", s.Uuid), zap.Error(err))
return err
}
zap.S().Infow("completed installation process for server", zap.String("server", s.Uuid))
}()
return nil
}
@ -372,3 +389,22 @@ func (ip *InstallationProcess) StreamOutput(id string) error {
return nil
}
// Makes a HTTP request to the Panel instance notifying it that the server has
// completed the installation process, and what the state of the server is. A boolean
// value of "true" means everything was successful, "false" means something went
// wrong and the server must be deleted and re-created.
func (s *Server) SyncInstallState(successful bool) error {
r := api.NewRequester()
rerr, err := r.SendInstallationStatus(s.Uuid, successful)
if rerr != nil || err != nil {
if err != nil {
return errors.WithStack(err)
}
return errors.New(rerr.String())
}
return nil
}