Skip to content

Commit 2dbb894

Browse files
authored
Merge pull request #81 from runatlantis/bootstrap
Error during atlantis bootstrap if ngrok running
2 parents 9ce2704 + dcb73d2 commit 2dbb894

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

bootstrap/bootstrap.go

+26-16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package bootstrap
1818
import (
1919
"context"
2020
"fmt"
21+
"net"
2122
"os"
2223
"os/exec"
2324
"os/signal"
@@ -80,7 +81,7 @@ Follow these instructions to create a token (we don't store any tokens):
8081
githubClient := &Client{client: github.NewClient(tp.Client()), ctx: context.Background()}
8182

8283
// Fork terraform example repo.
83-
colorstring.Printf("\n[white]=> forking repo ")
84+
colorstring.Println("\n[white]=> forking repo ")
8485
s.Start()
8586
if err := githubClient.CreateFork(terraformExampleRepoOwner, terraformExampleRepo); err != nil {
8687
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):
8990
return fmt.Errorf("didn't find forked repo %s/%s. fork unsuccessful", terraformExampleRepoOwner, terraformExampleRepoOwner)
9091
}
9192
s.Stop()
92-
colorstring.Println("\n[green]=> fork completed!")
93+
colorstring.Println("[green]=> fork completed!")
9394

9495
// Detect terraform and install it if not installed.
9596
_, err := exec.LookPath("terraform")
9697
if err != nil {
9798
colorstring.Println("[yellow]=> terraform not found in $PATH.")
98-
colorstring.Printf("[white]=> downloading terraform ")
99+
colorstring.Println("[white]=> downloading terraform ")
99100
s.Start()
100101
terraformDownloadURL := fmt.Sprintf("%s/terraform/%s/terraform_%s_%s_%s.zip", hashicorpReleasesURL, terraformVersion, terraformVersion, runtime.GOOS, runtime.GOARCH)
101102
if err = downloadAndUnzip(terraformDownloadURL, "/tmp/terraform.zip", "/tmp"); err != nil {
102103
return errors.Wrapf(err, "downloading and unzipping terraform")
103104
}
104-
colorstring.Println("\n[green]=> downloaded terraform successfully!")
105+
colorstring.Println("[green]=> downloaded terraform successfully!")
105106
s.Stop()
106107

107108
var terraformCmd *exec.Cmd
@@ -116,18 +117,27 @@ Follow these instructions to create a token (we don't store any tokens):
116117
}
117118

118119
// Download ngrok.
119-
colorstring.Printf("[white]=> downloading ngrok ")
120+
colorstring.Println("[white]=> downloading ngrok ")
120121
s.Start()
121122
ngrokURL := fmt.Sprintf("%s/ngrok-stable-%s-%s.zip", ngrokDownloadURL, runtime.GOOS, runtime.GOARCH)
122123
if err = downloadAndUnzip(ngrokURL, "/tmp/ngrok.zip", "/tmp"); err != nil {
123124
return errors.Wrapf(err, "downloading and unzipping ngrok")
124125
}
125126
s.Stop()
126-
colorstring.Println("\n[green]=> downloaded ngrok successfully!")
127+
colorstring.Println("[green]=> downloaded ngrok successfully!")
127128

128129
// Create ngrok tunnel.
129-
colorstring.Printf("[white]=> creating secure tunnel ")
130+
colorstring.Println("[white]=> creating secure tunnel")
130131
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+
131141
ngrokCmd, err := executeCmd("/tmp/ngrok", []string{"http", "4141"})
132142
if err != nil {
133143
return errors.Wrapf(err, "creating ngrok tunnel")
@@ -143,15 +153,15 @@ Follow these instructions to create a token (we don't store any tokens):
143153
// Wait for the tunnel to be up.
144154
time.Sleep(2 * time.Second)
145155
s.Stop()
146-
colorstring.Println("\n[green]=> started tunnel!")
156+
colorstring.Println("[green]=> started tunnel!")
147157
tunnelURL, err := getTunnelAddr()
148158
if err != nil {
149159
return errors.Wrapf(err, "getting tunnel url")
150160
}
151161
s.Stop()
152162

153163
// Start atlantis server.
154-
colorstring.Printf("[white]=> starting atlantis server ")
164+
colorstring.Println("[white]=> starting atlantis server")
155165
s.Start()
156166
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)})
157167
if err != nil {
@@ -164,31 +174,31 @@ Follow these instructions to create a token (we don't store any tokens):
164174
}()
165175
// When this function returns atlantis server should be stopped.
166176
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)
168178
fmt.Println("")
169179

170180
// Create atlantis webhook.
171-
colorstring.Printf("[white]=> creating atlantis webhook ")
181+
colorstring.Println("[white]=> creating atlantis webhook")
172182
s.Start()
173183
err = githubClient.CreateWebhook(githubUsername, terraformExampleRepo, fmt.Sprintf("%s/events", tunnelURL))
174184
if err != nil {
175185
return errors.Wrapf(err, "creating atlantis webhook")
176186
}
177187
s.Stop()
178-
colorstring.Println("\n[green]=> atlantis webhook created!")
188+
colorstring.Println("[green]=> atlantis webhook created!")
179189

180190
// 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")
182192
s.Start()
183193
pullRequestURL, err := githubClient.CreatePullRequest(githubUsername, terraformExampleRepo, "example", "master")
184194
if err != nil {
185195
return errors.Wrapf(err, "creating new pull request for repo %s/%s", githubUsername, terraformExampleRepo)
186196
}
187197
s.Stop()
188-
colorstring.Println("\n[green]=> pull request created!")
198+
colorstring.Println("[green]=> pull request created!")
189199

190200
// Open new pull request in the browser.
191-
colorstring.Printf("[white]=> opening pull request ")
201+
colorstring.Println("[white]=> opening pull request")
192202
s.Start()
193203
time.Sleep(2 * time.Second)
194204
_, err = executeCmd("open", []string{pullRequestURL})
@@ -198,7 +208,7 @@ Follow these instructions to create a token (we don't store any tokens):
198208
s.Stop()
199209

200210
// 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 ")
202212
s.Start()
203213
colorstring.Println("[green] [press Ctrl-c to exit]")
204214

bootstrap/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
var hashicorpReleasesURL = "https://releases.hashicorp.com"
3131
var terraformVersion = "0.10.8"
3232
var ngrokDownloadURL = "https://bin.equinox.io/c/4VmDzA7iaHb"
33-
var ngrokAPIURL = "http://localhost:4040"
33+
var ngrokAPIURL = "localhost:4040"
3434

3535
func readPassword() (string, error) {
3636
password, err := terminal.ReadPassword(syscall.Stdin)
@@ -90,7 +90,7 @@ func unzip(archive, target string) error {
9090
}
9191

9292
func getTunnelAddr() (string, error) {
93-
response, err := http.Get(fmt.Sprintf("%s/api/tunnels", ngrokAPIURL))
93+
response, err := http.Get(fmt.Sprintf("http://%s/api/tunnels", ngrokAPIURL))
9494
if err != nil {
9595
return "", err
9696
}

cmd/bootstrap.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ func (b *BootstrapCmd) Init() *cobra.Command {
3636
}
3737
return err
3838
},
39+
SilenceErrors: true,
3940
}
4041
}

0 commit comments

Comments
 (0)