Skip to content

Commit

Permalink
chg: fix: add handling for versions and fix bug with config include (#21
Browse files Browse the repository at this point in the history
)
  • Loading branch information
felipemarinho97 authored Sep 18, 2023
1 parent 758a3e6 commit 2023cb6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 77 deletions.
15 changes: 14 additions & 1 deletion cli/commands/scale.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package commands

import (
"github.com/felipemarinho97/dev-spaces/cli/config"
"github.com/felipemarinho97/dev-spaces/cli/util"
"github.com/felipemarinho97/dev-spaces/core"
"github.com/urfave/cli/v2"
)

func EditSpecCommand(c *cli.Context) error {
h := c.Context.Value("handler").(*core.Handler)
cfg := c.Context.Value("config").(*config.Config)

name := c.String("name")
minCPUs := c.Int("min-cpus")
Expand All @@ -19,13 +21,24 @@ func EditSpecCommand(c *cli.Context) error {
ub.Start()
defer ub.Stop()

_, err := h.EditSpec(c.Context, core.EditSpecOptions{
newSpec, err := h.EditSpec(c.Context, core.EditSpecOptions{
Name: name,
MinCPUs: minCPUs,
MinMemory: 1024 * minMemory,
MaxPrice: maxPrice,
SSHKey: identityFile,
})
if err != nil {
return err
}

// update SSH config entry
_, err = util.CreateSSHConfig(*cfg, newSpec.InstanceIP, name)
if err != nil {
h.Logger.Warn("Error updating SSH config entry: %s", err)
} else {
h.Logger.Info("Updated SSH config entry")
}

return err
}
14 changes: 7 additions & 7 deletions cli/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ func StartCommand(c *cli.Context) error {
return err
}

loginCommand := fmt.Sprintf("ssh -i <your-key.pem> -p 2222 -o StrictHostKeyChecking=no root@%s", out.PublicIP)

// create SSH config entry
customDNS, err := util.CreateSSHConfig(*cfg, out.PublicIP, name)
configPath, err := util.CreateSSHConfig(*cfg, out.PublicIP, name)
if err != nil {
log.Warn(fmt.Sprintf("Error creating SSH config entry for %s: %s", name, err))
} else {
log.Info(fmt.Sprintf("Created SSH config entry for %s: %s", name, customDNS))
log.Info(fmt.Sprintf("Created SSH config entry for %s.", name))
log.Info(fmt.Sprintf("You can customize the SSH config entry at %s", configPath))
loginCommand = fmt.Sprintf("ssh -i <your-key.pem> root@%s", name)
}

if wait {
Expand All @@ -58,13 +62,9 @@ func StartCommand(c *cli.Context) error {
if err != nil {
return err
}
host := customDNS
if host == "" {
host = out.PublicIP
}

log.Info("You can now ssh into your dev space with the following command: ")
fmt.Printf("$ ssh -i <your-key.pem> -p 2222 -o StrictHostKeyChecking=no root@%s\n", host)
fmt.Printf("$ %s\n", loginCommand)
}

return nil
Expand Down
59 changes: 47 additions & 12 deletions cli/util/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"os"
"regexp"

"github.com/felipemarinho97/dev-spaces/cli/config"
)
Expand All @@ -19,13 +20,17 @@ func CreateSSHConfig(config config.Config, ip, name string) (string, error) {
return "", err
}

// discard the version at the end of name (the number after the /)
re := regexp.MustCompile(`(.*)\/.*`)
name = re.ReplaceAllString(name, "$1")

// add entry to ssh config
err = putConfigEntry(sshConfigPath, name, ip)
if err != nil {
return "", err
}

return name, nil
return fmt.Sprintf("%s/%s", sshConfigPath, name), nil
}

func getSSHConfigPath() (string, error) {
Expand Down Expand Up @@ -55,13 +60,14 @@ func getSSHConfigPath() (string, error) {
}
}

// Include directive does not exist, append it
sshConfig, err = os.OpenFile(sshConfigPath, os.O_APPEND|os.O_WRONLY, 0600)
// Include directive does not exist, append it to the beginning of the file
sshConfigContent, err := os.ReadFile(sshConfigPath)
if err != nil {
return "", err
}

_, err = sshConfig.WriteString(fmt.Sprintf("Include %s/*\n", customSSHConfigPath))
newSSHConfigContent := fmt.Sprintf("Include %s/*\n%s", customSSHConfigPath, string(sshConfigContent))
err = os.WriteFile(sshConfigPath, []byte(newSSHConfigContent), 0644)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -97,16 +103,45 @@ func createSSHConfig() (string, error) {
}

func putConfigEntry(sshConfigPath, name, ip string) error {
// create entry file
sshConfig, err := os.Create(fmt.Sprintf("%s/%s", sshConfigPath, name))
if err != nil {
return err
// check if entry already exists
sshConfig, err := os.Open(fmt.Sprintf("%s/%s", sshConfigPath, name))
// if the file does not exist, create it
if os.IsNotExist(err) {
// create entry file
sshConfig, err := os.Create(fmt.Sprintf("%s/%s", sshConfigPath, name))
if err != nil {
return err
}
defer sshConfig.Close()

// write the entry
_, err = sshConfig.WriteString(fmt.Sprintf("Host %s\n\tHostName %s\n\tPort 2222\n\tStrictHostKeyChecking no\n\tUser root\n\t# IdentityFile <~/.ssh/your-key.pem>\n", name, ip))
if err != nil {
return err
}
}
defer sshConfig.Close()

// write the entry
_, err = sshConfig.WriteString(fmt.Sprintf("Host %s\n\tHostName %s\n\tPort 2222\n", name, ip))
if err != nil {
return err
// replace the HostName with the new IP address
scanner := bufio.NewScanner(sshConfig)
for scanner.Scan() {
line := scanner.Text()
re := regexp.MustCompile(`HostName\s.*`)
if re.MatchString(line) {
// replace the HostName
fileContent, err := os.ReadFile(fmt.Sprintf("%s/%s", sshConfigPath, name))
if err != nil {
return err
}

newFileContent := re.ReplaceAllString(string(fileContent), fmt.Sprintf("HostName %s", ip))
err = os.WriteFile(fmt.Sprintf("%s/%s", sshConfigPath, name), []byte(newFileContent), 0644)
if err != nil {
return err
}

return nil
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion core/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (h *Handler) Start(ctx context.Context, startOptions StartOptions) (StartOu
InstanceID: *instance.InstanceId,
Type: string(instance.InstanceType),
PublicIP: ip,
Port: 22,
Port: 2222,
DNS: *instance.PublicDnsName,
}, nil
}
Expand Down
Loading

0 comments on commit 2023cb6

Please sign in to comment.