2024-07-13 00:48:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/bitfield/script"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"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:06:04 +00:00
|
|
|
if err := runCmd(`mkdir -p $HOME/.ssh`); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-13 01:06:04 +00:00
|
|
|
if err := runCmd(`chmod 700 $HOME/.ssh`); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-13 01:06:04 +00:00
|
|
|
if err := writeFile(`${HOME}/.ssh/id_rsa`, s.cfg.PrivateKey); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-13 01:06:04 +00:00
|
|
|
if err := runCmd(`chmod -R 400 $HOME/.ssh/.`); 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:06:04 +00:00
|
|
|
if err := writeFile(`${HOME}/.ssh/config`, bfr.String()); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-07-13 01:06:04 +00:00
|
|
|
|
|
|
|
if err := runCmd(`chmod 400 $HOME/.ssh/config`); 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:06:04 +00:00
|
|
|
if err := runCmd(fmt.Sprintf(`ssh %v version`, sshUrl)); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("SSH connection good. Pushing repository")
|
2024-07-13 01:06:04 +00:00
|
|
|
if err := runCmd(fmt.Sprintf(`git remote add dokku '%v'`, remoteUrl); err != nil {
|
2024-07-13 00:48:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-13 01:06:04 +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:06:04 +00:00
|
|
|
func runCmd(cmd string) error {
|
|
|
|
fullCmd := os.ExpandEnv(cmd)
|
|
|
|
log.Printf(" .. [exec] %v", fullCmd)
|
|
|
|
return script.Exec(fullCmd).Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFile(path string, content string) error {
|
|
|
|
fullPath := os.ExpandEnv(path)
|
|
|
|
log.Printf(" .. [file] %v", fullPath)
|
|
|
|
|
|
|
|
_, err := script.Echo(content).WriteFile(fullPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-13 00:48:01 +00:00
|
|
|
var (
|
|
|
|
sshConfigTemplate = template.Must(template.New("ssh-config").Parse(`
|
|
|
|
Host *
|
|
|
|
StrictHostKeyChecking no
|
|
|
|
|
|
|
|
Host {{.Cfg.Host}}
|
|
|
|
HostName {{.Cfg.Host}}
|
|
|
|
User dokku
|
|
|
|
IdentityFile {{.Home}}/.ssh/id_rsa
|
|
|
|
`))
|
|
|
|
)
|