Skip to content

Commit

Permalink
feat: add docker support
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispinkney committed Oct 31, 2024
1 parent bc40c52 commit 22469ac
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 76 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ go.work.sum
.idea/

# env file
src/cmd/pecker.conf
cmd/woodpecker/pecker.conf
deployments/docker-compose.yml
35 changes: 20 additions & 15 deletions src/cmd/main.go → cmd/woodpecker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"fmt"
"github.com/robfig/cron/v3"
"os"
"path/filepath"
"woodpecker/src/config"
"woodpecker/src/constants"
"woodpecker/src/internal/utils"
"woodpecker/src/providers/namecheap"
"woodpecker/src/providers/porkbun"
"woodpecker/src/services"
"woodpecker/internal/config"
"woodpecker/internal/providers/namecheap"
"woodpecker/internal/providers/porkbun"
"woodpecker/internal/services"
"woodpecker/internal/utils"
)

func main() {
Expand All @@ -19,9 +17,8 @@ func main() {
fmt.Println("failed to get config path:", err)
os.Exit(1)
}
configPath := filepath.Join(configDir, constants.ConfigFilename)

loadConfig, err := config.LoadConfig(configPath)
loadConfig, err := config.LoadConfig()
if err != nil {
fmt.Println("error loading environment file:", err)
os.Exit(1)
Expand Down Expand Up @@ -77,14 +74,22 @@ func updateDNS(config *config.Config, configPath string) error {

fmt.Println("IP address has changed, proceeding to update DNS records...")

err = updatePorkbunDNS(config, ip)
if err != nil {
return err
if config.PorkbunAPIKey != "" && config.PorkbunSecretKey != "" {
err = updatePorkbunDNS(config, ip)
if err != nil {
return err
}
} else {
fmt.Println("skipping Porkbun DNS update as required config not provided")
}

err = updateNamecheapDNS(config, ip)
if err != nil {
return err
if config.NamecheapPassword != "" {
err = updateNamecheapDNS(config, ip)
if err != nil {
return err
}
} else {
fmt.Println("skipping Namecheap DNS update as required config not provided")
}

err = utils.WriteIPToFile(ip, configPath)
Expand Down
21 changes: 21 additions & 0 deletions deployments/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:1.23.0 AS builder

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

RUN go build -o woodpecker ./cmd/woodpecker

FROM alpine:latest

RUN apk add --no-cache libc6-compat

WORKDIR /app

COPY --from=builder /app/woodpecker .

CMD ["./woodpecker"]
28 changes: 28 additions & 0 deletions deployments/docker-compose.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
woodpecker:
build:
context: ..
dockerfile: deployments/Dockerfile
environment:
# IP config
IP_SERVICE: https://api.ipify.org # https://ifconfig.me || https://ipinfo.io/ip
CHECK_INTERVAL: 1 # interval in minutes to check IP address

# PorkBun config (optional)
PORKBUN_API_EDIT_URL: https://api.porkbun.com/api/json/v3/dns/editByNameType/
PORKBUN_API_RETRIEVE_URL: https://api.porkbun.com/api/json/v3/dns/retrieveByNameType/
PORKBUN_API_KEY: porkbun-api-key
PORKBUN_SECRET_KEY: porkbun-secret-key
PORKBUN_DOMAIN: example.com
PORKBUN_SUBDOMAIN: www # leave blank for root

# Namecheap config (optional)
NAMECHEAP_EDIT_URL: https://dynamicdns.park-your-domain.com/update
NAMECHEAP_PASSWORD: namecheap-password
NAMECHEAP_DOMAIN: example.com
NAMECHEAP_SUBDOMAIN: www # leave blank for root
volumes:
- ip-data:/app/ip-data

volumes:
ip-data:
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ module woodpecker

go 1.22

require github.com/joho/godotenv v1.5.1

require github.com/robfig/cron/v3 v3.0.1 // indirect
require github.com/robfig/cron/v3 v3.0.1
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
19 changes: 2 additions & 17 deletions src/config/config.go → internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"fmt"
"os"
"strconv"
"woodpecker/src/constants"

"github.com/joho/godotenv"
"woodpecker/internal/constants"
)

type Config struct {
Expand All @@ -24,12 +22,7 @@ type Config struct {
NamecheapSubdomain string
}

func LoadConfig(configPath string) (*Config, error) {
err := godotenv.Load(configPath)
if err != nil {
return nil, fmt.Errorf("failed to load config file %s: %v", constants.ConfigFilename, err)
}

func LoadConfig() (*Config, error) {
intervalStr := os.Getenv("CHECK_INTERVAL")
interval, err := strconv.Atoi(intervalStr)
if err != nil || interval <= 0 {
Expand All @@ -55,13 +48,5 @@ func LoadConfig(configPath string) (*Config, error) {
return nil, fmt.Errorf("missing required IP Service field in %s", constants.ConfigFilename)
}

if config.PorkbunAPIKey == "" || config.PorkbunSecretKey == "" || config.PorkbunDomain == "" || config.PorkbunEditByNameTypeURL == "" || config.PorkbunRetrieveByNameTypeURL == "" {
return nil, fmt.Errorf("missing required Porkbun key fields in %s", constants.ConfigFilename)
}

if config.NamecheapPassword == "" || config.NamecheapDomain == "" || config.NamecheapEditURL == "" {
return nil, fmt.Errorf("missing required Namecheap key fields in %s", constants.ConfigFilename)
}

return config, nil
}
5 changes: 5 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package constants

const ConfigFilename = "docker-compose.yml"
const CurrentIpFilename = "current_ip.conf"
const CurrentIPFilenameDir = "/app/ip-data"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"io"
"net/http"
"strings"
"woodpecker/src/config"
"woodpecker/src/providers"
"woodpecker/internal/config"
"woodpecker/internal/providers"
)

type Namecheap struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"woodpecker/src/config"
"woodpecker/src/providers"
"woodpecker/internal/config"
"woodpecker/internal/providers"
)

type Porkbun struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"net/http"
"strings"
"woodpecker/src/config"
"woodpecker/internal/config"
)

func GetPublicIP(config *config.Config) (string, error) {
Expand Down
14 changes: 2 additions & 12 deletions src/internal/utils/utils.go → internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"woodpecker/src/constants"
"woodpecker/internal/constants"
)

func GetAppPath() (string, error) {
var dir string

switch runtime.GOOS {
case "windows":
appData := os.Getenv("APPDATA")
dir = filepath.Join(appData, "woodpecker")
case "linux":
home := os.Getenv("HOME")
dir = filepath.Join(home, ".local", "share", "woodpecker")
}
dir := constants.CurrentIPFilenameDir

err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
Expand Down
17 changes: 0 additions & 17 deletions pecker.example.conf

This file was deleted.

4 changes: 0 additions & 4 deletions src/constants/constants.go

This file was deleted.

0 comments on commit 22469ac

Please sign in to comment.