1
0
mirror of https://github.com/newnius/YAO-scheduler.git synced 2025-06-07 22:31:55 +00:00
YAO-scheduler/src/util.go

200 lines
5.7 KiB
Go
Raw Normal View History

2019-03-04 09:19:55 +00:00
package main
import (
"strconv"
2019-03-20 03:14:07 +00:00
"math/rand"
"time"
"io"
"net/http"
2019-03-04 09:19:55 +00:00
)
2019-04-12 09:21:09 +00:00
type MsgSubmit struct {
Code int `json:"code"`
Error string `json:"error"`
}
2019-04-29 09:05:15 +00:00
type MsgPoolStatusHistory struct {
2019-04-29 12:57:32 +00:00
Code int `json:"code"`
Error string `json:"error"`
Data []PoolStatus `json:"data"`
2019-04-29 09:05:15 +00:00
}
2019-04-18 09:25:37 +00:00
type MsgStop struct {
Code int `json:"code"`
Error string `json:"error"`
}
2019-04-12 09:21:09 +00:00
type MsgSummary struct {
Code int `json:"code"`
Error string `json:"error"`
JobsFinished int `json:"jobs_finished"`
JobsRunning int `json:"jobs_running"`
JobsPending int `json:"jobs_pending"`
FreeGPU int `json:"gpu_free"`
UsingGPU int `json:"gpu_using"`
}
2019-04-29 09:05:15 +00:00
type MsgResource struct {
Code int `json:"code"`
Error string `json:"error"`
Resource map[string]NodeStatus `json:"resources"`
}
2019-03-25 07:36:30 +00:00
type MsgJobList struct {
Code int `json:"code"`
Error string `json:"error"`
2019-07-29 06:56:18 +00:00
Jobs []Job `json:"jobs"`
2019-03-25 07:36:30 +00:00
}
2019-03-20 03:14:07 +00:00
type MsgLog struct {
Code int `json:"code"`
Error string `json:"error"`
Logs string `json:"logs"`
}
type MsgTaskStatus struct {
Code int `json:"code"`
Error string `json:"error"`
Status TaskStatus `json:"status"`
}
type MsgJobStatus struct {
Code int `json:"code"`
Error string `json:"error"`
Status []TaskStatus `json:"status"`
}
type MsgCreate struct {
Code int `json:"code"`
Error string `json:"error"`
Id string `json:"id"`
}
type TaskStatus struct {
2019-06-14 03:10:07 +00:00
Id string `json:"id"`
HostName string `json:"hostname"`
Node string `json:"node"`
Image string `json:"image"`
ImageDigest string `json:"image_digest"`
Command string `json:"command"`
CreatedAt string `json:"created_at"`
FinishedAt string `json:"finished_at"`
Status string `json:"status"`
State map[string]interface{} `json:"state"`
2019-03-20 03:14:07 +00:00
}
type JobStatus struct {
Name string
tasks map[string]TaskStatus
}
2019-04-16 08:59:19 +00:00
type GPUStatus struct {
2019-03-04 09:19:55 +00:00
UUID string `json:"uuid"`
ProductName string `json:"product_name"`
PerformanceState string `json:"performance_state"`
2019-04-29 12:18:51 +00:00
MemoryTotal int `json:"memory_total"`
2019-03-04 09:19:55 +00:00
MemoryFree int `json:"memory_free"`
2019-03-20 03:14:07 +00:00
MemoryAllocated int `json:"memory_allocated"`
2019-03-04 09:19:55 +00:00
MemoryUsed int `json:"memory_used"`
UtilizationGPU int `json:"utilization_gpu"`
UtilizationMem int `json:"utilization_mem"`
TemperatureGPU int `json:"temperature_gpu"`
PowerDraw int `json:"power_draw"`
}
2019-04-16 08:59:19 +00:00
type NodeStatus struct {
2019-04-29 09:05:15 +00:00
ClientID string `json:"id"`
ClientHost string `json:"host"`
NumCPU int `json:"cpu_num"`
2019-04-29 09:15:21 +00:00
UtilCPU float64 `json:"cpu_load"`
MemTotal int `json:"mem_total"`
MemAvailable int `json:"mem_available"`
2019-04-29 09:05:15 +00:00
Status []GPUStatus `json:"status"`
2019-03-20 03:14:07 +00:00
}
type Job struct {
2019-07-12 07:12:51 +00:00
ID int `json:"id"`
Name string `json:"name"`
Tasks []Task `json:"tasks"`
Workspace string `json:"workspace"`
2019-08-01 02:42:37 +00:00
Group string `json:"group"`
2019-07-12 07:12:51 +00:00
Priority JobPriority `json:"priority"`
RunBefore int `json:"run_before"`
CreatedAt int `json:"created_at"`
UpdatedAt int `json:"updated_at"`
CreatedBy int `json:"created_by"`
Status State `json:"status"`
2019-03-20 03:14:07 +00:00
}
type Task struct {
Name string `json:"name"`
2019-04-12 09:21:09 +00:00
Image string `json:"image"`
2019-03-20 03:14:07 +00:00
Cmd string `json:"cmd"`
NumberCPU int `json:"cpu_number"`
Memory int `json:"memory"`
NumberGPU int `json:"gpu_number"`
MemoryGPU int `json:"gpu_memory"`
2019-03-04 09:19:55 +00:00
}
2019-07-29 06:56:18 +00:00
type Group struct {
2019-07-30 07:35:29 +00:00
Name string `json:"name"`
Weight int `json:"weight"`
Reserved bool `json:"reserved"`
NumGPU int `json:"quota_gpu"`
MemoryGPU int `json:"quota_gpu_mem"`
CPU int `json:"quota_cpu"`
Memory int `json:"quota_mem"`
2019-07-29 06:56:18 +00:00
}
type MsgGroupCreate struct {
Code int `json:"code"`
Error string `json:"error"`
}
type MsgGroupList struct {
Code int `json:"code"`
Error string `json:"error"`
Groups []Group `json:"groups"`
}
2019-03-04 09:19:55 +00:00
func str2int(str string, defaultValue int) int {
i, err := strconv.Atoi(str)
if err == nil {
return i
}
return defaultValue
}
2019-03-20 03:14:07 +00:00
func getUA() string {
rand.Seed(time.Now().Unix())
UAs := []string{
"Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0",
"Mozilla/5.0 (X11; Linux i586; rv:63.0) Gecko/20100101 Firefox/63.0",
"Mozilla/5.0 (Windows NT 6.2; WOW64; rv:63.0) Gecko/20100101 Firefox/63.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:10.0) Gecko/20100101 Firefox/62.0",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.13; ko; rv:1.9.1b2) Gecko/20081201 Firefox/60.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/58.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14931",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9",
}
return UAs[rand.Intn(len(UAs))]
}
func doRequest(method string, url string, r io.Reader, contentType string, referer string) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url, r)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("User-Agent", getUA())
req.Header.Set("Referer", referer)
resp, err := client.Do(req)
return resp, err
}