Skip to content

Commit

Permalink
Update deps to terrafrom
Browse files Browse the repository at this point in the history
  • Loading branch information
rybnovdws committed Jan 17, 2024
1 parent b61532d commit 29afc4b
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 491 deletions.
66 changes: 66 additions & 0 deletions cmd/deploy_script/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"os"

tcli "github.com/deweb-services/terraform-provider-dws/dws/provider/client"
"github.com/spf13/cobra"
)

var (
cpuCreateCfg tcli.DeploymentConfig
cpuDeleteCfg string

cpuCmd = &cobra.Command{
Use: "cpu",
Short: "deploy CPU instance",
}

cpuDeleteCmd = &cobra.Command{
Use: "delete",
Short: "delete cpu instance",
RunE: func(cmd *cobra.Command, args []string) error {
success := false
log.Debugf("delete cpu instance with uuid %s", cpuDeleteCfg)
cli := tcli.NewClient(cmd.Context(), clientCfg, tcli.ClientOptWithURL(APIURL))
err := cli.DeleteDeployment(cmd.Context(), cpuDeleteCfg)
if err == nil {
success = true
}
res := fmt.Sprintf("success=%t\n", success)
_ = os.WriteFile("result", []byte(res), 0644)
log.Debug(res)

if err != nil {
log.Errorf("delete cpu instance with uuid %s, error: %v", cpuDeleteCfg, err)
return err
}
return nil
},
}

cpuCreateCmd = &cobra.Command{
Use: "create",
Short: "create cpu instance",
RunE: func(cmd *cobra.Command, args []string) error {
log.Debugf("create cpu instance with config %v", cpuCreateCfg)
cli := tcli.NewClient(cmd.Context(), clientCfg, tcli.ClientOptWithURL(APIURL))
resp, err := cli.CreateDeployment(cmd.Context(), &cpuCreateCfg)
if err != nil {
log.Debugf("get cpu error: %s", err)
return err
}
res := fmt.Sprintf("id=%s\n", resp.ID)
if resp.Data != nil {
res += fmt.Sprintf("ip=%s\nipv6=%s\nygg=%s\n", resp.Data.IP, resp.Data.IPv6, resp.Data.Ygg)
}
if resp.EndTime != nil {
res += fmt.Sprintf("host=%d\n", *resp.EndTime)
}
_ = os.WriteFile("result", []byte(res), 0644)
// then check the error
return nil
},
}
)
85 changes: 85 additions & 0 deletions cmd/deploy_script/gpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"fmt"
"os"
"strings"
"time"

tcli "github.com/deweb-services/terraform-provider-dws/dws/provider/client"
"github.com/spf13/cobra"
)

var (
clientCfg tcli.DWSProviderConfiguration
gpuCreateCfg tcli.GPUConfig
gpuDeleteCfg string

gpuCmd = &cobra.Command{
Use: "gpu",
Short: "deploy GPU instance",
}
gpuDeleteCmd = &cobra.Command{
Use: "delete",
Short: "delete gpu instance",
RunE: func(cmd *cobra.Command, args []string) error {
success := false
log.Debugf("delete gpu instance with uuid %s", gpuDeleteCfg)
cli := tcli.NewClient(cmd.Context(), clientCfg, tcli.ClientOptWithURL(APIURL))
err := cli.DeleteGPU(cmd.Context(), gpuDeleteCfg)
if err == nil {
success = true
}
res := fmt.Sprintf("success=%t\n", success)
_ = os.WriteFile("result", []byte(res), 0644)
log.Debug(res)

if err != nil {
log.Errorf("delete gpu instance with uuid %s, error: %v", gpuDeleteCfg, err)
return err
}
return nil
},
}

gpuCreateCmd = &cobra.Command{
Use: "create",
Short: "create gpu instance",
RunE: func(cmd *cobra.Command, args []string) error {
log.Debugf("create gpu instance with config %v", gpuCreateCfg)
cli := tcli.NewClient(cmd.Context(), clientCfg, tcli.ClientOptWithURL(APIURL))
respCreate, err := cli.CreateGPU(cmd.Context(), &gpuCreateCfg)
if err != nil {
return fmt.Errorf("create gpu error: %w", err)
}
time.Sleep(time.Second * 30)
respGet := &tcli.RentedGpuInfoResponse{}
Loop:
for i := 0; i < maxTries; i++ {
respGet, err = cli.GetGPU(cmd.Context(), respCreate.UUID)
if err == nil {
switch strings.ToLower(respGet.ActualStatus) {
case "running":
break Loop
case "destroying", "exited":
err = fmt.Errorf("failed to create gpu")
break Loop
default:
log.Debugf("get gpu instance status: %s", respGet.ActualStatus)
}
} else {
log.Debugf("get gpu error: %s", err)
}
time.Sleep(sleepTime)
}
// write uuid to file anyway
res := fmt.Sprintf("uuid=%s\nhost=%s\nport=%d\n", respCreate.UUID, respGet.SshHost, respGet.SshPort)
_ = os.WriteFile("result", []byte(res), 0644)
// then check the error
if err != nil {
return fmt.Errorf("get created gpu parameters error: %s", err)
}
return nil
},
}
)
121 changes: 38 additions & 83 deletions cmd/deploy_script/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package main

