From a8259183779e2e7eff311ce9c54af9e5b89d0c4a Mon Sep 17 00:00:00 2001 From: Nikita Podshivalov Date: Mon, 31 Oct 2022 17:10:05 +0300 Subject: [PATCH] added explain mode for `ize tunnel` commands --- internal/commands/tunnel_down.go | 19 ++++++++++++++++++- internal/commands/tunnel_status.go | 22 ++++++++++++++++++++-- internal/commands/tunnel_up.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/internal/commands/tunnel_down.go b/internal/commands/tunnel_down.go index dad0f235..94683306 100644 --- a/internal/commands/tunnel_down.go +++ b/internal/commands/tunnel_down.go @@ -13,8 +13,14 @@ import ( "github.com/spf13/cobra" ) +var explainTunnelDownTmpl = ` +# Change to the dir and send an exit request +(cd {{.EnvDir}} && $(aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.cmd.value.tunnel.down')) +` + type TunnelDownOptions struct { - Config *config.Project + Config *config.Project + Explain bool } func NewTunnelDownOptions(project *config.Project) *TunnelDownOptions { @@ -33,6 +39,15 @@ func NewCmdTunnelDown(project *config.Project) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true + if o.Explain { + err := o.Config.Generate(explainTunnelDownTmpl, nil) + if err != nil { + return err + } + + return nil + } + err := o.Complete() if err != nil { return err @@ -52,6 +67,8 @@ func NewCmdTunnelDown(project *config.Project) *cobra.Command { }, } + cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown") + return cmd } diff --git a/internal/commands/tunnel_status.go b/internal/commands/tunnel_status.go index 26c96f90..5e92119b 100644 --- a/internal/commands/tunnel_status.go +++ b/internal/commands/tunnel_status.go @@ -9,9 +9,15 @@ import ( "github.com/spf13/cobra" ) +var explainTunnelStatusTmpl = ` +# Change to the dir and get status +(cd {{.EnvDir}} && $(aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.cmd.value.tunnel.status')) +` + type TunnelStatusOptions struct { - Config *config.Project - UI terminal.UI + Config *config.Project + UI terminal.UI + Explain bool } func NewTunnelStatusOptions(project *config.Project) *TunnelStatusOptions { @@ -29,6 +35,16 @@ func NewCmdTunnelStatus(project *config.Project) *cobra.Command { Long: "Tunnel running status", RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true + + if o.Explain { + err := o.Config.Generate(explainTunnelStatusTmpl, nil) + if err != nil { + return err + } + + return nil + } + err := o.Complete() if err != nil { return err @@ -48,6 +64,8 @@ func NewCmdTunnelStatus(project *config.Project) *cobra.Command { }, } + cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown") + return cmd } diff --git a/internal/commands/tunnel_up.go b/internal/commands/tunnel_up.go index c5160e43..5aaae7eb 100644 --- a/internal/commands/tunnel_up.go +++ b/internal/commands/tunnel_up.go @@ -40,6 +40,24 @@ ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartS {{end}} ` +var explainTunnelUpTmpl = ` +# Set variables +SSH_CONFIG={{.EnvDir}}/ssh.config +SSH_PUBLIC_KEY=$(cat ~/.ssh/id_rsa.pub) + +# Get bastion instance id +BASTION_INSTANCE_ID=$(aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.bastion_instance_id.value' + +# Get ssh config +aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.ssh_forward_config.value[]' > $SSH_CONFIG + +# Send ssh public key to instance +aws ssm send-command --instance-ids $BASTION_INSTANCE_ID --document-name AWS-RunShellScript --comment 'Add an SSH public key to authorized_keys' --parameters '{"commands": ["grep -qR \"$(SSH_PUBLIC_KEY)\" /home/ubuntu/.ssh/authorized_keys || echo \"$(SSH_PUBLIC_KEY)\" >> /home/ubuntu/.ssh/authorized_keys"]}' 1> /dev/null) + +# Change to the dir and up tunnel +(cd {{.EnvDir}} && $(aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.cmd.value.tunnel.up') -F $SSH_CONFIG) +` + type TunnelUpOptions struct { Config *config.Project PrivateKeyFile string @@ -48,6 +66,7 @@ type TunnelUpOptions struct { ForwardHost []string StrictHostKeyChecking bool Metadata bool + Explain bool } func NewTunnelUpFlags(project *config.Project) *TunnelUpOptions { @@ -65,6 +84,16 @@ func NewCmdTunnelUp(project *config.Project) *cobra.Command { Long: "Open tunnel with sending ssh key to remote server", RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true + + if o.Explain { + err := o.Config.Generate(explainTunnelUpTmpl, nil) + if err != nil { + return err + } + + return nil + } + err := o.Complete() if err != nil { return err @@ -90,6 +119,7 @@ func NewCmdTunnelUp(project *config.Project) *cobra.Command { cmd.Flags().StringVar(&o.PrivateKeyFile, "ssh-private-key", "", "set ssh key private path") cmd.PersistentFlags().BoolVar(&o.StrictHostKeyChecking, "strict-host-key-checking", true, "set strict host key checking") cmd.PersistentFlags().BoolVar(&o.Metadata, "use-ec2-metadata", false, "send ssh key to EC2 metadata (work only for Ubuntu versions > 20.0)") + cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown") return cmd }