digitalocean droplet power on & off work

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-30 23:30:14 -06:00
parent be03e85c2d
commit 44730e1b91
6 changed files with 80 additions and 20 deletions

View File

@ -20,6 +20,7 @@ func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet {
droplet := new(Droplet) droplet := new(Droplet)
droplet.ready = false droplet.ready = false
droplet.poll = dd // the information polled from the digital ocean API droplet.poll = dd // the information polled from the digital ocean API
droplet.ID = dd.ID
if (d.dGrid == nil) { if (d.dGrid == nil) {
d.dGrid = d.group.NewGrid("grid", 9, 1).Pad() d.dGrid = d.group.NewGrid("grid", 9, 1).Pad()
@ -104,11 +105,13 @@ func (d *Droplet) Update(dpoll *godo.Droplet) {
func (d *Droplet) PowerOn() { func (d *Droplet) PowerOn() {
if ! d.Exists() {return} if ! d.Exists() {return}
log.Info("droplet.PowerOn() should do it here") log.Info("droplet.PowerOn() should do it here")
myDo.PowerOn(d.ID)
} }
func (d *Droplet) PowerOff() { func (d *Droplet) PowerOff() {
if ! d.Exists() {return} if ! d.Exists() {return}
log.Info("droplet.PowerOff() here") log.Info("droplet.PowerOff() here")
myDo.PowerOff(d.ID)
} }
func (d *Droplet) Destroy() { func (d *Droplet) Destroy() {

View File

@ -2,30 +2,37 @@ package digitalocean
import ( import (
"context" "context"
"fmt"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"github.com/digitalocean/godo" "github.com/digitalocean/godo"
"go.wit.com/log"
) )
func GetSSHKeyID(token, name string) (string, error) { // func (d *DigitalOcean) ListDroplets() bool {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) func (d *DigitalOcean) ListSSHKeyID() error {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
oauthClient := oauth2.NewClient(context.Background(), tokenSource) oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient) client := godo.NewClient(oauthClient)
// List all keys. // List all keys.
keys, _, err := client.Keys.List(context.Background(), &godo.ListOptions{}) keys, _, err := client.Keys.List(context.Background(), &godo.ListOptions{})
if err != nil { if err != nil {
return "", err return err
} }
// Find the key by name. // Find the key by name.
for _, key := range keys { for i, key := range keys {
if key.Name == name { log.Info("found ssh i =", i)
return key.Fingerprint, nil log.Info("found ssh key.Name =", key.Name)
} log.Info("found ssh key.Fingerprint =", key.Fingerprint)
log.Info("found ssh key:", key)
// if key.Name == name {
// return key.Fingerprint, nil
// }
} }
return "", fmt.Errorf("SSH Key not found") // return fmt.Errorf("SSH Key not found")
return nil
} }

View File

@ -58,18 +58,21 @@ func (d *DigitalOcean) Hide() {
func (d *DigitalOcean) Update() bool { func (d *DigitalOcean) Update() bool {
if ! d.Ready() {return false} if ! d.Ready() {return false}
if ! d.ListDroplets() { d.ListSSHKeyID()
log.Error(d.err, "Error listing droplets") if d.ListDroplets() {
return false
}
for _, droplet := range d.dpolled { for _, droplet := range d.dpolled {
// check if the droplet ID already exists // check if the droplet ID already exists
if (d.dropMap[droplet.ID] != nil) { if (d.dropMap[droplet.ID] == nil) {
d.dropMap[droplet.ID] = d.NewDroplet(&droplet)
} else {
log.Info("droplet.Update()", droplet.ID, droplet.Name, "already exists") log.Info("droplet.Update()", droplet.ID, droplet.Name, "already exists")
d.dropMap[droplet.ID].Update(&droplet) d.dropMap[droplet.ID].Update(&droplet)
continue continue
} }
d.dropMap[droplet.ID] = d.NewDroplet(&droplet) }
} else {
log.Error(d.err, "Error listing droplets")
return false
} }
return true return true
} }

45
digitalocean/poweron.go Normal file
View File

@ -0,0 +1,45 @@
package digitalocean
import (
"context"
"golang.org/x/oauth2"
"github.com/digitalocean/godo"
"go.wit.com/log"
)
func (d *DigitalOcean) PowerOn(dropletID int) error {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
ctx := context.TODO()
// Create a request to power on the droplet.
_, _, err := client.DropletActions.PowerOn(ctx, dropletID)
if err != nil {
return err
}
log.Printf("Power-on signal sent to droplet with ID: %d\n", dropletID)
return nil
}
func (d *DigitalOcean) PowerOff(dropletID int) error {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
ctx := context.TODO()
// Create a request to power on the droplet.
_, _, err := client.DropletActions.PowerOff(ctx, dropletID)
if err != nil {
return err
}
log.Printf("Power-on signal sent to droplet with ID: %d\n", dropletID)
return nil
}

View File

@ -41,6 +41,8 @@ type ipButton struct {
} }
type Droplet struct { type Droplet struct {
ID int
ready bool ready bool
hidden bool hidden bool
err error err error

View File

@ -1,6 +1,6 @@
# export GO111MODULE="off" # export GO111MODULE="off"
run: build run: build
./control-panel-digitalocean --gui-debug ./control-panel-digitalocean --gui-debug --log-debug
build-release: build-release:
go get -v -u -x . go get -v -u -x .