-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsab.go
306 lines (283 loc) · 8.17 KB
/
csab.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"os"
"fmt"
"regexp"
"time"
"os/exec"
"io/ioutil"
confirm "github.com/aintnorest/csab/confirmation"
tparse "github.com/BurntSushi/toml"
)
type tomlConfig struct {
Title string
Chroots map[string]chroot
}
type chroot struct {
Name string
Backup_location string
Comments string
}
func tomlDecode(location string)(*tomlConfig) {
var config tomlConfig
if _, err := tparse.DecodeFile(location, &config); err != nil {
fmt.Println(err, "Not a Valid TOML")
}
return &config
}
func CreateConfig() {
//makes the Config File
ConfigTomil, err := os.Create("/home/chronos/user/Downloads/chrootconfig.toml")
if err != nil {
fmt.Println(err)
}
if err == nil {
defer ConfigTomil.Close()
}
_, err = ConfigTomil.WriteString("title = \"TOML Configuration for Crouton Start and Backup Automation\"\n\n[chroots]")
if err != nil {
fmt.Println(err)
}
}
func AppendConfig() {
File, err := os.OpenFile("/home/chronos/user/Downloads/chrootconfig.toml", os.O_RDWR|os.O_APPEND, 0660)
if err == nil {
defer File.Close()
} else {
fmt.Println(err)
return
}
var yn bool = true
var config tomlConfig
if _, err := tparse.DecodeFile("/home/chronos/user/Downloads/chrootconfig.toml", &config); err != nil {
fmt.Println(err)
return
}
for i := len(config.Chroots) + 1; yn == true; i++ {
fmt.Println("Would you like to add a Chroot to your config file? Y/N\n")
var input string
fmt.Scanln(&input)
yn, err := confirm.ConfirmationPrompt(input)
if err != nil {
fmt.Println("Confirmation Error")
}
if yn == true {
//Gather Config File Info into three variables
fmt.Println("\nAll the following inputs are case sensitive.")
fmt.Println("\nFirst enter the name of the chroot you are trying to add. If you didn't name the chroot with the -n flag, the default naming convention is the distro's version name. For example raring or wheezy. PLEASE DO NOT NAME SEPERATE CHROOTS THE SAME NAME.\nPlease enter the Chroot's name now:\n")
var chrootname string
fmt.Scanln(&chrootname)
fmt.Println("\nIf you have any comments to add about your chroot so that you can identify it more easily add them now.")
var chrootcomments string
fmt.Scanln(&chrootcomments)
fmt.Println("\nNext enter the full pathway location where you would like your backup stored. A good default is /home/chronos/user/Downloads/\n")
var chrootbl string
for {
fmt.Scanln(&chrootbl)
expr, err := regexp.Compile("[/]$")
if err == nil {
argumatch := expr.MatchString(chrootbl)
if argumatch == false {
chrootbl = chrootbl + "/"
}
}
ffile, err := os.Open(chrootbl)
if(err == nil) {
ffile.Close()
break }
fmt.Println("That wasn't a valid Pathway.\n")
fmt.Println("Re-enter a valid Pathway.\n")
}
_, err = File.WriteString("\n\n\t[chroots."+fmt.Sprintf("%d",i)+"]\n\tname = \""+chrootname+"\"\n\tcomments = \""+chrootcomments+"\"\n\tbackup_location = \""+chrootbl+"\"")
if err != nil {
fmt.Println(err)
}
}
if yn == false {
return
}
}
}
func durationSince(fpathway string) (Elapsed float64, err error){
file, err := os.Open(fpathway)
if err != nil {
return 0, err
}
info, err := file.Stat()
if err != nil {
fmt.Println(err)
}
if info != nil {
creationT := info.ModTime() // This should define a new variable :=
fElapsed := time.Since(creationT)
Elapsed = fElapsed.Hours()
}
file.Close()
return Elapsed, err
}
func worker(finishedChan chan struct{}) {
close(finishedChan)
}
func waiting(finishedChan chan struct{}) {
for {
timerChan := time.After(2 * time.Second)
select {
case <-finishedChan: {
return
}
case <- timerChan: {
fmt.Print(".")
}
}
}
}
func getFPFromConfig(config tomlConfig, index int) string{
chroot := config.Chroots[fmt.Sprintf("%d", index)]
bl := chroot.Backup_location
n := chroot.Name
return fmt.Sprintf("%s%sBackup.tar.gz", bl, n, )
}
func main() {
if len(os.Args) > 1 {
Arguments := os.Args[1]
expression, err := regexp.Compile("^([cC][oO][nN][fF][iI][gG]|[-][cC])$")
if err == nil {
argmatch := expression.MatchString(Arguments)
if argmatch == false {
fmt.Println("CSAB is a tool to startup a crouton created chroot and backup scheduler.\n\n Usage: \n\n\t CSAB \n\t\t Will launch the program. \n CSAB -c or config \n\t\t Will allow you to add chroots after you already have the config file setup. You can also just delete the config file.")
os.Exit(1)
}
if argmatch == true {
AppendConfig()
}
}
}
_, errrr := os.Open("/home/chronos/user/Downloads/chrootconfig.toml")
//if err == nil {
// file.Close()
//}
if errrr != nil {
CreateConfig()
AppendConfig()
}
var config tomlConfig
if _, err := tparse.DecodeFile("/home/chronos/user/Downloads/chrootconfig.toml", &config); err != nil {
fmt.Println(err)
return
}
//if errrr == nil {
// file.Close()
//}
fpathway := ""
var ChrootN int
if len(config.Chroots) == 1 {
fpathway = getFPFromConfig(config, 1)
ChrootN = 1
}
if len(config.Chroots) > 1 {
fmt.Println("\nHere are a list of your chroots:")
for chrootname, chroot := range config.Chroots {
fmt.Printf("\nChroot:%s - %s\n\tComments:%s\n", chrootname, chroot.Name, chroot.Comments)
}
fmt.Println("\nEnter the number of the chroot you wish to enter:\n")
fmt.Scanln(&ChrootN)
for {
if ChrootN >= 1 && ChrootN <= len(config.Chroots) {
break
}
fmt.Println("You entered an invalid chroot number. Please re-enter a valid number.\n")
fmt.Scanln(&ChrootN)
}
fpathway = getFPFromConfig(config, ChrootN)
}
fpathway = getFPFromConfig(config, ChrootN)
var BackupWindow float64 = 48
Duration, errr := durationSince(fpathway)
finishedChan := make(chan struct{})
if errr != nil {
fmt.Println("A backup file wasn't detected. One will be created now.\n")
go waiting(finishedChan)
scn := config.Chroots[fmt.Sprintf("%d", ChrootN)].Name
arg0 := "sudo"
arg1 := "bash"
arg2 := "-c"
arg3 := fmt.Sprintf("edit-chroot -f %s -b %s", fpathway, scn)
cmd := exec.Command(arg0, arg1, arg2, arg3)
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
worker(finishedChan)
}
fpathway = getFPFromConfig(config, ChrootN)
if Duration >= BackupWindow {
finishedChan := make(chan struct{})
fmt.Println("\nIt has been", BackupWindow, "hours since your last backup. Would you like to make a backup now?\n")
var input string
fmt.Scanln(&input)
fmt.Println("\n")
Yn, err := confirm.ConfirmationPrompt(input)
if err != nil {
fmt.Println("Confirmation Error")
}
if Yn == true {
go waiting(finishedChan)
scn := config.Chroots[fmt.Sprintf("%d", ChrootN)].Name
arg0 := "sudo"
arg1 := "bash"
arg2 := "-c"
arg3 := fmt.Sprintf("edit-chroot -f %s -b %s", fpathway, scn)
cmd := exec.Command(arg0, arg1, arg2, arg3)
err = cmd.Run()
if err != nil {
fmt.Println(err)
worker(finishedChan)
}
}
}
scn := config.Chroots[fmt.Sprintf("%d", ChrootN)].Name
gapfill := fmt.Sprintf("/usr/local/chroots/%s/etc/crouton/targets", scn)
content, err := ioutil.ReadFile(gapfill)
if err != nil {
fmt.Println(err)
}
var Targetfile string = fmt.Sprintf("%s", content)
re := regexp.MustCompile("[c][i][n][n][a][m][o][n]|[g][n][o][m][e]|[k][d][e]|[l][x][d][e]|[e][1][7]|[u][n][i][t][y]|[x][f][c][e]")
Desktopoptions := re.FindAllStringSubmatch(Targetfile, -1)
Scn := fmt.Sprintf("%s", scn)
if (len(Desktopoptions)) == 1 {
arg000 := fmt.Sprintf("%s", Desktopoptions[0][0])
arg00 := "sudo"
arg01 := "bash"
arg02 := "-c"
arg03 := "start"+arg000+" -n "+Scn
cmd2 := exec.Command(arg00, arg01, arg02, arg03)
err := cmd2.Run()
if err !=nil {
fmt.Println(err)
}
}
DO := fmt.Sprintf("%s", Desktopoptions)
if (len(Desktopoptions)) > 1 {
fmt.Println("Which Desktop would you like to run today?\n"+DO)
var dinput string
fmt.Scanln(&dinput)
bol := re.MatchString(dinput)
for {
if bol == true {
break
}
fmt.Println("You failed to enter which desktop you wish to run today please re-enter your selection now, case sensitive as always.\n")
fmt.Scanln(&dinput)
}
arg00 := "sudo"
arg01 := "bash"
arg02 := "-c"
arg03 := "start"+dinput+" -n "+Scn
cmd2 := exec.Command(arg00, arg01, arg02, arg03)
err := cmd2.Run()
if err !=nil {
fmt.Println(err)
}
}
}