|
| 1 | +package cloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "cli/api" |
| 5 | + "cli/format" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/olekukonko/tablewriter" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var community bool |
| 15 | +var disk int |
| 16 | +var memory int |
| 17 | +var vcpu int |
| 18 | +var secure bool |
| 19 | + |
| 20 | +var GetCloudCmd = &cobra.Command{ |
| 21 | + Use: "cloud [gpuCount]", |
| 22 | + Args: cobra.MaximumNArgs(1), |
| 23 | + Short: "get all cloud gpus", |
| 24 | + Long: "get all cloud gpus available on runpod.io", |
| 25 | + Run: func(cmd *cobra.Command, args []string) { |
| 26 | + var err error |
| 27 | + gpuCount := 1 |
| 28 | + if len(args) > 0 { |
| 29 | + gpuCount, err = strconv.Atoi(args[0]) |
| 30 | + cobra.CheckErr(err) |
| 31 | + if gpuCount <= 0 { |
| 32 | + cobra.CheckErr(fmt.Errorf("gpu count must be > 0: %d", gpuCount)) |
| 33 | + } |
| 34 | + } |
| 35 | + var secureCloud *bool |
| 36 | + if secure != community { |
| 37 | + secureCloud = &secure |
| 38 | + } |
| 39 | + input := &api.GetCloudInput{ |
| 40 | + GpuCount: gpuCount, |
| 41 | + MinMemoryInGb: memory, |
| 42 | + MinVcpuCount: vcpu, |
| 43 | + SecureCloud: secureCloud, |
| 44 | + TotalDisk: disk, |
| 45 | + } |
| 46 | + gpuTypes, err := api.GetCloud(input) |
| 47 | + cobra.CheckErr(err) |
| 48 | + |
| 49 | + data := [][]string{} |
| 50 | + for _, gpu := range gpuTypes { |
| 51 | + gpuType, ok := gpu.(map[string]interface{}) |
| 52 | + if !ok { |
| 53 | + continue |
| 54 | + } |
| 55 | + kv, ok := gpuType["lowestPrice"].(map[string]interface{}) |
| 56 | + if !ok || kv["minMemory"] == nil { |
| 57 | + continue |
| 58 | + } |
| 59 | + spotPrice, ok := kv["minimumBidPrice"].(float64) |
| 60 | + spotPriceString := "Reserved" |
| 61 | + if ok && spotPrice > 0 { |
| 62 | + spotPriceString = fmt.Sprintf("%.3f", spotPrice) |
| 63 | + } |
| 64 | + onDemandPrice, ok := kv["uninterruptablePrice"].(float64) |
| 65 | + onDemandPriceString := "Reserved" |
| 66 | + if ok && spotPrice > 0 { |
| 67 | + onDemandPriceString = fmt.Sprintf("%.3f", onDemandPrice) |
| 68 | + } |
| 69 | + row := []string{ |
| 70 | + fmt.Sprintf("%dx %s", gpuCount, kv["gpuTypeId"].(string)), |
| 71 | + fmt.Sprintf("%.f", kv["minMemory"]), |
| 72 | + fmt.Sprintf("%.f", kv["minVcpu"]), |
| 73 | + spotPriceString, |
| 74 | + onDemandPriceString, |
| 75 | + } |
| 76 | + data = append(data, row) |
| 77 | + } |
| 78 | + |
| 79 | + header := []string{"GPU Type", "Mem GB", "vCPU", "Spot $/HR", "OnDemand $/HR"} |
| 80 | + tb := tablewriter.NewWriter(os.Stdout) |
| 81 | + tb.SetHeader(header) |
| 82 | + tb.AppendBulk(data) |
| 83 | + format.TableDefaults(tb) |
| 84 | + tb.Render() |
| 85 | + }, |
| 86 | +} |
| 87 | + |
| 88 | +func init() { |
| 89 | + GetCloudCmd.Flags().BoolVarP(&community, "community", "c", false, "show listings from community cloud only") |
| 90 | + GetCloudCmd.Flags().IntVar(&disk, "disk", 0, "minimum disk size in GB you need") |
| 91 | + GetCloudCmd.Flags().IntVar(&memory, "mem", 0, "minimum sys memory size in GB you need") |
| 92 | + GetCloudCmd.Flags().IntVar(&vcpu, "vcpu", 0, "minimum vCPUs you need") |
| 93 | + GetCloudCmd.Flags().BoolVarP(&secure, "secure", "s", false, "show listings from secure cloud only") |
| 94 | +} |
0 commit comments