Correctly handle server responses and instantiation

This commit is contained in:
Dane Everitt 2020-10-31 11:13:40 -07:00
parent 41a67933eb
commit ca3becfb55
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 18 additions and 10 deletions

View File

@ -39,15 +39,21 @@ type InstallationScript struct {
} }
type allServerResponse struct { type allServerResponse struct {
Data []json.RawMessage `json:"data"` Data []RawServerData `json:"data"`
Meta Pagination `json:"meta"` Meta Pagination `json:"meta"`
}
type RawServerData struct {
Uuid string `json:"uuid"`
Settings json.RawMessage `json:"settings"`
ProcessConfiguration json.RawMessage `json:"process_configuration"`
} }
// Fetches all of the server configurations from the Panel API. This will initially load the // Fetches all of the server configurations from the Panel API. This will initially load the
// first 50 servers, and then check the pagination response to determine if more pages should // first 50 servers, and then check the pagination response to determine if more pages should
// be loaded. If so, those requests are spun-up in additional routines and the final resulting // be loaded. If so, those requests are spun-up in additional routines and the final resulting
// slice of all servers will be returned. // slice of all servers will be returned.
func (r *Request) GetServers() ([]json.RawMessage, error) { func (r *Request) GetServers() ([]RawServerData, error) {
resp, err := r.Get("/servers", D{"per_page": config.Get().RemoteQuery.BootServersPerPage}) resp, err := r.Get("/servers", D{"per_page": config.Get().RemoteQuery.BootServersPerPage})
if err != nil { if err != nil {
return nil, errors.WithStack(err) return nil, errors.WithStack(err)

View File

@ -45,25 +45,27 @@ func LoadDirectory() error {
log.WithField("total_configs", len(configs)).Info("processing servers returned by the API") log.WithField("total_configs", len(configs)).Info("processing servers returned by the API")
pool := workerpool.New(runtime.NumCPU()) pool := workerpool.New(runtime.NumCPU())
for uuid, data := range configs { log.Debugf("using %d workerpools to instantiate server instances", runtime.NumCPU())
uuid := uuid for _, data := range configs {
data := data data := data
pool.Submit(func() { pool.Submit(func() {
// Parse the json.RawMessage into an expected struct value. We do this here so that a single broken // Parse the json.RawMessage into an expected struct value. We do this here so that a single broken
// server does not cause the entire boot process to hang, and allows us to show more useful error // server does not cause the entire boot process to hang, and allows us to show more useful error
// messaging in the output. // messaging in the output.
d := api.ServerConfigurationResponse{} d := api.ServerConfigurationResponse{
Settings: data.Settings,
}
log.WithField("server", uuid).Info("creating new server object from API response") log.WithField("server", data.Uuid).Info("creating new server object from API response")
if err := json.Unmarshal(data, &d); err != nil { if err := json.Unmarshal(data.ProcessConfiguration, &d.ProcessConfiguration); err != nil {
log.WithField("server", uuid).WithField("error", err).Error("failed to parse server configuration from API response, skipping...") log.WithField("server", data.Uuid).WithField("error", err).Error("failed to parse server configuration from API response, skipping...")
return return
} }
s, err := FromConfiguration(d) s, err := FromConfiguration(d)
if err != nil { if err != nil {
log.WithField("server", uuid).WithField("error", err).Error("failed to load server, skipping...") log.WithField("server", data.Uuid).WithField("error", err).Error("failed to load server, skipping...")
return return
} }