Skip to content

Commit

Permalink
demo: Get a temporary license upon demo startup
Browse files Browse the repository at this point in the history
There is a wider discussion/design going on to serve licenses in
a more general way in the future, so this commit is not aiming
for a future-proof design, but instead an MVP to allow users to
demo enterprise features within `cockroach demo`.

We are not concerned about offline usage of enterprise features
as users can obtain a license and enable features manually using SET.

Fixes #40222.

Release note (cli change): cockroach demo attempts to contact a license
server to obtain a temporary license. cockroach demo now enables
telemetry for the demo cluster. This feature can be opted out of by
setting the `COCKROACH_SKIP_UPDATE_CHECK` environment variable
(https://www.cockroachlabs.com/docs/stable/diagnostics-reporting.html).
  • Loading branch information
rohany committed Aug 28, 2019
1 parent 7be1e52 commit 1291c5f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
75 changes: 72 additions & 3 deletions pkg/cli/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
"context"
gosql "database/sql"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/cli/cliflags"
Expand All @@ -41,14 +44,24 @@ interactive SQL prompt to it. Various datasets are available to be preloaded as
subcommands: e.g. "cockroach demo startrek". See --help for a full list.
By default, the 'movr' dataset is pre-loaded. You can also use --empty
to avoid pre-loading a dataset.`,
to avoid pre-loading a dataset.
cockroach demo attempts to obtain a temporary enterprise license for demoing
enterprise features and enable telemetry back to Cockroach Labs. In order to
disable this behavior, set the environment variable "COCKROACH_SKIP_UPDATE_CHECK".
`,
Example: ` cockroach demo`,
Args: cobra.NoArgs,
RunE: MaybeDecorateGRPCError(func(cmd *cobra.Command, _ []string) error {
return runDemo(cmd, nil /* gen */)
}),
}

// TODO (rohany): change this once another endpoint is setup for getting licenses.
// This URL grants a license that is valid for 1 hour.
const licenseURL = "https://register.cockroachdb.com/api/prodtest"
const demoOrg = "Cockroach Labs - Production Testing"

const defaultGeneratorName = "movr"

var defaultGenerator workload.Generator
Expand Down Expand Up @@ -95,6 +108,39 @@ func init() {
}
}

func getLicense() (string, error) {
client := &http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Get(licenseURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", errors.New("unable to connect to licensing endpoint")
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(bodyBytes), nil
}

func getAndApplyLicense(db *gosql.DB) error {
license, err := getLicense()
if err != nil {
return err
}
if _, err := db.Exec(`SET CLUSTER SETTING cluster.organization = $1`, demoOrg); err != nil {
return err
}
if _, err := db.Exec(`SET CLUSTER SETTING enterprise.license = $1`, license); err != nil {
return err
}
return nil
}

func setupTransientServers(
cmd *cobra.Command, gen workload.Generator,
) (connURL string, adminURL string, cleanup func(), err error) {
Expand Down Expand Up @@ -189,6 +235,29 @@ func setupTransientServers(
}
urlStr := url.String()

// Start up the update check loop.
// We don't do this in (*server.Server).Start() because we don't want it
// in tests.
if shouldEnableTelemetry() {
s.PeriodicallyCheckForUpdates(ctx)
// If we allow telemetry, then also try and get an enterprise license for the demo.
if cliCtx.isInteractive {
db, err := gosql.Open("postgres", urlStr)
if err != nil {
return ``, ``, cleanup, err
}
// Perform license acquisition asynchronously to avoid delay in cli startup.
go func() {
defer db.Close()
err := getAndApplyLicense(db)
if err != nil {
msg := `error attempting to acquire demo license %+v. Enterprise feature are not enabled in this session`
log.Warningf(ctx, msg, err)
}
}()
}
}

// If there is a load generator, create its database and load its
// fixture.
if gen != nil {
Expand Down Expand Up @@ -218,14 +287,14 @@ func runDemo(cmd *cobra.Command, gen workload.Generator) error {
gen = defaultGenerator
}

checkInteractive()

connURL, adminURL, cleanup, err := setupTransientServers(cmd, gen)
defer cleanup()
if err != nil {
return checkAndMaybeShout(err)
}

checkInteractive()

if cliCtx.isInteractive {
fmt.Printf(`#
# Welcome to the CockroachDB demo database!
Expand Down
6 changes: 5 additions & 1 deletion pkg/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ If problems persist, please see ` + base.DocsURL("cluster-setup-troubleshooting.
// Start up the update check loop.
// We don't do this in (*server.Server).Start() because we don't want it
// in tests.
if !envutil.EnvOrDefaultBool("COCKROACH_SKIP_UPDATE_CHECK", false) {
if shouldEnableTelemetry() {
s.PeriodicallyCheckForUpdates(ctx)
}

Expand Down Expand Up @@ -1076,6 +1076,10 @@ func logOutputDirectory() string {
return startCtx.logDir.String()
}

func shouldEnableTelemetry() bool {
return !envutil.EnvOrDefaultBool("COCKROACH_SKIP_UPDATE_CHECK", false)
}

// setupAndInitializeLoggingAndProfiling does what it says on the label.
// Prior to this however it determines suitable defaults for the
// logging output directory and the verbosity level of stderr logging.
Expand Down

0 comments on commit 1291c5f

Please sign in to comment.