...

Source file src/edge-infra.dev/hack/tools/hr/main.go

Documentation: edge-infra.dev/hack/tools/hr

     1  /*
     2  
     3   _
     4  | |
     5  | |__   _ __
     6  | '_ \ | '__|
     7  | | | || |
     8  |_| |_||_|
     9  
    10  
    11  
    12  
    13  CLI program written to populate existing edge info configmap with the correct cluster edge id
    14  The cli program can be run like so:
    15  
    16  Using Storage Bucket:
    17  eyede useStorage --databaseHost=localhost --databaseName=dev0 --databaseUser=postgres --databasePassword=****
    18  --topLevelProjectID=ret-edge-dev0-foreman --chariotTopic=chariot-rides
    19  
    20  Using Database:
    21  eyede useDatabase --databaseHost=localhost --databaseName=dev0 --databaseUser=postgres --databasePassword=****
    22  --topLevelProjectID=ret-edge-dev0-foreman --chariotTopic=chariot-rides
    23  
    24  The following flags are required for the cli to run:
    25  databaseHost, databaseName, databaseUser, databasePassword, topLevelProjectID and chariotTopic
    26  The dryRun flag is an optional and defaults to false if not specified.
    27  launchdarklyKey is also an optional flag that defaults to using cluster id if not set.
    28  */
    29  
    30  package main
    31  
    32  import (
    33  	"context"
    34  	"database/sql"
    35  	"errors"
    36  	"flag"
    37  	"fmt"
    38  	"os"
    39  
    40  	_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
    41  	"github.com/peterbourgon/ff/v3/ffcli"
    42  
    43  	populate "edge-infra.dev/hack/tools/hr/populate"
    44  	hrUtils "edge-infra.dev/hack/tools/hr/utils"
    45  	"edge-infra.dev/pkg/edge/api/clients"
    46  	"edge-infra.dev/pkg/edge/api/utils"
    47  	"edge-infra.dev/pkg/lib/cli/commands"
    48  	"edge-infra.dev/pkg/lib/gcp/cloudsql"
    49  )
    50  
    51  var (
    52  	fs                           = flag.NewFlagSet("eyede", flag.ExitOnError)
    53  	databaseHost                 = fs.String("databaseHost", os.Getenv("DatabaseHost"), "the host of the database to query")
    54  	databaseName                 = fs.String("databaseName", os.Getenv("DatabaseName"), "the name of the database to query")
    55  	databaseUser                 = fs.String("databaseUser", os.Getenv("DatabaseUser"), "the user of the specified database")
    56  	databasePassword             = fs.String("databasePassword", os.Getenv("DatabasePassword"), "the password of the specified database (optional for iam auth)")
    57  	topLevelProjectID            = fs.String("topLevelProjectID", os.Getenv("TopLevelProjectID"), "the top level project id")
    58  	bqDbName                     = fs.String("bqDbName", os.Getenv("BqDbName"), "the name of the bq table")
    59  	goog                         = fs.String("googleAppCreds", os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"), "goog app creds")
    60  	launchdarklyKey              = fs.String("launchdarklyKey", os.Getenv("LD_KEY"), "launch darkly sdk key")
    61  	errDatabaseHostRequired      = errors.New("error database host is required")
    62  	errDatabaseNameRequired      = errors.New("error database name is required")
    63  	errDatabaseUserRequired      = errors.New("error database user is required")
    64  	errTopLevelProjectIDRequired = errors.New("error top level project id is required")
    65  	errBqName                    = errors.New("error big query table name is required")
    66  )
    67  
    68  func main() {
    69  	err := fs.Parse(os.Args[2:])
    70  	if err != nil {
    71  		fmt.Println("Flag parsing error ", err)
    72  		os.Exit(1)
    73  	}
    74  	if err := validateRequiredFlags(); err != nil {
    75  		fmt.Println("Flag validation error ", err)
    76  		os.Exit(1)
    77  	}
    78  	if utils.IsNullOrEmpty(launchdarklyKey) {
    79  		os.Setenv("LD_KEY", *launchdarklyKey)
    80  	}
    81  	if !utils.IsNullOrEmpty(goog) {
    82  		os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", *goog)
    83  	}
    84  	hrConfig := hrUtils.New()
    85  	fmt.Println(`
    86   _
    87  | |
    88  | |__   _ __
    89  | '_ \ | '__|
    90  | | | || |
    91  |_| |_||_|`)
    92  	fmt.Printf("Connecting to Database: %s with user %s\n", *databaseName, *databaseUser)
    93  	db, err := connectToDatabase()
    94  	if err != nil {
    95  		fmt.Println("db connection error occurred ", err)
    96  		os.Exit(1)
    97  	}
    98  	bqService, err := clients.New(context.Background(), *topLevelProjectID, *bqDbName)
    99  	if err != nil {
   100  		fmt.Println("bq connection error occurred ", err)
   101  		os.Exit(1)
   102  	}
   103  	hrConfig.SetDBConnection(db)
   104  	hrConfig.SetBQClient(bqService)
   105  
   106  	pop := hrConfig.Exec("populate", "hr populate", "populate the helm workloads table", *bqDbName, populate.HelmWorkloadsTable, fs)
   107  
   108  	cmd := &ffcli.Command{
   109  		Name:       "hr",
   110  		ShortUsage: "hr --databaseHost=localhost --databaseName=dev0 --databaseUser=postgres --databasePassword=**** --topLevelProjectID=ret-dev-foreman --bqDbName=tableName",
   111  		ShortHelp:  "populate edge ids for all clusters",
   112  		Subcommands: []*ffcli.Command{
   113  			pop,
   114  			commands.Version(),
   115  		},
   116  	}
   117  	if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil {
   118  		fmt.Fprintf(os.Stderr, "error: %v\n", err)
   119  		os.Exit(1)
   120  	}
   121  }
   122  
   123  func validateRequiredFlags() error {
   124  	if utils.IsNullOrEmpty(databaseHost) {
   125  		return errDatabaseHostRequired
   126  	}
   127  	if utils.IsNullOrEmpty(databaseName) {
   128  		return errDatabaseNameRequired
   129  	}
   130  	if utils.IsNullOrEmpty(databaseUser) {
   131  		return errDatabaseUserRequired
   132  	}
   133  	if utils.IsNullOrEmpty(topLevelProjectID) {
   134  		return errTopLevelProjectIDRequired
   135  	}
   136  	if utils.IsNullOrEmpty(bqDbName) {
   137  		return errBqName
   138  	}
   139  	return nil
   140  }
   141  
   142  func connectToDatabase() (*sql.DB, error) {
   143  	conn := cloudsql.GCPPostgresConnection(*databaseHost).
   144  		DBName(*databaseName).
   145  		Username(*databaseUser)
   146  	if databasePassword != nil {
   147  		conn.Password(*databasePassword)
   148  	}
   149  	return conn.NewConnection()
   150  }
   151  

View as plain text