@@ -19,6 +19,7 @@ package cmd
1919import (
2020 "fmt"
2121 "os"
22+ "path/filepath"
2223 "strings"
2324 "time"
2425
@@ -158,7 +159,7 @@ var _upCmd = &cobra.Command{
158159 }
159160 }
160161
161- out , exitCode , err := runManagerWithClusterConfig ("/root/install.sh" , clusterConfig , awsCreds , _flagClusterEnv )
162+ out , exitCode , err := runManagerWithClusterConfig ("/root/install.sh" , clusterConfig , awsCreds , _flagClusterEnv , nil , nil )
162163 if err != nil {
163164 if clusterConfig .APIGatewaySetting == clusterconfig .PublicAPIGatewaySetting {
164165 awsClient .DeleteAPIGatewayByTag (clusterconfig .ClusterNameTag , clusterConfig .ClusterName ) // best effort deletion
@@ -238,6 +239,30 @@ var _upCmd = &cobra.Command{
238239 exit .Error (ErrorClusterUp (out + helpStr ))
239240 }
240241
242+ loadBalancer , err := awsClient .FindLoadBalancer (map [string ]string {
243+ clusterconfig .ClusterNameTag : clusterConfig .ClusterName ,
244+ "cortex.dev/load-balancer" : "operator" ,
245+ })
246+ if err != nil {
247+ exit .Error (errors .Append (err , fmt .Sprintf ("\n \n unable to locate operator load balancer; you can attempt to resolve this issue and configure your CLI environment by running `cortex cluster info --env %s`" , _flagClusterEnv )))
248+ }
249+ if loadBalancer == nil {
250+ exit .Error (ErrorNoOperatorLoadBalancer (_flagClusterEnv ))
251+ }
252+
253+ newEnvironment := cliconfig.Environment {
254+ Name : _flagClusterEnv ,
255+ Provider : types .AWSProviderType ,
256+ OperatorEndpoint : pointer .String ("https://" + * loadBalancer .DNSName ),
257+ AWSAccessKeyID : pointer .String (awsCreds .CortexAWSAccessKeyID ),
258+ AWSSecretAccessKey : pointer .String (awsCreds .CortexAWSSecretAccessKey ),
259+ }
260+
261+ err = addEnvToCLIConfig (newEnvironment )
262+ if err != nil {
263+ exit .Error (errors .Append (err , fmt .Sprintf ("unable to configure cli environment; you can attempt to resolve this issue and configure your CLI environment by running `cortex cluster info --env %s`" , _flagClusterEnv )))
264+ }
265+
241266 fmt .Printf (console .Bold ("\n an environment named \" %s\" has been configured for this cluster; append `--env %s` to cortex commands to connect to it (e.g. `cortex deploy --env %s`), or set it as your default with `cortex env default %s`\n " ), _flagClusterEnv , _flagClusterEnv , _flagClusterEnv , _flagClusterEnv )
242267 },
243268}
@@ -289,7 +314,7 @@ var _configureCmd = &cobra.Command{
289314 exit .Error (err )
290315 }
291316
292- out , exitCode , err := runManagerWithClusterConfig ("/root/install.sh --update" , clusterConfig , awsCreds , _flagClusterEnv )
317+ out , exitCode , err := runManagerWithClusterConfig ("/root/install.sh --update" , clusterConfig , awsCreds , _flagClusterEnv , nil , nil )
293318 if err != nil {
294319 exit .Error (err )
295320 }
@@ -375,6 +400,12 @@ var _downCmd = &cobra.Command{
375400 }
376401 }
377402
403+ // updating CLI env is best-effort, so ignore errors
404+ loadBalancer , _ := awsClient .FindLoadBalancer (map [string ]string {
405+ clusterconfig .ClusterNameTag : * accessConfig .ClusterName ,
406+ "cortex.dev/load-balancer" : "operator" ,
407+ })
408+
378409 if ! _flagClusterDisallowPrompt {
379410 prompt .YesOrExit (fmt .Sprintf ("your cluster named \" %s\" in %s will be spun down and all apis will be deleted, are you sure you want to continue?" , * accessConfig .ClusterName , * accessConfig .Region ), "" , "" )
380411 }
@@ -421,7 +452,7 @@ var _downCmd = &cobra.Command{
421452 }
422453
423454 fmt .Println ("○ spinning down the cluster ..." )
424- out , exitCode , err := runManagerAccessCommand ("/root/uninstall.sh" , * accessConfig , awsCreds , _flagClusterEnv )
455+ out , exitCode , err := runManagerAccessCommand ("/root/uninstall.sh" , * accessConfig , awsCreds , _flagClusterEnv , nil , nil )
425456 if err != nil {
426457 exit .Error (err )
427458 }
@@ -431,8 +462,24 @@ var _downCmd = &cobra.Command{
431462 exit .Error (ErrorClusterDown (out + helpStr ))
432463 }
433464
434- cachedConfigPath := cachedClusterConfigPath (* accessConfig .ClusterName , * accessConfig .Region )
435- os .Remove (cachedConfigPath )
465+ // best-effort deletion of cli environment(s)
466+ if loadBalancer != nil {
467+ envNames , isDefaultEnv , _ := getEnvNamesByOperatorEndpoint (* loadBalancer .DNSName )
468+ if len (envNames ) > 0 {
469+ for _ , envName := range envNames {
470+ removeEnvFromCLIConfig (envName )
471+ }
472+ fmt .Printf ("✓ deleted the %s environment configuration%s\n " , s .StrsAnd (envNames ), s .SIfPlural (len (envNames )))
473+ if isDefaultEnv {
474+ fmt .Println ("✓ set the default environment to local" )
475+ }
476+ }
477+ }
478+
479+ fmt .Printf ("\n please check CloudFormation to ensure that all resources for the %s cluster eventually become successfully deleted: %s\n " , * accessConfig .ClusterName , clusterstate .CloudFormationURL (* accessConfig .ClusterName , * accessConfig .Region ))
480+
481+ cachedClusterConfigPath := cachedClusterConfigPath (* accessConfig .ClusterName , * accessConfig .Region )
482+ os .Remove (cachedClusterConfigPath )
436483 },
437484}
438485
@@ -491,7 +538,7 @@ func cmdInfo(awsCreds AWSCredentials, accessConfig *clusterconfig.AccessConfig,
491538
492539 clusterConfig := refreshCachedClusterConfig (awsCreds , accessConfig , disallowPrompt )
493540
494- out , exitCode , err := runManagerWithClusterConfig ("/root/info.sh" , & clusterConfig , awsCreds , _flagClusterEnv )
541+ out , exitCode , err := runManagerWithClusterConfig ("/root/info.sh" , & clusterConfig , awsCreds , _flagClusterEnv , nil , nil )
495542 if err != nil {
496543 exit .Error (err )
497544 }
@@ -724,36 +771,43 @@ func updateInfoEnvironment(operatorEndpoint string, awsCreds AWSCredentials, dis
724771}
725772
726773func cmdDebug (awsCreds AWSCredentials , accessConfig * clusterconfig.AccessConfig ) {
727- out , exitCode , err := runManagerAccessCommand ("/root/debug.sh" , * accessConfig , awsCreds , _flagClusterEnv )
774+ // note: if modifying this string, also change it in files.IgnoreCortexDebug()
775+ debugFileName := fmt .Sprintf ("cortex-debug-%s.tgz" , time .Now ().UTC ().Format ("2006-01-02-15-04-05" ))
776+
777+ containerDebugPath := "/out/" + debugFileName
778+ copyFromPaths := []dockerCopyFromPath {
779+ {
780+ containerPath : containerDebugPath ,
781+ localDir : _cwd ,
782+ },
783+ }
784+
785+ out , exitCode , err := runManagerAccessCommand ("/root/debug.sh " + containerDebugPath , * accessConfig , awsCreds , _flagClusterEnv , nil , copyFromPaths )
728786 if err != nil {
729787 exit .Error (err )
730788 }
731789 if exitCode == nil || * exitCode != 0 {
732790 exit .Error (ErrorClusterDebug (out ))
733791 }
734792
735- timestamp := time .Now ().UTC ().Format ("2006-01-02-15-04-05" )
736- userDebugPath := fmt .Sprintf ("cortex-debug-%s.tgz" , timestamp ) // note: if modifying this string, also change it in files.IgnoreCortexDebug()
737- err = os .Rename (_debugPath , userDebugPath )
738- if err != nil {
739- exit .Error (errors .WithStack (err ))
740- }
741-
742- fmt .Println ("saved cluster info to ./" + userDebugPath )
793+ fmt .Println ("saved cluster info to ./" + debugFileName )
743794 return
744795}
745796
746797func refreshCachedClusterConfig (awsCreds AWSCredentials , accessConfig * clusterconfig.AccessConfig , disallowPrompt bool ) clusterconfig.Config {
747798 // add empty file if cached cluster doesn't exist so that the file output by manager container maintains current user permissions
748- cachedConfigPath := cachedClusterConfigPath (* accessConfig .ClusterName , * accessConfig .Region )
749- if ! files .IsFile (cachedConfigPath ) {
750- files .MakeEmptyFile (cachedConfigPath )
751- }
799+ cachedClusterConfigPath := cachedClusterConfigPath (* accessConfig .ClusterName , * accessConfig .Region )
800+ containerConfigPath := fmt .Sprintf ("/out/%s" , filepath .Base (cachedClusterConfigPath ))
752801
753- mountedConfigPath := mountedClusterConfigPath (* accessConfig .ClusterName , * accessConfig .Region )
802+ copyFromPaths := []dockerCopyFromPath {
803+ {
804+ containerPath : containerConfigPath ,
805+ localDir : files .Dir (cachedClusterConfigPath ),
806+ },
807+ }
754808
755809 fmt .Print ("syncing cluster configuration ...\n \n " )
756- out , exitCode , err := runManagerAccessCommand ("/root/refresh.sh " + mountedConfigPath , * accessConfig , awsCreds , _flagClusterEnv )
810+ out , exitCode , err := runManagerAccessCommand ("/root/refresh.sh " + containerConfigPath , * accessConfig , awsCreds , _flagClusterEnv , nil , copyFromPaths )
757811 if err != nil {
758812 exit .Error (err )
759813 }
@@ -762,7 +816,7 @@ func refreshCachedClusterConfig(awsCreds AWSCredentials, accessConfig *clusterco
762816 }
763817
764818 refreshedClusterConfig := & clusterconfig.Config {}
765- readCachedClusterConfigFile (refreshedClusterConfig , cachedConfigPath )
819+ readCachedClusterConfigFile (refreshedClusterConfig , cachedClusterConfigPath )
766820 return * refreshedClusterConfig
767821}
768822
0 commit comments