-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (159 loc) · 4.43 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Copyright (c) 2017 Red Hat, Inc.
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.
*/
// This is used to embed the project configuration into the binary of the tool,
// so that during run-time there is no need to have both the binary and the
// configuration files.
//
//go:generate go run scripts/embed.go -directory ../../.. -output tools/src/ovc/embedded.go project.conf image-specifications os-manifests
package main
// This tool loads and builds all the images.
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"ovc/build"
"ovc/log"
)
// ToolFunc is the type of functions that implement tools.
//
type ToolFunc func(project *build.Project) error
// This index contains the mapping from names to tool functions.
//
var tools = map[string]ToolFunc{
"build": buildTool,
"clean": cleanTool,
"deploy": deployTool,
"push": pushTool,
"save": saveTool,
}
// The name of the project file.
//
const conf = "project.conf"
func main() {
// Get the name of the tool:
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s TOOL [ARGS...]\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
name := os.Args[1]
// Find the function that corresponds to the tool name:
tool := tools[name]
if tool == nil {
fmt.Fprintf(os.Stderr, "Can't find tool named '%s'.\n", name)
os.Exit(1)
}
// Run the tool inside a different function, so that we can take
// advantage of the 'defer' mechanism:
os.Exit(run(name, tool))
}
func run(name string, tool ToolFunc) int {
// Open the log:
log.Open(name)
log.Info("Log file is '%s'", log.Path())
defer log.Close()
// Check if the project file exists. If doesn't exist then we
// need extract it, together with the rest of the source files
// of the project, from the embedded data.
file, _ := filepath.Abs(conf)
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Info("Extracting project")
tmp, err := ioutil.TempDir("", "project")
if err != nil {
log.Error("Can't create temporary directory for project: %s", err)
os.Exit(1)
}
defer os.RemoveAll(tmp)
err = extractData(embedded, tmp)
if err != nil {
log.Error("Can't extract project: %s", err)
os.Exit(1)
}
file = filepath.Join(tmp, conf)
}
// Load the project:
log.Info("Loading project file '%s'", file)
project, err := build.LoadProject(file)
if err != nil {
log.Error("%s", err)
return 1
}
defer project.Close()
// Call the tool function:
log.Debug("Running tool '%s'", name)
err = tool(project)
if err != nil {
log.Error("%s", err)
log.Error("Tool failed, check log file '%s' for details", log.Path())
return 1
} else {
log.Info("Tool finished successfully")
return 0
}
}
func extractData(data []byte, dir string) error {
// Open the data archive:
buffer := bytes.NewReader(data)
expand, err := gzip.NewReader(buffer)
if err != nil {
return err
}
archive := tar.NewReader(expand)
// Iterate through the entries of the archive and extract them
// to the output directory:
for {
header, err := archive.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
switch header.Typeflag {
case tar.TypeReg:
err = extractFile(archive, header, dir)
case tar.TypeDir:
err = extractDir(archive, header, dir)
default:
err = nil
}
if err != nil {
return err
}
}
return nil
}
func extractFile(archive *tar.Reader, header *tar.Header, dir string) error {
// Create the file:
path := filepath.Join(dir, header.Name)
info := header.FileInfo()
log.Debug("Extracting file '%s' to '%s'", header.Name, path)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode().Perm())
if err != nil {
return err
}
defer file.Close()
// Copy the contents:
_, err = io.Copy(file, archive)
return err
}
func extractDir(archive *tar.Reader, header *tar.Header, dir string) error {
path := filepath.Join(dir, header.Name)
info := header.FileInfo()
log.Debug("Extracting directory '%s' to '%s'", header.Name, path)
return os.Mkdir(path, info.Mode().Perm())
}