这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
8bc036b
wip: need to move readConfig out of validate
shahidhk Oct 31, 2018
4df6512
Merge branch 'master' into telemetry
shahidhk Nov 21, 2018
23c00e7
wip: need to get execution context in persistentprerun
shahidhk Nov 21, 2018
8ed51f2
Merge branch 'master' into telemetry
shahidhk Nov 27, 2018
5f1104e
add basic telemetry
shahidhk Nov 27, 2018
bda0983
set topic as cli for prod versions
shahidhk Nov 28, 2018
b458fb1
change config dir name and handle invalid uuids
shahidhk Nov 29, 2018
64e3649
add payload to send functions
shahidhk Nov 29, 2018
a9b438d
update telemetry url
shahidhk Nov 29, 2018
8cacdc5
Merge branch 'master' into telemetry
shahidhk Nov 29, 2018
49409e6
add telemetry to console
praveenweb Dec 7, 2018
8f6db58
add '--disable-telemetry' flag to server
rakeshkky Dec 11, 2018
1826416
template the disable_telemetry variable in console html
arvi3411301 Dec 13, 2018
bb44f54
Merge branch 'master' into telemetry
rakeshkky Jan 6, 2019
290a262
add separate envs for cli and console disable flags
shahidhk Jan 7, 2019
013e94f
separate disable telemetry env/flag for console and server
shahidhk Jan 7, 2019
0651edf
Merge branch 'master' into console-telemetry
praveenweb Jan 7, 2019
8432da2
Merge branch 'console-telemetry' into HEAD
praveenweb Jan 7, 2019
a92a1ee
remove disable-server-telemetry CLI flag and fix console.html rendering
rakeshkky Jan 7, 2019
e8e65f7
add bash script to get cli
shahidhk Jan 7, 2019
56f77a6
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
shahidhk Jan 7, 2019
8f4f314
anonymise content for telemetry
wawhal Jan 7, 2019
70a3066
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
wawhal Jan 7, 2019
447d822
send served_by and uuid from window.__env
wawhal Jan 7, 2019
68d8184
add uuid to console
shahidhk Jan 8, 2019
ce16d42
server telemetry WIP
ecthiender Jan 8, 2019
63185b4
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
ecthiender Jan 8, 2019
ec76cf9
initial working of server telemetry
ecthiender Jan 8, 2019
33b3026
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
shahidhk Jan 9, 2019
b20a04b
fix server migration bugs
shahidhk Jan 9, 2019
aaedcbe
Revert "remove disable-server-telemetry CLI flag and fix console.html…
shahidhk Jan 9, 2019
2f03dd3
Revert "separate disable telemetry env/flag for console and server"
shahidhk Jan 9, 2019
52b12ba
Revert "add separate envs for cli and console disable flags"
shahidhk Jan 9, 2019
7312e9b
add telemetry message
shahidhk Jan 9, 2019
f4c0aa9
prevent panic if globalconfig is not avaiable
shahidhk Jan 9, 2019
0d7f369
insert the uuid while startup
ecthiender Jan 9, 2019
c1bd1d2
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
ecthiender Jan 9, 2019
d102734
fix bug when migrating from catalog older than 7
ecthiender Jan 9, 2019
54e9345
reafactor telemetry logic
shahidhk Jan 9, 2019
0d4204c
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
shahidhk Jan 9, 2019
2f410fe
Merge branch 'master' into telemetry
shahidhk Jan 9, 2019
3b5900d
add cli uuid variable to console html
shahidhk Jan 9, 2019
8c9982b
add telemetry docs
shahidhk Jan 9, 2019
0798f89
add regex to sanitize url path
arvi3411301 Jan 9, 2019
bfffa63
move paths to a single constant
shahidhk Jan 10, 2019
8362e8b
cli: change disableTelemetry to enableTelemetry
shahidhk Jan 10, 2019
aa1e1c2
server: change disableTelemetry to enableTelemetry
shahidhk Jan 10, 2019
24f4ad6
console: change disableTelemetry to enableTelemetry
shahidhk Jan 10, 2019
23b1618
docs: change disableTelemetry to enableTelemetry
shahidhk Jan 10, 2019
4dfce32
server: update default of misc_state to empty object
shahidhk Jan 10, 2019
a17df81
server: track hdb_version
shahidhk Jan 10, 2019
524ad2b
added telemetry notification
arvi3411301 Jan 10, 2019
2542e69
wip telemetry
wawhal Jan 11, 2019
bd20aef
sending server_uuid in telemetry
wawhal Jan 11, 2019
8737902
server: add separate state columns for cli and console
shahidhk Jan 11, 2019
80325d2
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
shahidhk Jan 11, 2019
5900e58
add telemetry notice
ecthiender Jan 11, 2019
e6ecedc
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
ecthiender Jan 11, 2019
ce440c1
update notification dismiss to 15s and use console_state column for s…
arvi3411301 Jan 11, 2019
a234df3
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
arvi3411301 Jan 11, 2019
ad859a6
console: update link
shahidhk Jan 11, 2019
810e872
server: add doc link
shahidhk Jan 11, 2019
5a619ae
cli: change uuid query to json api
shahidhk Jan 11, 2019
89dd060
updated notification html
praveenweb Jan 11, 2019
fe3869a
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into HEAD
praveenweb Jan 11, 2019
adae58e
Merge branch 'master' into telemetry
shahidhk Jan 11, 2019
eb40d4d
run stylish-haskell
shahidhk Jan 11, 2019
b95ee5d
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
shahidhk Jan 11, 2019
95a98c4
cli: add help link
shahidhk Jan 11, 2019
86e9041
modify docs and add privacy policy for telemetry
dsandip Jan 13, 2019
422c7fb
Merge branch 'master' into telemetry
shahidhk Jan 16, 2019
f8a73a2
Merge branch 'master' into telemetry
shahidhk Jan 17, 2019
6b7a8d1
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
shahidhk Jan 17, 2019
488686f
modify docs
shahidhk Jan 17, 2019
81cb930
remove a comment
shahidhk Jan 17, 2019
cd57d48
added telemetry topic to console globals
praveenweb Jan 17, 2019
efab9d5
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into HEAD
praveenweb Jan 17, 2019
7df86e8
update cnsole/docs
shahidhk Jan 17, 2019
ebd905a
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
shahidhk Jan 17, 2019
586add8
fix isDevVersion; drop the v from version before parsing it
ecthiender Jan 17, 2019
42c46d8
minor fixes
shahidhk Jan 17, 2019
c8a899d
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
shahidhk Jan 17, 2019
2f1bde4
cli: refactor and fix tests
shahidhk Jan 18, 2019
7bfa33b
update missing statements
shahidhk Jan 18, 2019
b489e4a
udpate flag reference
shahidhk Jan 18, 2019
d2a19c6
fix review comments
ecthiender Jan 18, 2019
79a2568
fix to strip off urlPrefix from url path
arvi3411301 Jan 18, 2019
82d037c
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
arvi3411301 Jan 18, 2019
0910fc4
Merge branch 'master' into telemetry
shahidhk Jan 22, 2019
6b169f9
Merge branch 'master' of github.com:hasura/graphql-engine into telemetry
ecthiender Jan 28, 2019
d038ba1
fix review comment
ecthiender Jan 28, 2019
efb0a82
fix typos in help text
ecthiender Jan 28, 2019
44c303c
import react in notification
wawhal Jan 28, 2019
abdb776
add indentation to the config file generated
shahidhk Jan 28, 2019
ed51e0e
Merge branch 'telemetry' of github.com:shahidhk/graphql-engine into t…
shahidhk Jan 28, 2019
ddf7958
Merge branch 'master' into telemetry
0x777 Jan 28, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 127 additions & 14 deletions cli/Gopkg.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions cli/assets/assets.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion cli/assets/unversioned/console.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
dataApiVersion: {{.dataApiVersion}},
accessKey: {{.accessKey}},
urlPrefix: "/",
consoleMode: "cli"
consoleMode: "cli",
cliUUID: {{.cliUUID}},
enableTelemetry: {{.enableTelemetry}}
};
</script>
</head>
Expand Down
4 changes: 3 additions & 1 deletion cli/assets/v1.0-alpha/console.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
dataApiVersion: {{.dataApiVersion}},
accessKey: {{.accessKey}},
urlPrefix: "/",
consoleMode: "cli"
consoleMode: "cli",
cliUUID: {{.cliUUID}},
enableTelemetry: {{.enableTelemetry}}
};
</script>
</head>
Expand Down
4 changes: 3 additions & 1 deletion cli/assets/v1.0/console.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
dataApiVersion: {{.dataApiVersion}},
accessKey: {{.accessKey}},
urlPrefix: "/",
consoleMode: "cli"
consoleMode: "cli",
cliUUID: {{.cliUUID}},
enableTelemetry: {{.enableTelemetry}}
};
</script>
</head>
Expand Down
204 changes: 188 additions & 16 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
package cli

