2024-07-13 00:48:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2024-07-13 01:21:00 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2024-07-13 00:48:01 +00:00
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Services struct {
|
|
|
|
cfg config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServices(cfg config) *Services {
|
|
|
|
return &Services{cfg: cfg}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Services) AddPrivateKey() error {
|
|
|
|
log.Println("Adding private key")
|
|
|
|
|
2024-07-13 01:21:00 +00:00
|
|
|
if err := os.MkdirAll(os.ExpandEnv("${HOME}/.ssh"), 0700); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-13 01:13:40 +00:00
|
|
|
if err := writeFile(`${HOME}/.ssh/id_rsa`, s.cfg.PrivateKey, 0400); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Services) ConfigureSSH() error {
|
|
|
|
log.Println("Configuring SSH")
|
|
|
|
|
|
|
|
var bfr bytes.Buffer
|
|
|
|
if err := sshConfigTemplate.Execute(&bfr, struct {
|
|
|
|
Cfg config
|
|
|
|
Home string
|
|
|
|
}{
|
|
|
|
Cfg: s.cfg,
|
|
|
|
Home: os.Getenv("HOME"),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-13 01:13:40 +00:00
|
|
|
if err := writeFile(`${HOME}/.ssh/config`, bfr.String(), 0400); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Services) PushRepository() error {
|
|
|
|
sshUrl := fmt.Sprintf(`dokku@%v`, s.cfg.HostName)
|
|
|
|
remoteUrl := fmt.Sprintf(`ssh://%v:22/%v`, sshUrl, s.cfg.AppName)
|
|
|
|
|
|
|
|
log.Println("Testing Dokku SSH connection")
|
2024-07-13 01:21:00 +00:00
|
|
|
if err := runCmd("ssh", sshUrl, "version"); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("SSH connection good. Pushing repository")
|
2024-07-13 01:21:00 +00:00
|
|
|
if err := runCmd("git", "remote", "add", "dokku", remoteUrl); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-13 01:21:00 +00:00
|
|
|
if err := runCmd("git", "push", "dokku", "main"); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-13 01:21:00 +00:00
|
|
|
func runCmd(cmd ...string) error {
|
|
|
|
log.Printf(" .. [exec] %v", strings.Join(cmd, " "))
|
|
|
|
e := exec.Command(cmd[0], cmd[1:]...)
|
|
|
|
e.Stderr = os.Stderr
|
|
|
|
e.Stdout = os.Stdout
|
|
|
|
return e.Run()
|
2024-07-13 01:06:04 +00:00
|
|
|
}
|
|
|
|
|
2024-07-13 01:13:40 +00:00
|
|
|
func writeFile(path string, content string, mode os.FileMode) error {
|
2024-07-13 01:06:04 +00:00
|
|
|
fullPath := os.ExpandEnv(path)
|
2024-07-13 01:21:00 +00:00
|
|
|
log.Printf(" .. [file] %v (%v bytes)", fullPath, len(content))
|
2024-07-13 01:06:04 +00:00
|
|
|
|
2024-07-13 01:13:40 +00:00
|
|
|
return os.WriteFile(fullPath, []byte(content), mode)
|
2024-07-13 01:06:04 +00:00
|
|
|
}
|
|
|
|
|
2024-07-13 00:48:01 +00:00
|
|
|
var (
|
|
|
|
sshConfigTemplate = template.Must(template.New("ssh-config").Parse(`
|
|
|
|
Host *
|
|
|
|
StrictHostKeyChecking no
|
|
|
|
|
2024-07-13 01:31:33 +00:00
|
|
|
Host {{.Cfg.HostName}}
|
|
|
|
HostName {{.Cfg.HostName}}
|
2024-07-13 00:48:01 +00:00
|
|
|
User dokku
|
|
|
|
IdentityFile {{.Home}}/.ssh/id_rsa
|
|
|
|
`))
|
|
|
|
)
|