-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·175 lines (142 loc) · 3.49 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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"time"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-tools/go-steputils/tools"
"github.com/dt-tools/servermanager"
)
// ConfigsModel ...
type ConfigsModel struct {
WaitForBoot string
BootTimeout string
}
func createConfigsModelFromEnvs() ConfigsModel {
return ConfigsModel{
WaitForBoot: os.Getenv("wait_for_boot"),
BootTimeout: os.Getenv("boot_timeout"),
}
}
func (configs ConfigsModel) print() {
log.Infof("Configs:")
log.Printf("- WaitForBoot: %s", configs.WaitForBoot)
log.Printf("- BootTimeout: %s", configs.BootTimeout)
}
func (configs ConfigsModel) validate() error {
if configs.WaitForBoot == "" {
return errors.New("no WaitForBoot parameter specified")
}
if configs.BootTimeout == "" {
return errors.New("no BootTimeout parameter specified")
}
return nil
}
func failf(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}
func main() {
configs := createConfigsModelFromEnvs()
fmt.Println()
configs.print()
if err := configs.validate(); err != nil {
failf("Issue with input: %s", err)
}
// ---
server, err := servermanager.New()
if err != nil {
failf("Failed to create server model, error: %s", err)
}
//
// Start AVD image
fmt.Println()
log.Infof("Start Server")
options := []string{}
startServerCommand := server.StartServerCommand(options...)
startServerCmd := startServerCommand.GetCmd()
e := make(chan error)
// Redirect output
stdoutReader, err := startServerCmd.StdoutPipe()
if err != nil {
failf("Failed to redirect output, error: %s", err)
}
outScanner := bufio.NewScanner(stdoutReader)
go func() {
for outScanner.Scan() {
line := outScanner.Text()
fmt.Println(line)
}
}()
if err := outScanner.Err(); err != nil {
failf("Scanner failed, error: %s", err)
}
// Redirect error
stderrReader, err := startServerCmd.StderrPipe()
if err != nil {
failf("Failed to redirect error, error: %s", err)
}
errScanner := bufio.NewScanner(stderrReader)
go func() {
for errScanner.Scan() {
line := errScanner.Text()
log.Warnf(line)
}
}()
if err := errScanner.Err(); err != nil {
failf("Scanner failed, error: %s", err)
}
// ---
go func() {
// Start emulator
log.Printf("$ %s", command.PrintableCommandArgs(false, startServerCmd.Args))
fmt.Println()
if err := startServerCmd.Run(); err != nil {
e <- err
return
}
}()
go func() {
// Wait until device is booted
if configs.WaitForBoot == "true" {
bootInProgress := true
for bootInProgress {
time.Sleep(5 * time.Second)
log.Printf("> Checking if server booted...")
booted, err := server.IsBooted()
if err != nil {
e <- err
return
}
bootInProgress = !booted
}
log.Donef("> server booted")
}
e <- nil
}()
timeout, err := strconv.ParseInt(configs.BootTimeout, 10, 64)
if err != nil {
failf("Failed to parse BootTimeout parameter, error: %s", err)
}
select {
case <-time.After(time.Duration(timeout) * time.Second):
if err := startServerCmd.Process.Kill(); err != nil {
failf("Failed to kill server command, error: %s", err)
}
failf("Start server timed out")
case err := <-e:
if err != nil {
failf("Failed to start server, error: %s", err)
}
}
// ---
if err := tools.ExportEnvironmentWithEnvman("BITRISE_SERVER_READY", "true"); err != nil {
log.Warnf("Failed to export environment (BITRISE_SERVER_READY), error: %s", err)
}
fmt.Println()
log.Donef("Server booted")
}