-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from fabi200123/version-v3-test
Update garm-provider-common
- Loading branch information
Showing
16 changed files
with
855 additions
and
243 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,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 | ||
} |
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,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) | ||
} | ||
} |
Oops, something went wrong.