-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
128 lines (103 loc) · 3.29 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2022 Emanuele Rocca
package main
import (
"flag"
"log"
"os"
"path/filepath"
"time"
"github.com/hashicorp/logutils"
)
// ParseFlags parses the CLI flags and returns: the configuration directory as
// string, a bool for debugging output, and another bool for dryRun.
func ParseFlags() (string, bool, bool) {
var confDir string
defaultConfDir := filepath.Join(os.Getenv("HOME"), "pets")
flag.StringVar(&confDir, "conf-dir", defaultConfDir, "Pets configuration directory")
debug := flag.Bool("debug", false, "Show debugging output")
dryRun := flag.Bool("dry-run", false, "Only show changes without applying them")
flag.Parse()
return confDir, *debug, *dryRun
}
// GetLogFilter returns a LevelFilter suitable for log.SetOutput().
func GetLogFilter(debug bool) *logutils.LevelFilter {
minLogLevel := "INFO"
if debug {
minLogLevel = "DEBUG"
}
return &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "ERROR"},
MinLevel: logutils.LogLevel(minLogLevel),
Writer: os.Stdout,
}
}
func main() {
startTime := time.Now()
confDir, debug, dryRun := ParseFlags()
log.SetOutput(GetLogFilter(debug))
// Print distro family
family := WhichPackageManager()
if family == APT {
log.Println("[DEBUG] Running on a Debian-like system")
} else if family == YUM {
log.Println("[DEBUG] Running on a RedHat-like system")
}
// *** Config parser ***
// Generate a list of PetsFiles from the given config directory.
log.Println("[DEBUG] * configuration parsing starts *")
files, err := ParseFiles(confDir)
if err != nil {
log.Println(err)
}
log.Printf("[INFO] Found %d pets configuration files", len(files))
log.Println("[DEBUG] * configuration parsing ends *")
// *** Config validator ***
log.Println("[DEBUG] * configuration validation starts *")
globalErrors := CheckGlobalConstraints(files)
if globalErrors != nil {
log.Println(globalErrors)
// Global validation errors mean we should stop the whole update.
return
}
// Check validation errors in individual files. At this stage, the
// command in the "pre" validation directive may not be installed yet.
// Ignore PathErrors for now. Get a list of valid files.
goodPets := CheckLocalConstraints(files, true)
log.Println("[DEBUG] * configuration validation ends *")
// Generate the list of actions to perform.
actions := NewPetsActions(goodPets)
// *** Update visualizer ***
// Display:
// - packages to install
// - files created/modified
// - content diff (maybe?)
// - owner changes
// - permissions changes
// - which post-update commands will be executed
for _, action := range actions {
log.Println("[INFO]", action)
}
if dryRun {
log.Println("[INFO] user requested dry-run mode, not applying any changes")
return
}
// *** Update executor ***
// Install missing packages
// Create missing directories
// Run pre-update command and stop the update if it fails
// Update files
// Change permissions/owners
// Run post-update commands
exitStatus := 0
for _, action := range actions {
log.Printf("[INFO] running '%s'\n", action.Command)
err = action.Perform()
if err != nil {
log.Printf("[ERROR] performing action %s: %s\n", action, err)
exitStatus = 1
break
}
}
log.Printf("[INFO] pets run took %v\n", time.Since(startTime).Round(time.Millisecond))
os.Exit(exitStatus)
}