2024-01-01 15:33:08 -06:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
2024-01-24 22:55:34 -06:00
|
|
|
|
2024-01-01 15:33:08 -06:00
|
|
|
"github.com/digitalocean/godo"
|
2024-01-24 22:55:34 -06:00
|
|
|
"golang.org/x/oauth2"
|
2024-01-01 15:33:08 -06:00
|
|
|
|
2024-01-24 22:55:34 -06:00
|
|
|
"go.wit.com/lib/gadgets"
|
2024-01-01 15:33:08 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
// "go.wit.com/gui"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
// createDroplet creates a new droplet in the specified region with the given name.
|
|
|
|
func createDroplet(token, name, region, size, image string) (*godo.Droplet, error) {
|
|
|
|
// Create an OAuth2 token.
|
|
|
|
tokenSource := &oauth2.Token{
|
|
|
|
AccessToken: token,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an OAuth2 client.
|
|
|
|
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
|
|
|
|
|
|
|
|
// Create a DigitalOcean client with the OAuth2 client.
|
|
|
|
client := godo.NewClient(oauthClient)
|
|
|
|
|
|
|
|
// Define the create request.
|
|
|
|
createRequest := &godo.DropletCreateRequest{
|
|
|
|
Name: name,
|
|
|
|
Region: region,
|
|
|
|
Size: size,
|
|
|
|
Image: godo.DropletCreateImage{
|
|
|
|
Slug: image,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the droplet.
|
|
|
|
ctx := context.TODO()
|
|
|
|
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newDroplet, nil
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (d *DigitalOcean) Create(name string, region string, size string, image string) {
|
|
|
|
// Create a new droplet.
|
|
|
|
droplet, err := d.createDropletNew(name, region, size, image)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("digitalocean.Create() Something went wrong: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("digitalocean.Create() droplet ID %d with name %s\n", droplet.ID, droplet.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// createDroplet creates a new droplet in the specified region with the given name.
|
|
|
|
func (d *DigitalOcean) createDropletNew(name, region, size, image string) (*godo.Droplet, error) {
|
|
|
|
log.Infof("digitalocean.createDropletNew() START name =", name)
|
|
|
|
// Create an OAuth2 token.
|
|
|
|
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
|
|
|
|
|
|
|
|
// Create an OAuth2 client.
|
|
|
|
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
|
|
|
|
|
|
|
|
// Create a DigitalOcean client with the OAuth2 client.
|
|
|
|
client := godo.NewClient(oauthClient)
|
|
|
|
|
|
|
|
var sshKeys []godo.DropletCreateSSHKey
|
|
|
|
log.Info("digitalocean.createDropletNew() about to get keys. client =", client)
|
|
|
|
|
|
|
|
// Find the key by name.
|
|
|
|
for i, key := range d.sshKeys {
|
|
|
|
log.Info("found ssh i =", i, key.Name)
|
|
|
|
log.Verbose("found ssh key.Name =", key.Name)
|
|
|
|
log.Verbose("found ssh key.Fingerprint =", key.Fingerprint)
|
|
|
|
log.Verbose("found ssh key:", key)
|
|
|
|
/*
|
2024-01-24 22:55:34 -06:00
|
|
|
sshKeys = []godo.DropletCreateSSHKey{
|
|
|
|
{ID: key.ID},
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
*/
|
|
|
|
sshKeys = append(sshKeys, godo.DropletCreateSSHKey{ID: key.ID})
|
2024-01-24 22:55:34 -06:00
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
|
|
|
|
// Define the create request.
|
|
|
|
createRequest := &godo.DropletCreateRequest{
|
|
|
|
Name: name,
|
|
|
|
Region: region,
|
|
|
|
Size: size,
|
|
|
|
Image: godo.DropletCreateImage{
|
|
|
|
Slug: image,
|
|
|
|
},
|
2024-01-24 22:55:34 -06:00
|
|
|
IPv6: true, // Enable IPv6
|
2024-01-01 15:33:08 -06:00
|
|
|
SSHKeys: sshKeys, // Add SSH key IDs here
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the droplet.
|
|
|
|
ctx := context.TODO()
|
|
|
|
log.Info("digitalocean.createDropletNew() about to do client.Create(). ctx =", ctx)
|
|
|
|
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
|
|
|
|
log.Infof("digitalocean.createDropletNew() END newDroplet =", newDroplet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newDroplet, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var myCreate *windowCreate
|
|
|
|
|
|
|
|
// This is initializes the main DO object
|
|
|
|
// You can only have one of these
|
|
|
|
func InitCreateWindow() *windowCreate {
|
2024-01-24 22:55:34 -06:00
|
|
|
if !myDo.Ready() {
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
if myCreate != nil {
|
|
|
|
myCreate.Show()
|
|
|
|
return myCreate
|
|
|
|
}
|
|
|
|
myCreate = new(windowCreate)
|
|
|
|
myCreate.ready = false
|
|
|
|
|
|
|
|
myCreate.window = myDo.parent.NewWindow("Create Droplet")
|
|
|
|
|
|
|
|
// make a group label and a grid
|
|
|
|
myCreate.group = myCreate.window.NewGroup("droplets:").Pad()
|
|
|
|
myCreate.grid = myCreate.group.NewGrid("grid", 2, 1).Pad()
|
2024-01-24 22:55:34 -06:00
|
|
|
|
2024-01-27 14:22:24 -06:00
|
|
|
myCreate.name = gadgets.NewBasicEntry(myCreate.grid, "Name").SetText("test.wit.com")
|
2024-01-01 15:33:08 -06:00
|
|
|
|
|
|
|
myCreate.region = gadgets.NewBasicDropdown(myCreate.grid, "Region")
|
|
|
|
|
|
|
|
regions := myDo.listRegions()
|
|
|
|
|
|
|
|
// Print details of each region.
|
|
|
|
log.Info("Available Regions:")
|
|
|
|
for i, region := range regions {
|
|
|
|
log.Infof("i: %d, Slug: %s, Name: %s, Available: %v\n", i, region.Slug, region.Name, region.Available)
|
|
|
|
log.Spew(i, region)
|
|
|
|
if len(region.Sizes) == 0 {
|
|
|
|
log.Info("Skipping region. No available sizes region =", region.Name)
|
|
|
|
} else {
|
|
|
|
s := region.Name + " (" + region.Slug + ")"
|
2024-01-24 22:55:34 -06:00
|
|
|
if myCreate.regionSlug == "" {
|
2024-01-01 15:33:08 -06:00
|
|
|
myCreate.regionSlug = region.Slug
|
|
|
|
}
|
2024-01-27 14:22:24 -06:00
|
|
|
myCreate.region.AddText(s)
|
2024-01-01 15:33:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
myCreate.region.Custom = func() {
|
2024-01-27 14:22:24 -06:00
|
|
|
s := myCreate.region.String()
|
2024-01-01 15:33:08 -06:00
|
|
|
log.Info("create droplet region changed to:", s)
|
|
|
|
for _, region := range regions {
|
|
|
|
if s == region.Name {
|
|
|
|
log.Info("Found region! slug =", myCreate.regionSlug, region)
|
|
|
|
myCreate.regionSelected = region
|
|
|
|
log.Info("Found region! Now update all the sizes count =", len(region.Sizes))
|
|
|
|
for _, size := range region.Sizes {
|
|
|
|
log.Info("Size: ", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
myCreate.size = gadgets.NewBasicCombobox(myCreate.grid, "Size")
|
2024-01-27 14:22:24 -06:00
|
|
|
myCreate.size.AddText("s-1vcpu-1gb")
|
|
|
|
myCreate.size.AddText("s-1vcpu-1gb-amd")
|
|
|
|
myCreate.size.AddText("s-1vcpu-1gb-intel")
|
|
|
|
myCreate.size.AddText("s-2vcpu-4gb-120gb-intel")
|
|
|
|
myCreate.size.SetText("s-2vcpu-4gb-120gb-intel")
|
2024-01-01 15:33:08 -06:00
|
|
|
myCreate.size.Custom = func() {
|
2024-01-27 14:22:24 -06:00
|
|
|
size := myCreate.size.String()
|
2024-01-01 15:33:08 -06:00
|
|
|
log.Info("Create() need to verify size exists in region. Digital Ocean size.Slug =", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
myCreate.memory = gadgets.NewBasicDropdown(myCreate.grid, "Memory")
|
2024-01-27 14:22:24 -06:00
|
|
|
myCreate.memory.AddText("1 GB")
|
|
|
|
myCreate.memory.AddText("2 GB")
|
|
|
|
myCreate.memory.AddText("4 GB")
|
|
|
|
myCreate.memory.AddText("8 GB")
|
|
|
|
myCreate.memory.AddText("16 GB")
|
|
|
|
myCreate.memory.AddText("32 GB")
|
|
|
|
myCreate.memory.AddText("64 GB")
|
|
|
|
myCreate.memory.AddText("96 GB")
|
|
|
|
myCreate.memory.AddText("128 GB")
|
|
|
|
myCreate.memory.AddText("256 GB")
|
2024-01-01 15:33:08 -06:00
|
|
|
myCreate.memory.Custom = func() {
|
|
|
|
for _, size := range myCreate.regionSelected.Sizes {
|
|
|
|
log.Info("Size: ", size)
|
|
|
|
}
|
|
|
|
myCreate.UpdateSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
myCreate.image = gadgets.NewBasicCombobox(myCreate.grid, "Image")
|
2024-01-27 14:22:24 -06:00
|
|
|
myCreate.image.AddText("debian-12-x64")
|
|
|
|
myCreate.image.AddText("ubuntu-20-04-x64")
|
|
|
|
myCreate.image.SetText("debian-12-x64")
|
2024-01-01 15:33:08 -06:00
|
|
|
|
|
|
|
// myCreate.nvme = gadgets.NewBasicCheckbox(myCreate.grid, "NVMe")
|
|
|
|
|
|
|
|
myCreate.group.NewLabel("Create Droplet")
|
|
|
|
|
|
|
|
// box := myCreate.group.NewBox("vBox", false).Pad()
|
|
|
|
box := myCreate.group.NewBox("hBox", true).Pad()
|
2024-01-24 22:55:34 -06:00
|
|
|
box.NewButton("Cancel", func() {
|
2024-01-01 15:33:08 -06:00
|
|
|
myCreate.Hide()
|
|
|
|
})
|
2024-01-24 22:55:34 -06:00
|
|
|
box.NewButton("Create", func() {
|
2024-01-27 14:22:24 -06:00
|
|
|
name := myCreate.name.String()
|
|
|
|
size := myCreate.size.String()
|
2024-01-01 15:33:08 -06:00
|
|
|
region := myCreate.regionSlug
|
2024-01-27 14:22:24 -06:00
|
|
|
image := myCreate.image.String()
|
2024-01-24 22:55:34 -06:00
|
|
|
if region == "" {
|
2024-01-01 15:33:08 -06:00
|
|
|
log.Info("Create() droplet name =", name, "region =", region, "size =", size, "image", image)
|
|
|
|
log.Info("Create() region lookup failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Info("Create() droplet name =", name, "region =", region, "size =", size, "image", image)
|
|
|
|
myDo.Create(name, region, size, image)
|
|
|
|
myCreate.Hide()
|
|
|
|
})
|
|
|
|
|
|
|
|
myCreate.ready = true
|
|
|
|
myDo.create = myCreate
|
|
|
|
return myCreate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the size
|
|
|
|
func (d *windowCreate) UpdateSize() {
|
2024-01-24 22:55:34 -06:00
|
|
|
if !d.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
log.Info("Now find the size. sizes count =", len(myCreate.regionSelected.Sizes))
|
|
|
|
var s string
|
2024-01-27 14:22:24 -06:00
|
|
|
m := myCreate.memory.String()
|
2024-01-01 15:33:08 -06:00
|
|
|
switch m {
|
|
|
|
case "1 GB":
|
|
|
|
s = "cpu-1gb-"
|
|
|
|
case "2 GB":
|
|
|
|
s = "cpu-2gb-"
|
|
|
|
case "4 GB":
|
|
|
|
s = "cpu-4gb-"
|
|
|
|
case "8 GB":
|
|
|
|
s = "cpu-8gb-"
|
|
|
|
case "16 GB":
|
|
|
|
s = "cpu-16gb-"
|
|
|
|
case "32 GB":
|
|
|
|
s = "cpu-32gb-"
|
|
|
|
case "64 GB":
|
|
|
|
s = "cpu-64gb-"
|
|
|
|
case "96 GB":
|
|
|
|
s = "cpu-96gb-"
|
|
|
|
case "128 GB":
|
|
|
|
s = "cpu-128gb-"
|
|
|
|
case "256 GB":
|
|
|
|
s = "cpu-256gb-"
|
|
|
|
default:
|
|
|
|
s = "cpu-4gb-"
|
|
|
|
}
|
|
|
|
for _, size := range myCreate.regionSelected.Sizes {
|
|
|
|
if strings.Contains(size, s) {
|
|
|
|
log.Info("Found Size! size.Slug =", size, "contains", s)
|
2024-01-27 14:22:24 -06:00
|
|
|
myCreate.size.SetText(size)
|
2024-01-01 15:33:08 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-01-27 14:22:24 -06:00
|
|
|
log.Info("memory =", myCreate.memory.String())
|
2024-01-01 15:33:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the status is valid
|
|
|
|
func (d *windowCreate) Ready() bool {
|
2024-01-24 22:55:34 -06:00
|
|
|
if d == nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
return d.ready
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *windowCreate) Show() {
|
2024-01-24 22:55:34 -06:00
|
|
|
if !d.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
log.Info("digitalocean.Show() window")
|
|
|
|
if d.hidden {
|
|
|
|
d.window.Show()
|
|
|
|
}
|
|
|
|
d.hidden = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *windowCreate) Hide() {
|
2024-01-24 22:55:34 -06:00
|
|
|
if !d.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
log.Info("digitalocean.Hide() window")
|
2024-01-24 22:55:34 -06:00
|
|
|
if !d.hidden {
|
2024-01-01 15:33:08 -06:00
|
|
|
d.window.Hide()
|
|
|
|
}
|
|
|
|
d.hidden = true
|
|
|
|
}
|