add ability to cli to update public-facing ip based on dyndns [ci skip]
This commit is contained in:
parent
7e643b3bd2
commit
2289552292
2 changed files with 84 additions and 1 deletions
53
cli/main.go
53
cli/main.go
|
|
@ -4,6 +4,7 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
|
|
@ -16,10 +17,12 @@ const (
|
|||
useCaseFlagName = "use-case"
|
||||
repoRootFlagName = "repo-root"
|
||||
printResultOnlyFlagName = "result-only"
|
||||
dynamicDnsDomainDefault = "viktorbarzin.ddns.net"
|
||||
publicDomainDefault = "viktorbarzin.me"
|
||||
)
|
||||
|
||||
var (
|
||||
validUseCases = []string{"setup-vpn", setupOpenWRTDNSFlagName}
|
||||
validUseCases = []string{vpnUseCaseFlagName, setupOpenWRTDNSFlagName, addEmailAliasUseCase}
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -46,6 +49,10 @@ func run() error {
|
|||
emailToForwardTo := flag.String(emailAliasFlagName, "", "Email which is used to forward emails to.")
|
||||
fromDomain := flag.String(fromEmailDomainFlagName, "@viktorbarzin.me", "Domain name which will receive emails. Example @viktorbarzin.me")
|
||||
|
||||
// settings for updating the main domain using the dyndns domain
|
||||
dynDnsDomain := flag.String(dynDnsDomainFlagName, dynamicDnsDomainDefault, "Dynamic DNS domain to check against - used to update the main domain")
|
||||
publicDomain := flag.String(publicDomainFlagName, publicDomainDefault, "Public domain to update")
|
||||
|
||||
// Flag definitions above!
|
||||
flag.Parse()
|
||||
|
||||
|
|
@ -149,6 +156,50 @@ func run() error {
|
|||
return errors.Wrapf(err, "failed to push changes")
|
||||
}
|
||||
glog.Infof("successfully added %s -> %s email aliasing", emailAlias, *emailToForwardTo)
|
||||
case updatePublicIPUseCaseFlagName:
|
||||
// Resolve the dynamic dns record
|
||||
publicDNSIps, err := net.LookupIP(*publicDomain)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to resolve IP addresses")
|
||||
}
|
||||
if len(publicDNSIps) < 1 {
|
||||
return fmt.Errorf("no ips found for %s", *dynDnsDomain)
|
||||
}
|
||||
|
||||
// Resolve the dynamic dns record
|
||||
dynamicDNSIps, err := net.LookupIP(*dynDnsDomain)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to resolve IP addresses")
|
||||
}
|
||||
if len(dynamicDNSIps) < 1 {
|
||||
return fmt.Errorf("no ips found for %s", *dynDnsDomain)
|
||||
}
|
||||
|
||||
currIP, newIP := publicDNSIps[0], dynamicDNSIps[0]
|
||||
if currIP.Equal(newIP) {
|
||||
glog.Infof("IPs of dyndns and current ip match, nothing to do: current=%s, dyndns=%s", currIP, newIP)
|
||||
// return nil // TODO: uncomment
|
||||
}
|
||||
// setup git repo
|
||||
gitFs, err := NewGitFS(repository)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to initialize git fs")
|
||||
}
|
||||
worktree, err := gitFs.repo.Worktree()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to get worktree")
|
||||
}
|
||||
err = updatePublicIP(gitFs, currIP, newIP)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update public ip: %w", err)
|
||||
}
|
||||
// // commit changes
|
||||
if _, err = worktree.Commit("Update public ip and ns records", &git.CommitOptions{All: true, Author: &object.Signature{Name: "Webhook Handler Bot"}}); err != nil {
|
||||
return errors.Wrapf(err, "failed to commit")
|
||||
}
|
||||
if err = gitFs.Push(); err != nil {
|
||||
return errors.Wrapf(err, "failed to push changes")
|
||||
}
|
||||
default:
|
||||
err = errors.New(fmt.Sprintf("unsupported use case: %s", *useCase))
|
||||
}
|
||||
|
|
|
|||
32
cli/update_viktorbarzin_me.go
Normal file
32
cli/update_viktorbarzin_me.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
dynDnsDomainFlagName = "dynamic-domain"
|
||||
publicDomainFlagName = "public-domain"
|
||||
updatePublicIPUseCaseFlagName = "kek"
|
||||
|
||||
tfvarsFileRelative = "/terraform.tfvars"
|
||||
)
|
||||
|
||||
func updatePublicIP(gitFs *GitFS, currIp, newIp net.IP) error {
|
||||
println(currIp.String())
|
||||
println(newIp.String())
|
||||
|
||||
f, err := (*gitFs.fs).OpenFile(tfvarsFileRelative, os.O_RDONLY, 0644)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to open tfvars file: %s", tfvarsFileRelative)
|
||||
}
|
||||
bytes, err := ioutil.ReadAll(f)
|
||||
println(string(bytes))
|
||||
|
||||
return errors.New("test")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue