-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Zoran Zorica <[email protected]>
- Loading branch information
Showing
13 changed files
with
422 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// main.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/daytonaio/daytona-provider-digitalocean/main_test/util" | ||
) | ||
|
||
func main() { | ||
optionsJson := `{"name":"my-droplet","region":"nyc3","size":"s-1vcpu-1gb","image":"ubuntu-22-04-x64","userData":"#cloud-config\npackages:\n - nginx"}` | ||
|
||
// Create the droplet with the target options | ||
droplet, err := util.CreateDroplet(optionsJson) | ||
if err != nil { | ||
log.Fatalf("Error creating droplet: %v", err) | ||
} | ||
|
||
fmt.Println(droplet) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/digitalocean/godo" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
type TargetOptions struct { | ||
Name string `json:"name"` | ||
Region string `json:"region"` | ||
Size string `json:"size"` | ||
Image string `json:"image"` | ||
UserData string `json:"userData"` | ||
} | ||
|
||
type DropletInfo struct { | ||
Name string | ||
PublicIP string | ||
} | ||
|
||
func (d *DropletInfo) String() string { | ||
return fmt.Sprintf("Created droplet:\n Name = %s\n Public IP = %s", d.Name, d.PublicIP) | ||
} | ||
|
||
func CreateDroplet(optionsJson string) (*DropletInfo, error) { | ||
// Parse the JSON string into a TargetOptions struct | ||
var targetOptions TargetOptions | ||
err := json.Unmarshal([]byte(optionsJson), &targetOptions) | ||
if err != nil { | ||
log.Fatalf("Error parsing target options: %v", err) | ||
} | ||
|
||
// Get the DigitalOcean token from the environment variable | ||
doToken := os.Getenv("DIGITALOCEAN_ACCESS_TOKEN") | ||
if doToken == "" { | ||
log.Fatal("DIGITALOCEAN_ACCESS_TOKEN environment variable is not set") | ||
} | ||
|
||
// Create a new DigitalOcean client | ||
oauthClient := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: doToken})) | ||
client := godo.NewClient(oauthClient) | ||
|
||
// Generate instance object | ||
instance := &godo.DropletCreateRequest{ | ||
Name: targetOptions.Name, | ||
Region: targetOptions.Region, | ||
Size: targetOptions.Size, | ||
Image: godo.DropletCreateImage{ | ||
Slug: targetOptions.Image, | ||
}, | ||
UserData: targetOptions.UserData, | ||
Tags: []string{"daytona"}, | ||
} | ||
|
||
// Create the droplet | ||
droplet, _, err := client.Droplets.Create(context.Background(), instance) | ||
if err != nil { | ||
log.Fatalf("Error creating droplet: %v", err) | ||
} | ||
|
||
// Poll the droplet's status until it becomes active | ||
for { | ||
droplet, _, err = client.Droplets.Get(context.Background(), droplet.ID) | ||
if err != nil { | ||
log.Fatalf("Error getting droplet: %v", err) | ||
} | ||
|
||
if droplet.Status == "active" { | ||
break | ||
} | ||
|
||
time.Sleep(time.Second * 10) | ||
} | ||
|
||
// Extract the droplet's name and public IP | ||
name := droplet.Name | ||
var publicIP string | ||
for _, network := range droplet.Networks.V4 { | ||
if network.Type == "public" { | ||
publicIP = network.IPAddress | ||
break | ||
} | ||
} | ||
|
||
// Create a new DropletInfo object | ||
dropletInfo := &DropletInfo{ | ||
Name: name, | ||
PublicIP: publicIP, | ||
} | ||
|
||
return dropletInfo, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package options | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type Options struct { | ||
MachineID string | ||
MachineFolder string | ||
|
||
Region string | ||
DiskImage string | ||
DiskSize string | ||
MachineType string | ||
Token string | ||
} | ||
|
||
func FromEnv(skipMachine bool) (*Options, error) { | ||
retOptions := &Options{} | ||
|
||
var err error | ||
if !skipMachine { | ||
retOptions.MachineID, err = fromEnvOrError("MACHINE_ID") | ||
if err != nil { | ||
return nil, err | ||
} | ||
// prefix with devpod- | ||
retOptions.MachineID = "devpod-" + retOptions.MachineID | ||
|
||
retOptions.MachineFolder, err = fromEnvOrError("MACHINE_FOLDER") | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
retOptions.Token, err = fromEnvOrError("TOKEN") | ||
if err != nil { | ||
return nil, err | ||
} | ||
retOptions.DiskSize, err = fromEnvOrError("DISK_SIZE") | ||
if err != nil { | ||
return nil, err | ||
} | ||
retOptions.DiskImage, err = fromEnvOrError("DISK_IMAGE") | ||
if err != nil { | ||
return nil, err | ||
} | ||
retOptions.MachineType, err = fromEnvOrError("MACHINE_TYPE") | ||
if err != nil { | ||
return nil, err | ||
} | ||
retOptions.Region, err = fromEnvOrError("REGION") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return retOptions, nil | ||
} | ||
|
||
func fromEnvOrError(name string) (string, error) { | ||
val := os.Getenv(name) | ||
if val == "" { | ||
return "", fmt.Errorf("couldn't find option %s in environment, please make sure %s is defined", name, name) | ||
} | ||
|
||
return val, nil | ||
} |
Oops, something went wrong.