mirror of
https://github.com/Telecominfraproject/OpenNetworkLinux.git
synced 2025-12-25 17:27:01 +00:00
Merge pull request #287 from carlroth/master
Clean up platform detection
This commit is contained in:
@@ -149,6 +149,11 @@ if test "$onie_platform"; then
|
||||
else
|
||||
onie_platform=$(onie-sysinfo -p 2>/dev/null) || :
|
||||
fi
|
||||
if test "$onie_arch"; then
|
||||
:
|
||||
else
|
||||
onie_arch=$(onie-sysinfo -c 2>/dev/null) || :
|
||||
fi
|
||||
if test "$onie_platform"; then
|
||||
:
|
||||
elif test -r /etc/machine.conf; then
|
||||
@@ -479,6 +484,11 @@ mkdir -p "${rootdir}/etc/onl"
|
||||
cp /dev/null "${rootdir}/etc/onl/installer.conf"
|
||||
echo "onl_version=\"$onl_version\"" >> "${rootdir}/etc/onl/installer.conf"
|
||||
|
||||
# pass in the platform identifier, otherwise encoded in
|
||||
# machine-XXX.conf and onie-sysinfo
|
||||
echo "onie_platform=$onie_platform" >> "${rootdir}/etc/onl/installer.conf"
|
||||
echo "onie_arch=$onie_arch" >> "${rootdir}/etc/onl/installer.conf"
|
||||
|
||||
# Generate the MD5 signature for ourselves for future reference.
|
||||
installer_md5=$(md5sum "$0" | awk '{print $1}')
|
||||
echo "installer_md5=\"$installer_md5\"" >> "${rootdir}/etc/onl/installer.conf"
|
||||
|
||||
@@ -123,9 +123,7 @@ installer_mkchroot() {
|
||||
fi
|
||||
|
||||
# export ONIE defines to the installer, if they exist
|
||||
if test -r /etc/machine.conf; then
|
||||
cp /etc/machine.conf "${rootdir}/etc/machine.conf"
|
||||
fi
|
||||
cp /etc/machine*.conf "${rootdir}/etc/."
|
||||
|
||||
# export ONL defines to the installer
|
||||
mkdir -p "${rootdir}/etc/onl"
|
||||
|
||||
@@ -250,21 +250,38 @@ class App(SubprocessMixin, object):
|
||||
def findPlatform(self):
|
||||
|
||||
plat = arch = None
|
||||
if self.machineConf is not None:
|
||||
|
||||
def _p2a(plat):
|
||||
if plat.startswith('x86-64'):
|
||||
return 'x86_64'
|
||||
else:
|
||||
return plat.partition('-')[0]
|
||||
|
||||
# recover platform specifier from installer configuration
|
||||
if plat is None:
|
||||
plat = getattr(self.installerConf, 'onie_platform', None)
|
||||
if plat:
|
||||
self.log.info("ONL installer running chrooted.")
|
||||
plat = plat.replace('_', '-').replace('.', '-')
|
||||
arch = getattr(self.installerConf, 'onie_arch', None)
|
||||
|
||||
# recover platform specifier from legacy ONIE machine.conf
|
||||
if plat is None and self.machineConf is not None:
|
||||
plat = getattr(self.machineConf, 'onie_platform', None)
|
||||
arch = getattr(self.machineConf, 'onie_arch', None)
|
||||
if plat and arch:
|
||||
if plat:
|
||||
self.log.info("ONL installer running under ONIE.")
|
||||
plat = plat.replace('_', '-').replace('.', '-')
|
||||
elif os.path.exists("/etc/onl/platform"):
|
||||
|
||||
# recover platform specifier from ONL runtime
|
||||
if plat is None and os.path.exists("/etc/onl/platform"):
|
||||
with open("/etc/onl/platform") as fd:
|
||||
plat = fd.read().strip()
|
||||
if plat.startswith('x86-64'):
|
||||
arch = 'x86_64'
|
||||
else:
|
||||
arch = plat.partition('-')[0]
|
||||
self.log.info("ONL installer running under ONL or ONL loader.")
|
||||
|
||||
if plat is not None and arch is None:
|
||||
arch = _p2a(plat)
|
||||
|
||||
if plat and arch:
|
||||
self.installerConf.installer_platform = plat
|
||||
self.installerConf.installer_arch = arch
|
||||
|
||||
@@ -63,18 +63,30 @@ class ConfFileBase(ConfBase):
|
||||
PATH = None
|
||||
# Override me
|
||||
|
||||
def __init__(self, path=None):
|
||||
SHELL = False
|
||||
# override me
|
||||
|
||||
def __init__(self, path=None, shell=False):
|
||||
self.__dict__['path'] = path or self.PATH
|
||||
self.__dict__['shell'] = shell or self.SHELL
|
||||
ConfBase.__init__(self)
|
||||
|
||||
def _parse(self):
|
||||
self.__dict__['_data'] = {}
|
||||
with open(self.path) as fd:
|
||||
for line in fd.xreadlines():
|
||||
if self.SHELL:
|
||||
cmd = "IFS=; set -e; . '%s'; set +e; set | egrep ^[a-zA-Z][a-zA-Z0-9_]*=" % self.path
|
||||
buf = subprocess.check_output(cmd, shell=True)
|
||||
for line in buf.splitlines(False):
|
||||
self._feedLine(line)
|
||||
else:
|
||||
with open(self.path) as fd:
|
||||
for line in fd.xreadlines():
|
||||
self._feedLine(line)
|
||||
|
||||
class MachineConf(ConfFileBase):
|
||||
"""XXX roth -- deprecated, machine.conf is executable shell now."""
|
||||
PATH = "/etc/machine.conf"
|
||||
SHELL = True
|
||||
|
||||
class InstallerConf(ConfFileBase):
|
||||
PATH = "/etc/onl/installer.conf"
|
||||
|
||||
@@ -12,6 +12,7 @@ import shutil
|
||||
import argparse
|
||||
import fnmatch
|
||||
import subprocess
|
||||
import glob
|
||||
|
||||
from onl.install.InstallUtils import InitrdContext
|
||||
from onl.install.InstallUtils import ProcMountsParser
|
||||
@@ -58,9 +59,9 @@ class App(SubprocessMixin):
|
||||
self.log.info("onie directory is %s", octx.onieDir)
|
||||
self.log.info("initrd directory is %s", octx.initrdDir)
|
||||
|
||||
src = os.path.join(octx.initrdDir, "etc/machine.conf")
|
||||
dst = os.path.join(ctx.dir, "etc/machine.conf")
|
||||
if os.path.exists(src):
|
||||
srcPat = os.path.join(octx.initrdDir, "etc/machine*.conf")
|
||||
for src in glob.glob(srcPat):
|
||||
dst = os.path.join(ctx.dir, "etc", os.path.split(src)[1])
|
||||
self.log.debug("+ /bin/cp %s %s", src, dst)
|
||||
shutil.copy2(src, dst)
|
||||
|
||||
@@ -99,6 +100,13 @@ class App(SubprocessMixin):
|
||||
pass
|
||||
installerConf.installer_zip = os.path.split(zipPath)[1]
|
||||
|
||||
import onl.platform.current
|
||||
plat = onl.platform.current.OnlPlatformName
|
||||
if plat.startswith('x86-64'):
|
||||
plat = 'x86_64' + plat[6:]
|
||||
installerConf.onie_platform = plat
|
||||
installerConf.onie_arch = plat.partition('-')[0]
|
||||
|
||||
# finalize the local installer.conf
|
||||
dst = os.path.join(ctx.dir, "etc/onl/installer.conf")
|
||||
with open(dst, "w") as fd:
|
||||
|
||||
@@ -17,6 +17,7 @@ import yaml
|
||||
import onl.YamlUtils
|
||||
import subprocess
|
||||
import platform
|
||||
import ast
|
||||
|
||||
class OnlInfoObject(object):
|
||||
DEFAULT_INDENT=" "
|
||||
@@ -256,9 +257,11 @@ class OnlPlatformBase(object):
|
||||
mc = self.basedir_onl("etc/onie/machine.json")
|
||||
if not os.path.exists(mc):
|
||||
data = {}
|
||||
mcconf = subprocess.check_output("""onie-shell -c "cat /etc/machine.conf" """, shell=True)
|
||||
mcconf = subprocess.check_output("""onie-shell -c "IFS=; . /etc/machine.conf; set | egrep ^onie_.*=" """, shell=True)
|
||||
for entry in mcconf.split():
|
||||
(k,e,v) = entry.partition('=')
|
||||
if v and (v.startswith("'") or v.startswith('"')):
|
||||
v = ast.literal_eval(v)
|
||||
if e:
|
||||
data[k] = v
|
||||
|
||||
|
||||
@@ -17,14 +17,27 @@
|
||||
import os, sys
|
||||
import importlib
|
||||
import subprocess
|
||||
import ast
|
||||
|
||||
def platform_name_get():
|
||||
# Determine the current platform name.
|
||||
platform = None
|
||||
if os.path.exists("/etc/onl/platform"):
|
||||
|
||||
# running ONL proper
|
||||
if platform is None and os.path.exists("/etc/onl/platform"):
|
||||
with open("/etc/onl/platform", 'r') as f:
|
||||
platform=f.read().strip()
|
||||
elif os.path.exists("/bin/onie-sysinfo"):
|
||||
|
||||
# in the middle of an ONL install
|
||||
if platform is None and os.path.exists("/etc/onl/installer.conf"):
|
||||
with open("/etc/onl/installer.conf") as f:
|
||||
lines = f.readlines(False)
|
||||
lines = [x for x in lines if x.startswith('onie_platform')]
|
||||
if lines:
|
||||
platform = lines[0].partition('=')[2].strip()
|
||||
|
||||
# running ONIE
|
||||
if platform is None and os.path.exists("/bin/onie-sysinfo"):
|
||||
try:
|
||||
platform = subprocess.check_output(('/bin/onie-sysinfo', '-p',)).strip()
|
||||
except subprocess.CalledProcessError as what:
|
||||
@@ -32,7 +45,9 @@ def platform_name_get():
|
||||
sys.stderr.write(">>> %s\n" % line)
|
||||
sys.stderr.write("onie-sysinfo failed with code %d\n" % what.returncode)
|
||||
platform = None
|
||||
elif os.path.exists("/usr/bin/onie-shell"):
|
||||
|
||||
# running ONL loader, with access to ONIE
|
||||
if platform is None and os.path.exists("/usr/bin/onie-shell"):
|
||||
try:
|
||||
platform = subprocess.check_output(('/usr/bin/onie-shell', '-c', "onie-sysinfo -p",)).strip()
|
||||
except subprocess.CalledProcessError as what:
|
||||
@@ -40,12 +55,16 @@ def platform_name_get():
|
||||
sys.stderr.write(">>> %s\n" % line)
|
||||
sys.stderr.write("onie-sysinfo (onie-shell) failed with code %d\n" % what.returncode)
|
||||
platform = None
|
||||
elif os.path.exists("/etc/machine.conf"):
|
||||
with open("/etc/machine.conf", 'r') as f:
|
||||
lines = f.readlines(False)
|
||||
lines = [x for x in lines if x.startswith('onie_platform=')]
|
||||
if lines:
|
||||
platform = lines[0].partition('=')[2].strip()
|
||||
|
||||
# legacy ONIE environment (including parsable shell in machine.conf)
|
||||
if platform is None and os.path.exists("/etc/machine.conf"):
|
||||
cmd = "IFS=; . /tmp/machine.conf; set | egrep ^onie_platform="
|
||||
buf = subprocess.check_output(cmd)
|
||||
if buf:
|
||||
platform = buf.partition('=')[2].strip()
|
||||
if platform.startswith('"') or platform.startswith("'"):
|
||||
platform = ast.literal_eval(platform)
|
||||
|
||||
if platform is None:
|
||||
raise RuntimeError("cannot find a platform declaration")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user