-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.go
162 lines (147 loc) · 5.03 KB
/
archive.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
// Copyright 2015 The LUCI Authors.
//
// 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.
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/maruel/subcommands"
"github.com/TriggerMail/luci-go/auth"
"github.com/TriggerMail/luci-go/client/archiver"
"github.com/TriggerMail/luci-go/client/internal/common"
"github.com/TriggerMail/luci-go/client/isolated"
"github.com/TriggerMail/luci-go/common/data/text/units"
"github.com/TriggerMail/luci-go/common/isolatedclient"
)
func cmdArchive(defaultAuthOpts auth.Options) *subcommands.Command {
return &subcommands.Command{
UsageLine: "archive <options>...",
ShortDesc: "creates a .isolated file and uploads the tree to an isolate server",
LongDesc: `Given a list of files and directories, creates a .isolated file and uploads the
tree to to an isolate server.
When specifying directories and files, you must also specify a current working
directory for that file or directory. The current working directory will not
be included in the archived path. For example, to isolate './usr/foo/bar' and
have it appear as 'foo/bar' in the .isolated, specify '-files ./usr:foo/bar' or
'-files usr:foo/bar'. When the .isolated is then downloaded, it will then appear
under 'foo/bar' in the desired directory.
Note that '.' may be omitted in general, so to upload 'foo' from the current
working directory, '-files :foo' is sufficient.`,
CommandRun: func() subcommands.CommandRun {
c := archiveRun{}
c.commonFlags.Init(defaultAuthOpts)
c.Flags.Var(&c.dirs, "dirs", "Directory(ies) to archive. Specify as <working directory>:<relative path to dir>")
c.Flags.Var(&c.files, "files", "Individual file(s) to archive. Specify as <working directory>:<relative path to file>")
c.Flags.Var(&c.blacklist, "blacklist",
"List of regexp to use as blacklist filter when uploading directories")
c.Flags.StringVar(&c.dumpHash, "dump-hash", "",
"Write the composite isolated hash to a file")
c.Flags.StringVar(&c.isolated, "isolated", "",
"Write the composite isolated to a file")
return &c
},
}
}
type archiveRun struct {
commonFlags
dirs isolated.ScatterGather
files isolated.ScatterGather
blacklist common.Strings
dumpHash string
isolated string
}
func (c *archiveRun) Parse(a subcommands.Application, args []string) error {
if err := c.commonFlags.Parse(); err != nil {
return err
}
if len(args) != 0 {
return errors.New("position arguments not expected")
}
return nil
}
func (c *archiveRun) main(a subcommands.Application, args []string) (err error) {
start := time.Now()
out := os.Stdout
var authClient *http.Client
authClient, err = c.createAuthClient()
if err != nil {
return
}
isolatedClient := isolatedclient.New(nil, authClient, c.isolatedFlags.ServerURL, c.isolatedFlags.Namespace, nil, nil)
ctx := c.defaultFlags.MakeLoggingContext(os.Stderr)
arch := archiver.New(ctx, isolatedClient, out)
defer func() {
// This waits for all uploads.
if cerr := arch.Close(); err == nil {
err = cerr
return
}
}()
common.CancelOnCtrlC(arch)
opts := isolated.ArchiveOptions{
Files: c.files,
Dirs: c.dirs,
Blacklist: []string(c.blacklist),
Isolated: c.isolated,
}
if len(c.isolated) != 0 {
var dumpIsolated *os.File
dumpIsolated, err = os.Create(c.isolated)
if err != nil {
return
}
// This is OK to close before arch because isolated.Archive
// does the writing (it's not handed off elsewhere).
defer dumpIsolated.Close()
opts.LeakIsolated = dumpIsolated
}
item := isolated.Archive(ctx, arch, &opts)
if err = item.Error(); err != nil {
return
}
item.WaitForHashed()
if len(c.dumpHash) != 0 {
if err = ioutil.WriteFile(c.dumpHash, []byte(item.Digest()), 0644); err != nil {
return
}
}
if !c.defaultFlags.Quiet {
duration := time.Since(start)
stats := arch.Stats()
fmt.Fprintf(os.Stderr, "Hits : %5d (%s)\n", stats.TotalHits(), stats.TotalBytesHits())
fmt.Fprintf(os.Stderr, "Misses : %5d (%s)\n", stats.TotalMisses(), stats.TotalBytesPushed())
fmt.Fprintf(os.Stderr, "Duration: %s\n", units.Round(duration, time.Millisecond))
}
return
}
func (c *archiveRun) Run(a subcommands.Application, args []string, _ subcommands.Env) int {
if err := c.Parse(a, args); err != nil {
fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
return 1
}
cl, err := c.defaultFlags.StartTracing()
if err != nil {
fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
return 1
}
defer cl.Close()
if err := c.main(a, args); err != nil {
fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
return 1
}
return 0
}