gocage/cmd/update.go

94 lines
2.4 KiB
Go

package cmd
import (
"os"
"fmt"
//"log"
"time"
"strings"
)
// Internal usage only
func updateJail(jail *Jail) error {
// Create default config as temporary file
cfgFile, err := os.CreateTemp("", "gocage-jail-update-")
if err != nil {
return err
}
cfgFile.Write([]byte(fbsdUpdateConfig))
defer cfgFile.Close()
defer os.Remove(cfgFile.Name())
// Folder containing update/upgrade temporary files. Common so we save bandwith when upgrading multiple jails
// TODO: Variabilize /iocage/freebsd-update
_, err = os.Stat("/iocage/freebsd-update")
if os.IsNotExist(err) {
if err := os.Mkdir("/iocage/freebsd-update", 0755); err != nil {
return err
}
}
cmd := fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s fetch",
cfgFile.Name(), jail.RootPath, jail.Config.Release)
err = executeCommandWithOutputToStdout(cmd)
if err != nil {
return err
}
cmd = fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s install",
cfgFile.Name(), jail.RootPath, jail.Config.Release)
err = executeCommandWithOutputToStdout(cmd)
if err != nil {
return err
}
// Get and write new release into config.json
updateVersion(jail)
return nil
}
func UpdateJail(args []string) {
// Current jail were stopping
var cj *Jail
var err error
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, []string{""}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue
}
// We cant update basejail as system is readonly
if strings.EqualFold(cj.Config.Jailtype, "basejail") {
fmt.Printf("%s is a basejail using %s system files. Please update %s!\n", cj.Name, cj.Config.Origin, cj.Config.Origin)
continue
}
fmt.Printf(" > Snapshot jail %s\n", cj.Name)
// Set snapshot name
dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05"))
gSnapshotName = fmt.Sprintf("goc_update_%s_%s", cj.Config.Release, curDate)
err := createJailSnapshot(*cj)
if err != nil {
fmt.Printf(" > Snapshot jail %s: ERROR: %s\n", cj.Name, err.Error())
return
}
fmt.Printf(" > Snapshot jail %s: OK\n", cj.Name)
fmt.Printf(" > Update jail %s\n", cj.Name)
err = updateJail(cj)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
} else {
fmt.Printf(" > Update jail %s: OK\n", cj.Name)
}
}
}