Skip to content

Commit

Permalink
Added aws vpn init
Browse files Browse the repository at this point in the history
  • Loading branch information
Andriiklymiuk committed Mar 29, 2024
1 parent 9e8fcf3 commit 5ca286b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func GetCorgiServicesMap(corgi *utils.CorgiCompose) map[string]interface{} {
corgiServicesMap[utils.UseDockerInConfig] = corgi.UseDocker
}

if corgi.UseAwsVpn {
corgiServicesMap[utils.UseAwsVpnInConfig] = corgi.UseAwsVpn
}

return corgiServicesMap
}

Expand Down Expand Up @@ -377,6 +381,7 @@ func UpdateCorgiComposeFileWithMap(corgiMap map[string]interface{}) {
utils.BeforeStartInConfig,
utils.AfterStartInConfig,
utils.UseDockerInConfig,
utils.UseAwsVpnInConfig,
} {
if section, exists := corgiMap[sectionKey]; exists {
if sectionArr, ok := section.([]string); ok {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

var APP_VERSION = "1.3.2"
var APP_VERSION = "1.3.3"

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down
7 changes: 7 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ func runRun(cmd *cobra.Command, _ []string) {

utils.CleanFromScratch(cmd, *corgi)

if corgi.UseAwsVpn {
err = utils.AwsVpnInit()
if err != nil {
fmt.Println("AWS VPN init failed", err)
}
}

if corgi.UseDocker {
err = utils.DockerInit(cmd)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var StartInConfig = "start"
var BeforeStartInConfig = "beforeStart"
var AfterStartInConfig = "afterStart"
var UseDockerInConfig = "useDocker"
var UseAwsVpnInConfig = "useAwsVpn"

var RootDbServicesFolder = "corgi_services/db_services"
var ServicesItemsFromFlag []string
Expand Down Expand Up @@ -109,6 +110,7 @@ type CorgiCompose struct {
AfterStart []string `yaml:"afterStart,omitempty"`

UseDocker bool `yaml:"useDocker,omitempty"`
UseAwsVpn bool `yaml:"useAwsVpn,omitempty"`
}

type CorgiComposeYaml struct {
Expand All @@ -122,6 +124,7 @@ type CorgiComposeYaml struct {
AfterStart []string `yaml:"afterStart,omitempty"`

UseDocker bool `yaml:"useDocker,omitempty"`
UseAwsVpn bool `yaml:"useAwsVpn,omitempty"`
}

var CorgiComposePath string
Expand Down Expand Up @@ -156,6 +159,7 @@ func GetCorgiServices(cobra *cobra.Command) (*CorgiCompose, error) {
corgi.Start = corgiYaml.Start
corgi.AfterStart = corgiYaml.AfterStart
corgi.UseDocker = corgiYaml.UseDocker
corgi.UseAwsVpn = corgiYaml.UseAwsVpn

dbServicesData := corgiYaml.DatabaseServices

Expand Down
59 changes: 59 additions & 0 deletions utils/startCommands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package utils

import (
"bytes"
"fmt"
"os/exec"
"runtime"
"strings"
"time"

"github.com/briandowns/spinner"
)

func AwsVpnInit() error {
if runtime.GOOS == "linux" {
return fmt.Errorf("this function is not intended to run on Linux")
}

s := spinner.New(spinner.CharSets[39], 100*time.Millisecond)
s.Suffix = " doing woof magic to start aws vpn"

for {
s.Start()
cmd := exec.Command("ps", "ax")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
s.Stop()
return fmt.Errorf("failed to execute ps command: %v", err)
}

vpnAlive := false
for _, line := range strings.Split(out.String(), "\n") {
if strings.Contains(line, "AWS") && strings.Contains(line, "isAlive") {
vpnAlive = true
break
}
}

if !vpnAlive {
startCmd := exec.Command("open", "-a", "AWS VPN Client")
if err := startCmd.Run(); err != nil {
s.Stop()
return fmt.Errorf("failed to start AWS VPN Client: %v", err)
}

s.Suffix = " Waiting for AWS VPN to start..."
time.Sleep(5 * time.Second)
} else {
s.Stop()
fmt.Println("\nAWS vpn is opened, so waiting an additional time till you login in your account")
time.Sleep(10 * time.Second)
break
}
}

return nil
}

0 comments on commit 5ca286b

Please sign in to comment.