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" "fmt"
"net" "net"
"regexp" "regexp"
"strconv"
"strings" "strings"
"io/ioutil" "io/ioutil"
"golang.org/x/net/route" "golang.org/x/net/route"
@ -190,34 +189,17 @@ func getArch() (string, error) {
func getFreeBSDVersion() (FreeBSDVersion, error) { func getFreeBSDVersion() (FreeBSDVersion, error) {
var version FreeBSDVersion 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") out, err := executeCommand("/bin/freebsd-version")
if err != nil { if err != nil {
return version, fmt.Errorf("Error executing \"/bin/freebsd-version\": %v", err) return version, fmt.Errorf("Error executing \"/bin/freebsd-version\": %v", err)
} }
if re.MatchString(out) { version, err = freebsdVersionToStruct(out)
version.major, err = strconv.Atoi(re.FindStringSubmatch(out)[1]) if err != nil {
if err != nil { return version, err
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
}
}
} }
return version, nil return version, nil
} }

View File

@ -14,7 +14,7 @@ import (
) )
const ( const (
gVersion = "0.42d" gVersion = "0.42g"
// TODO : Get from $jail_zpool/defaults.json // TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000 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`, Long: `Let this show you how much fail I had to get this *cough* perfect`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fv, _ := getFreeBSDVersion() 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 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 // Snapshot before updating
dt := time.Now() dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05")) curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05"))
snapshotName := fmt.Sprintf("gocage_update_%s_%s", v, curDate) 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 { if err != nil {
fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err) fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err)
return 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 { if err != nil {
fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err) fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err)
} else { } else {

View File

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

View File

@ -8,6 +8,7 @@ import (
"sort" "sort"
"bufio" "bufio"
"errors" "errors"
"regexp"
"os/exec" "os/exec"
"reflect" "reflect"
"strconv" "strconv"
@ -1110,13 +1111,10 @@ func getDevfsRuleset(ruleset int) []string {
if err != nil { if err != nil {
return []string{} return []string{}
} }
// Get rid of the last "\n" return strings.Split(out, "\n")[:len(strings.Split(out, "\n"))]
return strings.Split(out, "\n")[:len(strings.Split(out, "\n"))-1]
} }
func copyDevfsRuleset(ruleset int, srcrs int) error { func copyDevfsRuleset(ruleset int, srcrs int) error {
// Resulting ruleset as an array of line
//var result []string
out := getDevfsRuleset(srcrs) out := getDevfsRuleset(srcrs)
for _, line := range out { for _, line := range out {
//fields := strings.Fields(line) //fields := strings.Fields(line)
@ -1287,6 +1285,36 @@ func setJailConfigUpdated(jail *Jail) error {
return nil 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) { func getVersion(jail *Jail) (string, error) {
cvers, err := executeCommand(fmt.Sprintf("%s/bin/freebsd-version", jail.RootPath)) cvers, err := executeCommand(fmt.Sprintf("%s/bin/freebsd-version", jail.RootPath))
if err != nil { if err != nil {