-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (97 loc) · 3.22 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
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"net/url"
"os"
"os/signal"
"strings"
"time"
)
var passwordStdin = flag.Bool("password-stdin", false, "if you want to use a password from stdin")
var username = flag.String("username", "", "if you're authenticating, you should set the username")
var compressionLevel = flag.Int("compression-level", 0, "compresssion level\n gzip: from 0 (no compression at all) to 9 (max compression)\nzstd (default): from 0 (no compression at all) to 3 (max compression)")
var compressionAlgorithm = flag.String("compression-algo", Zstd, "compresssion algorithm to use, can be either gzip or zstd")
var nJobs = flag.Int("push-workers", 3, "number of workers that should be asynchronously running")
var dryRun = flag.Bool("dry-run", false, "If you want to inspect the layers of the image")
var inMemory = flag.Bool("in-memory", false, "If you don't want to write to /var/lib/push")
var tryCache = flag.Bool("try-cache", false, "If you want the push tool to try to pre-compress the layer to check cache in remote registry")
func main() {
ctx, cancel := context.WithCancel(context.Background())
signalHandler := make(chan os.Signal, 5)
signal.Notify(signalHandler, os.Interrupt)
go func() {
<-signalHandler
cancel()
}()
defer func() {
cancel()
close(signalHandler)
}()
if len(os.Args) <= 2 {
fmt.Fprintln(os.Stderr, "usage: <command> <image> <url>")
os.Exit(-1)
}
flag.CommandLine.Parse(os.Args[3:])
if *compressionLevel == 0 {
fmt.Println("warning: no compression level means not compressed", os.Args[3:])
}
switch *compressionAlgorithm {
case Zstd, Gzip:
default:
fmt.Fprintln(os.Stderr, "unknown algorithm", *compressionAlgorithm)
fmt.Fprintln(os.Stderr, "only gzip or zstd are allowed")
os.Exit(-1)
}
c := configuration{}
c.username = *username
if *passwordStdin {
r := bufio.NewReader(os.Stdin)
password, err := r.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, "Reading password:", err)
os.Exit(1)
}
c.password = strings.TrimSpace(string(password))
} else if c.username != "" {
fmt.Fprintln(os.Stderr, "we need --password-stdin if you want authentication")
os.Exit(1)
}
n := time.Now()
fmt.Println("> Loading DB into memory")
db, err := newDb()
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading overlay2 database", err.Error())
os.Exit(1)
}
manifest, err := generateManifestFromDocker(ctx, os.Args[1], db)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if *dryRun {
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", "\t")
encoder.Encode(manifest)
return
}
fmt.Println("> Startup was done in", time.Since(n))
p := newPusher(&manifest, db, *nJobs, &c)
urlRaw := os.Args[2]
u, err := url.Parse("http://" + urlRaw)
if err != nil {
fmt.Fprintln(os.Stderr, "Parsing url", err)
os.Exit(1)
}
path := u.Path
tagIndex := strings.LastIndex(path, ":")
domainWithProto := "http://" + u.Host
if err := p.push(ctx, domainWithProto, path[:tagIndex], path[tagIndex+1:], pushConfiguration{compressionLevel: *compressionLevel, algo: *compressionAlgorithm}); err != nil {
fmt.Fprintln(os.Stderr, "pushing image", err.Error())
os.Exit(1)
}
fmt.Println("> Finished in", time.Since(n))
}