...

Source file src/edge-infra.dev/hack/tools/eyede/utils/utils.go

Documentation: edge-infra.dev/hack/tools/eyede/utils

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"flag"
     7  	"fmt"
     8  
     9  	"github.com/peterbourgon/ff/v3"
    10  	"github.com/peterbourgon/ff/v3/ffcli"
    11  
    12  	chariotClientApi "edge-infra.dev/pkg/edge/chariot/client"
    13  	"edge-infra.dev/pkg/edge/constants/api/fleet"
    14  )
    15  
    16  const (
    17  	Filter               = "chariot"
    18  	eyedeComponentOwner  = "eyede"
    19  	getBannerProjectID   = "SELECT project_id from banners"
    20  	GetClustersForBanner = "SELECT cluster_edge_id, cluster_name, project_id, bsl_site_id, location, banner_edge_id from clusters WHERE project_id = $1"
    21  	LongHelp             = `
    22  CLI program written to populate existing edge info configmap with the correct cluster edge id.
    23  
    24  The cli program can be run like so:
    25  
    26  eyede --databaseHost=localhost --databaseName=dev0 --databaseUser=postgres --databasePassword=**** 
    27  --topLevelProjectID=ret-dev-foreman --chariotTopic=chariot-rides
    28  		
    29  The following flags are required for the cli to run:
    30  databaseHost, databaseName, databaseUser, databasePassword, topLevelProjectID and chariotTopic
    31  		
    32  The dryRun flag is an optional and defaults to false if not specified. launchdarklyKey is also an optional flag that defaults to using cluster id if not set.`
    33  )
    34  
    35  type EyedeCommand struct {
    36  	DBConnection *sql.DB
    37  }
    38  
    39  func New() *EyedeCommand {
    40  	return &EyedeCommand{}
    41  }
    42  
    43  func (e *EyedeCommand) SetDBConnection(db *sql.DB) *EyedeCommand {
    44  	e.DBConnection = db
    45  	return e
    46  }
    47  
    48  func (e EyedeCommand) Exec(name, shortUsage, shortHelp, foremanProjectID, topic, edgeAPIURL string, dryRun bool, callback func(ctx context.Context, db *sql.DB, projectID, foremanProjectID, topic, edgeAPIURL string, dryRun bool) bool, fs *flag.FlagSet) *ffcli.Command {
    49  	return &ffcli.Command{
    50  		Name:       name,
    51  		ShortUsage: shortUsage,
    52  		ShortHelp:  shortHelp,
    53  		LongHelp:   LongHelp,
    54  		FlagSet:    fs,
    55  		Options:    []ff.Option{ff.WithEnvVarNoPrefix()},
    56  		Exec: func(ctx context.Context, _ []string) error {
    57  			return queryBanners(ctx, e.DBConnection, foremanProjectID, topic, edgeAPIURL, dryRun, callback)
    58  		},
    59  	}
    60  }
    61  
    62  func queryBanners(ctx context.Context, db *sql.DB, foremanProjectID, topic, edgeAPIURL string, dryRun bool, callback func(ctx context.Context, db *sql.DB, projectID, foremanProjectID, topic, edgeAPIURL string, dryRun bool) bool) error {
    63  	banners, err := db.QueryContext(ctx, getBannerProjectID)
    64  	if err != nil {
    65  		fmt.Println("db error occurred ", err)
    66  		return err
    67  	}
    68  	for banners.Next() {
    69  		var projectID string
    70  		if err := banners.Scan(&projectID); err != nil {
    71  			fmt.Println("failed to scan banner projectID ", err)
    72  			return err
    73  		}
    74  		next := callback(ctx, db, projectID, foremanProjectID, topic, edgeAPIURL, dryRun) //nolint
    75  		if next {
    76  			continue
    77  		}
    78  	}
    79  	return nil
    80  }
    81  
    82  func CreateChariotMessage(projectID, id, filter, configMap string) *chariotClientApi.ChariotMessage {
    83  	return chariotClientApi.NewChariotMessage().
    84  		SetBanner(projectID).
    85  		SetCluster(id).
    86  		SetDir(filter).
    87  		SetOperation(chariotClientApi.Create).
    88  		SetOwner(eyedeComponentOwner).
    89  		AddObject(configMap)
    90  }
    91  
    92  func InvokeChariot(ctx context.Context, topLevelProjectID, chariotTopic string, message *chariotClientApi.ChariotMessage) error {
    93  	return message.Publish(ctx, topLevelProjectID, chariotTopic, nil)
    94  }
    95  
    96  func AssertFleetAndStoreType(fleetType, storeType string) {
    97  	if err := fleet.IsValid(fleetType); err != nil {
    98  		AssertFleetAndStoreType(storeType, fleetType)
    99  	}
   100  }
   101  

View as plain text