WIP on start, go fmt on *

This commit is contained in:
yo
2022-04-24 16:49:54 +02:00
parent dbd9153513
commit 43f26d099f
12 changed files with 1498 additions and 1169 deletions

View File

@ -1,13 +1,13 @@
package cmd
import (
"os"
"fmt"
"time"
"bufio"
"errors"
"fmt"
"os"
"regexp"
"strings"
"time"
)
/********************************************************************************
@ -16,13 +16,13 @@ import (
func ListJailsSnapshots(args []string) {
var jailNames []string
var snapshots []Snapshot
if len(args) > 0 {
for _, a := range args {
jailNames = append(jailNames, a)
}
}
}
if len(jailNames) == 0 || len(args) == 0 {
for _, j := range gJails {
snapshots = append(snapshots, listJailSnapshots(j)...)
@ -36,16 +36,15 @@ func ListJailsSnapshots(args []string) {
}
}
}
displaySnapshotsFields(snapshots, []string{"Jailname","Name","Creation","Referenced","Used"})
displaySnapshotsFields(snapshots, []string{"Jailname", "Name", "Creation", "Referenced", "Used"})
}
/********************************************************************************
* List all snapshots a jail have
*******************************************************************************/
func listJailSnapshots(jail Jail) []Snapshot {
var snapshots []Snapshot
// 1. List all datasets
// TODO : Include mounted filesystems?
rs := strings.Split(jail.RootPath, "/")
@ -56,10 +55,10 @@ func listJailSnapshots(jail Jail) []Snapshot {
fmt.Printf("Error: %s\n", err.Error())
return snapshots
}
dateLayout := "Mon Jan _2 15:04 2006"
loc, _ := time.LoadLocation(gTimeZone)
for _, line := range strings.Split(out, "\n") {
if len(line) > 0 {
ls := strings.Split(line, "\t")
@ -71,37 +70,36 @@ func listJailSnapshots(jail Jail) []Snapshot {
}
// Get subdir to append to snapshot name
subdir := strings.Replace(strings.Split(ls[0], "@")[0], rootDataset, "", 1)
snapshots = append(snapshots, Snapshot{Dsname: ls[0],
Name: fmt.Sprintf("%s%s", strings.Split(ls[0], "@")[1], subdir),
Jailname: jail.Name,
Name: fmt.Sprintf("%s%s", strings.Split(ls[0], "@")[1], subdir),
Jailname: jail.Name,
Mountpoint: ls[1],
Used: ls[2],
Used: ls[2],
Referenced: ls[3],
Creation: creationts})
Creation: creationts})
}
}
// Sort snapshots by creation date
ss := initSnapshotSortStruct()
SnapshotsOrderedBy(ss.CreationInc).Sort(snapshots)
return snapshots
}
/********************************************************************************
* Create snapshot for jail(s)
*******************************************************************************/
func CreateJailSnapshot(args []string) {
var jailNames []string
if len(args) > 0 {
for _, a := range args {
jailNames = append(jailNames, a)
}
}
}
for _, cj := range gJails {
for _, jn := range jailNames {
if strings.EqualFold(cj.Name, jn) {
@ -125,7 +123,7 @@ func createJailSnapshot(jail Jail) error {
return err
}
fmt.Printf("Snapshot %s@%s created\n", rootDataset, gSnapshotName)
return nil
}
@ -134,13 +132,13 @@ func createJailSnapshot(jail Jail) error {
*******************************************************************************/
func DeleteJailSnapshot(args []string) {
var jailNames []string
if len(args) > 0 {
for _, a := range args {
jailNames = append(jailNames, a)
}
}
}
for _, cj := range gJails {
for _, jn := range jailNames {
if strings.EqualFold(cj.Name, jn) {
@ -155,7 +153,7 @@ func DeleteJailSnapshot(args []string) {
*******************************************************************************/
func deleteJailSnapshot(jail Jail) error {
var snaptodel []string
// Get all recursive snapshots
rs := strings.Split(jail.RootPath, "/")
rootDataset := fmt.Sprintf("%s%s", jail.Zpool, strings.Join(rs[:len(rs)-1], "/"))
@ -165,18 +163,18 @@ func deleteJailSnapshot(jail Jail) error {
fmt.Printf("Error: listing snapshots: %s\n", err.Error())
return nil
}
for _, line := range strings.Split(out, "\n") {
if len(line) > 0 {
ls := strings.Split(line, "@")
matched, _ := regexp.Match(fmt.Sprintf("^%s(\\/.*)?$", gSnapshotName), []byte(ls[1]))
if matched {
snaptodel = append(snaptodel, strings.Join(ls, "@"))
}
}
}
for _, s := range snaptodel {
cmd := fmt.Sprintf("zfs destroy %s", s)
_, err := executeCommand(cmd)
@ -186,19 +184,19 @@ func deleteJailSnapshot(jail Jail) error {
}
fmt.Printf("Snapshot %s deleted\n", s)
}
return nil
}
func RollbackJailSnapshot(args []string) error {
var jailNames []string
if len(args) > 0 {
for _, a := range args {
jailNames = append(jailNames, a)
}
}
}
for _, cj := range gJails {
for _, jn := range jailNames {
if strings.EqualFold(cj.Name, jn) {
@ -206,7 +204,7 @@ func RollbackJailSnapshot(args []string) error {
}
}
}
return nil
}
@ -216,7 +214,7 @@ func RollbackJailSnapshot(args []string) error {
*******************************************************************************/
func rollbackJailSnapshot(jail Jail) error {
var snaptorb []string
if jail.Running {
fmt.Printf("Jail should be stoped to rollback, should we stop and rollback? (y/n)\n")
scanr := bufio.NewScanner(os.Stdin)
@ -231,7 +229,7 @@ func rollbackJailSnapshot(jail Jail) error {
}
}
}
// We need to rollback parent and childs
// Get all recursive snapshots
rs := strings.Split(jail.RootPath, "/")
@ -242,18 +240,18 @@ func rollbackJailSnapshot(jail Jail) error {
fmt.Printf("Error: listing snapshots: %s\n", err.Error())
return err
}
for _, line := range strings.Split(out, "\n") {
if len(line) > 0 {
ls := strings.Split(line, "@")
matched, _ := regexp.Match(fmt.Sprintf("^%s(\\/.*)?$", gSnapshotName), []byte(ls[1]))
if matched {
snaptorb = append(snaptorb, strings.Join(ls, "@"))
}
}
}
for _, s := range snaptorb {
cmd := fmt.Sprintf("zfs rollback -r %s", s)
_, err := executeCommand(cmd)
@ -263,6 +261,6 @@ func rollbackJailSnapshot(jail Jail) error {
}
}
fmt.Printf("Jail is back to %s\n", gSnapshotName)
return nil
}