/
main.go
189 lines (163 loc) · 4.95 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
184
185
186
187
188
189
package main
import (
"fmt"
"github.com/docker/go-units"
"github.com/go-debos/fakemachine"
"github.com/jessevdk/go-flags"
"os"
"strings"
)
type Options struct {
Backend string `short:"b" long:"backend" description:"Virtualisation backend to use" default:"auto"`
Volumes []string `short:"v" long:"volume" description:"volume to mount"`
Images []string `short:"i" long:"image" description:"image to add"`
EnvironVars map[string]string `short:"e" long:"environ-var" description:"Environment variables (use -e VARIABLE:VALUE syntax)"`
Memory int `short:"m" long:"memory" description:"Amount of memory for the fakemachine in megabytes"`
CPUs int `short:"c" long:"cpus" description:"Number of CPUs for the fakemachine"`
ScratchSize string `short:"s" long:"scratchsize" description:"On-disk scratch space size (with a unit suffix, e.g. 4G); if unset, memory backed scratch space is used"`
ShowBoot bool `long:"show-boot" description:"Show boot/console messages from the fakemachine"`
}
var options Options
var parser = flags.NewParser(&options, flags.Default)
func warnLocalhost(variable string, value string) {
message := `WARNING: Environment variable %[1]s contains a reference to
localhost. This may not work when running from fakemachine.
Consider using an address that is valid on your network.`
if strings.Contains(value, "localhost") ||
strings.Contains(value, "127.0.0.1") ||
strings.Contains(value, "::1") {
fmt.Printf(message, variable)
}
}
func SetupVolumes(m *fakemachine.Machine, options Options) {
for _, v := range options.Volumes {
parts := strings.Split(v, ":")
switch len(parts) {
case 1:
m.AddVolume(parts[0])
case 2:
m.AddVolumeAt(parts[0], parts[1])
default:
fmt.Fprintln(os.Stderr, "Failed to parse volume:", v)
os.Exit(1)
}
}
}
func SetupImages(m *fakemachine.Machine, options Options) {
for _, i := range options.Images {
parts := strings.Split(i, ":")
var err error
var l string
switch len(parts) {
case 1:
l, err = m.CreateImage(parts[0], -1)
case 2:
var size int64
size, err = units.FromHumanSize(parts[1])
if err != nil {
break
}
l, err = m.CreateImage(parts[0], size)
default:
fmt.Fprintf(os.Stderr, "Failed to parse image: %s\n", i)
os.Exit(1)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create image: %s %v\n", i, err)
os.Exit(1)
}
fmt.Printf("Exposing %s as %s\n", parts[0], l)
}
}
func SetupEnviron(m *fakemachine.Machine, options Options) {
// Initialize environment variables map
EnvironVars := make(map[string]string)
// These are the environment variables that will be detected on the
// host and propagated to fakemachine. These are listed lower case, but
// they are detected and configured in both lower case and upper case.
var environ_vars = [...]string {
"http_proxy",
"https_proxy",
"ftp_proxy",
"rsync_proxy",
"all_proxy",
"no_proxy",
}
// First add variables from host
for _, e := range environ_vars {
lowerVar := strings.ToLower(e) // lowercase not really needed
lowerVal := os.Getenv(lowerVar)
if lowerVal != "" {
EnvironVars[lowerVar] = lowerVal
}
upperVar := strings.ToUpper(e)
upperVal := os.Getenv(upperVar)
if upperVal != "" {
EnvironVars[upperVar] = upperVal
}
}
// Then add/overwrite with variables from command line
for k, v := range options.EnvironVars {
// Allows the user to unset environ variables with -e
if v == "" {
delete(EnvironVars, k)
} else {
EnvironVars[k] = v
}
}
// Puts in a format that is compatible with output of os.Environ()
if EnvironVars != nil {
EnvironString := []string{}
for k, v := range EnvironVars {
warnLocalhost(k, v)
EnvironString = append(EnvironString, fmt.Sprintf("%s=%s", k, v))
}
m.SetEnviron(EnvironString) // And save the resulting environ vars on m
}
}
func main() {
// append the list of available backends to the commandline argument parser
opt := parser.FindOptionByLongName("backend")
opt.Choices = fakemachine.BackendNames()
args, err := parser.Parse()
if err != nil {
flagsErr, ok := err.(*flags.Error)
if ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
m, err := fakemachine.NewMachineWithBackend(options.Backend)
if err != nil {
fmt.Printf("fakemachine: %v\n", err)
os.Exit(1)
}
m.SetShowBoot(options.ShowBoot)
SetupVolumes(m, options)
SetupImages(m, options)
SetupEnviron(m, options)
if options.ScratchSize != "" {
size, err := units.FromHumanSize(options.ScratchSize)
if err != nil {
fmt.Fprintf(os.Stderr, "fakemachine: Couldn't parse scratch size: %v\n", err)
os.Exit(1)
}
m.SetScratch(size, "")
}
if options.Memory > 0 {
m.SetMemory(options.Memory)
}
if options.CPUs > 0 {
m.SetNumCPUs(options.CPUs)
}
command := "/bin/bash"
if len(args) > 0 {
command = strings.Join(args, " ")
}
ret, err := m.Run(command)
if err != nil {
fmt.Fprintf(os.Stderr, "fakemachine: %v\n", err)
}
os.Exit(ret)
}