Skip to content

Commit

Permalink
Modifying both hosts files (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldahlke authored Jun 23, 2023
1 parent c21ee9b commit 85b2d68
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 63 deletions.
24 changes: 19 additions & 5 deletions sct/sct.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sct_logic.update import updateKubeconfigs
from sct_common.run_command import run_command, run_command_no_output
from sct_common.sct_tools import *
from sct_data.shellcmd import ShellCmd
from sct_data.systemtools import SCT_OS



Expand Down Expand Up @@ -116,10 +116,24 @@ def parseArguments():
while cThread.isUp():
continue
cThread.join()
sct_sudo, sct_hostctl, sct_windows = getSystemtools()
sct_hostctl.addArg('remove')
sct_hostctl.addArg('sc')
run_command(sct_hostctl.args)
sct_systemtools = getSystemtools()
if sct_systemtools.os == SCT_OS.DEVCONTAINER:
sct_removeargs = list()
sct_addargs = sct_systemtools.getArgs(SCT_OS.WSL2)
sct_removeargs.append(sct_addargs[0])
sct_removeargs.append(sct_addargs[1])
sct_removeargs.append(sct_addargs[2])
sct_removeargs.append(sct_addargs[3])
sct_removeargs.append('remove')
sct_removeargs.append('sc')
run_command(sct_removeargs)
sct_systemtools.addArg('remove', SCT_OS.DEVCONTAINER)
sct_systemtools.addArg('sc', SCT_OS.DEVCONTAINER)
run_command(sct_systemtools.getArgs(SCT_OS.DEVCONTAINER))
else:
sct_systemtools.addArg('remove', sct_systemtools.os)
sct_systemtools.addArg('sc', sct_systemtools.os)
run_command(sct_systemtools.getArgs(sct_systemtools.os))
disableSudochache()
print("Tunneling terminated")
break
Expand Down
77 changes: 42 additions & 35 deletions sct/sct_common/sct_tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os, sys, pathlib
from sct_common.sctexception import SCTException
from sct_data.shellcmd import ShellCmd
from sct_data.systemtools import SystemTools, SCT_OS
import socket

def sizeof_fmt(num, suffix='B'):
Expand Down Expand Up @@ -28,57 +28,64 @@ def get_absolute_path(a_file):
return a_file_abs

def getSystemtools():
sct_sudo = "sudo"
sct_hostctl: ShellCmd = ShellCmd()
sct_windows = False
user_home = ""
sct_systemtools: SystemTools = SystemTools()
ip_addr = "127.0.0.1"
user_home = pathlib.Path.home()
if user_home == None:
sys.exit(1)
if sys.platform.startswith("linux"):
# linux or wsl
user_home = pathlib.Path.home()
if user_home == None:
sys.exit(1)
rc = os.system('uname -a | grep -i microsoft 2>&1 > /dev/null')
if rc == 0: # wsl or dev-container, booth are using MS generated Linux systems
rc = os.system('uname -a | grep -i microsoft 2>&1 > /dev/null')
if not os.environ.get('REMOTE_CONTAINERS'):
sct_sudo = "gsudo.exe"
sct_hostctl.addArg('gsudo.exe')
sct_hostctl.addArg('hostctl.exe')
sct_windows = True
# WSL
sct_systemtools.sudo = "gsudo.exe"
sct_systemtools.addArg('gsudo.exe', SCT_OS.WSL2)
sct_systemtools.addArg('hostctl.exe', SCT_OS.WSL2)
sct_systemtools.os = SCT_OS.WSL2
else:
# Remote container
sct_sudo = "sudo"
sct_hostctl.addArg('sudo')
sct_hostctl.addArg('hostctl')
# This will modify the mounted hosts file in WSL2
sct_systemtools.sudo = "sudo"
sct_systemtools.addArg('sudo', SCT_OS.WSL2)
sct_systemtools.addArg('hostctl', SCT_OS.WSL2)
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
sct_hostctl.addArg('--host-file')
ip_addr = socket.gethostbyname(hostname)
sct_systemtools.addArg('--host-file', SCT_OS.WSL2)
sct_hostfile = os.path.join(pathlib.Path.home(),'.hosts')
sct_hostctl.addArg(sct_hostfile)
sct_hostctl.addArg ('--ip')
sct_hostctl.addArg (ip_addr)
sct_systemtools.addArg(sct_hostfile, SCT_OS.WSL2)
sct_systemtools.addArg ('--ip', SCT_OS.WSL2)
sct_systemtools.addArg (ip_addr, SCT_OS.WSL2)
sct_systemtools.os = SCT_OS.DEVCONTAINER
# Setting the host entry also inside the devcontainer
sct_systemtools.addArg('sudo', SCT_OS.DEVCONTAINER)
sct_systemtools.addArg('hostctl', SCT_OS.DEVCONTAINER)
else:
sct_sudo = "sudo"
sct_hostctl.addArg('sudo')
sct_hostctl.addArg('hostctl')
sct_windows = False
# Plain Linux
sct_systemtools.sudo = "sudo"
sct_systemtools.addArg('sudo', SCT_OS.LINUX)
sct_systemtools.addArg('hostctl', SCT_OS.LINUX)
sct_systemtools.os = SCT_OS.LINUX
elif sys.platform.startswith("win32"):
sct_sudo = "gsudo.exe"
sct_hostctl.addArg('gsudo.exe')
sct_hostctl.addArg('hostctl.exe')
sct_windows = True
return (sct_sudo, sct_hostctl, sct_windows)
# WIndows
sct_systemtools.sudo = "gsudo.exe"
sct_systemtools.addArg('gsudo.exe', SCT_OS.WINDOWS)
sct_systemtools.addArg('hostctl.exe', SCT_OS.WINDOWS)
sct_systemtools.os = SCT_OS.WINDOWS
else:
return None
return sct_systemtools

def enableSudochache ():
sct_sudo, sct_hostctl, sct_windows = getSystemtools()
if sct_windows:
sct_systemtools = getSystemtools()
if sct_systemtools.os == SCT_OS.WINDOWS:
# os.system('{} --loglevel none'.format(sct_sudo))
os.system('{} --loglevel none cache on -d -1'.format(sct_sudo))
os.system('{} --loglevel none cache on -d -1'.format(sct_systemtools.sudo))

def disableSudochache ():
sct_sudo, sct_hostctl, sct_windows = getSystemtools()
if sct_windows:
sct_systemtools = getSystemtools()
if sct_systemtools.os == SCT_OS.WINDOWS:
# os.system('{} config --global --reset'.format(sct_sudo))
os.system('{} --loglevel none cache off'.format(sct_sudo))
os.system('{} --loglevel none cache off'.format(sct_systemtools.sudo))

16 changes: 0 additions & 16 deletions sct/sct_data/shellcmd.py

This file was deleted.

51 changes: 51 additions & 0 deletions sct/sct_data/systemtools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import Dict
from enum import Enum
from sct_common.sctexception import SCTException

class SCT_OS(Enum):
LINUX = 1
WSL2 = 2
DEVCONTAINER = 3
WINDOWS = 4

class SystemTools:
'''
Dataclass that stores System Tools.
'''
def __init__(self):
'''
The clustername like 'sc-staging-legacy', the hostname of the API server and the port to access the k8s API.
'''
self.devcontainer_args = list()
self.wsl2_args = list()
self.linux_args = list()
self.windows_args = list()
self.sudo = "sudo"
self.os = SCT_OS.LINUX


def addArg(self, arg, os):
# Depending on the os we use the correct parameter list
if os == SCT_OS.LINUX:
self.linux_args.append(arg)
elif os == SCT_OS.WSL2:
self.wsl2_args.append(arg)
elif os == SCT_OS.DEVCONTAINER:
self.devcontainer_args.append(arg)
elif os == SCT_OS.WINDOWS:
self.windows_args.append(arg)
else:
return None

def getArgs(self, os):
# Depending on the os we use the correct parameter list
if os == SCT_OS.LINUX:
return self.linux_args
elif os == SCT_OS.WSL2:
return self.wsl2_args
elif os == SCT_OS.DEVCONTAINER:
return self.devcontainer_args
elif os == SCT_OS.WINDOWS:
return self.windows_args
else:
return None
21 changes: 14 additions & 7 deletions sct/sct_logic/tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sshtunnel
from sct_common.run_command import run_command
from sct_common.sct_tools import getSystemtools
from sct_data.shellcmd import ShellCmd
from sct_data.systemtools import SCT_OS

class TunnelThreading(object):
'''
Expand All @@ -29,12 +29,19 @@ def do_something():
sleep(2)
self.tunnel_is_up = True
#
sct_sudo, sct_hostctl, sct_windows = getSystemtools()
sct_hostctl.addArg('add')
sct_hostctl.addArg('domains')
sct_hostctl.addArg('sc')
sct_hostctl.addArg(api_server_host)
run_command(sct_hostctl.args)
sct_systemtools = getSystemtools()
sct_systemtools.addArg('add', sct_systemtools.os)
if sct_systemtools.os == SCT_OS.DEVCONTAINER: sct_systemtools.addArg('add', SCT_OS.WSL2)
sct_systemtools.addArg('domains', sct_systemtools.os)
if sct_systemtools.os == SCT_OS.DEVCONTAINER: sct_systemtools.addArg('domains', SCT_OS.WSL2)
sct_systemtools.addArg('sc', sct_systemtools.os)
if sct_systemtools.os == SCT_OS.DEVCONTAINER: sct_systemtools.addArg('sc', SCT_OS.WSL2)
sct_systemtools.addArg(api_server_host, sct_systemtools.os)
if sct_systemtools.os == SCT_OS.DEVCONTAINER: sct_systemtools.addArg(api_server_host, SCT_OS.WSL2)

run_command(sct_systemtools.getArgs(sct_systemtools.os))
if sct_systemtools.os == SCT_OS.DEVCONTAINER: run_command(sct_systemtools.getArgs(SCT_OS.WSL2))

while True:
if not stopper.is_set():
stopper.wait(2)
Expand Down

0 comments on commit 85b2d68

Please sign in to comment.