This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
197 lines (161 loc) · 4.27 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
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
/*
* swift-backup - A simple tool for backing up files to OpenStack Swift
* (c) 2017 Giuseppe Paterno' <[email protected]>
* Released under GPL v3 (see LICENSE)
*/
package main
import (
"io"
"net/http"
"os"
"crypto/md5"
"crypto/tls"
"encoding/hex"
"flag"
"path"
"strings"
"github.com/alexcesaro/log/stdlog"
"github.com/ncw/swift"
)
// Global vars
var (
username string
password string
project string
authurl string
region string
deleteFile bool
)
// Init with the paramenters
func init() {
flag.StringVar(&username, "os-username", "admin", "Swift username")
flag.StringVar(&password, "os-password", "password", "Swift password")
flag.StringVar(&project, "os-project", "admin", "Project name")
flag.StringVar(&authurl, "os-authurl", "https://os.ch-ti1.server.one/v2.0/", "Keystone endpoint")
flag.StringVar(®ion, "os-region", "ch-ti1", "Region name")
flag.BoolVar(&deleteFile, "delete-after", false, "Delete source file afterwards")
}
// There's no builtin to have a string in a slice
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// Main (upload) function
func main() {
logger := stdlog.GetFromFlags()
flag.Parse()
// Check if args
if (len(flag.Args()) < 2) || (len(flag.Args()) > 2) {
logger.Error("Usage: swift-backup <container> <file>")
os.Exit(1)
}
// We are running semi-trusted cert, set to insecure
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConnsPerHost: 2048,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Setup container/filename vars
var container = flag.Args()[0]
var filename = flag.Args()[1]
var tgt_filename = path.Base(filename)
//logger.Debugf("Password: %s", password)
logger.Debugf("Container: %s", container)
logger.Debugf("Keystone endpoint: %s", authurl)
logger.Debugf("Username: %s", username)
logger.Debugf("Password: %s", strings.Repeat("*", len(password)))
logger.Debugf("Project: %s", project)
logger.Debugf("Filename: %s", filename)
logger.Debugf("Target filename: %s", tgt_filename)
c := swift.Connection{
UserName: username,
ApiKey: password,
AuthUrl: authurl,
Region: region,
Tenant: project,
Transport: transport,
}
// Authenticate
err := c.Authenticate()
if err != nil {
// Bad Request is a trap :)
if err == swift.BadRequest {
logger.Error("Authentication denied")
} else {
logger.Error(err)
}
os.Exit(1)
}
if !c.Authenticated() {
logger.Error("Not authenticated")
os.Exit(1)
}
// Check if container is among the list of containers
containers, _ := c.ContainerNames(nil)
logger.Debugf("Containers: %s", containers)
if !stringInSlice(container, containers) {
logger.Errorf("Container %s do not exist", container)
os.Exit(1)
}
// Upload the file
hash := md5.New()
in, err := os.Open(filename)
if err != nil {
logger.Error(err)
os.Exit(1)
}
// Get hash of file
io.Copy(hash, in)
hashResult := hex.EncodeToString(hash.Sum(nil))
logger.Debugf("Hash: %s", hashResult)
// Rewind the file
in.Seek(0, 0)
// Create object in Swift
out, err := c.ObjectCreate(container, tgt_filename, true, hashResult, "", nil)
if err != nil {
logger.Error(err)
}
// Copy the file to object storage
if n, err := io.Copy(out, in); err != nil {
logger.Error(err)
} else {
logger.Debugf("Copied %v bytes", n)
}
// Closing swift file and check for errors
err = out.Close()
if err != nil {
logger.Error(err)
os.Exit(1)
}
// Closing input file
in.Close()
// Unlink if delete after
// Check hash before deleting
if deleteFile {
obj, _, err := c.Object(container, filename)
if err != nil {
logger.Error("An error occurred while verifying hash")
os.Exit(1)
}
logger.Debugf("Remote hash: %s", obj.Hash)
logger.Debugf("Local hash: %s", hashResult)
// If hashes do not match, do not delete, corruption happened :(
if obj.Hash != hashResult {
logger.Error("Hash error while comparing local and remote, cowardly refusing removing local file")
os.Exit(1)
}
// Now deleting file
logger.Debug("Hashes matches, deleting file")
err = os.Remove(filename)
if err != nil {
logger.Error("An error occurred while removing source file")
os.Exit(1)
}
}
// Clean exit :)
os.Exit(0)
}