-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for loading a hosts file
Signed-off-by: Blaine Gardner <[email protected]>
- Loading branch information
Showing
2 changed files
with
85 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const ( | ||
defaultHostsFile = "_node-list" | ||
) | ||
|
||
func getAddrsFromHostsFile(hostGroups []string, hostsFile string) ([]string, error) { | ||
f, err := os.Open(hostsFile) | ||
if err != nil { | ||
return []string{}, fmt.Errorf("could not load hosts file %s: %v", hostsFile, err) | ||
} | ||
|
||
fileGroups, err := getAllGroupsInFile(f) | ||
if err != nil { | ||
return []string{}, fmt.Errorf("error parsing hosts file %s: %v", hostsFile, err) | ||
} | ||
|
||
// Make a '${<group>}' argument for each group | ||
gVars := []string{} | ||
for _, g := range hostGroups { | ||
if _, ok := fileGroups[g]; !ok { | ||
return []string{}, fmt.Errorf("host group %s not found in hosts file %s", g, hostsFile) | ||
} | ||
gVars = append(gVars, fmt.Sprintf("${%s}", g)) | ||
} | ||
|
||
// Source the hosts file, and echo all the groups without newlines | ||
cmd := exec.Command("/bin/bash", "-c", | ||
fmt.Sprintf("source %s ; echo %s", hostsFile, strings.Join(gVars, " "))) | ||
o, err := cmd.CombinedOutput() | ||
// convert to string which has exactly one newline | ||
os := strings.TrimRight(string(o), "\n") | ||
if err != nil { | ||
return []string{}, fmt.Errorf("could not get groups %v from %s: %v\n%s", hostGroups, hostsFile, err, os) | ||
} | ||
|
||
addrs := strings.Split(os, " ") | ||
return addrs, nil | ||
} | ||
|
||
func getAllGroupsInFile(f *os.File) (map[string]bool, error) { | ||
scanner := bufio.NewScanner(f) | ||
fileGroups := map[string]bool{} | ||
// Regex to match Bash variable definition of a host group. Matches: <varname>=" | ||
// <varname> can be any bash variable; the double quote is required | ||
varRegex, _ := regexp.Compile("^([a-zA-Z_][a-zA-Z0-9_]+)=\"") | ||
for scanner.Scan() { | ||
l := strings.TrimLeft(scanner.Text(), " \t") | ||
if m := varRegex.FindStringSubmatch(l); m != nil { | ||
fileGroups[m[1]] = true | ||
} | ||
} | ||
return fileGroups, scanner.Err() | ||
} |
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