|
| 1 | +/* |
| 2 | + * Copyright © 2019 – 2020 Red Hat Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +/* |
| 20 | + import ( |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "strings" |
| 25 | +
|
| 26 | + "github.com/containers/toolbox/pkg/shell" |
| 27 | + "github.com/containers/toolbox/pkg/utils" |
| 28 | + "github.com/sirupsen/logrus" |
| 29 | + "github.com/spf13/cobra" |
| 30 | + ) |
| 31 | +
|
| 32 | + var ( |
| 33 | + forbiddenBinaryShims = []string{ |
| 34 | + "toolbox", |
| 35 | + } |
| 36 | + ) |
| 37 | +
|
| 38 | + var createShimCmd = &cobra.Command{ |
| 39 | + Use: "create-shim", |
| 40 | + Short: "Create a binary shim for a command to be run on the host", |
| 41 | + Hidden: true, |
| 42 | + Example: "asdasdasd", |
| 43 | + Args: cobra.ExactArgs(1), |
| 44 | + RunE: createShim, |
| 45 | + } |
| 46 | +
|
| 47 | + func init() { |
| 48 | + // flags := initContainerCmd.Flags() |
| 49 | +
|
| 50 | + createShimCmd.SetHelpFunc(createShimHelp) |
| 51 | + rootCmd.AddCommand(createShimCmd) |
| 52 | + } |
| 53 | +
|
| 54 | + func createShim(cmd *cobra.Command, args []string) error { |
| 55 | + if !utils.IsInsideContainer() { |
| 56 | + var builder strings.Builder |
| 57 | + fmt.Fprintf(&builder, "the 'create-shim' command can only be used inside containers\n") |
| 58 | + fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase) |
| 59 | +
|
| 60 | + errMsg := builder.String() |
| 61 | + return errors.New(errMsg) |
| 62 | + } |
| 63 | +
|
| 64 | + shimBinary := fmt.Sprintf("/usr/libexec/toolbox/%s", args[0]) |
| 65 | +
|
| 66 | + if !utils.PathExists(hostRunnerShim.containerPath) || !utils.PathExists(sudoShim.containerPath) { |
| 67 | + var builder strings.Builder |
| 68 | + fmt.Fprintf(&builder, "This toolbox container is not set up for creating binary shims\n") |
| 69 | + fmt.Fprintf(&builder, "You're possibly trying to use a newer Toolbox in a container created with an older version\n") |
| 70 | + fmt.Fprintf(&builder, "Try to update the Toolbox binary used by this container") |
| 71 | +
|
| 72 | + errMsg := builder.String() |
| 73 | + return errors.New(errMsg) |
| 74 | + } |
| 75 | +
|
| 76 | + if utils.PathExists(shimBinary) { |
| 77 | + fmt.Printf("The requested shim binary already exists.\n") |
| 78 | + return nil |
| 79 | + } |
| 80 | +
|
| 81 | + err := redirectPath(shimBinary, hostRunnerShim.containerPath, false) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("Failed to create shim binary %s: %w", shimBinary, err) |
| 84 | + } |
| 85 | +
|
| 86 | + return nil |
| 87 | + } |
| 88 | +
|
| 89 | + func createShimHelp(cmd *cobra.Command, args []string) { |
| 90 | + if utils.IsInsideContainer() { |
| 91 | + if !utils.IsInsideToolboxContainer() { |
| 92 | + fmt.Fprintf(os.Stderr, "Error: this is not a toolbox container\n") |
| 93 | + return |
| 94 | + } |
| 95 | +
|
| 96 | + if _, err := utils.ForwardToHost(); err != nil { |
| 97 | + fmt.Fprintf(os.Stderr, "Error: %s\n", err) |
| 98 | + return |
| 99 | + } |
| 100 | +
|
| 101 | + return |
| 102 | + } |
| 103 | +
|
| 104 | + if err := utils.ShowManual("toolbox-create-shim"); err != nil { |
| 105 | + fmt.Fprintf(os.Stderr, "Error: %s\n", err) |
| 106 | + return |
| 107 | + } |
| 108 | + } |
| 109 | +
|
| 110 | + func runOnHost(command string, commandLineArgs []string) (int, error) { |
| 111 | + envOptions := utils.GetEnvOptionsForPreservedVariables() |
| 112 | +
|
| 113 | + var flatpakSpawnArgs []string |
| 114 | +
|
| 115 | + flatpakSpawnArgs = append(flatpakSpawnArgs, envOptions...) |
| 116 | +
|
| 117 | + flatpakSpawnArgs = append(flatpakSpawnArgs, []string{ |
| 118 | + "--host", |
| 119 | + command, |
| 120 | + }...) |
| 121 | +
|
| 122 | + flatpakSpawnArgs = append(flatpakSpawnArgs, commandLineArgs...) |
| 123 | +
|
| 124 | + logrus.Debug("Forwarding to host:") |
| 125 | + logrus.Debugf("%s", command) |
| 126 | + for _, arg := range commandLineArgs { |
| 127 | + logrus.Debugf("%s", arg) |
| 128 | + } |
| 129 | +
|
| 130 | + exitCode, err := shell.RunWithExitCode("flatpak-spawn", os.Stdin, os.Stdout, nil, flatpakSpawnArgs...) |
| 131 | + if err != nil { |
| 132 | + return exitCode, err |
| 133 | + } |
| 134 | +
|
| 135 | + return exitCode, nil |
| 136 | + } |
| 137 | +*/ |
0 commit comments