Skip to content

Commit 53290b8

Browse files
authored
Merge pull request #436 from nanobox-io/feature/events
submit events to the production nanobox servers
2 parents 789f883 + 27115b9 commit 53290b8

File tree

7 files changed

+127
-10
lines changed

7 files changed

+127
-10
lines changed

commands/commands.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"github.com/nanobox-io/nanobox/commands/registry"
1212
"github.com/nanobox-io/nanobox/commands/server"
1313
"github.com/nanobox-io/nanobox/models"
14+
"github.com/nanobox-io/nanobox/processors"
1415
"github.com/nanobox-io/nanobox/util/config"
1516
"github.com/nanobox-io/nanobox/util/display"
16-
"github.com/nanobox-io/nanobox/util/mixpanel"
1717
"github.com/nanobox-io/nanobox/util/update"
1818
)
1919

@@ -41,8 +41,9 @@ var (
4141
Long: ``,
4242
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
4343

44-
// report the command usage to mixpanel
45-
mixpanel.Report(strings.Replace(ccmd.CommandPath(), "nanobox ", "", 1))
44+
// report the command to nanobox
45+
processors.SubmitLog(strings.Replace(ccmd.CommandPath(), "nanobox ", "", 1))
46+
// mixpanel.Report(strings.Replace(ccmd.CommandPath(), "nanobox ", "", 1))
4647

4748
// alert the user if an update is needed
4849
update.Check()

models/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ type Config struct {
2323
DockerMachineNetworkSpace string `json:"docker-machine-network-space"`
2424
NativeNetworkSpace string `json:"native-network-space"`
2525

26-
LockPort int `json:"lock-port"`
26+
Anonymous bool `json:"anonymous"`
27+
LockPort int `json:"lock-port"`
2728
}
2829

2930
// Save persists the Config to the database

processors/configure_set.go

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func ConfigureSet(key, val string) error {
3232
config.LockPort, _ = strconv.Atoi(val)
3333
case "ci-mode", "ci_mode":
3434
config.CIMode = val == "true" || val == "t" || val == "1"
35+
case "anonymous":
36+
config.Anonymous = val == "true" || val == "t" || val == "1"
3537
}
3638

3739
return config.Save()

processors/submit_log.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package processors
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"runtime"
7+
8+
"github.com/nanobox-io/nanobox/models"
9+
"github.com/nanobox-io/nanobox/util"
10+
"github.com/nanobox-io/nanobox/util/config"
11+
"github.com/nanobox-io/nanobox/util/odin"
12+
)
13+
14+
func SubmitLog(args string) error {
15+
// if we are running as privilage we dont submit
16+
if util.IsPrivileged() {
17+
return nil
18+
}
19+
20+
auth, _ := models.LoadAuth()
21+
conf, _ := models.LoadConfig()
22+
if conf.CIMode {
23+
return nil
24+
}
25+
26+
if auth.Key == "" && !conf.Anonymous {
27+
err := Login("", "", "")
28+
if err != nil {
29+
return err
30+
}
31+
}
32+
33+
app := ""
34+
35+
env, err := models.FindEnvByID(config.EnvID())
36+
if strings.Contains(args, "deploy") || strings.Contains(args, "tunnel") || strings.Contains(args, "console") {
37+
if err == nil {
38+
remote, ok := env.Remotes["default"]
39+
if ok {
40+
app = remote.ID
41+
}
42+
}
43+
}
44+
45+
// tell nanobox
46+
go odin.SubmitEvent(
47+
fmt.Sprintf("desktop%s", strings.Replace(args, " ", "/", -1)),
48+
fmt.Sprintf("desktop command: nanobox %s", args),
49+
app,
50+
map[string]interface{}{
51+
"os": runtime.GOOS,
52+
"provider": conf.Provider,
53+
"mount-type": conf.MountType,
54+
"boxfile": env.UserBoxfile,
55+
},
56+
)
57+
58+
return nil
59+
}

