Last active
September 19, 2022 16:54
cloudflare ddns and pages deploy cleaner&helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"errors" | |
"fmt" | |
"github.com/cloudflare/cloudflare-go" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
) | |
const ( | |
token = "xxxxxxx" | |
apiKey = "xxxxxxxx" | |
user = "xxxxx@qq.com" | |
accountId = "xxxxxx" | |
) | |
func main() { | |
options := []cloudflare.Option{ | |
cloudflare.UsingRetryPolicy(5, 1, 1), | |
} | |
api, err := cloudflare.New(apiKey, user, options...) | |
if err != nil { | |
log.Fatal(err) | |
} | |
zoneID, err := api.ZoneIDByName("cccc.tech") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Fetch only AAAA type records | |
record := cloudflare.DNSRecord{Name: "nb.cccc.tech"} | |
ctx := context.Background() | |
recs, err := api.DNSRecords(ctx, zoneID, record) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, r := range recs { | |
var ( | |
ip string | |
err error | |
) | |
switch r.Type { | |
case "A": | |
ip, err = getIPV4() | |
if err != nil { | |
log.Fatal(err) | |
return | |
} | |
case "AAAA": | |
ip, err = getIpV6() | |
if err != nil { | |
log.Fatal(err) | |
return | |
} | |
} | |
if ip == "" { | |
log.Fatal("Ip not fetch") | |
return | |
} | |
updateDnsErr := api.UpdateDNSRecord(ctx, zoneID, r.ID, cloudflare.DNSRecord{ | |
Type: r.Type, | |
Content: ip, | |
TTL: 1, | |
}) | |
if updateDnsErr != nil { | |
log.Fatal(updateDnsErr) | |
} else { | |
log.Println("update dns record ok,type:", r.Type) | |
} | |
} | |
pageOpt := cloudflare.PaginationOptions{Page: 1, PerPage: 5} | |
projects, _, err := api.ListPagesProjects(context.Background(), accountId, pageOpt) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, project := range projects { | |
updateHugoVersionEnv(project, api) | |
latestId := project.LatestDeployment.ID | |
// remove history deploy | |
deployments, _, err := api.ListPagesDeployments( | |
context.Background(), | |
cloudflare.AccountIdentifier(accountId), | |
cloudflare.ListPagesDeploymentsParams{ | |
ProjectName: project.Name, | |
PaginationOptions: cloudflare.PaginationOptions{ | |
Page: 1, | |
PerPage: 20, | |
}, | |
}, | |
) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for i, deployment := range deployments { | |
if deployment.ID == latestId { | |
continue | |
} | |
if err := api.DeletePagesDeployment( | |
context.Background(), | |
cloudflare.AccountIdentifier(accountId), | |
project.Name, | |
deployment.ID, | |
); err != nil { | |
log.Println("delete deploy:", i, "failed", err) | |
} | |
log.Println("delete deploy:", i, "success") | |
} | |
} | |
os.Exit(0) | |
} | |
func updateHugoVersionEnv(project cloudflare.PagesProject, api *cloudflare.API) error { | |
release, err := GetGithubLatestRelease("gohugoio/hugo") | |
if err != nil { | |
release = "0.103.1" | |
} | |
release = release[1:] | |
ProductionEnv := make(map[string]cloudflare.PagesProjectDeploymentVar) | |
ProductionEnv["HUGO_VERSION"] = cloudflare.PagesProjectDeploymentVar{Value: release} | |
project.DeploymentConfigs.Production.EnvVars = ProductionEnv | |
api.UpdatePagesProject(context.Background(), accountId, project.Name, project) | |
return err | |
} | |
func getIPV4() (string, error) { | |
ip := getIp("https://api.ipify.org") | |
if ip == "" { | |
return ip, errors.New("can not fetch ip v4") | |
} | |
log.Println("get ipv4 address from server->", ip) | |
return ip, nil | |
} | |
func getIp(ipUrl string) string { | |
defer func() { | |
err := recover() | |
if err != nil { | |
} | |
}() | |
resp, _ := http.Get(ipUrl) | |
defer resp.Body.Close() | |
data, _ := io.ReadAll(resp.Body) | |
ip := string(data) | |
return ip | |
} | |
func getIpV6() (string, error) { | |
ip := getIp("https://api6.ipify.org") | |
if ip == "" { | |
return ip, errors.New("can not fetch ip v6") | |
} | |
log.Println("get ipv6 address from server->", ip) | |
return ip, nil | |
} | |
func GetGithubLatestRelease(repoName string) (string, error) { | |
type Tag struct { | |
ReleaseTag string `json:"tag_name"` | |
} | |
repo := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repoName) | |
resp, err := http.Get(repo) | |
defer resp.Body.Close() | |
if err != nil { | |
return "", err | |
} | |
all, err := io.ReadAll(resp.Body) | |
if err != nil { | |
return "", err | |
} | |
var t Tag | |
if err := json.Unmarshal(all, &t); err != nil { | |
return "", err | |
} | |
return t.ReleaseTag, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment