add cli option to update ip based on the dyndns
This commit is contained in:
parent
a54d0777a9
commit
e9dcb3840f
2 changed files with 51 additions and 8 deletions
|
|
@ -178,7 +178,7 @@ func run() error {
|
||||||
currIP, newIP := publicDNSIps[0], dynamicDNSIps[0]
|
currIP, newIP := publicDNSIps[0], dynamicDNSIps[0]
|
||||||
if currIP.Equal(newIP) {
|
if currIP.Equal(newIP) {
|
||||||
glog.Infof("IPs of dyndns and current ip match, nothing to do: current=%s, dyndns=%s", currIP, 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
|
// setup git repo
|
||||||
gitFs, err := NewGitFS(repository)
|
gitFs, err := NewGitFS(repository)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
@ -13,20 +15,61 @@ const (
|
||||||
publicDomainFlagName = "public-domain"
|
publicDomainFlagName = "public-domain"
|
||||||
updatePublicIPUseCaseFlagName = "kek"
|
updatePublicIPUseCaseFlagName = "kek"
|
||||||
|
|
||||||
tfvarsFileRelative = "/terraform.tfvars"
|
maintfFileRelative = "/main.tf"
|
||||||
)
|
)
|
||||||
|
|
||||||
func updatePublicIP(gitFs *GitFS, currIp, newIp net.IP) error {
|
func updatePublicIP(gitFs *GitFS, currIp, newIp net.IP) error {
|
||||||
println(currIp.String())
|
/* Steps to update:
|
||||||
println(newIp.String())
|
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, "<current_ip>", "<new_ip>")
|
||||||
|
1.2) switch <new_ip> and <currenct_ip>
|
||||||
|
1.3) replace second ip (<new_ip> or after the switch <current_ip>) 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()
|
defer f.Close()
|
||||||
if err != nil {
|
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)
|
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, "<current_ip>", "<new_ip>")
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue