Skip to content

Commit

Permalink
create PTF command
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Apr 22, 2024
1 parent 7180f93 commit c3a7514
Show file tree
Hide file tree
Showing 42 changed files with 1,424 additions and 503 deletions.
74 changes: 1 addition & 73 deletions mgradm/cmd/inspect/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ package inspect
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -45,7 +42,7 @@ func kuberneteInspect(
}
}

inspectResult, err := InspectKubernetes(serverImage, flags.PullPolicy)
inspectResult, err := shared_kubernetes.InspectKubernetes(serverImage, flags.PullPolicy)
if err != nil {
return fmt.Errorf(L("inspect command failed: %s"), err)
}
Expand All @@ -60,72 +57,3 @@ func kuberneteInspect(

return nil
}

// InspectKubernetes check values on a given image and deploy.
func InspectKubernetes(serverImage string, pullPolicy string) (map[string]string, error) {
for _, binary := range []string{"kubectl", "helm"} {
if _, err := exec.LookPath(binary); err != nil {
return map[string]string{}, fmt.Errorf(L("install %s before running this command"), binary)
}
}

scriptDir, err := os.MkdirTemp("", "mgradm-*")
defer os.RemoveAll(scriptDir)
if err != nil {
return map[string]string{}, fmt.Errorf(L("failed to create temporary directory: %s"), err)
}

if err := adm_utils.GenerateInspectContainerScript(scriptDir); err != nil {
return map[string]string{}, err
}

command := path.Join(adm_utils.InspectOutputFile.Directory, adm_utils.InspectScriptFilename)

const podName = "inspector"

//delete pending pod and then check the node, because in presence of more than a pod GetNode return is wrong
if err := shared_kubernetes.DeletePod(podName, shared_kubernetes.ServerFilter); err != nil {
return map[string]string{}, fmt.Errorf(L("cannot delete %s: %s"), podName, err)
}

//this is needed because folder with script needs to be mounted
nodeName, err := shared_kubernetes.GetNode("uyuni")
if err != nil {
return map[string]string{}, fmt.Errorf(L("cannot find node running uyuni: %s"), err)
}

//generate deploy data
deployData := types.Deployment{
APIVersion: "v1",
Spec: &types.Spec{
RestartPolicy: "Never",
NodeName: nodeName,
Containers: []types.Container{
{
Name: podName,
VolumeMounts: append(utils.PgsqlRequiredVolumeMounts,
types.VolumeMount{MountPath: "/var/lib/uyuni-tools", Name: "var-lib-uyuni-tools"}),
Image: serverImage,
},
},
Volumes: append(utils.PgsqlRequiredVolumes,
types.Volume{Name: "var-lib-uyuni-tools", HostPath: &types.HostPath{Path: scriptDir, Type: "Directory"}}),
},
}
//transform deploy data in JSON
override, err := shared_kubernetes.GenerateOverrideDeployment(deployData)
if err != nil {
return map[string]string{}, err
}
err = shared_kubernetes.RunPod(podName, shared_kubernetes.ServerFilter, serverImage, pullPolicy, command, override)
if err != nil {
return map[string]string{}, fmt.Errorf(L("cannot run inspect pod: %s"), err)
}

inspectResult, err := adm_utils.ReadInspectData(scriptDir)
if err != nil {
return map[string]string{}, fmt.Errorf(L("cannot inspect data: %s"), err)
}

return inspectResult, err
}
52 changes: 1 addition & 51 deletions mgradm/cmd/inspect/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ package inspect
import (
"encoding/json"
"fmt"
"os"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/podman"
adm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
"github.com/uyuni-project/uyuni-tools/shared"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
Expand Down Expand Up @@ -40,7 +38,7 @@ func podmanInspect(
return fmt.Errorf(L("failed to find the image of the currently running server container: %s"))
}
}
inspectResult, err := InspectPodman(serverImage, flags.PullPolicy)
inspectResult, err := shared_podman.Inspect(serverImage, flags.PullPolicy)
if err != nil {
return fmt.Errorf(L("inspect command failed: %s"), err)
}
Expand All @@ -54,51 +52,3 @@ func podmanInspect(

return nil
}

// InspectPodman check values on a given image and deploy.
func InspectPodman(serverImage string, pullPolicy string) (map[string]string, error) {
scriptDir, err := os.MkdirTemp("", "mgradm-*")
defer os.RemoveAll(scriptDir)
if err != nil {
return map[string]string{}, fmt.Errorf(L("failed to create temporary directory: %s"), err)
}

inspectedHostValues, err := adm_utils.InspectHost()
if err != nil {
return map[string]string{}, fmt.Errorf(L("cannot inspect host values: %s"), err)
}

pullArgs := []string{}
_, scc_user_exist := inspectedHostValues["host_scc_username"]
_, scc_user_password := inspectedHostValues["host_scc_password"]
if scc_user_exist && scc_user_password {
pullArgs = append(pullArgs, "--creds", inspectedHostValues["host_scc_username"]+":"+inspectedHostValues["host_scc_password"])
}

preparedImage, err := shared_podman.PrepareImage(serverImage, pullPolicy, pullArgs...)
if err != nil {
return map[string]string{}, err
}

if err := adm_utils.GenerateInspectContainerScript(scriptDir); err != nil {
return map[string]string{}, err
}

podmanArgs := []string{
"-v", scriptDir + ":" + adm_utils.InspectOutputFile.Directory,
"--security-opt", "label:disable",
}

err = podman.RunContainer("uyuni-inspect", preparedImage, podmanArgs,
[]string{adm_utils.InspectOutputFile.Directory + "/" + adm_utils.InspectScriptFilename})
if err != nil {
return map[string]string{}, err
}

inspectResult, err := adm_utils.ReadInspectData(scriptDir)
if err != nil {
return map[string]string{}, fmt.Errorf(L("cannot inspect data: %s"), err)
}

return inspectResult, err
}
3 changes: 1 addition & 2 deletions mgradm/cmd/install/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/spf13/cobra"
install_shared "github.com/uyuni-project/uyuni-tools/mgradm/cmd/install/shared"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/podman"
adm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
"github.com/uyuni-project/uyuni-tools/shared"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
shared_podman "github.com/uyuni-project/uyuni-tools/shared/podman"
Expand Down Expand Up @@ -52,7 +51,7 @@ func installForPodman(
return errors.New(L("install podman before running this command"))
}

