From e9dcb3840f4496b721e5bd343814ad96d7160d23 Mon Sep 17 00:00:00 2001 From: Viktor Barzin Date: Wed, 10 May 2023 15:42:57 +0000 Subject: [PATCH] add cli option to update ip based on the dyndns --- cli/main.go | 2 +- cli/update_viktorbarzin_me.go | 57 ++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/cli/main.go b/cli/main.go index a17486a4..c8f3224d 100644 --- a/cli/main.go +++ b/cli/main.go @@ -178,7 +178,7 @@ func run() error { 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 + return nil } // setup git repo gitFs, err := NewGitFS(repository) diff --git a/cli/update_viktorbarzin_me.go b/cli/update_viktorbarzin_me.go index 5bf6e390..bd6016f5 100644 --- a/cli/update_viktorbarzin_me.go +++ b/cli/update_viktorbarzin_me.go @@ -1,9 +1,11 @@ package main import ( + "fmt" "io/ioutil" "net" "os" + "strings" "github.com/pkg/errors" ) @@ -13,20 +15,61 @@ const ( publicDomainFlagName = "public-domain" updatePublicIPUseCaseFlagName = "kek" - tfvarsFileRelative = "/terraform.tfvars" + maintfFileRelative = "/main.tf" ) func updatePublicIP(gitFs *GitFS, currIp, newIp net.IP) error { - println(currIp.String()) - println(newIp.String()) + /* Steps to update: + 1. Read main.tf where we update the bind config with the public ip (replace all occurrences of the public ip) + 1.1) read the line where the variable is specified i.e + bind_db_viktorbarzin_me = replace(var.bind_db_viktorbarzin_me, "", "") + 1.2) switch and + 1.3) replace second ip ( or after the switch ) with the new_ip + 2. Update godaddy glue record - f, err := (*gitFs.fs).OpenFile(tfvarsFileRelative, os.O_RDONLY, 0644) + */ + newMainTfContents, err := getNewContent(gitFs, currIp, newIp) + if err != nil { + return errors.Wrapf(err, "failed to get updated main.tf contents") + } + f, err := (*gitFs.fs).OpenFile(maintfFileRelative, os.O_WRONLY|os.O_CREATE, 0644) + if err != nil { + return errors.Wrapf(err, "failed to open file %s for writing", maintfFileRelative) + } + if _, err = f.Write([]byte(newMainTfContents)); err != nil { + return errors.Wrapf(err, "failed to write back new contents to %s:\n %s", maintfFileRelative, newMainTfContents) + } + return nil +} + +// Get updated contents of main.tf +func getNewContent(gitFs *GitFS, currIp, newIp net.IP) (string, error) { + f, err := (*gitFs.fs).OpenFile(maintfFileRelative, os.O_RDONLY, 0644) defer f.Close() if err != nil { - return errors.Wrapf(err, "failed to open tfvars file: %s", tfvarsFileRelative) + return "", errors.Wrapf(err, "failed to open tfvars file: %s", maintfFileRelative) } bytes, err := ioutil.ReadAll(f) - println(string(bytes)) + contents := string(bytes) - return errors.New("test") + newLines := []string{} + for _, line := range strings.Split(contents, "\n") { + lineToAdd := line + // if line is the one that sets un the bind config + if strings.HasPrefix(line, " bind_db_viktorbarzin_me") { + // extract old and new ip + // line example: + // bind_db_viktorbarzin_me = replace(var.bind_db_viktorbarzin_me, "", "") + lineToAdd = strings.Replace(lineToAdd, "\"", "", -1) // remove all quotes + lineToAdd = strings.Replace(lineToAdd, ")", "", -1) // remove the trailing closing bracket + splitByComma := strings.Split(lineToAdd, ",") + if len(splitByComma) != 3 { + return "", fmt.Errorf("invalid line; got: %s", line) + } + newIpStr := strings.ReplaceAll(splitByComma[2], " ", "") + lineToAdd = fmt.Sprintf(" bind_db_viktorbarzin_me = replace(var.bind_db_viktorbarzin_me, \"%s\", \"%s\")", newIpStr, newIp.String()) + } + newLines = append(newLines, lineToAdd) + } + return strings.Join(newLines, "\n"), nil }