This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime: set local DNS server as system default resolver
- Loading branch information
1 parent
92dc47b
commit 575264b
Showing
17 changed files
with
273 additions
and
12 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,65 @@ | ||
import re | ||
import shutil | ||
import tempfile | ||
|
||
SYSTEM_RESOLV_CONF = "/etc/resolv.conf" | ||
|
||
|
||
def update_resolv_conf(name_servers, resolv_conf=None): | ||
if not resolv_conf: | ||
resolv_conf = SYSTEM_RESOLV_CONF | ||
|
||
old_name_servers, search, sort_list, options = parse_resolv_conf( | ||
resolv_conf) | ||
|
||
# write to temp file first | ||
resolv_conf_temp = tempfile.mktemp(prefix=f"resolv.conf_") | ||
with open(resolv_conf_temp, "w") as f: | ||
for name_server in name_servers: | ||
f.write(f'nameserver {name_server}\n') | ||
if search: | ||
search_list_string = " ".join(search) | ||
f.write(f'search {search_list_string}\n') | ||
if sort_list: | ||
sort_list_string = " ".join(sort_list) | ||
f.write(f'sortlist {sort_list_string}\n') | ||
if options: | ||
options_string = " ".join(options) | ||
f.write(f'options {options_string}\n') | ||
|
||
# move overwritten | ||
shutil.move(resolv_conf_temp, resolv_conf) | ||
|
||
|
||
def get_resolv_conf_name_servers(resolv_conf): | ||
if not resolv_conf: | ||
resolv_conf = SYSTEM_RESOLV_CONF | ||
|
||
name_servers, _, _, _ = parse_resolv_conf(resolv_conf) | ||
return name_servers | ||
|
||
|
||
def parse_resolv_conf(resolv_conf): | ||
name_servers = [] | ||
search = None | ||
sort_list = None | ||
options = None | ||
with open(resolv_conf, "r") as f: | ||
lines = f.readlines() | ||
for line in lines: | ||
if line.startswith("#"): | ||
continue | ||
tokens = re.split(' |\t', line) | ||
if not tokens: | ||
continue | ||
if tokens[0] == "nameserver": | ||
if len(tokens) != 2: | ||
continue | ||
name_servers.append(tokens[1]) | ||
elif tokens[0] == "search" or tokens[0] == "domain": | ||
search = tokens[1:] | ||
elif tokens[0] == "sortlist": | ||
sort_list = tokens[1:] | ||
elif tokens[0] == "options": | ||
options = tokens[1:] | ||
return name_servers, search, sort_list, options |
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
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,17 @@ | ||
import argparse | ||
|
||
from cloudtik.runtime.bind.utils import configure_upstream | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Configuring runtime.") | ||
parser.add_argument('--head', action='store_true', default=False, | ||
help='Configuring for head node.') | ||
args = parser.parse_args() | ||
|
||
configure_upstream(args.head) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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
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
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,19 @@ | ||
import argparse | ||
|
||
from cloudtik.core._private.util.resolv_conf import update_resolv_conf | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Update the /etc/resolv.conf with a list of name servers.") | ||
parser.add_argument( | ||
"name_servers", | ||
nargs=argparse.REMAINDER, | ||
) | ||
args = parser.parse_args() | ||
if args.name_servers: | ||
update_resolv_conf(name_servers=args.name_servers) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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
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
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
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
Oops, something went wrong.