import (
"encoding/json"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/hasura/graphql-engine/cli/telemetry"
"github.com/hasura/graphql-engine/cli/util"

"github.com/briandowns/spinner"
"github.com/gofrs/uuid"
"github.com/hasura/graphql-engine/cli/version"
colorable "github.com/mattn/go-colorable"
homedir "github.com/mitchellh/go-homedir"
Expand All @@ -37,11 +43,19 @@ const (
// Other constants used in the package
const (
// Name of the global configuration directory
GLOBAL_CONFIG_DIR_NAME = ".hasura-graphql"
GLOBAL_CONFIG_DIR_NAME = ".hasura"
// Name of the global configuration file
GLOBAL_CONFIG_FILE_NAME = "config.json"
)

// String constants
const (
StrTelemetryNotice = `Help us improve Hasura! The cli collects anonymized usage stats which
allow us to keep improving Hasura at warp speed. To opt-out or read more,
visit https://docs.hasura.io/1.0/graphql/manual/guides/telemetry.html
`
)

// HasuraGraphQLConfig has the config values required to contact the server.
type HasuraGraphQLConfig struct {
// Endpoint for the GraphQL Engine
Expand All @@ -62,6 +76,15 @@ func (hgc *HasuraGraphQLConfig) ParseEndpoint() error {
return nil
}

// GlobalConfig is the configuration object stored in the GlobalConfigFile.
type GlobalConfig struct {
// UUID used for telemetry, generated on first run.
UUID string `json:"uuid"`

// Indicate if telemetry is enabled or not
EnableTelemetry bool `json:"enable_telemetry"`
}

// ExecutionContext contains various contextual information required by the cli
// at various points of it's execution. Values are filled in by the
// initializers and passed on to each command. Commands can also fill in values
Expand All @@ -71,6 +94,12 @@ type ExecutionContext struct {
// correctly render example strings etc.
CMDName string

// ID is a unique ID for this Execution
ID string

// ServerUUID is the unique ID for the server this execution is contacting.
ServerUUID string

// Spinner is the global spinner object used to show progress across the cli.
Spinner *spinner.Spinner
// Logger is the global logger object to print logs.
Expand All @@ -96,6 +125,9 @@ type ExecutionContext struct {
// stored.
GlobalConfigFile string

// GlobalConfig holds all the configuration options.
GlobalConfig *GlobalConfig

// IsStableRelease indicates if the CLI release is stable or not.
IsStableRelease bool
// Version indicates the version object
Expand All @@ -106,6 +138,17 @@ type ExecutionContext struct {

// LogLevel indicates the logrus default logging level
LogLevel string

// Telemetry collects the telemetry data throughout the execution
Telemetry *telemetry.Data
}

// NewExecutionContext returns a new instance of execution context
func NewExecutionContext() *ExecutionContext {
ec := &ExecutionContext{}
ec.Telemetry = telemetry.BuildEvent()
ec.Telemetry.Version = version.BuildVersion
return ec
}

// Prepare as the name suggests, prepares the ExecutionContext ec by
Expand All @@ -128,14 +171,37 @@ func (ec *ExecutionContext) Prepare() error {
// populate version
ec.setVersion()

// setup global config directory
err := ec.setupGlobalConfigDir()
// setup global config
err := ec.setupGlobalConfig()
if err != nil {
// TODO(shahidhk): should this be a failure?
return errors.Wrap(err, "setting up global config directory failed")
}

// read global config
err = ec.readGlobalConfig()
if err != nil {
return errors.Wrap(err, "setting up config directory failed")
return errors.Wrap(err, "reading global config failed")
}

// initialize a blank config
ec.Config = &HasuraGraphQLConfig{}
if ec.Config == nil {
ec.Config = &HasuraGraphQLConfig{}
}

// generate an execution id
if ec.ID == "" {
id := "00000000-0000-0000-0000-000000000000"
u, err := uuid.NewV4()
if err == nil {
id = u.String()
} else {
ec.Logger.Debugf("generating uuid for execution ID failed, %v", err)
}
ec.ID = id
ec.Logger.Debugf("execution id: %v", ec.ID)
}
ec.Telemetry.ExecutionID = ec.ID

return nil
}
Expand Down Expand Up @@ -171,7 +237,17 @@ func (ec *ExecutionContext) Validate() error {
ec.Logger.Debug("graphql engine access_key: ", ec.Config.AccessKey)

// get version from the server and match with the cli version
return ec.checkServerVersion()
err = ec.checkServerVersion()
if err != nil {
return errors.Wrap(err, "version check")
}

state := util.GetServerState(ec.Config.Endpoint, ec.Config.AccessKey, ec.Version.ServerSemver, ec.Logger)
ec.ServerUUID = state.UUID
ec.Telemetry.ServerUUID = ec.ServerUUID
ec.Logger.Debugf("server: uuid: %s", ec.ServerUUID)

return nil
}

func (ec *ExecutionContext) checkServerVersion() error {
Expand All @@ -180,6 +256,7 @@ func (ec *ExecutionContext) checkServerVersion() error {
return errors.Wrap(err, "failed to get version from server")
}
ec.Version.SetServerVersion(v)
ec.Telemetry.ServerVersion = ec.Version.GetServerVersion()
isCompatible, reason := ec.Version.CheckCLIServerCompatibility()
ec.Logger.Debugf("versions: cli: [%s] server: [%s]", ec.Version.GetCLIVersion(), ec.Version.GetServerVersion())
ec.Logger.Debugf("compatibility check: [%v] %v", isCompatible, reason)
Expand All @@ -189,27 +266,56 @@ func (ec *ExecutionContext) checkServerVersion() error {
return nil
}

// readGlobalConfig reads the configuration from global config file env vars,
// through viper.
func (ec *ExecutionContext) readGlobalConfig() error {
// need to get existing viper because https://github.com/spf13/viper/issues/233
v := viper.New()
v.SetEnvPrefix("HASURA_GRAPHQL")
v.AutomaticEnv()
v.SetConfigName("config")
v.AddConfigPath(ec.GlobalConfigDir)
err := v.ReadInConfig()
if err != nil {
return errors.Wrap(err, "cannor read global config from file/env")
}
if ec.GlobalConfig == nil {
ec.Logger.Debugf("global config is not pre-set, reading from current env")
ec.GlobalConfig = &GlobalConfig{
UUID: v.GetString("uuid"),
EnableTelemetry: v.GetBool("enable_telemetry"),
}
} else {
ec.Logger.Debugf("global config is pre-set to %#v", ec.GlobalConfig)
}
ec.Logger.Debugf("global config: uuid: %v", ec.GlobalConfig.UUID)
ec.Logger.Debugf("global config: enableTelemetry: %v", ec.GlobalConfig.EnableTelemetry)
// set if telemetry can be beamed or not
ec.Telemetry.CanBeam = ec.GlobalConfig.EnableTelemetry
ec.Telemetry.UUID = ec.GlobalConfig.UUID
return nil
}

// readConfig reads the configuration from config file, flags and env vars,
// through viper.
func (ec *ExecutionContext) readConfig() error {
// need to get existing viper because https://github.com/spf13/viper/issues/233
v := ec.Viper
v.SetDefault("endpoint", "http://localhost:8080")
v.SetDefault("access_key", "")
v.SetEnvPrefix("HASURA_GRAPHQL")
v.AutomaticEnv()
v.SetConfigName("config")
v.SetDefault("endpoint", "http://localhost:8080")
v.SetDefault("access_key", "")
v.AddConfigPath(ec.ExecutionDirectory)
err := v.ReadInConfig()
if err != nil {
return errors.Wrap(err, "cannor read config file")
return errors.Wrap(err, "cannor read config from file/env")
}
ec.Config = &HasuraGraphQLConfig{
Endpoint: v.GetString("endpoint"),
AccessKey: v.GetString("access_key"),
}
err = ec.Config.ParseEndpoint()
return err
return ec.Config.ParseEndpoint()
}

// setupSpinner creates a default spinner if the context does not already have
Expand Down Expand Up @@ -249,24 +355,90 @@ func (ec *ExecutionContext) setupLogger() {
}
ec.Logger.SetLevel(level)
}

// set the logger for telemetry
if ec.Telemetry.Logger == nil {
ec.Telemetry.Logger = ec.Logger
}
}

// setupGlobalConfigDir ensures that global config directory exists and the
// paths are correctly set.
func (ec *ExecutionContext) setupGlobalConfigDir() error {
// setupGlobConfig ensures that global config directory and file exists and
// reads it into the GlobalConfig object.
func (ec *ExecutionContext) setupGlobalConfig() error {
if len(ec.GlobalConfigDir) == 0 {
ec.Logger.Debug("global config directory is not pre-set, defaulting")
home, err := homedir.Dir()
if err != nil {
return errors.Wrap(err, "cannot get home directory")
}
globalConfigDir := filepath.Join(home, GLOBAL_CONFIG_DIR_NAME)
ec.GlobalConfigDir = globalConfigDir
ec.Logger.Debugf("global config directory set as '%s'", ec.GlobalConfigDir)
}
err := os.MkdirAll(ec.GlobalConfigDir, os.ModePerm)
if err != nil {
return errors.Wrap(err, "cannot create config directory")
return errors.Wrap(err, "cannot create global config directory")
}
if len(ec.GlobalConfigFile) == 0 {
ec.GlobalConfigFile = filepath.Join(ec.GlobalConfigDir, GLOBAL_CONFIG_FILE_NAME)
ec.Logger.Debugf("global config file set as '%s'", ec.GlobalConfigFile)
}
_, err = os.Stat(ec.GlobalConfigFile)
if os.IsNotExist(err) {
// file does not exist, teat as first run and create it
ec.Logger.Debug("global config file does not exist, this could be the first run, creating it...")
u, err := uuid.NewV4()
if err != nil {
return errors.Wrap(err, "failed to generate uuid")
}
gc := GlobalConfig{
UUID: u.String(),
EnableTelemetry: true,
}
data, err := json.MarshalIndent(gc, "", " ")
if err != nil {
return errors.Wrap(err, "cannot marshal json for config file")
}
err = ioutil.WriteFile(ec.GlobalConfigFile, data, 0644)
if err != nil {
return errors.Wrap(err, "writing global config file failed")
}
ec.Logger.Debugf("global config file written at '%s' with content '%v'", ec.GlobalConfigFile, string(data))
// also show a notice about telemetry
ec.Logger.Info(StrTelemetryNotice)
} else if os.IsExist(err) || err == nil {
// file exists, verify contents
ec.Logger.Debug("global config file exisits, verifying contents")
data, err := ioutil.ReadFile(ec.GlobalConfigFile)
if err != nil {
return errors.Wrap(err, "reading global config file failed")
}
var gc GlobalConfig
err = json.Unmarshal(data, &gc)
if err != nil {
return errors.Wrap(err, "global config file not a valid json")
}
_, err = uuid.FromString(gc.UUID)
if err != nil {
ec.Logger.Debugf("invalid uuid '%s' in global config: %v", gc.UUID, err)
// create a new UUID
ec.Logger.Debug("global config file exists, but uuid is invalid, creating a new one...")
u, err := uuid.NewV4()
if err != nil {
return errors.Wrap(err, "failed to generate uuid")
}
gc.UUID = u.String()
data, err := json.Marshal(gc)
if err != nil {
return errors.Wrap(err, "cannot marshal json for config file")
}
err = ioutil.WriteFile(ec.GlobalConfigFile, data, 0644)
if err != nil {
return errors.Wrap(err, "writing global config file failed")
}
ec.Logger.Debugf("global config file written at '%s' with content '%v'", ec.GlobalConfigFile, string(data))
}
}
ec.GlobalConfigFile = filepath.Join(ec.GlobalConfigDir, GLOBAL_CONFIG_FILE_NAME)
return nil
}

Expand Down
16 changes: 7 additions & 9 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ func init() {

func TestPrepare(t *testing.T) {
logger, _ := test.NewNullLogger()
ec := &cli.ExecutionContext{
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
}
ec := cli.NewExecutionContext()
ec.Logger = logger
ec.Spinner = spinner.New(spinner.CharSets[7], 100*time.Millisecond)
ec.Spinner.Writer = &fake.FakeWriter{}
err := ec.Prepare()
if err != nil {
Expand All @@ -47,16 +46,15 @@ func TestPrepare(t *testing.T) {
t.Fatalf("global config file: expected $HOME/%s/%s, got %s", cli.GLOBAL_CONFIG_DIR_NAME, cli.GLOBAL_CONFIG_FILE_NAME, ec.GlobalConfigFile)
}
if ec.Config == nil {
t.Fatal("nil HasuraGraphQLConfig")
t.Fatal("got empty Config")
}
}

func TestValidate(t *testing.T) {
logger, _ := test.NewNullLogger()
ec := &cli.ExecutionContext{
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
}
ec := cli.NewExecutionContext()
ec.Logger = logger
ec.Spinner = spinner.New(spinner.CharSets[7], 100*time.Millisecond)
ec.Spinner.Writer = &fake.FakeWriter{}
ec.ExecutionDirectory = filepath.Join(os.TempDir(), "hasura-gql-tests-"+strconv.Itoa(rand.Intn(1000)))
ec.Viper = viper.New()
Expand Down
Loading