2019-01-28 16:55:28 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-04-28 14:59:57 +03:00
|
|
|
"crypto/tls"
|
2021-09-27 17:52:46 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2021-07-23 12:49:44 +03:00
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
"github.com/parnurzeal/gorequest"
|
2020-04-28 14:59:57 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-28 16:55:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServerState is the state of Hasura stored on the server.
|
|
|
|
type ServerState struct {
|
|
|
|
UUID string
|
|
|
|
CLIState map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type hdbVersion struct {
|
|
|
|
UUID string `json:"hasura_uuid"`
|
|
|
|
CLIState map[string]interface{} `json:"cli_state"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetServerState queries a server for the state.
|
2021-01-18 20:11:05 +03:00
|
|
|
func GetServerState(endpoint string, adminSecret string, config *tls.Config, hasMetadataV3 bool, log *logrus.Logger) *ServerState {
|
2019-01-28 16:55:28 +03:00
|
|
|
state := &ServerState{
|
|
|
|
UUID: "00000000-0000-0000-0000-000000000000",
|
|
|
|
}
|
2021-01-18 20:11:05 +03:00
|
|
|
|
2021-04-05 17:00:21 +03:00
|
|
|
if hasMetadataV3 {
|
2021-01-18 20:11:05 +03:00
|
|
|
payload := `
|
|
|
|
{
|
|
|
|
"type": "get_catalog_state",
|
|
|
|
"args": {}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
req := gorequest.New()
|
|
|
|
if config != nil {
|
|
|
|
req.TLSClientConfig(config)
|
|
|
|
}
|
|
|
|
req.Post(endpoint).Send(payload)
|
|
|
|
req.Set("X-Hasura-Admin-Secret", adminSecret)
|
|
|
|
|
2021-04-05 17:00:21 +03:00
|
|
|
var r struct {
|
2021-01-18 20:11:05 +03:00
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
_, _, errs := req.EndStruct(&r)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
log.Debugf("server state: errors: %v", errs)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
state.UUID = r.ID
|
2021-04-05 17:00:21 +03:00
|
|
|
} else {
|
2021-01-18 20:11:05 +03:00
|
|
|
state := &ServerState{
|
|
|
|
UUID: "00000000-0000-0000-0000-000000000000",
|
|
|
|
}
|
|
|
|
payload := `{
|
2019-01-28 16:55:28 +03:00
|
|
|
"type": "select",
|
|
|
|
"args": {
|
|
|
|
"table": {
|
|
|
|
"schema": "hdb_catalog",
|
|
|
|
"name": "hdb_version"
|
|
|
|
},
|
|
|
|
"columns": [
|
|
|
|
"hasura_uuid",
|
|
|
|
"cli_state"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
req := gorequest.New()
|
|
|
|
if config != nil {
|
|
|
|
req.TLSClientConfig(config)
|
|
|
|
}
|
|
|
|
req.Post(endpoint).Send(payload)
|
|
|
|
req.Set("X-Hasura-Admin-Secret", adminSecret)
|
|
|
|
|
|
|
|
var r []hdbVersion
|
|
|
|
_, _, errs := req.EndStruct(&r)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
log.Debugf("server state: errors: %v", errs)
|
|
|
|
return state
|
|
|
|
}
|
2019-01-28 16:55:28 +03:00
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
if len(r) != 1 {
|
|
|
|
log.Debugf("invalid response: %v", r)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
state.UUID = r[0].UUID
|
|
|
|
state.CLIState = r[0].CLIState
|
|
|
|
}
|
2019-01-28 16:55:28 +03:00
|
|
|
return state
|
2021-01-18 20:11:05 +03:00
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
}
|
2021-09-27 17:52:46 +03:00
|
|
|
|
|
|
|
func GetServerStatus(endpoint string) (err error) {
|
|
|
|
uri, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while parsing the endpoint :%w", err)
|
|
|
|
}
|
|
|
|
uri.Path = path.Join(uri.Path, "healthz")
|
|
|
|
resp, err := http.Get(uri.String())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("making http request failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("request failed: url: %s status code: %v status: %s", uri.String(), resp.StatusCode, resp.Status)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|