@@ -18,6 +18,7 @@ package bootstrap
18
18
import (
19
19
"context"
20
20
"fmt"
21
+ "net"
21
22
"os"
22
23
"os/exec"
23
24
"os/signal"
@@ -80,7 +81,7 @@ Follow these instructions to create a token (we don't store any tokens):
80
81
githubClient := & Client {client : github .NewClient (tp .Client ()), ctx : context .Background ()}
81
82
82
83
// Fork terraform example repo.
83
- colorstring .Printf ("\n [white]=> forking repo " )
84
+ colorstring .Println ("\n [white]=> forking repo " )
84
85
s .Start ()
85
86
if err := githubClient .CreateFork (terraformExampleRepoOwner , terraformExampleRepo ); err != nil {
86
87
return errors .Wrapf (err , "forking repo %s/%s" , terraformExampleRepoOwner , terraformExampleRepo )
@@ -89,19 +90,19 @@ Follow these instructions to create a token (we don't store any tokens):
89
90
return fmt .Errorf ("didn't find forked repo %s/%s. fork unsuccessful" , terraformExampleRepoOwner , terraformExampleRepoOwner )
90
91
}
91
92
s .Stop ()
92
- colorstring .Println ("\n [green]=> fork completed!" )
93
+ colorstring .Println ("[green]=> fork completed!" )
93
94
94
95
// Detect terraform and install it if not installed.
95
96
_ , err := exec .LookPath ("terraform" )
96
97
if err != nil {
97
98
colorstring .Println ("[yellow]=> terraform not found in $PATH." )
98
- colorstring .Printf ("[white]=> downloading terraform " )
99
+ colorstring .Println ("[white]=> downloading terraform " )
99
100
s .Start ()
100
101
terraformDownloadURL := fmt .Sprintf ("%s/terraform/%s/terraform_%s_%s_%s.zip" , hashicorpReleasesURL , terraformVersion , terraformVersion , runtime .GOOS , runtime .GOARCH )
101
102
if err = downloadAndUnzip (terraformDownloadURL , "/tmp/terraform.zip" , "/tmp" ); err != nil {
102
103
return errors .Wrapf (err , "downloading and unzipping terraform" )
103
104
}
104
- colorstring .Println ("\n [green]=> downloaded terraform successfully!" )
105
+ colorstring .Println ("[green]=> downloaded terraform successfully!" )
105
106
s .Stop ()
106
107
107
108
var terraformCmd * exec.Cmd
@@ -116,18 +117,27 @@ Follow these instructions to create a token (we don't store any tokens):
116
117
}
117
118
118
119
// Download ngrok.
119
- colorstring .Printf ("[white]=> downloading ngrok " )
120
+ colorstring .Println ("[white]=> downloading ngrok " )
120
121
s .Start ()
121
122
ngrokURL := fmt .Sprintf ("%s/ngrok-stable-%s-%s.zip" , ngrokDownloadURL , runtime .GOOS , runtime .GOARCH )
122
123
if err = downloadAndUnzip (ngrokURL , "/tmp/ngrok.zip" , "/tmp" ); err != nil {
123
124
return errors .Wrapf (err , "downloading and unzipping ngrok" )
124
125
}
125
126
s .Stop ()
126
- colorstring .Println ("\n [green]=> downloaded ngrok successfully!" )
127
+ colorstring .Println ("[green]=> downloaded ngrok successfully!" )
127
128
128
129
// Create ngrok tunnel.
129
- colorstring .Printf ("[white]=> creating secure tunnel " )
130
+ colorstring .Println ("[white]=> creating secure tunnel" )
130
131
s .Start ()
132
+ // Check if there is already an ngrok running by seeing if there's already
133
+ // something bound to its API port.
134
+ conn , err := net .Dial ("tcp" , ngrokAPIURL )
135
+ // We expect an error.
136
+ if err == nil {
137
+ conn .Close () // nolint: errcheck
138
+ return errors .New ("unable to start ngrok because there is already something bound to its API port: " + ngrokAPIURL )
139
+ }
140
+
131
141
ngrokCmd , err := executeCmd ("/tmp/ngrok" , []string {"http" , "4141" })
132
142
if err != nil {
133
143
return errors .Wrapf (err , "creating ngrok tunnel" )
@@ -143,15 +153,15 @@ Follow these instructions to create a token (we don't store any tokens):
143
153
// Wait for the tunnel to be up.
144
154
time .Sleep (2 * time .Second )
145
155
s .Stop ()
146
- colorstring .Println ("\n [green]=> started tunnel!" )
156
+ colorstring .Println ("[green]=> started tunnel!" )
147
157
tunnelURL , err := getTunnelAddr ()
148
158
if err != nil {
149
159
return errors .Wrapf (err , "getting tunnel url" )
150
160
}
151
161
s .Stop ()
152
162
153
163
// Start atlantis server.
154
- colorstring .Printf ("[white]=> starting atlantis server " )
164
+ colorstring .Println ("[white]=> starting atlantis server" )
155
165
s .Start ()
156
166
atlantisCmd , err := executeCmd (os .Args [0 ], []string {"server" , "--gh-user" , githubUsername , "--gh-token" , githubToken , "--data-dir" , "/tmp/atlantis/data" , "--atlantis-url" , tunnelURL , "--repo-whitelist" , fmt .Sprintf ("github.com/%s/%s" , githubUsername , terraformExampleRepo )})
157
167
if err != nil {
@@ -164,31 +174,31 @@ Follow these instructions to create a token (we don't store any tokens):
164
174
}()
165
175
// When this function returns atlantis server should be stopped.
166
176
defer atlantisCmd .Process .Kill ()
167
- colorstring .Printf ("\n [green]=> atlantis server is now securely exposed at [bold][underline]%s" , tunnelURL )
177
+ colorstring .Printf ("[green]=> atlantis server is now securely exposed at [bold][underline]%s\n " , tunnelURL )
168
178
fmt .Println ("" )
169
179
170
180
// Create atlantis webhook.
171
- colorstring .Printf ("[white]=> creating atlantis webhook " )
181
+ colorstring .Println ("[white]=> creating atlantis webhook" )
172
182
s .Start ()
173
183
err = githubClient .CreateWebhook (githubUsername , terraformExampleRepo , fmt .Sprintf ("%s/events" , tunnelURL ))
174
184
if err != nil {
175
185
return errors .Wrapf (err , "creating atlantis webhook" )
176
186
}
177
187
s .Stop ()
178
- colorstring .Println ("\n [green]=> atlantis webhook created!" )
188
+ colorstring .Println ("[green]=> atlantis webhook created!" )
179
189
180
190
// Create a new pr in the example repo.
181
- colorstring .Printf ("[white]=> creating a new pull request " )
191
+ colorstring .Println ("[white]=> creating a new pull request" )
182
192
s .Start ()
183
193
pullRequestURL , err := githubClient .CreatePullRequest (githubUsername , terraformExampleRepo , "example" , "master" )
184
194
if err != nil {
185
195
return errors .Wrapf (err , "creating new pull request for repo %s/%s" , githubUsername , terraformExampleRepo )
186
196
}
187
197
s .Stop ()
188
- colorstring .Println ("\n [green]=> pull request created!" )
198
+ colorstring .Println ("[green]=> pull request created!" )
189
199
190
200
// Open new pull request in the browser.
191
- colorstring .Printf ("[white]=> opening pull request " )
201
+ colorstring .Println ("[white]=> opening pull request" )
192
202
s .Start ()
193
203
time .Sleep (2 * time .Second )
194
204
_ , err = executeCmd ("open" , []string {pullRequestURL })
@@ -198,7 +208,7 @@ Follow these instructions to create a token (we don't store any tokens):
198
208
s .Stop ()
199
209
200
210
// Wait for ngrok and atlantis server process to finish.
201
- colorstring .Printf ( " \n [_green_][light_green]atlantis is running " )
211
+ colorstring .Println ( " [_green_][light_green]atlantis is running " )
202
212
s .Start ()
203
213
colorstring .Println ("[green] [press Ctrl-c to exit]" )
204
214
0 commit comments