|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package advanced |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/microsoft/azldev/internal/app/azldev" |
| 11 | + "github.com/microsoft/azldev/internal/app/azldev/core/buildenvfactory" |
| 12 | + "github.com/microsoft/azldev/internal/buildenv" |
| 13 | + "github.com/microsoft/azldev/internal/rpm/mock" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +func mockOnAppInit(_ *azldev.App, parentCmd *cobra.Command) { |
| 18 | + parentCmd.AddCommand(NewMockCmd()) |
| 19 | +} |
| 20 | + |
| 21 | +// Constructs a [cobra.Command] for the "mock" subcommand hierarchy. |
| 22 | +func NewMockCmd() *cobra.Command { |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "mock", |
| 25 | + Short: "Run RPM mock tool", |
| 26 | + } |
| 27 | + |
| 28 | + cmd.AddCommand(NewBuildRPMCmd()) |
| 29 | + cmd.AddCommand(NewShellCmd()) |
| 30 | + |
| 31 | + return cmd |
| 32 | +} |
| 33 | + |
| 34 | +// Options controlling how to run mock commands. |
| 35 | +type MockCmdOptions struct { |
| 36 | + // Path to the .cfg file to use with mock. |
| 37 | + MockConfigPath string |
| 38 | +} |
| 39 | + |
| 40 | +// Options controlling how to build an RPM from a source RPM. |
| 41 | +type BuildRPMOptions struct { |
| 42 | + // Common mock options. |
| 43 | + MockCmdOptions |
| 44 | + |
| 45 | + // Path to the SRPM to build. |
| 46 | + SRPMPath string |
| 47 | + // Path to the output directory for final RPM files. |
| 48 | + OutputDirPath string |
| 49 | +} |
| 50 | + |
| 51 | +// Constructs a [cobra.Command] for the "mock build-rpms" subcommand. |
| 52 | +func NewBuildRPMCmd() *cobra.Command { |
| 53 | + options := &BuildRPMOptions{} |
| 54 | + |
| 55 | + // We don't *require* a valid project configuration, but may use one if it's available. |
| 56 | + cmd := &cobra.Command{ |
| 57 | + Use: "build-rpms", |
| 58 | + Short: "Use mock to build an RPM", |
| 59 | + RunE: azldev.RunFuncWithoutRequiredConfig(func(env *azldev.Env) (results interface{}, err error) { |
| 60 | + return BuildRPMS(env, options) |
| 61 | + }), |
| 62 | + } |
| 63 | + |
| 64 | + cmd.Flags().StringVarP(&options.MockConfigPath, "config", "c", "", "Path to the mock .cfg file") |
| 65 | + cmd.Flags().StringVar(&options.SRPMPath, "srpm", "", "Path to the SRPM to build") |
| 66 | + cmd.Flags().StringVarP(&options.OutputDirPath, "output-dir", "o", "", "Path to output directory") |
| 67 | + |
| 68 | + _ = cmd.MarkFlagRequired("srpm") |
| 69 | + _ = cmd.MarkFlagRequired("output-dir") |
| 70 | + |
| 71 | + return cmd |
| 72 | +} |
| 73 | + |
| 74 | +// Options controlling how to run a shell command in a mock environment. |
| 75 | +type ShellOptions struct { |
| 76 | + // Common mock options. |
| 77 | + MockCmdOptions |
| 78 | + |
| 79 | + // Whether or not to enable external network access from within the mock root the shell is executed in. |
| 80 | + EnableNetwork bool |
| 81 | +} |
| 82 | + |
| 83 | +// Constructs a [cobra.Command] for the 'mock shell' command. |
| 84 | +func NewShellCmd() *cobra.Command { |
| 85 | + options := &ShellOptions{} |
| 86 | + |
| 87 | + // We don't *require* a valid project configuration, but may use one if it's available. |
| 88 | + cmd := &cobra.Command{ |
| 89 | + Use: "shell", |
| 90 | + Short: "Enter mock shell", |
| 91 | + RunE: azldev.RunFuncWithoutRequiredConfigWithExtraArgs( |
| 92 | + func(env *azldev.Env, extraArgs []string) (results interface{}, err error) { |
| 93 | + return true, RunShell(env, options, extraArgs) |
| 94 | + }, |
| 95 | + ), |
| 96 | + } |
| 97 | + |
| 98 | + cmd.Flags().StringVarP(&options.MockConfigPath, "config", "c", "", "Path to the mock .cfg file") |
| 99 | + cmd.Flags().BoolVar(&options.EnableNetwork, "enable-network", false, "Enable network access in the mock root") |
| 100 | + |
| 101 | + return cmd |
| 102 | +} |
| 103 | + |
| 104 | +// Builds RPMs from sources, using options. |
| 105 | +func BuildRPMS(env *azldev.Env, options *BuildRPMOptions) (results interface{}, err error) { |
| 106 | + runner, err := makeMockRunner(env, &options.MockCmdOptions) |
| 107 | + if err != nil { |
| 108 | + return results, err |
| 109 | + } |
| 110 | + |
| 111 | + buildOptions := mock.RPMBuildOptions{} |
| 112 | + |
| 113 | + evt := env.StartEvent("Building RPM with mock", |
| 114 | + "srpmPath", options.SRPMPath, "outputDirPath", options.OutputDirPath) |
| 115 | + |
| 116 | + defer evt.End() |
| 117 | + |
| 118 | + // Build! |
| 119 | + err = runner.BuildRPM(env, options.SRPMPath, options.OutputDirPath, buildOptions) |
| 120 | + if err != nil { |
| 121 | + return results, fmt.Errorf("failed to build RPM: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + return true, nil |
| 125 | +} |
| 126 | + |
| 127 | +// Executes an interactive shell within a mock root. Uses the provided [ShellOptions] to configure the shell. |
| 128 | +func RunShell(env *azldev.Env, options *ShellOptions, extraArgs []string) error { |
| 129 | + runner, err := makeMockRunner(env, &options.MockCmdOptions) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + if options.EnableNetwork { |
| 135 | + runner.EnableNetwork() |
| 136 | + } |
| 137 | + |
| 138 | + cmd, err := runner.CmdInChroot(env, extraArgs, true /*interactive*/) |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("failed to create shell command: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + cmd.SetStdin(os.Stdin) |
| 144 | + cmd.SetStdout(os.Stdout) |
| 145 | + cmd.SetStderr(os.Stderr) |
| 146 | + |
| 147 | + err = cmd.Run(env) |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("failed to run shell: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func makeMockRunner(env *azldev.Env, options *MockCmdOptions) (runner *mock.Runner, err error) { |
| 156 | + // If we have an explicit config path, then use it. |
| 157 | + if options.MockConfigPath != "" { |
| 158 | + return mock.NewRunner(env, options.MockConfigPath), nil |
| 159 | + } |
| 160 | + |
| 161 | + // Otherwise, try to find the right one for this environment. |
| 162 | + factory, err := buildenvfactory.NewMockRootFactoryForEnv(env) |
| 163 | + if err != nil { |
| 164 | + return nil, fmt.Errorf("failed to create mock root factory: %w", err) |
| 165 | + } |
| 166 | + |
| 167 | + root, err := factory.CreateMockRoot(buildenv.CreateOptions{}) |
| 168 | + if err != nil { |
| 169 | + return nil, fmt.Errorf("failed to create mock root: %w", err) |
| 170 | + } |
| 171 | + |
| 172 | + return root.GetRunner(), nil |
| 173 | +} |
0 commit comments