|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +// Package providerplugin implements calling the provider specific csctl plugin. |
| 15 | +package providerplugin |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "os/exec" |
| 21 | + "path/filepath" |
| 22 | + |
| 23 | + "github.com/SovereignCloudStack/csctl/pkg/clusterstack" |
| 24 | +) |
| 25 | + |
| 26 | +// GetProviderExecutable returns the path to the provider plugin (like "csctl-docker"). |
| 27 | +// If there is not "config" for the provider in csctl.yaml, then needed is false and path is the empty string. |
| 28 | +func GetProviderExecutable(config *clusterstack.CsctlConfig) (needed bool, path string, err error) { |
| 29 | + if config.Config.Provider.Config.Method == "" { |
| 30 | + return false, "", nil |
| 31 | + } |
| 32 | + pluginName := "csctl-" + config.Config.Provider.Type |
| 33 | + _, err = os.Stat(pluginName) |
| 34 | + if err == nil { |
| 35 | + path, err := filepath.Abs(pluginName) |
| 36 | + if err != nil { |
| 37 | + return false, "", fmt.Errorf("failed to find a path: %w", err) |
| 38 | + } |
| 39 | + return true, path, nil |
| 40 | + } |
| 41 | + path, err = exec.LookPath(pluginName) |
| 42 | + if err != nil { |
| 43 | + return false, "", fmt.Errorf("could not find plugin %s in $PATH or current working directory", pluginName) |
| 44 | + } |
| 45 | + return true, path, nil |
| 46 | +} |
| 47 | + |
| 48 | +// CreateNodeImages calls the provider plugin command to create nodes images. |
| 49 | +func CreateNodeImages(config *clusterstack.CsctlConfig, clusterStackPath, clusterStackReleaseDir string) error { |
| 50 | + needed, path, err := GetProviderExecutable(config) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + if !needed { |
| 55 | + fmt.Printf("No provider specific configuration in csctl.yaml. No need to call a plugin for provider %q\n", config.Config.Provider.Type) |
| 56 | + return nil |
| 57 | + } |
| 58 | + args := []string{"create-node-images", clusterStackPath, clusterStackReleaseDir} |
| 59 | + fmt.Printf("Calling Provider Plugin: %s\n", path) |
| 60 | + cmd := exec.Command(path, args...) // #nosec G204 |
| 61 | + cmd.Stdout = os.Stdout |
| 62 | + cmd.Stderr = os.Stderr |
| 63 | + |
| 64 | + if err := cmd.Run(); err != nil { |
| 65 | + return fmt.Errorf("error executing command: %w", err) |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
0 commit comments