scripts/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ set -e
99
printf "\nBuilding nanobox...\n"
1010

1111
# build nanobox
12-
gox -ldflags "-X main.bugsnagToken=$BUGSNAG_TOKEN -X github.com/nanobox-io/nanobox/util/mixpanel.token=$MIXPANEL_TOKEN" -osarch "darwin/amd64 linux/amd64 windows/amd64" -output="./.build/v2/{{.OS}}/{{.Arch}}/nanobox"
12+
gox -ldflags "-X main.bugsnagToken=$BUGSNAG_TOKEN -X github.com/nanobox-io/nanobox/util/odin.apiKey=$API_KEY" -osarch "darwin/amd64 linux/amd64 windows/amd64" -output="./.build/v2/{{.OS}}/{{.Arch}}/nanobox"
1313

1414
printf "\nBuilding nanobox updater...\n"
1515

util/display/command_error.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import (
88
"strings"
99

1010
"github.com/nanobox-io/nanobox/commands/registry"
11+
"github.com/nanobox-io/nanobox/models"
1112
"github.com/nanobox-io/nanobox/util"
13+
"github.com/nanobox-io/nanobox/util/config"
14+
"github.com/nanobox-io/nanobox/util/odin"
15+
1216
)
1317

1418
var (
@@ -35,10 +39,34 @@ func CommandErr(err error) {
3539

3640
cause, context := parseCommandErr(err)
3741

38-
fmt.Println()
39-
fmt.Printf("Error : %s\n", cause)
40-
fmt.Printf("Context : %s\n", context)
41-
fmt.Println()
42+
output := fmt.Sprintf(`
43+
Error : %s
44+
Context : %s
45+
`, cause, context)
46+
47+
app := ""
48+
env, err := models.FindEnvByID(config.EnvID())
49+
if err == nil {
50+
remote, ok := env.Remotes["default"]
51+
if ok {
52+
app = remote.ID
53+
}
54+
}
55+
56+
// submit error to nanobox
57+
odin.SubmitEvent(
58+
"desktop#error",
59+
"an error occurred running nanobox desktop",
60+
app,
61+
map[string]interface{}{
62+
"error": cause,
63+
"context": context,
64+
"boxfile": env.UserBoxfile,
65+
},
66+
)
67+
68+
// display error to user
69+
fmt.Println(output)
4270

4371
if runtime.GOOS == "windows" {
4472
// The update process was spawned in a separate window, which will

util/odin/odin.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
var (
2929
// set the default endpoint to nanobox
3030
endpoint = "nanobox"
31+
apiKey string
3132
)
3233

3334
type (
@@ -154,6 +155,29 @@ func GetPreviousBuild(appID string) (string, error) {
154155
return "", nil
155156
}
156157

158+
func SubmitEvent(action, message, app string, meta map[string]interface{}) error {
159+
params := url.Values{}
160+
params.Set("api_key", apiKey)
161+
162+
request := struct {
163+
Action string `json:"action"`
164+
App string `json:"eventable_id,omitempty"`
165+
Meta map[string]interface{} `json:"meta"`
166+
Message string `json:"message"`
167+
}{
168+
Action: action,
169+
App: app,
170+
Meta: meta,
171+
Message: message,
172+
}
173+
174+
err := doRequest("POST", "events", params, map[string]interface{}{"event": request}, nil)
175+
if err != nil {
176+
return err
177+
}
178+
179+
return nil
180+
}
157181
// doRequest ...
158182
func doRequest(method, path string, params url.Values, requestBody, responseBody interface{}) error {
159183

@@ -183,7 +207,9 @@ func doRequest(method, path string, params url.Values, requestBody, responseBody
183207
if params == nil {
184208
params = url.Values{}
185209
}
186-
params.Set("auth_token", auth.Key)
210+
if auth.Key != "" {
211+
params.Set("auth_token", auth.Key)
212+
}
187213

188214
// fetch the correct url from the endpoint
189215
url := odinURL()

0 commit comments

Comments
 (0)