-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
277 additions
and
491 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
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 | ||
}, | ||
} | ||
) |
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,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 | ||
}, | ||
} | ||
) |
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
Oops, something went wrong.