From 37fea55e42ac577914741d365d9ba01bca97b0e0 Mon Sep 17 00:00:00 2001 From: yo Date: Sun, 20 Nov 2022 20:20:20 +0100 Subject: [PATCH] Add update command --- cmd/update.go | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 cmd/update.go diff --git a/cmd/update.go b/cmd/update.go new file mode 100644 index 0000000..71670f1 --- /dev/null +++ b/cmd/update.go @@ -0,0 +1,157 @@ +package cmd + +import ( + "os" + "fmt" + //"log" + "time" +) + +const ( + fbsdUpdateConfig = ` + # $FreeBSD: releng/12.2/usr.sbin/freebsd-update/freebsd-update.conf 337338 2018-08-04 22:25:41Z brd $ + + # Trusted keyprint. Changing this is a Bad Idea unless you've received + # a PGP-signed email from telling you to + # change it and explaining why. + KeyPrint 800651ef4b4c71c27e60786d7b487188970f4b4169cc055784e21eb71d410cc5 + + # Server or server pool from which to fetch updates. You can change + # this to point at a specific server if you want, but in most cases + # using a "nearby" server won't provide a measurable improvement in + # performance. + ServerName update.FreeBSD.org + + # Components of the base system which should be kept updated. + Components world + + # Example for updating the userland and the kernel source code only: + # Components src/base src/sys world + + # Paths which start with anything matching an entry in an IgnorePaths + # statement will be ignored. + IgnorePaths + + # Paths which start with anything matching an entry in an IDSIgnorePaths + # statement will be ignored by "freebsd-update IDS". + IDSIgnorePaths /usr/share/man/cat + IDSIgnorePaths /usr/share/man/whatis + IDSIgnorePaths /var/db/locate.database + IDSIgnorePaths /var/log + + # Paths which start with anything matching an entry in an UpdateIfUnmodified + # statement will only be updated if the contents of the file have not been + # modified by the user (unless changes are merged; see below). + UpdateIfUnmodified /etc/ /var/ /root/ /.cshrc /.profile + + # When upgrading to a new FreeBSD release, files which match MergeChanges + # will have any local changes merged into the version from the new release. + MergeChanges /etc/ + + ### Default configuration options: + + # Directory in which to store downloaded updates and temporary + # files used by FreeBSD Update. + # WorkDir /var/db/freebsd-update + + # Destination to send output of "freebsd-update cron" if an error + # occurs or updates have been downloaded. + # MailTo root + + # Is FreeBSD Update allowed to create new files? + # AllowAdd yes + + # Is FreeBSD Update allowed to delete files? + # AllowDelete yes + + # If the user has modified file ownership, permissions, or flags, should + # FreeBSD Update retain this modified metadata when installing a new version + # of that file? + # KeepModifiedMetadata yes + + # When upgrading between releases, should the list of Components be + # read strictly (StrictComponents yes) or merely as a list of components + # which *might* be installed of which FreeBSD Update should figure out + # which actually are installed and upgrade those (StrictComponents no)? + # StrictComponents no + + # When installing a new kernel perform a backup of the old one first + # so it is possible to boot the old kernel in case of problems. + # BackupKernel yes + + # If BackupKernel is enabled, the backup kernel is saved to this + # directory. + # BackupKernelDir /boot/kernel.old + + # When backing up a kernel also back up debug symbol files? + # BackupKernelSymbolFiles no + + # Create a new boot environment when installing patches + # CreateBootEnv yes + ` +) + +// 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()) + + cmd := fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s fetch install", + cfgFile.Name(), jail.RootPath, jail.Config.Release) + + fmt.Printf("DEBUG: Prepare to execute \"%s\"\n", cmd) + + err = executeCommandWithOutputToStdout(cmd) + if err != nil { + return err + } + + // Get and write new release into config.json + + + 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, gJails) + if err != nil { + fmt.Printf("Error getting jail: %s\n", err) + 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) + } + } +}