Skip to content
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
19 changes: 2 additions & 17 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ func runImport(cmd *cobra.Command, args []string) {
}

func getImportVersionProduct(path string) (string, string) {
var versionfile string
versionfile = path + "/version.txt"
versionfile := path + "/version.txt"
version, err := utils.ScannerFunc(versionfile, "version")
if err != nil {
log.Error().Msg("Version not found.")
}
product, err := utils.ScannerFunc(versionfile, "product_name")
if err != nil {
log.Fatal().Msg("Product not found")
log.Error().Msg("Product not found")
}
log.Debug().Msgf("Import Product: %s; Version: %s", product, version)
return version, product
Expand Down Expand Up @@ -195,20 +194,6 @@ func runImageFileSync(absImportDir string, serverConfig string) {
if err != nil {
log.Fatal().Err(err).Msg("Error importing image files")
}

pillarImportDir := path.Join(absImportDir, "images", "pillars")
err = utils.FolderExists(pillarImportDir)
if err != nil {
if os.IsNotExist(err) {
log.Debug().Msg("No pillar files to import")
return
} else {
log.Fatal().Err(err).Msg("Error reading import folder for pillars")
}
}

log.Info().Msg("Copying image pillar files")
pillarDumper.ImportImagePillars(pillarImportDir, utils.GetCurrentServerFQDN(serverConfig))
}

func importSqlFile(absImportDir string) {
Expand Down
19 changes: 3 additions & 16 deletions cmd/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,15 @@ package cmd
import (
"os"
"testing"
)

// create a temp file with a dummy password
func createTempFile(t *testing.T) string {
t.Helper()
f, err := os.CreateTemp("", "passwordfile")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
_, err = f.WriteString("filepassword\n")
if err != nil {
t.Fatalf("failed to write to temp file: %v", err)
}
f.Close()
return f.Name()
}
"github.com/uyuni-project/inter-server-sync/tests"
)

// TestGetXMLRPCPassword tests the getXMLRPCPassword function
// Checks the priority of password sources: xmlRpcPasswordFile flag, stdin, and xmlRpcPassword flag.
func TestGetXMLRPCPassword(t *testing.T) {
// Create the temp file once
tmpFile := createTempFile(t)
tmpFile := tests.CreateTempFile(t, "filepassword\n")
defer os.Remove(tmpFile)

tests := []struct {
Expand Down
102 changes: 5 additions & 97 deletions dumper/pillarDumper/pillarDumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,113 +6,21 @@ package pillarDumper

import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"

"github.com/rs/zerolog/log"
"github.com/uyuni-project/inter-server-sync/dumper"
"github.com/uyuni-project/inter-server-sync/schemareader"
"github.com/uyuni-project/inter-server-sync/utils"
)

var serverDataDir = "/srv/susemanager/pillar_data/"
var replacePattern = "{SERVER_FQDN}"

func DumpImagePillars(outputDir string, orgIds []uint, serverConfig string) {
log.Debug().Msgf("Dumping pillars to %s", outputDir)
fqdn := utils.GetCurrentServerFQDN(serverConfig)

sourceDir := filepath.Join(serverDataDir, "images")
orgDir, err := os.Open(sourceDir)
if err != nil {
log.Fatal().Err(err)
}
defer orgDir.Close()
orgDirInfo, err := orgDir.ReadDir(-1)

// If orgIds is empty, set it to 0 so all orgs would be exported
if len(orgIds) == 0 {
orgIds = []uint{0}
}

for _, org := range orgDirInfo {
for _, orgId := range orgIds {
if org.Type().IsDir() && (orgId == 0 || org.Name() == fmt.Sprintf("org%d", orgId)) {
DumpPillars(path.Join(sourceDir, org.Name()), path.Join(outputDir, org.Name()), fqdn, replacePattern)
}
}

}
}

func DumpPillars(sourceDir, outputDir, sourceFQDN, targetFQDN string) {
log.Trace().Msgf("Pillar dump for %s, replacing FQDN %s", sourceDir, sourceFQDN)

pillarDir, err := os.Open(sourceDir)
if err != nil {
log.Fatal().Err(err)
}
defer pillarDir.Close()
pillarDirInfo, err := pillarDir.ReadDir(-1)

for _, pillar := range pillarDirInfo {
if pillar.Type().IsRegular() {
pillarFilePath := path.Join(sourceDir, pillar.Name())
pillarTargetPath := path.Join(outputDir, pillar.Name())
log.Trace().Msgf("Parsing and copying pillar from %s to %s", pillarFilePath, pillarTargetPath)

_, err := dumper.ModifyCopy(pillarFilePath,
pillarTargetPath,
sourceFQDN, targetFQDN)
if err != nil {
log.Fatal().Err(err)
}
os.Chmod(pillarTargetPath, 0640)
cmd := exec.Command("chown", "salt:susemanager", pillarTargetPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatal().Err(err).Msg("Error processing image pillar files")
}
}
}
}

// 4.2 and older stores pillars in files
// image export replaces hostnames in image pillars, we need to replace them to correct SUMA on import
func ImportImagePillars(sourceDir string, fqdn string) {
log.Debug().Msgf("Importing image pillars from %s", sourceDir)
orgDir, err := os.Open(sourceDir)
func UpdateImagePillars(serverConfig string) {
fqdn, err := utils.GetCurrentServerFQDN(serverConfig)
if err != nil {
log.Fatal().Err(err)
}
defer orgDir.Close()
orgDirInfo, err := orgDir.ReadDir(-1)

for _, org := range orgDirInfo {
if org.Type().IsDir() {
targetDir := path.Join(serverDataDir, "images", org.Name())
DumpPillars(path.Join(sourceDir, org.Name()), targetDir, replacePattern, fqdn)

cmd := exec.Command("chown", "salt:susemanager", targetDir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatal().Err(err).Msg("Error importing image pillar files")

}
}
log.Error().Msgf("FQDN of server not found, images pillar will not be updated")
return
}
}

// 4.3 and newer stores pillars in database
// image export replaces hostnames in image pillars, we need to replace them to correct SUMA on import
func UpdateImagePillars(serverConfig string) {
fqdn := utils.GetCurrentServerFQDN(serverConfig)

checkQuery := "SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'susesaltpillar')"
db := schemareader.GetDBconnection(serverConfig)
Expand All @@ -137,7 +45,7 @@ func UpdateImagePillars(serverConfig string) {
replacePattern, fqdn)
log.Trace().Msgf("Updating pillar files using query '%s'", sqlQuery)
log.Info().Msg("Updating image pillars if needed")
rows, err = db.Query(sqlQuery)
_, err = db.Query(sqlQuery)
if err != nil {
log.Fatal().Err(err).Msgf("Error updating image pillars")
}
Expand Down
11 changes: 3 additions & 8 deletions entityDumper/imageDataDumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/rs/zerolog/log"
"github.com/uyuni-project/inter-server-sync/dumper"
"github.com/uyuni-project/inter-server-sync/dumper/osImageDumper"
"github.com/uyuni-project/inter-server-sync/dumper/pillarDumper"
"github.com/uyuni-project/inter-server-sync/schemareader"
"github.com/uyuni-project/inter-server-sync/sqlUtil"
)
Expand Down Expand Up @@ -268,13 +267,9 @@ func dumpImageData(db *sql.DB, writer *bufio.Writer, options DumperOptions) {
var outputFolderImagesAbs = filepath.Join(outputFolderAbs, "images")
ValidateExportFolder(outputFolderImagesAbs)
dumpImageStores(db, writer, schemaMetadata, options, "os_image")
if dumpOSImageTables(db, writer, schemaMetadata, options, outputFolderImagesAbs) {
var outputFolderPillarAbs = filepath.Join(outputFolderAbs, "images", "pillars")
ValidateExportFolder(outputFolderPillarAbs)
pillarDumper.DumpImagePillars(outputFolderPillarAbs, options.Orgs, options.ServerConfig)
if !options.MetadataOnly {
osImageDumper.DumpOsImages(outputFolderImagesAbs, options.Orgs)
}
if dumpOSImageTables(db, writer, schemaMetadata, options, outputFolderImagesAbs) && !options.MetadataOnly {
// Pillars are transfered as part of the sql export
osImageDumper.DumpOsImages(outputFolderImagesAbs, options.Orgs)
}
// This is needed for containers to be able to export their respective tables
markAsUnexported(schemaMetadata, []string{"suseimagestore", "suseimageprofile"})
Expand Down
1 change: 1 addition & 0 deletions inter-server-sync.changes.oholecek.remove_42_support
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- remove support for 4.2 file based pillars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- use correct hostname detection for 5.x servers
(bsc#1253322)
17 changes: 17 additions & 0 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"os"
"testing"

"github.com/DATA-DOG/go-sqlmock"
)
Expand Down Expand Up @@ -113,3 +115,18 @@ func checkErr(err error) {
panic(err)
}
}

// create a temp file with a dummy password
func CreateTempFile(t *testing.T, content string) string {
t.Helper()
f, err := os.CreateTemp("", "issv2")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
_, err = f.WriteString(content)
if err != nil {
t.Fatalf("failed to write to temp file: %v", err)
}
f.Close()
return f.Name()
}
18 changes: 12 additions & 6 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,21 @@ func GetCurrentServerVersion(serverConfig string) (string, string) {
return version, product
}

func GetCurrentServerFQDN(serverConfig string) string {
func GetCurrentServerFQDN(serverConfig string) (string, error) {
files := []string{serverConfig}
files = append(files, getDefaultConfigs()...)
property := []string{"cobbler.host"}
property := []string{"java.hostname"}
p, err := getProperty(files, property)
if err != nil {
log.Error().Msgf("FQDN of server not found, images pillar may not be processed correclty")
return ""
// This still might be 4.3 server, try cobbler.host
property = []string{"cobbler.host"}
p, err = getProperty(files, property)
if err != nil {
log.Error().Msgf(", images pillar may not be processed correctly")
return "", fmt.Errorf("FQDN of server not found")
}
}
return p
return p, nil
}

func getProperty(filePaths []string, names []string) (string, error) {
Expand All @@ -125,7 +130,8 @@ func getProperty(filePaths []string, names []string) (string, error) {
func ScannerFunc(path string, search string) (string, error) {
f, err := os.Open(path)
if err != nil {
log.Fatal().Msgf("Couldn't open file: %s", path)
log.Error().Msgf("Couldn't open file: %s", path)
return "", err
}
defer f.Close()
scanner := bufio.NewScanner(f)
Expand Down
Loading