Add Run() function
* Add Run() helper for external providers * Make GARM_CONTROLLER_ID env var common to all commands Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
parent
e29d5db72c
commit
b17c921a7c
2 changed files with 66 additions and 0 deletions
61
runner/providers/external/execution/execution.go
vendored
61
runner/providers/external/execution/execution.go
vendored
|
|
@ -2,6 +2,7 @@ package execution
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -73,6 +74,10 @@ func (e Environment) Validate() error {
|
|||
return fmt.Errorf("error accessing config file: %w", err)
|
||||
}
|
||||
|
||||
if e.ControllerID == "" {
|
||||
return fmt.Errorf("missing GARM_CONTROLLER_ID")
|
||||
}
|
||||
|
||||
switch e.Command {
|
||||
case CreateInstanceCommand:
|
||||
if e.BootstrapParams.Name == "" {
|
||||
|
|
@ -102,3 +107,59 @@ func (e Environment) Validate() error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Run(ctx context.Context, provider ExternalProvider, env Environment) (string, error) {
|
||||
var ret string
|
||||
switch env.Command {
|
||||
case CreateInstanceCommand:
|
||||
instance, err := provider.CreateInstance(ctx, env.BootstrapParams)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create instance in provider: %w", err)
|
||||
}
|
||||
|
||||
asJs, err := json.Marshal(instance)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to marshal response: %w", err)
|
||||
}
|
||||
ret = string(asJs)
|
||||
case GetInstanceCommand:
|
||||
instance, err := provider.GetInstance(ctx, env.InstanceID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get instance from provider: %w", err)
|
||||
}
|
||||
asJs, err := json.Marshal(instance)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to marshal response: %w", err)
|
||||
}
|
||||
ret = string(asJs)
|
||||
case ListInstancesCommand:
|
||||
instances, err := provider.ListInstances(ctx, env.PoolID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to list instances from provider: %w", err)
|
||||
}
|
||||
asJs, err := json.Marshal(instances)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to marshal response: %w", err)
|
||||
}
|
||||
ret = string(asJs)
|
||||
case DeleteInstanceCommand:
|
||||
if err := provider.DeleteInstance(ctx, env.InstanceID); err != nil {
|
||||
return "", fmt.Errorf("failed to delete instance from provider: %w", err)
|
||||
}
|
||||
case RemoveAllInstancesCommand:
|
||||
if err := provider.RemoveAllInstances(ctx); err != nil {
|
||||
return "", fmt.Errorf("failed to destroy environment: %w", err)
|
||||
}
|
||||
case StartInstanceCommand:
|
||||
if err := provider.Start(ctx, env.InstanceID); err != nil {
|
||||
return "", fmt.Errorf("failed to start instance: %w", err)
|
||||
}
|
||||
case StopInstanceCommand:
|
||||
if err := provider.Stop(ctx, env.InstanceID, true); err != nil {
|
||||
return "", fmt.Errorf("failed to stop instance: %w", err)
|
||||
}
|
||||
default:
|
||||
return "", fmt.Errorf("invalid command: %s", env.Command)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
|
|
|||
5
runner/providers/external/external.go
vendored
5
runner/providers/external/external.go
vendored
|
|
@ -100,6 +100,7 @@ func (e *external) CreateInstance(ctx context.Context, bootstrapParams params.Bo
|
|||
func (e *external) DeleteInstance(ctx context.Context, instance string) error {
|
||||
asEnv := []string{
|
||||
fmt.Sprintf("GARM_COMMAND=%s", execution.DeleteInstanceCommand),
|
||||
fmt.Sprintf("GARM_CONTROLLER_ID=%s", e.controllerID),
|
||||
fmt.Sprintf("GARM_INSTANCE_ID=%s", instance),
|
||||
fmt.Sprintf("GARM_PROVIDER_CONFIG_FILE=%s", e.cfg.External.ConfigFile),
|
||||
}
|
||||
|
|
@ -115,6 +116,7 @@ func (e *external) DeleteInstance(ctx context.Context, instance string) error {
|
|||
func (e *external) GetInstance(ctx context.Context, instance string) (params.Instance, error) {
|
||||
asEnv := []string{
|
||||
fmt.Sprintf("GARM_COMMAND=%s", execution.GetInstanceCommand),
|
||||
fmt.Sprintf("GARM_CONTROLLER_ID=%s", e.controllerID),
|
||||
fmt.Sprintf("GARM_INSTANCE_ID=%s", instance),
|
||||
fmt.Sprintf("GARM_PROVIDER_CONFIG_FILE=%s", e.cfg.External.ConfigFile),
|
||||
}
|
||||
|
|
@ -137,6 +139,7 @@ func (e *external) GetInstance(ctx context.Context, instance string) (params.Ins
|
|||
func (e *external) ListInstances(ctx context.Context, poolID string) ([]params.Instance, error) {
|
||||
asEnv := []string{
|
||||
fmt.Sprintf("GARM_COMMAND=%s", execution.ListInstancesCommand),
|
||||
fmt.Sprintf("GARM_CONTROLLER_ID=%s", e.controllerID),
|
||||
fmt.Sprintf("GARM_POOL_ID=%s", poolID),
|
||||
fmt.Sprintf("GARM_PROVIDER_CONFIG_FILE=%s", e.cfg.External.ConfigFile),
|
||||
}
|
||||
|
|
@ -171,6 +174,7 @@ func (e *external) RemoveAllInstances(ctx context.Context) error {
|
|||
func (e *external) Stop(ctx context.Context, instance string, force bool) error {
|
||||
asEnv := []string{
|
||||
fmt.Sprintf("GARM_COMMAND=%s", execution.StopInstanceCommand),
|
||||
fmt.Sprintf("GARM_CONTROLLER_ID=%s", e.controllerID),
|
||||
fmt.Sprintf("GARM_INSTANCE_ID=%s", instance),
|
||||
fmt.Sprintf("GARM_PROVIDER_CONFIG_FILE=%s", e.cfg.External.ConfigFile),
|
||||
}
|
||||
|
|
@ -185,6 +189,7 @@ func (e *external) Stop(ctx context.Context, instance string, force bool) error
|
|||
func (e *external) Start(ctx context.Context, instance string) error {
|
||||
asEnv := []string{
|
||||
fmt.Sprintf("GARM_COMMAND=%s", execution.StartInstanceCommand),
|
||||
fmt.Sprintf("GARM_CONTROLLER_ID=%s", e.controllerID),
|
||||
fmt.Sprintf("GARM_INSTANCE_ID=%s", instance),
|
||||
fmt.Sprintf("GARM_PROVIDER_CONFIG_FILE=%s", e.cfg.External.ConfigFile),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue