forked from e-XpertSolutions/f5-auto-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
156 lines (132 loc) · 3.72 KB
/
util.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
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"io"
"os"
"path/filepath"
"strings"
"github.com/e-XpertSolutions/f5-rest-client/f5"
"github.com/e-XpertSolutions/f5-rest-client/f5/ltm"
"github.com/e-XpertSolutions/f5-rest-client/f5/sys"
)
func isExcluded(name string, excl []string) bool {
for _, pattern := range excl {
matched, err := filepath.Match(pattern, name)
if err == nil && matched {
fmt.Printf("%q is excluded by pattern %q\n", name, pattern)
return true
} else if err != nil {
fmt.Println("err = ", err)
}
fmt.Printf("%q is not excluded by pattern %q\n", name, pattern)
}
return false
}
func uploadNewFile(tx *f5.Client, name, path string) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("cannot read file %q: %v", path, err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return fmt.Errorf("cannot stat file %q: %v", path, err)
}
sysClient := sys.New(tx)
if err := sysClient.FileIFile().CreateFromFile(name, f, info.Size()); err != nil {
return fmt.Errorf("an error occured while uploading %q: %v", path, err)
}
ltmClient := ltm.New(tx)
if err := ltmClient.IFile().Create(name, name); err != nil {
return fmt.Errorf("cannot create file %q in ltm ifiles: %v", path, err)
}
return nil
}
func uploadExistingFile(tx *f5.Client, name, path string) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("cannot read file %q: %v", path, err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return fmt.Errorf("cannot stat file %q: %v", path, err)
}
sysClient := sys.New(tx)
if err := sysClient.FileIFile().EditFromFile(name, f, info.Size()); err != nil {
return fmt.Errorf("an error occured while re-uploading %q: %v", path, err)
}
ltmClient := ltm.New(tx)
if err := ltmClient.IFile().Edit(name, name); err != nil {
return fmt.Errorf("cannot update file %q in ltm ifiles: %v", path, err)
}
return nil
}
func deleteFile(tx *f5.Client, name string) error {
ltmClient := ltm.New(tx)
if err := ltmClient.IFile().Delete(name); err != nil {
return fmt.Errorf("cannot delete ltm ifile %q: %v", name, err)
}
sysClient := sys.New(tx)
if err := sysClient.FileIFile().Delete(name); err != nil {
return fmt.Errorf("cannot delete ifile %q: %v", name, err)
}
if err := sysClient.FileIFile().Delete(name); err != nil {
return fmt.Errorf("cannot delete ifile %q from disk: %v", name, err)
}
return nil
}
func isSameRevision(tx *f5.Client, name, path string) (bool, error) {
sysClient := sys.New(tx)
ifile, err := sysClient.FileIFile().Get(name)
if err != nil {
return false, fmt.Errorf("cannot get ifile meta for %q: %v", name, err)
}
f, err := os.Open(path)
if err != nil {
return false, fmt.Errorf("cannot open file %q: %v", path, err)
}
defer f.Close()
algo, _, checksum := splitChecksum(ifile.Checksum)
var h hash.Hash
switch strings.ToLower(algo) {
case "sha1":
h = sha1.New()
case "sha256":
h = sha256.New()
case "sha512":
h = sha512.New()
case "md5":
h = md5.New()
default:
return false, fmt.Errorf("unsupported algo %q for file %q", algo, path)
}
if _, err := io.Copy(h, f); err != nil {
return false, fmt.Errorf("cannot write file %q into hash function of type %q: %v", path, algo, err)
}
expectedChecksum := hex.EncodeToString(h.Sum(nil)[:])
return checksum == expectedChecksum, nil
}
func splitChecksum(ifileChecksum string) (algo, opts, checksum string) {
elmts := strings.Split(ifileChecksum, ":")
switch l := len(elmts); l {
case 0:
return
case 1:
checksum = elmts[0]
case 2:
algo = elmts[0]
checksum = elmts[1]
default: // 3 or more
algo = elmts[0]
opts = strings.Join(elmts[1:l-1], ":")
checksum = elmts[l-1]
}
return
}