Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/dockify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
httppkg "github.com/coderbuzz/dockify/internal/http"
"github.com/coderbuzz/dockify/internal/scheduler"
"github.com/coderbuzz/dockify/internal/server"
"github.com/coderbuzz/dockify/internal/ssh"
"github.com/coderbuzz/dockify/internal/settings"
)

Expand Down Expand Up @@ -61,6 +62,12 @@ func main() {
appRepo := app.NewRepository(database)
appSvc := app.NewService(appRepo, serverRepo, cfClient, sch)

if cfg.DevMock {
svc.SetConnFactory(ssh.MockFactory())
appSvc.SetConnFactory(ssh.MockFactory())
log.Println("DEV MOCK MODE: using mock SSH client")
}

settingsSvc := settings.NewService(database, version)
settingsHandler := settings.NewHandler(settingsSvc)

Expand Down
6 changes: 4 additions & 2 deletions internal/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (h *WebHandler) AppAddForm(w http.ResponseWriter, r *http.Request, render R
envVars := strings.TrimSpace(r.FormValue("env_vars"))
volumes := strings.TrimSpace(r.FormValue("volumes"))

if compose == "" && image != "" {
if image != "" && (compose == "" || !strings.Contains(compose, "services:")) {
compose = generateCompose(image, port, envVars, volumes)
}

Expand Down Expand Up @@ -461,7 +461,7 @@ func (h *WebHandler) AppEditForm(w http.ResponseWriter, r *http.Request, render
envVars := strings.TrimSpace(r.FormValue("env_vars"))
volumes := strings.TrimSpace(r.FormValue("volumes"))

if compose == "" && image != "" {
if image != "" && (compose == "" || !strings.Contains(compose, "services:")) {
compose = generateCompose(image, port, envVars, volumes)
}

Expand Down Expand Up @@ -503,6 +503,7 @@ func (h *WebHandler) AppEditForm(w http.ResponseWriter, r *http.Request, render
return
}

saveFormSecrets(r, h.service, id)
saveFormFiles(r, h.service, id)

go h.service.Redeploy(id)
Expand Down Expand Up @@ -590,6 +591,7 @@ func (h *WebHandler) AppStartWeb(w http.ResponseWriter, r *http.Request, render
}

func saveFormSecrets(r *http.Request, svc *Service, appID int64) {
svc.DeleteSecrets(appID)
keys := r.Form["secret_key"]
vals := r.Form["secret_val"]
for i, k := range keys {
Expand Down
23 changes: 16 additions & 7 deletions internal/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ type Service struct {
serverRepo *server.Repository
cf *cloudflare.Client
scheduler *scheduler.Scheduler
connFactory ssh.Factory
}

func (s *Service) SetConnFactory(f ssh.Factory) {
s.connFactory = f
}

func NewService(repo *Repository, serverRepo *server.Repository, cf *cloudflare.Client, sch *scheduler.Scheduler) *Service {
return &Service{repo: repo, serverRepo: serverRepo, cf: cf, scheduler: sch}
return &Service{repo: repo, serverRepo: serverRepo, cf: cf, scheduler: sch, connFactory: ssh.RealFactory()}
}

func (s *Service) List() ([]App, error) {
Expand Down Expand Up @@ -60,6 +65,10 @@ func (s *Service) DeleteSecret(appID int64, key string) error {
return s.repo.DeleteSecret(appID, key)
}

func (s *Service) DeleteSecrets(appID int64) error {
return s.repo.DeleteSecrets(appID)
}

func (s *Service) ListFiles(appID int64) ([]AppFile, error) {
return s.repo.ListFiles(appID)
}
Expand Down Expand Up @@ -119,7 +128,7 @@ func (s *Service) deployWithCommit(id int64, commitSHA string) {
return
}

client, err := ssh.Connect(svr.Host, svr.Port, svr.User, svr.SSHKey)
client, err := s.connFactory(svr.Host, svr.Port, svr.User, svr.SSHKey)
if err != nil {
s.recordDeployment(id, svr.ID, StatusFailed, fmt.Sprintf("SSH connect: %v", err), commitSHA, app.Compose)
s.repo.UpdateStatus(id, StatusFailed)
Expand Down Expand Up @@ -258,7 +267,7 @@ func (s *Service) FetchLogs(id int64, tail int) (string, error) {
return "", fmt.Errorf("server not found")
}

client, err := ssh.Connect(svr.Host, svr.Port, svr.User, svr.SSHKey)
client, err := s.connFactory(svr.Host, svr.Port, svr.User, svr.SSHKey)
if err != nil {
return "", fmt.Errorf("SSH connect: %w", err)
}
Expand Down Expand Up @@ -301,7 +310,7 @@ func (s *Service) Undeploy(id int64) error {
return nil
}

client, err := ssh.Connect(svr.Host, svr.Port, svr.User, svr.SSHKey)
client, err := s.connFactory(svr.Host, svr.Port, svr.User, svr.SSHKey)
if err != nil {
log.Printf("Undeploy %q: SSH connect failed, cleaning up DB: %v", app.Name, err)
s.repo.DeleteDNSRecords(app.ID)
Expand Down Expand Up @@ -357,7 +366,7 @@ func (s *Service) Stop(id int64) error {
return fmt.Errorf("server not found")
}

client, err := ssh.Connect(svr.Host, svr.Port, svr.User, svr.SSHKey)
client, err := s.connFactory(svr.Host, svr.Port, svr.User, svr.SSHKey)
if err != nil {
return fmt.Errorf("SSH connect: %w", err)
}
Expand Down Expand Up @@ -387,7 +396,7 @@ func (s *Service) Start(id int64) error {
return fmt.Errorf("server not found")
}

client, err := ssh.Connect(svr.Host, svr.Port, svr.User, svr.SSHKey)
client, err := s.connFactory(svr.Host, svr.Port, svr.User, svr.SSHKey)
if err != nil {
return fmt.Errorf("SSH connect: %w", err)
}
Expand Down Expand Up @@ -464,7 +473,7 @@ func (s *Service) GetDeployment(id int64) (*Deployment, error) {

var _ = time.Now

func dockerComposeCmd(c *ssh.Client) string {
func dockerComposeCmd(c ssh.Connector) string {
out, err := c.Exec("command -v docker-compose 2>/dev/null || echo docker compose")
if err != nil {
return "docker compose"
Expand Down
4 changes: 2 additions & 2 deletions internal/caddy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ type upstream struct {
}

type Client struct {
ssh *ssh.Client
ssh ssh.Connector
}

func NewClient(sshClient *ssh.Client) *Client {
func NewClient(sshClient ssh.Connector) *Client {
return &Client{ssh: sshClient}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Config struct {

CloudflareAPIToken string
CloudflareZoneID string

DevMock bool
}

func Load() *Config {
Expand All @@ -28,6 +30,8 @@ func Load() *Config {
AdminPass: os.Getenv("DOCKIFY_ADMIN_PASSWORD"),
CloudflareAPIToken: os.Getenv("CLOUDFLARE_API_TOKEN"),
CloudflareZoneID: os.Getenv("CLOUDFLARE_ZONE_ID"),

DevMock: os.Getenv("DOCKIFY_DEV_MOCK") == "true",
}

os.MkdirAll(cfg.DataDir, 0700)
Expand Down
Loading
Loading