Skip to content

Commit

Permalink
Merge pull request #1 from fabi200123/version-v3-test
Browse files Browse the repository at this point in the history
Update garm-provider-common
  • Loading branch information
fabi200123 authored Aug 9, 2024
2 parents fdfe819 + 5f6acef commit bb1df97
Show file tree
Hide file tree
Showing 16 changed files with 855 additions and 243 deletions.
91 changes: 91 additions & 0 deletions execution/common/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2023 Cloudbase Solutions SRL
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package common

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"

gErrors "github.com/cloudbase/garm-provider-common/errors"
"github.com/cloudbase/garm-provider-common/params"
"github.com/mattn/go-isatty"
)

type ExecutionCommand string

const (
CreateInstanceCommand ExecutionCommand = "CreateInstance"
DeleteInstanceCommand ExecutionCommand = "DeleteInstance"
GetInstanceCommand ExecutionCommand = "GetInstance"
ListInstancesCommand ExecutionCommand = "ListInstances"
StartInstanceCommand ExecutionCommand = "StartInstance"
StopInstanceCommand ExecutionCommand = "StopInstance"
RemoveAllInstancesCommand ExecutionCommand = "RemoveAllInstances"
GetVersionCommand ExecutionCommand = "GetVersion"
)

const (
// ExitCodeNotFound is an exit code that indicates a Not Found error
ExitCodeNotFound int = 30
// ExitCodeDuplicate is an exit code that indicates a duplicate error
ExitCodeDuplicate int = 31
)

func GetBoostrapParamsFromStdin(c ExecutionCommand) (params.BootstrapInstance, error) {
var bootstrapParams params.BootstrapInstance
if c == CreateInstanceCommand {
if isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()) {
return params.BootstrapInstance{}, fmt.Errorf("%s requires data passed into stdin", CreateInstanceCommand)
}

var data bytes.Buffer
if _, err := io.Copy(&data, os.Stdin); err != nil {
return params.BootstrapInstance{}, fmt.Errorf("failed to copy bootstrap params")
}

if data.Len() == 0 {
return params.BootstrapInstance{}, fmt.Errorf("%s requires data passed into stdin", CreateInstanceCommand)
}

if err := json.Unmarshal(data.Bytes(), &bootstrapParams); err != nil {
return params.BootstrapInstance{}, fmt.Errorf("failed to decode instance params: %w", err)
}
if bootstrapParams.ExtraSpecs == nil {
// Initialize ExtraSpecs as an empty JSON object
bootstrapParams.ExtraSpecs = json.RawMessage([]byte("{}"))
}

return bootstrapParams, nil
}

// If the command is not CreateInstance, we don't need to read from stdin
return params.BootstrapInstance{}, nil
}

func ResolveErrorToExitCode(err error) int {
if err != nil {
if errors.Is(err, gErrors.ErrNotFound) {
return ExitCodeNotFound
} else if errors.Is(err, gErrors.ErrDuplicateEntity) {
return ExitCodeDuplicate
}
return 1
}
return 0
}
15 changes: 5 additions & 10 deletions execution/v0.1.0/commands.go → execution/common/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
// License for the specific language governing permissions and limitations
// under the License.

package execution

type ExecutionCommand string
package common

const (
CreateInstanceCommand ExecutionCommand = "CreateInstance"
DeleteInstanceCommand ExecutionCommand = "DeleteInstance"
GetInstanceCommand ExecutionCommand = "GetInstance"
ListInstancesCommand ExecutionCommand = "ListInstances"
StartInstanceCommand ExecutionCommand = "StartInstance"
StopInstanceCommand ExecutionCommand = "StopInstance"
RemoveAllInstancesCommand ExecutionCommand = "RemoveAllInstances"
// Version v0.1.0
Version010 = "v0.1.0"
// Version v0.1.1
Version011 = "v0.1.1"
)
80 changes: 80 additions & 0 deletions execution/execution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2023 Cloudbase Solutions SRL
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package execution

import (
"context"
"fmt"
"os"

"github.com/cloudbase/garm-provider-common/execution/common"
executionv010 "github.com/cloudbase/garm-provider-common/execution/v0.1.0"
executionv011 "github.com/cloudbase/garm-provider-common/execution/v0.1.1"
)

type ExternalProvider interface {
executionv010.ExternalProvider
executionv011.ExternalProvider
}

type Environment struct {
EnvironmentV010 executionv010.EnvironmentV010
EnvironmentV011 executionv011.EnvironmentV011
InterfaceVersion string
ProviderConfigFile string
ControllerID string
}

func GetEnvironment() (Environment, error) {
interfaceVersion := os.Getenv("GARM_INTERFACE_VERSION")

switch interfaceVersion {
case common.Version010:
env, err := executionv010.GetEnvironment()
if err != nil {
return Environment{}, err
}
return Environment{
EnvironmentV010: env,
ProviderConfigFile: env.ProviderConfigFile,
ControllerID: env.ControllerID,
InterfaceVersion: interfaceVersion,
}, nil
case common.Version011:
env, err := executionv011.GetEnvironment()
if err != nil {
return Environment{}, err
}
return Environment{
EnvironmentV011: env,
ProviderConfigFile: env.ProviderConfigFile,
ControllerID: env.ControllerID,
InterfaceVersion: interfaceVersion,
}, nil
default:
return Environment{}, fmt.Errorf("unsupported interface version: %s", interfaceVersion)
}
}

func Run(ctx context.Context, provider ExternalProvider, env Environment) (string, error) {
switch env.InterfaceVersion {
case common.Version010:
return executionv010.Run(ctx, provider, env.EnvironmentV010)
case common.Version011:
return executionv011.Run(ctx, provider, env.EnvironmentV011)
default:
return "", fmt.Errorf("unsupported interface version: %s", env.InterfaceVersion)
}
}
Loading

0 comments on commit bb1df97

Please sign in to comment.