Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add upgrade and support ptf command #249

Merged
merged 15 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ jobs:
-o ./bin \
./...
- name: Build nok8s
- name: Build with all tags
run: |
mkdir -p ./bin
go build \
-tags netgo,nok8s \
-tags netgo,nok8s,ptf \
-ldflags "-X github.com/uyuni-project/uyuni-tools/shared/utils.Version=${{ env.VERSION }}" \
-o ./bin \
./...
Expand Down
8 changes: 6 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
set -e
mkdir -p ./bin

build_tags=$1
if [ -n "${build_tags}" ]; then
build_tags="-tags ${build_tags}"
fi

tag=$(git describe --tags --abbrev=0)
version=$(git describe --tags --abbrev=0 | cut -f 3 -d '-')
offset=$(git rev-list --count ${tag}..)
commit_id=$(git rev-parse --short HEAD)

VERSION_NAME=github.com/uyuni-project/uyuni-tools/shared/utils.Version

CGO_ENABLED=0 go build -ldflags "-X \"${VERSION_NAME}=${version}-${offset} (${commit_id})\"" -o ./bin ./...
CGO_ENABLED=0 go build ${build_tags} -ldflags "-X ${VERSION_NAME}=${tag}-${offset}" -o ./bin ./...

for shell in "bash" "zsh" "fish"; do
COMPLETION_FILE="./bin/completion.${shell}"
Expand Down
22 changes: 20 additions & 2 deletions check_localizable
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# SPDX-License-Identifier: Apache-2.0

res=0
grep -r . --include '*.go' --exclude '*_test.go' -n \
-e 'fmt\.Errorf("[^"]\+"' \
-e 'errors.New("[^"]\+"' \
Expand All @@ -12,6 +13,23 @@ grep -r . --include '*.go' --exclude '*_test.go' -n \
-e '\(Short\|Long\): \+["`]'

if test $? -eq 0; then
echo "Fix the non localizable strings"
exit 1
echo -e "Fix the non localizable strings\n"
res=1
fi

grep -r . --include '*.go' --exclude '*_test.go' -n \
-e '\(Trace\|Debug\)()\(\.Err(err)\)\?\.Msgf\?(N\?L("' \

if test $? -eq 0; then
echo -e "Trace and debug messages shouldn't be localizable\n"
res=1
fi

grep -r . --include '*.go' --exclude '*_test.go' -n \
-e '\(Short\|Long\): \+L(["`][a-z]'
if test $? -eq 0; then
echo -e "Short and Long messages shouldn't start with a lowercase letter\n"
res=1
fi

exit $res
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 @@ -78,7 +77,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
16 changes: 16 additions & 0 deletions mgradm/cmd/support/ptf/noptf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0
//go:build !ptf

package ptf

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

// NewCommand is the command for creates supportptf.
func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
return nil
}
47 changes: 47 additions & 0 deletions mgradm/cmd/support/ptf/podman/podman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0
//go:build ptf

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:"test"`
CustomerId string `mapstructure:"user"`
}

// 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.AddImagePTFlag(podmanCmd)
utils.AddPTFFlag(podmanCmd)

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

package podman

import (
"errors"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/podman"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
podman_shared "github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

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{}
if err := flags.checkParameters(); err != nil {
return err
}
return podman.Upgrade(flags.Image, dummyMigration, args)
}

func (flags *podmanPTFFlags) checkParameters() error {
if flags.TestId != "" && flags.PTFId != "" {
return errors.New(L("ptf and test flags cannot be set simultaneously "))
}
if flags.TestId == "" && flags.PTFId == "" {
return errors.New(L("ptf and test flags cannot be empty simultaneously "))
}
if flags.CustomerId == "" {
return errors.New(L("user flag cannot be empty"))
}
serverImage, err := podman_shared.GetRunningImage(podman_shared.ServerContainerName)
if err != nil {
return err
}

suffix := "ptf"
if flags.TestId != "" {
suffix = "test"
}
flags.Image.Name, err = utils.ComputePTF(flags.CustomerId, flags.PTFId, serverImage, suffix)
if err != nil {
return err
}
log.Info().Msgf(L("The image computed is: %s"), flags.Image.Name)
return nil
}
Loading