From 40144101709638b2330f85e885bbddd17c0d097b Mon Sep 17 00:00:00 2001 From: Rohan Yadav Date: Tue, 27 Aug 2019 16:09:47 -0700 Subject: [PATCH] demo: Try to get a temporary license upon demo startup Fixes #40222. Release note (cli change): cockroach demo tries to get a temporary enterprise license upon startup. --- pkg/cli/demo.go | 53 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/pkg/cli/demo.go b/pkg/cli/demo.go index db9d08d6a2b2..96e021b125a3 100644 --- a/pkg/cli/demo.go +++ b/pkg/cli/demo.go @@ -14,13 +14,17 @@ 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" "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/server" + "github.com/cockroachdb/cockroach/pkg/sql/lex" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" "github.com/cockroachdb/cockroach/pkg/util" "github.com/cockroachdb/cockroach/pkg/util/log" @@ -49,6 +53,11 @@ to avoid pre-loading a dataset.`, }), } +// TODO (rohany): change this once another endpoint is setup for getting licenses. +// This URL grants 1 licenses that work for 1 hour. +const licenseURL = "https://register.cockroachdb.com/api/prodtest" +const demoOrg = "Cockroach Labs - Production Testing" + const defaultGeneratorName = "movr" var defaultGenerator workload.Generator @@ -95,6 +104,25 @@ func init() { } } +func getLicense() (string, error) { + client := &http.Client{ + Timeout: time.Second, + } + resp, err := client.Get(licenseURL) + if err != nil { + return "", nil + } + 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 setupTransientServers( cmd *cobra.Command, gen workload.Generator, ) (connURL string, adminURL string, cleanup func(), err error) { @@ -189,15 +217,28 @@ func setupTransientServers( } urlStr := url.String() + db, err := gosql.Open("postgres", urlStr) + if err != nil { + return ``, ``, cleanup, err + } + defer db.Close() + + // Load a license. + if cliCtx.isInteractive { + license, err := getLicense() + if err == nil { + if _, err := db.Exec(`SET CLUSTER SETTING cluster.organization = ` + lex.EscapeSQLString(demoOrg)); err != nil { + return ``, ``, cleanup, err + } + if _, err := db.Exec(`SET CLUSTER SETTING enterprise.license = ` + lex.EscapeSQLString(license)); err != nil { + return ``, ``, cleanup, err + } + } + } + // If there is a load generator, create its database and load its // fixture. if gen != nil { - db, err := gosql.Open("postgres", urlStr) - if err != nil { - return ``, ``, cleanup, err - } - defer db.Close() - if _, err := db.Exec(`CREATE DATABASE ` + gen.Meta().Name); err != nil { return ``, ``, cleanup, err }