better handling of version/patch especiallly for snapshots, use updateWorkDir

This commit is contained in:
yo 2024-10-19 09:57:36 +02:00
parent fb4010378f
commit 6acea0d25b
5 changed files with 62 additions and 40 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt"
"net"
"regexp"
"strconv"
"strings"
"io/ioutil"
"golang.org/x/net/route"
@ -190,34 +189,17 @@ func getArch() (string, error) {
func getFreeBSDVersion() (FreeBSDVersion, error) {
var version FreeBSDVersion
regex := `([0-9]{1,2})(\.)?([0-9]{1,2})?\-([^\-]*)(\-)?(p[0-9]{1,2})?`
re := regexp.MustCompile(regex)
out, err := executeCommand("/bin/freebsd-version")
if err != nil {
return version, fmt.Errorf("Error executing \"/bin/freebsd-version\": %v", err)
}
if re.MatchString(out) {
version.major, err = strconv.Atoi(re.FindStringSubmatch(out)[1])
if err != nil {
return version, err
}
version.minor, err = strconv.Atoi(re.FindStringSubmatch(out)[3])
if err != nil {
return version, err
}
version.flavor = strings.Trim(re.FindStringSubmatch(out)[4], "\n")
// Skip the 'p' starting patch level
if len(re.FindStringSubmatch(out)[6]) > 0 {
version.patchLevel, err = strconv.Atoi(re.FindStringSubmatch(out)[6][1:])
if err != nil {
return version, err
}
}
version, err = freebsdVersionToStruct(out)
if err != nil {
return version, err
}
return version, nil
}

View File

@ -14,7 +14,7 @@ import (
)
const (
gVersion = "0.42d"
gVersion = "0.42g"
// TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000
@ -99,7 +99,11 @@ It support iocage jails and can coexist with iocage.`,
Long: `Let this show you how much fail I had to get this *cough* perfect`,
Run: func(cmd *cobra.Command, args []string) {
fv, _ := getFreeBSDVersion()
fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor)
if fv.patchLevel > 0 {
fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s-p%d\n", gVersion, fv.major, fv.minor, fv.flavor, fv.patchLevel)
} else {
fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor)
}
},
}

View File

@ -78,16 +78,23 @@ func UpdateJail(args []string) {
}
fakeJail.Config.Release = v
// Remove patch level from Release
fv, err := freebsdVersionToStruct(fakeJail.Config.Release)
if err != nil {
fmt.Printf("Error converting release %s: %v\n", fakeJail.Config.Release, err)
return
}
release := fmt.Sprintf("%d.%d-%s", fv.major, fv.minor, fv.flavor)
// Snapshot before updating
dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05"))
snapshotName := fmt.Sprintf("gocage_update_%s_%s", v, curDate)
err = zfsSnapshot(fmt.Sprintf("%s/releases/%s", ds.ZFSDataset, fakeJail.Config.Release), snapshotName)
err = zfsSnapshot(fmt.Sprintf("%s/releases/%s", ds.ZFSDataset, release), snapshotName)
if err != nil {
fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err)
return
}
err = zfsSnapshot(fmt.Sprintf("%s/releases/%s/root", ds.ZFSDataset, fakeJail.Config.Release), snapshotName)
err = zfsSnapshot(fmt.Sprintf("%s/releases/%s/root", ds.ZFSDataset, release), snapshotName)
if err != nil {
fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err)
} else {

View File

@ -8,6 +8,7 @@ import (
//"log"
"time"
"strings"
"github.com/spf13/viper"
)
// Internal usage only
@ -18,19 +19,20 @@ func upgradeJail(jail *Jail, version string) error {
return err
}
cfgFile.Write([]byte(fbsdUpdateConfig))
defer cfgFile.Close()
defer os.Remove(cfgFile.Name())
// Folder containing update/uipgrade temporary files. Common so we save bandwith when upgrading multiple jails
// TODO: Variabilize /iocage/freebsd-update
_, err = os.Stat("/iocage/freebsd-update")
// Folder containing update/upgrade temporary files. Mutualized so we save bandwith when upgrading multiple jails
uwd := viper.GetString("updateWorkDir")
if len(uwd) == 0 {
return fmt.Errorf("updateWorkDir not set in configuration")
}
_, err = os.Stat(uwd)
if os.IsNotExist(err) {
if err := os.Mkdir("/iocage/freebsd-update", 0755); err != nil {
if err := os.Mkdir(uwd, 0755); err != nil {
return err
}
}
cfgFile.Write([]byte(strings.Replace(fbsdUpdateConfig, "TO-BE-REPLACED-WITH-UPDATEWORKDIR", uwd, 1)))
defer cfgFile.Close()
defer os.Remove(cfgFile.Name())
// Get current version. Won't work on stopped jail.
fbsdvers, err := executeCommandInJail(jail, "/bin/freebsd-version")
@ -39,7 +41,6 @@ func upgradeJail(jail *Jail, version string) error {
return err
}
fbsdvers = strings.TrimRight(fbsdvers, "\n")
//fbsdvers := jail.Config.Release
cmd := fmt.Sprintf("/usr/sbin/freebsd-update -f %s -b %s --currently-running %s -r %s upgrade",
cfgFile.Name(), jail.RootPath, fbsdvers, version)

View File

@ -8,6 +8,7 @@ import (
"sort"
"bufio"
"errors"
"regexp"
"os/exec"
"reflect"
"strconv"
@ -1110,13 +1111,10 @@ func getDevfsRuleset(ruleset int) []string {
if err != nil {
return []string{}
}
// Get rid of the last "\n"
return strings.Split(out, "\n")[:len(strings.Split(out, "\n"))-1]
return strings.Split(out, "\n")[:len(strings.Split(out, "\n"))]
}
func copyDevfsRuleset(ruleset int, srcrs int) error {
// Resulting ruleset as an array of line
//var result []string
out := getDevfsRuleset(srcrs)
for _, line := range out {
//fields := strings.Fields(line)
@ -1287,6 +1285,36 @@ func setJailConfigUpdated(jail *Jail) error {
return nil
}
func freebsdVersionToStruct(rawVersion string) (FreeBSDVersion, error) {
var version FreeBSDVersion
var err error
regex := `([0-9]{1,2})(\.)?([0-9]{1,2})?\-([^\-]*)(\-)?(p[0-9]{1,2})?`
re := regexp.MustCompile(regex)
if re.MatchString(rawVersion) {
version.major, err = strconv.Atoi(re.FindStringSubmatch(rawVersion)[1])
if err != nil {
return version, err
}
version.minor, err = strconv.Atoi(re.FindStringSubmatch(rawVersion)[3])
if err != nil {
return version, err
}
version.flavor = strings.Trim(re.FindStringSubmatch(rawVersion)[4], "\n")
// Skip the 'p' starting patch level
if len(re.FindStringSubmatch(rawVersion)[6]) > 0 {
version.patchLevel, err = strconv.Atoi(re.FindStringSubmatch(rawVersion)[6][1:])
if err != nil {
return version, err
}
}
}
return version, nil
}
func getVersion(jail *Jail) (string, error) {
cvers, err := executeCommand(fmt.Sprintf("%s/bin/freebsd-version", jail.RootPath))
if err != nil {