You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ne...@apache.org on 2017/01/13 23:36:09 UTC

[13/29] incubator-trafficcontrol git commit: Make create_ts_databases use flag package again, and the new influxdb package

Make create_ts_databases use flag package again, and the new influxdb package


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/4290ea5f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/4290ea5f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/4290ea5f

Branch: refs/heads/master
Commit: 4290ea5f1f178fe13abf8bc263ab2984389da238
Parents: 3d95afa
Author: sbogacz <sb...@zvelo.com>
Authored: Tue Jan 10 19:45:24 2017 -0700
Committer: David Neuman <da...@gmail.com>
Committed: Fri Jan 13 23:33:56 2017 +0000

----------------------------------------------------------------------
 .../create/create_ts_databases.go               | 82 ++++----------------
 traffic_stats/influxdb_tools/create/main.go     | 37 ---------
 2 files changed, 15 insertions(+), 104 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/4290ea5f/traffic_stats/influxdb_tools/create/create_ts_databases.go
----------------------------------------------------------------------
diff --git a/traffic_stats/influxdb_tools/create/create_ts_databases.go b/traffic_stats/influxdb_tools/create/create_ts_databases.go
index fa1260a..64d1de4 100644
--- a/traffic_stats/influxdb_tools/create/create_ts_databases.go
+++ b/traffic_stats/influxdb_tools/create/create_ts_databases.go
@@ -20,11 +20,11 @@ under the License.
 package main
 
 import (
+	"flag"
 	"fmt"
 
+	"github.com/apache/incubator-trafficcontrol/traffic_stats/influxdb"
 	influx "github.com/influxdata/influxdb/client/v2"
-	"github.com/pkg/errors"
-	"github.com/urfave/cli"
 )
 
 const (
@@ -33,76 +33,24 @@ const (
 	daily           = "daily_stats"
 )
 
-func createFlags() []cli.Flag {
-	return []cli.Flag{
-		cli.StringFlag{
-			Name:  "url",
-			Usage: "The influxdb url and port",
-			Value: "http://localhost:8086",
-		},
-		cli.IntFlag{
-			Name:  "replication",
-			Usage: "The number of nodes in the cluster",
-			Value: 3,
-		},
-		cli.StringFlag{
-			Name:  "user",
-			Usage: "The influxdb username used to create DBs",
-			Value: "",
-		},
-		cli.StringFlag{
-			Name:  "password",
-			Usage: "The influxdb password used to create DBs",
-			Value: "",
-		},
-	}
-}
+func main() {
+	// get influx db flags
+	config := &influxdb.Config{}
+	config.Flags("")
+	var replication int
+	flag.IntVar(&replication, "replication", 3, "The number of nodes in the cluster")
 
-func create(c *cli.Context) error {
-	influxURL := c.String("url")
-	replication := c.Int("replication")
-	user := c.String("user")
-	password := c.String("password")
-
-	fmt.Printf("creating datbases for influxUrl: %s with a replication of %d using user %s\n", influxURL, replication, user)
-	client, err := influx.NewHTTPClient(influx.HTTPConfig{
-		Addr:     influxURL,
-		Username: user,
-		Password: password,
-	})
+	fmt.Printf("creating datbases for influxUrl: %s with a replication of %d using user %s\n", config.URL, replication, config.User)
+	client, err := config.NewHTTPClient()
 	if err != nil {
-		return errors.Wrap(err, "Error creating influx client")
-	}
-	_, _, err = client.Ping(10)
-	if err != nil {
-		return errors.Wrap(err, "Error creating influx client")
+		fmt.Printf("Unable to run create_ts_datases command, failed to get influxdb client: %v\n", err)
+		return
 	}
 
 	createCacheStats(client, replication)
 	createDailyStats(client, replication)
 	createDeliveryServiceStats(client, replication)
-	return nil
-}
 
-// queryDB takes a variadic argument for the target database so as to make
-// passing the variable optional, however, if passed, only the first db passed
-// in will be used
-func queryDB(client influx.Client, cmd string, dbs ...string) (res []influx.Result, err error) {
-	db := ""
-	if len(dbs) > 0 {
-		db = dbs[0]
-	}
-	q := influx.Query{
-		Command:  cmd,
-		Database: db,
-	}
-	if response, err := client.Query(q); err == nil {
-		if response.Error() != nil {
-			return res, response.Error()
-		}
-		res = response.Results
-	}
-	return res, nil
 }
 
 func createCacheStats(client influx.Client, replication int) {
@@ -146,7 +94,7 @@ func createDailyStats(client influx.Client, replication int) {
 }
 
 func createDatabase(client influx.Client, db string) {
-	_, err := queryDB(client, fmt.Sprintf("CREATE DATABASE %s", db))
+	err := influxdb.Create(client, fmt.Sprintf("CREATE DATABASE %s", db))
 	if err != nil {
 		fmt.Printf("An error occured creating the %v database: %v\n", db, err)
 		return
@@ -159,7 +107,7 @@ func createRetentionPolicy(client influx.Client, db string, name string, duratio
 	if isDefault {
 		qString += " DEFAULT"
 	}
-	_, err := queryDB(client, qString)
+	err := influxdb.Create(client, qString)
 	if err != nil {
 		fmt.Printf("An error occured creating the retention policy %s on database: %s:  %v\n", name, db, err)
 		return
@@ -168,7 +116,7 @@ func createRetentionPolicy(client influx.Client, db string, name string, duratio
 }
 
 func createContinuousQuery(client influx.Client, name string, query string) {
-	_, err := queryDB(client, query)
+	err := influxdb.Create(client, query)
 	if err != nil {
 		fmt.Printf("An error occured creating continuous query %s: %v\n", name, err)
 		return

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/4290ea5f/traffic_stats/influxdb_tools/create/main.go
----------------------------------------------------------------------
diff --git a/traffic_stats/influxdb_tools/create/main.go b/traffic_stats/influxdb_tools/create/main.go
deleted file mode 100644
index 986901b..0000000
--- a/traffic_stats/influxdb_tools/create/main.go
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements.  See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership.  The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License.  You may obtain a copy of the License at
-
-  http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing,
-software distributed under the License is distributed on an
-"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-KIND, either express or implied.  See the License for the
-specific language governing permissions and limitations
-under the License.
-*/
-
-package main
-
-import (
-	"os"
-
-	"github.com/urfave/cli"
-)
-
-func main() {
-	app := cli.NewApp()
-	app.Name = "create_ts_databases"
-	app.Version = "0.1.0"
-	app.Usage = "create_ts_databases provides a cli tool for creating the requisite influxdb databases"
-	app.Flags = createFlags()
-	app.Action = create
-
-	app.Run(os.Args)
-}