2019-09-23 03:47:38 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2019-09-23 04:22:16 +00:00
|
|
|
const (
|
2019-12-01 02:07:05 +00:00
|
|
|
ProcessStopCommand = "command"
|
|
|
|
ProcessStopSignal = "signal"
|
2019-09-23 04:22:16 +00:00
|
|
|
ProcessStopNativeStop = "stop"
|
|
|
|
)
|
|
|
|
|
2019-12-22 21:21:21 +00:00
|
|
|
// Holds the server configuration data returned from the Panel. When a server process
|
|
|
|
// is started, Wings communicates with the Panel to fetch the latest build information
|
|
|
|
// as well as get all of the details needed to parse the given Egg.
|
|
|
|
//
|
|
|
|
// This means we do not need to hit Wings each time part of the server is updated, and
|
|
|
|
// the Panel serves as the source of truth at all times. This also means if a configuration
|
|
|
|
// is accidentally wiped on Wings we can self-recover without too much hassle, so long
|
|
|
|
// as Wings is aware of what servers should exist on it.
|
|
|
|
type ServerConfigurationResponse struct {
|
|
|
|
Settings json.RawMessage `json:"settings"`
|
|
|
|
ProcessConfiguration *ProcessConfiguration `json:"process_configuration"`
|
|
|
|
}
|
|
|
|
|
2019-12-28 22:57:19 +00:00
|
|
|
// Defines installation script information for a server process. This is used when
|
|
|
|
// a server is installed for the first time, and when a server is marked for re-installation.
|
|
|
|
type InstallationScript struct {
|
|
|
|
ContainerImage string `json:"container_image"`
|
|
|
|
Entrypoint string `json:"entrypoint"`
|
|
|
|
Script string `json:"script"`
|
|
|
|
}
|
|
|
|
|
2020-10-31 18:13:40 +00:00
|
|
|
type RawServerData struct {
|
|
|
|
Uuid string `json:"uuid"`
|
|
|
|
Settings json.RawMessage `json:"settings"`
|
|
|
|
ProcessConfiguration json.RawMessage `json:"process_configuration"`
|
2020-10-31 18:05:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 03:47:38 +00:00
|
|
|
// Fetches the server configuration and returns the struct for it.
|
2020-10-31 17:04:20 +00:00
|
|
|
func (r *Request) GetServerConfiguration(uuid string) (ServerConfigurationResponse, error) {
|
|
|
|
var cfg ServerConfigurationResponse
|
2020-10-17 19:06:47 +00:00
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
resp, err := r.Get(fmt.Sprintf("/servers/%s", uuid), nil)
|
2019-09-23 03:47:38 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return cfg, err
|
2019-09-23 03:47:38 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
if resp.HasError() {
|
|
|
|
return cfg, resp.Error()
|
2019-09-23 03:47:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
if err := resp.Bind(&cfg); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return cfg, err
|
2019-09-23 03:47:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
return cfg, nil
|
2019-09-23 03:47:38 +00:00
|
|
|
}
|
2019-12-28 22:57:19 +00:00
|
|
|
|
|
|
|
// Fetches installation information for the server process.
|
2020-10-31 17:04:20 +00:00
|
|
|
func (r *Request) GetInstallationScript(uuid string) (InstallationScript, error) {
|
|
|
|
var is InstallationScript
|
|
|
|
resp, err := r.Get(fmt.Sprintf("/servers/%s/install", uuid), nil)
|
2019-12-28 22:57:19 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return is, err
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
if resp.HasError() {
|
|
|
|
return is, resp.Error()
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
if err := resp.Bind(&is); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return is, err
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
return is, nil
|
2020-01-19 21:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marks a server as being installed successfully or unsuccessfully on the panel.
|
2020-10-31 17:04:20 +00:00
|
|
|
func (r *Request) SendInstallationStatus(uuid string, successful bool) error {
|
|
|
|
resp, err := r.Post(fmt.Sprintf("/servers/%s/install", uuid), D{"successful": successful})
|
2020-01-19 21:30:54 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-01-19 21:30:54 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
if resp.HasError() {
|
|
|
|
return resp.Error()
|
2020-01-19 21:30:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
return nil
|
2020-01-19 21:30:54 +00:00
|
|
|
}
|
2020-04-04 05:17:26 +00:00
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
func (r *Request) SendArchiveStatus(uuid string, successful bool) error {
|
|
|
|
resp, err := r.Post(fmt.Sprintf("/servers/%s/archive", uuid), D{"successful": successful})
|
2020-04-04 05:17:26 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-04-04 05:17:26 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
return resp.Error()
|
2020-04-04 05:17:26 +00:00
|
|
|
}
|
2020-04-04 22:15:49 +00:00
|
|
|
|
2020-12-25 21:32:41 +00:00
|
|
|
func (r *Request) SendTransferStatus(uuid string, successful bool) error {
|
|
|
|
state := "failure"
|
|
|
|
if successful {
|
|
|
|
state = "success"
|
2020-04-04 22:15:49 +00:00
|
|
|
}
|
2020-12-25 21:32:41 +00:00
|
|
|
resp, err := r.Get(fmt.Sprintf("/servers/%s/transfer/%s", uuid, state), nil)
|
2020-04-04 22:15:49 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-04-04 22:15:49 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2020-10-31 17:04:20 +00:00
|
|
|
return resp.Error()
|
2020-08-04 23:39:54 +00:00
|
|
|
}
|