forked from GoogleCloudPlatform/compute-image-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgce_export.go
208 lines (178 loc) · 5.07 KB
/
gce_export.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
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// export streams a local disk to a Google Compute Engine image file in a Google Cloud Storage bucket.
package main
import (
"archive/tar"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"regexp"
"strings"
"time"
"cloud.google.com/go/storage"
humanize "github.com/dustin/go-humanize"
gzip "github.com/klauspost/pgzip"
"google.golang.org/api/option"
)
var (
disk = flag.String("disk", "", "disk to copy, on linux this would be something like '/dev/sda', and on Windows '\\\\.\\PhysicalDrive0'")
gcsPath = flag.String("gcs_path", "", "GCS path to upload the image to, gs://my-bucket/image.tar.gz")
oauth = flag.String("oauth", "", "path to oauth json file")
licenses = flag.String("licenses", "", "comma deliminated list of licenses to add to the image")
noconfirm = flag.Bool("y", false, "skip confirmation")
level = flag.Int("level", 3, "level of compression from 1-9, 1 being best speed, 9 being best compression")
gsRegex = regexp.MustCompile(`^gs://([a-z0-9][-_.a-z0-9]*)/(.+)$`)
)
// progress is a io.Writer that updates total in Write.
type progress struct {
total int64
}
func (p *progress) Write(b []byte) (int, error) {
p.total += int64(len(b))
return len(b), nil
}
func splitLicenses(input string) []string {
if input == "" {
return nil
}
var ls []string
for _, l := range strings.Split(input, ",") {
ls = append(ls, l)
}
return ls
}
func splitGCSPath(p string) (string, string, error) {
matches := gsRegex.FindStringSubmatch(p)
if matches != nil {
return matches[1], matches[2], nil
}
return "", "", fmt.Errorf("%q is not a valid GCS path", p)
}
func main() {
flag.Parse()
if *gcsPath == "" {
log.Fatal("The flag -gcs_path must be provided")
}
if *disk == "" {
log.Fatal("The flag -disk must be provided")
}
bkt, obj, err := splitGCSPath(*gcsPath)
if err != nil {
log.Fatal(err)
}
file, err := os.Open(*disk)
if err != nil {
log.Fatal(err)
}
defer file.Close()
size, err := diskLength(file)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithServiceAccountFile(*oauth))
if err != nil {
log.Fatal(err)
}
w := client.Bucket(bkt).Object(obj).NewWriter(ctx)
up := progress{}
gw, err := gzip.NewWriterLevel(io.MultiWriter(&up, w), *level)
if err != nil {
log.Fatal(err)
}
rp := progress{}
tw := tar.NewWriter(io.MultiWriter(&rp, gw))
ls := splitLicenses(*licenses)
fmt.Printf("Disk %s is %s, compressed size will most likely be much smaller.\n", *disk, humanize.IBytes(uint64(size)))
if ls != nil {
fmt.Printf("Exporting disk with licenses %q to gs://%s/%s.\n", ls, bkt, obj)
} else {
fmt.Printf("Exporting disk to gs://%s/%s.\n", bkt, obj)
}
if !*noconfirm {
fmt.Print("Continue? (y/N): ")
var c string
fmt.Scanln(&c)
c = strings.ToLower(c)
if c != "y" && c != "yes" {
fmt.Println("Aborting")
os.Exit(0)
}
}
fmt.Println("Beginning copy...")
start := time.Now()
if ls != nil {
type lsJSON struct {
Licenses []string `json:"licenses"`
}
body, err := json.Marshal(lsJSON{Licenses: ls})
if err != nil {
log.Fatal(err)
}
if err := tw.WriteHeader(&tar.Header{
Name: "manifest.json",
Size: int64(len(body)),
}); err != nil {
log.Fatal(err)
}
if _, err := tw.Write([]byte(body)); err != nil {
log.Fatal(err)
}
}
if err := tw.WriteHeader(&tar.Header{
Name: "disk.raw",
Size: size,
}); err != nil {
log.Fatal(err)
}
// This function only serves to update progress for the user.
go func() {
time.Sleep(5 * time.Second)
var oldUpload int64
var oldRead int64
var oldSince int64
totalSize := humanize.IBytes(uint64(size))
for {
since := int64(time.Since(start).Seconds())
diskSpd := humanize.IBytes(uint64((rp.total - oldRead) / (since - oldSince)))
upldSpd := humanize.IBytes(uint64((up.total - oldUpload) / (since - oldSince)))
uploadTotal := humanize.IBytes(uint64(up.total))
readTotal := humanize.IBytes(uint64(rp.total))
fmt.Printf("Read %s of %s (%s/sec),", readTotal, totalSize, diskSpd)
fmt.Printf(" total uploaded size: %s (%s/sec)\n", uploadTotal, upldSpd)
oldUpload = up.total
oldRead = rp.total
oldSince = since
time.Sleep(45 * time.Second)
}
}()
if _, err := io.CopyN(tw, file, size); err != nil {
log.Fatal(err)
}
if err := tw.Close(); err != nil {
log.Fatal(err)
}
if err := gw.Close(); err != nil {
log.Fatal(err)
}
if err := w.Close(); err != nil {
log.Fatal(err)
}
fmt.Println("Finished export in", time.Since(start))
}