-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (141 loc) · 4.46 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
package main
import (
"./package"
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/docker/docker/api/types"
. "github.com/docker/docker/client"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
const defaultDockerAPIVersion = "v1.40"
func UploadToS3(bucket string,repository string, filename string) {
time := time.Now().Local().Format("2006.01.02-15:04:05")
file, err := os.Open("/tmp/"+filename)
if err != nil {
exitErrorf("Unable to open file %q, %v", err)
}
defer file.Close()
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
svc := s3.New(sess)
_, err = svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(repository),
})
uploader := s3manager.NewUploader(sess)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(repository+"/"+time+"_"+filename),
Body: file,
})
if err != nil {
exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err)
}
fmt.Printf("Successfully uploaded %q to %q\n", filename, bucket)
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
func GetTagListFromDockerAPI(username string, password string,org string,repository string) _package.Tags{
//do login
payloadBytes, err := json.Marshal(_package.Payload{Username: username, Password:password })
request, err := http.NewRequest("POST", "https://hub.docker.com/v2/users/login/", bytes.NewReader(payloadBytes))
request.Header.Set("Content-Type", "application/json")
response, err := http.DefaultClient.Do(request)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
//get token
bodyBytes, err := ioutil.ReadAll(response.Body)
var token _package.Token
err = json.Unmarshal(bodyBytes, &token)
if err != nil {
log.Fatal(err)
}
//lists tags for a repository
request, err = http.NewRequest("GET", os.ExpandEnv("https://hub.docker.com/v2/repositories/"+org+"/"+repository+"/tags/?page_size=10000"), nil)
request.Header.Set("Authorization", os.ExpandEnv("JWT "+token.Token))
response, err = http.DefaultClient.Do(request)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
bodyBytes, err = ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var tags _package.Tags
err = json.Unmarshal(bodyBytes, &tags)
return tags
}
func DoBackupFromDockerCli(username string,password string,org string, repository string,tag _package.ResultTags,bucket string) {
//pull image by tag
client, err := NewClientWithOpts(WithVersion(defaultDockerAPIVersion))
authConfig := types.AuthConfig{
Username: username,
Password: password,
}
encodedJSON, err := json.Marshal(authConfig)
out, err := client.ImagePull(context.Background(),org+"/"+repository+":"+tag.Name,types.ImagePullOptions{RegistryAuth: base64.URLEncoding.EncodeToString(encodedJSON)})
if err != nil {
log.Fatal(err)
}
defer out.Close()
io.Copy(os.Stdout, out)
//save image
var imageSlice []string
imageSlice = append(imageSlice,org+"/"+ repository+":"+tag.Name)
image,_ := client.ImageSave(context.Background(),imageSlice)
buf := new(bytes.Buffer)
buf.ReadFrom(image)
//create file and compress image
writer, err := os.Create("/tmp/"+repository+":"+tag.Name+"-"+tag.LastUpdated+".gz")
if err != nil {
log.Fatal(err)
}
defer writer.Close()
archiver := gzip.NewWriter(writer)
archiver.Name = repository+":"+tag.Name
defer archiver.Close()
io.Copy(archiver, buf)
//remove image
_,err = client.ImageRemove(context.Background(),org+"/"+ repository+":"+tag.Name,types.ImageRemoveOptions{})
if err != nil {
log.Fatal(err)
}
//upload to S3
UploadToS3(bucket,repository,repository+":"+tag.Name+"-"+tag.LastUpdated+".gz")
}
func main() {
if len(os.Args) != 6 {
exitErrorf("The repository name, organization name, Docker Username, Docker Password and Bucket name are required.(5 args)\n")
}
fmt.Println("Starting...")
repository := os.Args[1]
org := os.Args[2]
username:= os.Args[3]
password:= os.Args[4]
bucket:= os.Args[5]
tags:=GetTagListFromDockerAPI(username,password,org,repository)
fmt.Println(len(tags.Results),"tags found...")
for index := 0 ;index < len(tags.Results);index++{
DoBackupFromDockerCli(username,password,org,repository,tags.Results[index],bucket)
}
fmt.Println("Finished.")
}