inspectedHostValues, err := adm_utils.InspectHost()
inspectedHostValues, err := utils.InspectHost()
if err != nil {
return fmt.Errorf(L("cannot inspect host values: %s"), err)
}
Expand Down
50 changes: 50 additions & 0 deletions mgradm/cmd/support/ptf/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

//go:build !nok8s

package kubernetes

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/install/shared"
cmd_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

type kubernetesInstallFlags struct {
shared.InstallFlags `mapstructure:",squash"`
Helm cmd_utils.HelmFlags
}

// NewCommand for kubernetes installation.
func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
kubernetesCmd := &cobra.Command{
Use: "kubernetes",
Short: L("Install a PTF or Test package on a kubernetes cluster"),
Long: L(`Install a PTR of Test package on a kubernetes cluster
The support ptf command assumes the following:
* kubectl and helm are installed locally
* a working kubectl configuration should be set to connect to the cluster to deploy to
The helm values file will be overridden with the values from the command parameters or configuration.
NOTE: installing on a remote cluster is not supported yet!
`),

Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var flags kubernetesInstallFlags
return utils.CommandHelper(globalFlags, cmd, args, &flags, ptfForKubernetes)
},
}

shared.AddInstallFlags(kubernetesCmd)
cmd_utils.AddHelmInstallFlag(kubernetesCmd)

return kubernetesCmd
}
16 changes: 16 additions & 0 deletions mgradm/cmd/support/ptf/kubernetes/nobuild.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2023 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

//go:build nok8s

package kubernetes

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared/types"
)

func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
return nil
}
23 changes: 23 additions & 0 deletions mgradm/cmd/support/ptf/kubernetes/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

//go:build !nok8s

package kubernetes

import (
"errors"

"github.com/spf13/cobra"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
)

func ptfForKubernetes(globalFlags *types.GlobalFlags,
flags *kubernetesInstallFlags,
cmd *cobra.Command,
args []string,
) error {
return errors.New(L("PTF command for kubernetes is not implemented yet"))
}
44 changes: 44 additions & 0 deletions mgradm/cmd/support/ptf/podman/podman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package podman

import (
"github.com/spf13/cobra"
mgradm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

type podmanPTFFlags struct {
Image types.ImageFlags `mapstructure:",squash"`
PTFId string `mapstructure:"ptf"`
TestId string `mapstructure:"ptf"`
}

// NewCommand for podman installation.
func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
podmanCmd := &cobra.Command{
Use: "podman",

Short: L("install a PTF or Test package on podman"),
Long: L(`Install a PTF or Test package on podman
The support ptf podman command assumes podman is installed locally and
the host machine is register to SCC.
NOTE: for now installing on a remote podman is not supported!
`),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var flags podmanPTFFlags
return utils.CommandHelper(globalFlags, cmd, args, &flags, ptfForPodman)
},
}

mgradm_utils.AddImageFlag(podmanCmd)

return podmanCmd
}
23 changes: 23 additions & 0 deletions mgradm/cmd/support/ptf/podman/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package podman

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
)

func ptfForPodman(
globalFlags *types.GlobalFlags,
flags *podmanPTFFlags,
cmd *cobra.Command,
args []string,
) error {
//we don't want to perform a postgres version upgrade when installing a PTF.
//in that case, we can use the upgrade command.
dummyMigration := types.ImageFlags{}
return podman.Upgrade(flags.Image, dummyMigration, args)
}
32 changes: 32 additions & 0 deletions mgradm/cmd/support/ptf/ptf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package ptf

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/support/ptf/kubernetes"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/support/ptf/podman"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

// NewCommand is the command for creates supportptf.
func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
ptfCmd := &cobra.Command{
Use: "ptf",
Short: L("install a PTF"),
}

utils.AddBackendFlag(ptfCmd)

ptfCmd.AddCommand(podman.NewCommand(globalFlags))

if kubernetesCmd := kubernetes.NewCommand(globalFlags); kubernetesCmd != nil {
ptfCmd.AddCommand(kubernetesCmd)
}

return ptfCmd
}
2 changes: 2 additions & 0 deletions mgradm/cmd/support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package support
import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/support/config"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/support/ptf"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
)
Expand All @@ -19,6 +20,7 @@ func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
Long: L("Commands for support operations"),
}
supportCmd.AddCommand(config.NewCommand(globalFlags))
supportCmd.AddCommand(ptf.NewCommand(globalFlags))

return supportCmd
}
Loading

0 comments on commit c3a7514

Please sign in to comment.