2024-01-19 21:02:52 +00:00
|
|
|
// Copyright (C) 2023-2024 Umorpha Systems
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-20 17:21:18 +00:00
|
|
|
"crypto/rand"
|
2024-01-19 21:02:52 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-01-20 17:21:18 +00:00
|
|
|
"math/big"
|
2024-01-19 21:02:52 +00:00
|
|
|
"net/http"
|
2024-01-20 17:21:18 +00:00
|
|
|
"net/url"
|
2024-01-19 21:02:52 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/datawire/ocibuild/pkg/cliutil"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2024-01-19 21:02:27 +00:00
|
|
|
source "git.mothstuff.lol/lukeshu/eclipse"
|
2024-01-20 17:16:25 +00:00
|
|
|
"git.mothstuff.lol/lukeshu/eclipse/lib/common"
|
2024-01-19 21:02:27 +00:00
|
|
|
"git.mothstuff.lol/lukeshu/eclipse/lib/follower"
|
2024-01-19 21:02:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
argparser := &cobra.Command{
|
|
|
|
Use: os.Args[0] + " [flags] JOBS_URL",
|
2024-01-20 21:08:44 +00:00
|
|
|
Short: "Choose a job from the leader server",
|
2024-01-19 21:02:27 +00:00
|
|
|
Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
|
2024-01-19 21:02:52 +00:00
|
|
|
|
|
|
|
SilenceErrors: true, // we'll handle this ourselves after .ExecuteContext()
|
|
|
|
SilenceUsage: true, // FlagErrorFunc will handle this
|
|
|
|
|
|
|
|
CompletionOptions: cobra.CompletionOptions{
|
|
|
|
DisableDefaultCmd: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
argparser.SetFlagErrorFunc(cliutil.FlagErrorFunc)
|
|
|
|
argparser.SetHelpTemplate(cliutil.HelpTemplate)
|
|
|
|
|
2024-01-19 21:02:27 +00:00
|
|
|
var cfgFile string
|
|
|
|
argparser.Flags().StringVar(&cfgFile, "config", source.DefaultFollowerConfigFile,
|
|
|
|
"Config file to use")
|
|
|
|
_ = argparser.MarkFlagFilename("config", "yml", "yaml")
|
|
|
|
|
|
|
|
argparser.RunE = func( *cobra.Command, []string) error {
|
|
|
|
return Pick(cfgFile)
|
2024-01-19 21:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := argparser.ExecuteContext(ctx); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v: error: %v\n", argparser.CommandPath(), err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randInt(max int) (int, error) {
|
2024-01-20 17:21:18 +00:00
|
|
|
bigN, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
2024-01-19 21:02:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(bigN.Int64()), nil
|
|
|
|
}
|
|
|
|
|
2024-01-19 21:02:27 +00:00
|
|
|
func Pick(cfgFile string) error {
|
|
|
|
cfg, err := follower.LoadConfig(cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(cfg.LeaderURL)
|
2024-01-19 21:02:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.Path = "/jobs/"
|
|
|
|
u.RawQuery = url.Values{
|
|
|
|
"status": []string{"new"},
|
2024-01-20 17:21:18 +00:00
|
|
|
"wait": []string{"true"},
|
2024-01-19 21:02:52 +00:00
|
|
|
}.Encode()
|
|
|
|
|
|
|
|
resp, err := http.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
err := fmt.Errorf("HTTP %s", resp.Status)
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
if len(body) > 0 {
|
|
|
|
err = fmt.Errorf("%w\n%s", err, body)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-20 17:21:18 +00:00
|
|
|
bodyBytes, err := io.ReadAll(resp.Body)
|
2024-01-19 21:02:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-20 17:16:25 +00:00
|
|
|
var options []common.Job
|
2024-01-19 21:02:52 +00:00
|
|
|
if err := json.Unmarshal(bodyBytes, &options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := randInt(len(options))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Path = fmt.Sprintf("/jobs/%d/", options[n].ID)
|
|
|
|
u.RawQuery = ""
|
|
|
|
|
|
|
|
fmt.Println(u)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|