-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.go
82 lines (65 loc) · 2.32 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
package main
import (
"flag"
"io"
"log"
"os"
"path"
"github.com/percona/percona-postgresql-operator/percona/extensions"
)
func main() {
var storageType, endpoint, region, bucket, key, extensionPath string
var install, uninstall bool
flag.StringVar(&storageType, "type", "", "Storage type")
flag.StringVar(&endpoint, "endpoint", "", "Storage endpoint")
flag.StringVar(®ion, "region", "", "Storage region")
flag.StringVar(&bucket, "bucket", "", "Storage bucket")
flag.StringVar(&extensionPath, "extension-path", "", "Extension installation path")
flag.StringVar(&key, "key", "", "Extension archive key")
flag.BoolVar(&install, "install", false, "Install extension")
flag.BoolVar(&uninstall, "uninstall", false, "Uninstall extension")
flag.Parse()
if (install && uninstall) || (!install && !uninstall) {
log.Fatalf("ERROR: set either -install or -uninstall")
}
log.Printf("starting extension installer for %s/%s (%s) in %s", bucket, key, storageType, region)
storage := initStorage(extensions.StorageType(storageType), endpoint, bucket, region)
packageName := key + ".tar.gz"
object, err := storage.Get(packageName)
if err != nil {
log.Fatalf("ERROR: failed to get object: %v", err)
}
archivePath := path.Join("/tmp", packageName)
// TODO: /tmp is not a good place due to its size limitation.
dst, err := os.Create(archivePath)
if err != nil {
log.Fatalf("ERROR: failed to create file: %v", err)
}
defer dst.Close()
written, err := io.Copy(dst, object)
if err != nil {
log.Fatalf("ERROR: failed to copy object: %v", err)
}
log.Printf("copied %d bytes to %s", written, dst.Name())
switch {
case install:
log.Printf("installing extension %s", archivePath)
if err := extensions.Install(key, archivePath, extensionPath); err != nil {
log.Fatalf("ERROR: failed to install extension: %v", err)
}
case uninstall:
log.Printf("uninstalling extension %s", archivePath)
if err := extensions.Uninstall(archivePath); err != nil {
log.Fatalf("ERROR: failed to uninstall extension: %v", err)
}
}
}
func initStorage(storageType extensions.StorageType, endpoint, bucket, region string) extensions.ObjectGetter {
switch storageType {
case extensions.StorageTypeS3:
return extensions.NewS3(endpoint, region, bucket)
default:
log.Fatalf("unknown storage type: %s", os.Getenv("STORAGE_TYPE"))
}
return nil
}