import (
"fmt"
"os"
"strings"
"time"

"github.com/deweb-services/deployment-script/internal/dws"
"github.com/deweb-services/deployment-script/internal/types"
"github.com/deweb-services/deployment-script/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -16,89 +12,21 @@ import (
const (
maxTries = 100
sleepTime = 10 * time.Second
APIURL = "https://app.nodeshift.com"
)

var (
clientCfg types.DWSProviderConfiguration
gpuCreateCfg types.GPUCreateConfig
gpuDeleteCfg types.GPUDeleteConfig
log = logger.Logger().Sugar().Named("deployment-script")
rootCmd = &cobra.Command{
log = logger.Logger().Sugar().Named("deployment-script")
rootCmd = &cobra.Command{
Use: "deploy",
Short: "deploy instance",
}

gpuCmd = &cobra.Command{
Use: "gpu",
Short: "deploy GPU instance",
}
gpuDeleteCmd = &cobra.Command{
Use: "delete",
Short: "delete gpu instance",
RunE: func(cmd *cobra.Command, args []string) error {
success := false
log.Debugf("delete gpu instance with uuid %s", gpuDeleteCfg.UUID)
cli := dws.NewClient(cmd.Context(), &clientCfg, log, dws.ClientOptWithURL(types.APIURL))
err := cli.DeleteGPU(cmd.Context(), gpuDeleteCfg.UUID)
if err == nil {
success = true
}
res := fmt.Sprintf("success=%t\n", success)
_ = os.WriteFile("result", []byte(res), 0644)
log.Debug(res)

if err != nil {
log.Errorf("delete gpu instance with uuid %s, error: %v", gpuDeleteCfg.UUID, err)
return err
}
return nil
},
}

gpuCreateCmd = &cobra.Command{
Use: "create",
Short: "create gpu instance",
RunE: func(cmd *cobra.Command, args []string) error {
log.Debugf("create gpu instance with config %v", gpuCreateCfg)
cli := dws.NewClient(cmd.Context(), &clientCfg, log, dws.ClientOptWithURL(types.APIURL))
respCreate, err := cli.CreateGPU(cmd.Context(), &gpuCreateCfg)
if err != nil {
return fmt.Errorf("create gpu error: %w", err)
}
time.Sleep(time.Second * 30)
respGet := &types.RentedGpuInfoResponse{}
Loop:
for i := 0; i < maxTries; i++ {
respGet, err = cli.GetGPU(cmd.Context(), respCreate.UUID)
if err == nil {
switch strings.ToLower(respGet.ActualStatus) {
case "running":
break Loop
case "destroying", "exited":
err = fmt.Errorf("failed to create gpu")
break Loop
default:
log.Debugf("get gpu instance status: %s", respGet.ActualStatus)
}
} else {
log.Debugf("get gpu error: %s", err)
}
time.Sleep(sleepTime)
}
// write uuid to file anyway
res := fmt.Sprintf("uuid=%s\nhost=%s\nport=%d\n", respCreate.UUID, respGet.SshHost, respGet.SshPort)
_ = os.WriteFile("result", []byte(res), 0644)
// then check the error
if err != nil {
return fmt.Errorf("get created gpu parameters error: %s", err)
}
return nil
},
}
)

func init() {
cobra.OnInitialize(initConfig)
cobra.OnInitialize(func() {
viper.AutomaticEnv()
})
rootCmd.PersistentFlags().StringVar(&clientCfg.AccessKey, "access_key", "access_key", "access key for dws platform")
rootCmd.PersistentFlags().StringVar(&clientCfg.SecretAccessKey, "secret_key", "secret_key", "secret key for dws platform")

Expand All @@ -117,15 +45,42 @@ func init() {
_ = viper.BindPFlag("region", rootCmd.Flags().Lookup("region"))
gpuCmd.AddCommand(gpuCreateCmd)

gpuDeleteCmd.Flags().StringVar(&gpuDeleteCfg.UUID, "uuid", "uuid", "uuid of deployment to delete")
gpuDeleteCmd.Flags().StringVar(&gpuDeleteCfg, "uuid", "uuid", "uuid of deployment to delete")
_ = viper.BindPFlag("uuid", rootCmd.Flags().Lookup("uuid"))

gpuCmd.AddCommand(gpuDeleteCmd)
rootCmd.AddCommand(gpuCmd)
}

func initConfig() {
viper.AutomaticEnv()
cpuCreateCmd.Flags().StringVar(&cpuCreateCfg.Region, "region", "region", "Region where you want to deploy like 'USA'")
cpuCreateCmd.Flags().StringVar(&cpuCreateCfg.ImageVersion, "image", "Ubuntu-v22.04", "OS Image used to install on the target Vitrual Machine Deployment like 'Ubuntu-v22.04'")
cpuCreateCmd.Flags().IntVar(&cpuCreateCfg.CPU, "cpu", 1, "number of CPU cores for your deployment")
cpuCreateCmd.Flags().IntVar(&cpuCreateCfg.RAM, "ram", 1, "Amount of RAM for your Deployment in GB")
cpuCreateCmd.Flags().IntVar(&cpuCreateCfg.Hdd, "disk_size", 10, "Disk size for your Deployment in GB")
cpuCreateCmd.Flags().StringVar(&cpuCreateCfg.HddType, "disk_type", "hdd", "Disk type for your Deployment. Available options: hdd, ssd")
cpuCreateCmd.Flags().BoolVar(&cpuCreateCfg.Ipv4, "assign_public_ipv4", false, "If true assigns a public ipv4 address for your Deployment")
cpuCreateCmd.Flags().BoolVar(&cpuCreateCfg.Ipv4, "assign_public_ipv6", false, "If true assigns a public ipv6 address for your Deployment")
cpuCreateCmd.Flags().BoolVar(&cpuCreateCfg.Ygg, "assign_ygg_ip", false, "If true assigns a yggdrasil address for your Deployment")
cpuCreateCmd.Flags().StringVar(&cpuCreateCfg.NetworkUUID, "vpc_id", "", "ID of the vpc to deploy your VM into")
cpuCreateCmd.Flags().StringVar(&cpuCreateCfg.SSHKey, "ssh_key", "", "SSH key to add to the target VM to make it possible to connect to your VM")
cpuCreateCmd.Flags().StringVar(&cpuCreateCfg.HostName, "host_name", "", "Host name for your Deployment")
_ = viper.BindPFlag("region", rootCmd.Flags().Lookup("region"))
_ = viper.BindPFlag("image", rootCmd.Flags().Lookup("image"))
_ = viper.BindPFlag("cpu", rootCmd.Flags().Lookup("cpu"))
_ = viper.BindPFlag("ram", rootCmd.Flags().Lookup("ram"))
_ = viper.BindPFlag("disk_size", rootCmd.Flags().Lookup("disk_size"))
_ = viper.BindPFlag("disk_type", rootCmd.Flags().Lookup("disk_type"))
_ = viper.BindPFlag("assign_public_ipv4", rootCmd.Flags().Lookup("assign_public_ipv4"))
_ = viper.BindPFlag("assign_public_ipv6", rootCmd.Flags().Lookup("assign_public_ipv6"))
_ = viper.BindPFlag("assign_ygg_ip", rootCmd.Flags().Lookup("assign_ygg_ip"))
_ = viper.BindPFlag("vpc_id", rootCmd.Flags().Lookup("vpc_id"))
_ = viper.BindPFlag("ssh_key", rootCmd.Flags().Lookup("ssh_key"))
_ = viper.BindPFlag("host_name", rootCmd.Flags().Lookup("host_name"))
cpuCmd.AddCommand(cpuCreateCmd)

cpuDeleteCmd.Flags().StringVar(&cpuDeleteCfg, "uuid", "uuid", "uuid of deployment to delete")
_ = viper.BindPFlag("uuid", rootCmd.Flags().Lookup("uuid"))
cpuCmd.AddCommand(cpuDeleteCmd)

rootCmd.AddCommand(cpuCmd)
}

func main() {
Expand Down
35 changes: 24 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,41 @@ go 1.21.3

require (
github.com/aws/aws-sdk-go v1.46.6
github.com/aws/aws-sdk-go-v2/service/s3 v1.40.2
github.com/deweb-services/terraform-provider-dws v0.0.6
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
go.uber.org/zap v1.26.0
)

require (
github.com/aws/aws-sdk-go-v2 v1.21.2 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.14 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.43 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.37 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.1.6 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.15 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.38 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.37 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.15.6 // indirect
github.com/aws/smithy-go v1.15.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.21.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.13 // indirect
github.com/aws/aws-sdk-go-v2/config v1.18.42 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.40 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.43 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.1.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.14 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.36 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.35 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.15.4 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.39.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.14.1 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.17.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.22.0 // indirect
github.com/aws/smithy-go v1.14.2 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.8.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
Expand Down
Loading

0 comments on commit 29afc4b

Please sign in to comment.