Add support for distro_family (#213)
Obtained from /etc/os-release: ID_LIKE. Alternate attributes f & distro_family.
This commit is contained in:
parent
32bc9abb0c
commit
5ae553b078
9 changed files with 187 additions and 64 deletions
|
@ -68,6 +68,17 @@ def tst_distro(runner):
|
|||
return distro
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def tst_distro_family(runner):
|
||||
"""Test session's distro_family"""
|
||||
family = ''
|
||||
with contextlib.suppress(Exception):
|
||||
run = runner(command=[
|
||||
'grep', '-oP', r'ID_LIKE=\K.+', '/etc/os-release'], report=False)
|
||||
family = run.out.strip()
|
||||
return family
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def tst_sys():
|
||||
"""Test session's uname value"""
|
||||
|
|
|
@ -85,13 +85,15 @@ def test_relative_link(runner, paths, yadm_alt):
|
|||
'##a.$tst_arch', '##arch.$tst_arch', '##architecture.$tst_arch',
|
||||
'##o.$tst_sys', '##os.$tst_sys',
|
||||
'##d.$tst_distro', '##distro.$tst_distro',
|
||||
'##f.$tst_distro_family', '##distro_family.$tst_distro_family',
|
||||
'##c.$tst_class', '##class.$tst_class',
|
||||
'##h.$tst_host', '##hostname.$tst_host',
|
||||
'##u.$tst_user', '##user.$tst_user',
|
||||
])
|
||||
def test_alt_conditions(
|
||||
runner, paths,
|
||||
tst_arch, tst_sys, tst_distro, tst_host, tst_user, suffix):
|
||||
tst_arch, tst_sys, tst_distro, tst_distro_family, tst_host, tst_user,
|
||||
suffix):
|
||||
"""Test conditions supported by yadm alt"""
|
||||
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
|
||||
|
||||
|
@ -103,6 +105,7 @@ def test_alt_conditions(
|
|||
tst_arch=tst_arch,
|
||||
tst_sys=tst_sys,
|
||||
tst_distro=tst_distro,
|
||||
tst_distro_family=tst_distro_family,
|
||||
tst_class=tst_class,
|
||||
tst_host=tst_host,
|
||||
tst_user=tst_user,
|
||||
|
|
26
test/test_unit_query_distro_family.py
Normal file
26
test/test_unit_query_distro_family.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
"""Unit tests: query_distro_family"""
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'condition', ['os-release', 'os-release-quotes', 'missing'])
|
||||
def test_query_distro_family(runner, yadm, tmp_path, condition):
|
||||
"""Match ID_LIKE when present"""
|
||||
test_family = 'testfamily'
|
||||
os_release = tmp_path.joinpath('os-release')
|
||||
if 'os-release' in condition:
|
||||
quotes = '"' if 'quotes' in condition else ''
|
||||
os_release.write_text(
|
||||
f"testing\nID_LIKE={quotes}{test_family}{quotes}\nfamily")
|
||||
script = f"""
|
||||
YADM_TEST=1 source {yadm}
|
||||
OS_RELEASE="{os_release}"
|
||||
query_distro_family
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
assert run.success
|
||||
assert run.err == ''
|
||||
if 'os-release' in condition:
|
||||
assert run.out.rstrip() == test_family
|
||||
else:
|
||||
assert run.out.rstrip() == ''
|
|
@ -18,17 +18,21 @@ CONDITION = {
|
|||
'labels': ['d', 'distro'],
|
||||
'modifier': 4,
|
||||
},
|
||||
'distro_family': {
|
||||
'labels': ['f', 'distro_family'],
|
||||
'modifier': 8,
|
||||
},
|
||||
'class': {
|
||||
'labels': ['c', 'class'],
|
||||
'modifier': 8,
|
||||
'modifier': 16,
|
||||
},
|
||||
'hostname': {
|
||||
'labels': ['h', 'hostname'],
|
||||
'modifier': 16,
|
||||
'modifier': 32,
|
||||
},
|
||||
'user': {
|
||||
'labels': ['u', 'user'],
|
||||
'modifier': 32,
|
||||
'modifier': 64,
|
||||
},
|
||||
}
|
||||
TEMPLATE_LABELS = ['t', 'template', 'yadm']
|
||||
|
|
|
@ -70,17 +70,20 @@ def test_set_local_alt_values(
|
|||
assert f"user='{tst_user}'" in run.out
|
||||
|
||||
|
||||
def test_distro(runner, yadm):
|
||||
"""Assert that local_distro is set"""
|
||||
def test_distro_and_family(runner, yadm):
|
||||
"""Assert that local_distro/local_distro_family are set"""
|
||||
|
||||
script = f"""
|
||||
YADM_TEST=1 source {yadm}
|
||||
function config() {{ echo "$1"; }}
|
||||
function query_distro() {{ echo "testdistro"; }}
|
||||
function query_distro_family() {{ echo "testfamily"; }}
|
||||
set_local_alt_values
|
||||
echo "distro='$local_distro'"
|
||||
echo "distro_family='$local_distro_family'"
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
assert run.success
|
||||
assert run.err == ''
|
||||
assert run.out.strip() == "distro='testdistro'"
|
||||
assert "distro='testdistro'" in run.out
|
||||
assert "distro_family='testfamily'" in run.out
|
||||
|
|
|
@ -10,6 +10,7 @@ LOCAL_SYSTEM = "default_Test+@-!^System"
|
|||
LOCAL_HOST = "default_Test+@-!^Host"
|
||||
LOCAL_USER = "default_Test+@-!^User"
|
||||
LOCAL_DISTRO = "default_Test+@-!^Distro"
|
||||
LOCAL_DISTRO_FAMILY = "default_Test+@-!^Family"
|
||||
TEMPLATE = f'''
|
||||
start of template
|
||||
default class = >{{{{yadm.class}}}}<
|
||||
|
@ -18,6 +19,7 @@ default os = >{{{{yadm.os}}}}<
|
|||
default host = >{{{{yadm.hostname}}}}<
|
||||
default user = >{{{{yadm.user}}}}<
|
||||
default distro = >{{{{yadm.distro}}}}<
|
||||
default distro_family = >{{{{yadm.distro_family}}}}<
|
||||
{{% if yadm.class == "else1" %}}
|
||||
wrong else 1
|
||||
{{% else %}}
|
||||
|
@ -80,6 +82,16 @@ Included section for distro = {{{{yadm.distro}}}} ({{{{yadm.distro}}}} again)
|
|||
{{% if yadm.distro == "wrongdistro2" %}}
|
||||
wrong distro 2
|
||||
{{% endif %}}
|
||||
{{% if yadm.distro_family == "wrongfamily1" %}}
|
||||
wrong family 1
|
||||
{{% endif %}}
|
||||
{{% if yadm.distro_family == "{LOCAL_DISTRO_FAMILY}" %}}
|
||||
Included section for distro_family = \
|
||||
{{{{yadm.distro_family}}}} ({{{{yadm.distro_family}}}} again)
|
||||
{{% endif %}}
|
||||
{{% if yadm.distro_family == "wrongfamily2" %}}
|
||||
wrong family 2
|
||||
{{% endif %}}
|
||||
end of template
|
||||
'''
|
||||
EXPECTED = f'''
|
||||
|
@ -90,6 +102,7 @@ default os = >{LOCAL_SYSTEM}<
|
|||
default host = >{LOCAL_HOST}<
|
||||
default user = >{LOCAL_USER}<
|
||||
default distro = >{LOCAL_DISTRO}<
|
||||
default distro_family = >{LOCAL_DISTRO_FAMILY}<
|
||||
Included section from else
|
||||
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
|
||||
Multiple lines
|
||||
|
@ -98,6 +111,8 @@ Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
|
|||
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
|
||||
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
|
||||
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
|
||||
Included section for distro_family = \
|
||||
{LOCAL_DISTRO_FAMILY} ({LOCAL_DISTRO_FAMILY} again)
|
||||
end of template
|
||||
'''
|
||||
|
||||
|
@ -155,6 +170,7 @@ def test_template_default(runner, yadm, tmpdir):
|
|||
local_host="{LOCAL_HOST}"
|
||||
local_user="{LOCAL_USER}"
|
||||
local_distro="{LOCAL_DISTRO}"
|
||||
local_distro_family="{LOCAL_DISTRO_FAMILY}"
|
||||
template_default "{input_file}" "{output_file}"
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
|
|
|
@ -9,6 +9,7 @@ LOCAL_SYSTEM = "esh_Test+@-!^System"
|
|||
LOCAL_HOST = "esh_Test+@-!^Host"
|
||||
LOCAL_USER = "esh_Test+@-!^User"
|
||||
LOCAL_DISTRO = "esh_Test+@-!^Distro"
|
||||
LOCAL_DISTRO_FAMILY = "esh_Test+@-!^Family"
|
||||
TEMPLATE = f'''
|
||||
start of template
|
||||
esh class = ><%=$YADM_CLASS%><
|
||||
|
@ -17,6 +18,7 @@ esh os = ><%=$YADM_OS%><
|
|||
esh host = ><%=$YADM_HOSTNAME%><
|
||||
esh user = ><%=$YADM_USER%><
|
||||
esh distro = ><%=$YADM_DISTRO%><
|
||||
esh distro_family = ><%=$YADM_DISTRO_FAMILY%><
|
||||
<% if [ "$YADM_CLASS" = "wrongclass1" ]; then -%>
|
||||
wrong class 1
|
||||
<% fi -%>
|
||||
|
@ -71,6 +73,16 @@ Included section for distro = <%=$YADM_DISTRO%> (<%=$YADM_DISTRO%> again)
|
|||
<% if [ "$YADM_DISTRO" = "wrongdistro2" ]; then -%>
|
||||
wrong distro 2
|
||||
<% fi -%>
|
||||
<% if [ "$YADM_DISTRO_FAMILY" = "wrongfamily1" ]; then -%>
|
||||
wrong family 1
|
||||
<% fi -%>
|
||||
<% if [ "$YADM_DISTRO_FAMILY" = "{LOCAL_DISTRO_FAMILY}" ]; then -%>
|
||||
Included section for distro_family = \
|
||||
<%=$YADM_DISTRO_FAMILY%> (<%=$YADM_DISTRO_FAMILY%> again)
|
||||
<% fi -%>
|
||||
<% if [ "$YADM_DISTRO" = "wrongfamily2" ]; then -%>
|
||||
wrong family 2
|
||||
<% fi -%>
|
||||
end of template
|
||||
'''
|
||||
EXPECTED = f'''
|
||||
|
@ -81,12 +93,15 @@ esh os = >{LOCAL_SYSTEM}<
|
|||
esh host = >{LOCAL_HOST}<
|
||||
esh user = >{LOCAL_USER}<
|
||||
esh distro = >{LOCAL_DISTRO}<
|
||||
esh distro_family = >{LOCAL_DISTRO_FAMILY}<
|
||||
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
|
||||
Included section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
|
||||
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
|
||||
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
|
||||
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
|
||||
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
|
||||
Included section for distro_family = \
|
||||
{LOCAL_DISTRO_FAMILY} ({LOCAL_DISTRO_FAMILY} again)
|
||||
end of template
|
||||
'''
|
||||
|
||||
|
@ -113,6 +128,7 @@ def test_template_esh(runner, yadm, tmpdir):
|
|||
local_host="{LOCAL_HOST}"
|
||||
local_user="{LOCAL_USER}"
|
||||
local_distro="{LOCAL_DISTRO}"
|
||||
local_distro_family="{LOCAL_DISTRO_FAMILY}"
|
||||
template_esh "{input_file}" "{output_file}"
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
|
|
|
@ -10,6 +10,7 @@ LOCAL_SYSTEM = "j2_Test+@-!^System"
|
|||
LOCAL_HOST = "j2_Test+@-!^Host"
|
||||
LOCAL_USER = "j2_Test+@-!^User"
|
||||
LOCAL_DISTRO = "j2_Test+@-!^Distro"
|
||||
LOCAL_DISTRO_FAMILY = "j2_Test+@-!^Family"
|
||||
TEMPLATE = f'''
|
||||
start of template
|
||||
j2 class = >{{{{YADM_CLASS}}}}<
|
||||
|
@ -18,6 +19,7 @@ j2 os = >{{{{YADM_OS}}}}<
|
|||
j2 host = >{{{{YADM_HOSTNAME}}}}<
|
||||
j2 user = >{{{{YADM_USER}}}}<
|
||||
j2 distro = >{{{{YADM_DISTRO}}}}<
|
||||
j2 distro_family = >{{{{YADM_DISTRO_FAMILY}}}}<
|
||||
{{%- if YADM_CLASS == "wrongclass1" %}}
|
||||
wrong class 1
|
||||
{{%- endif %}}
|
||||
|
@ -72,6 +74,16 @@ Included section for distro = {{{{YADM_DISTRO}}}} ({{{{YADM_DISTRO}}}} again)
|
|||
{{%- if YADM_DISTRO == "wrongdistro2" %}}
|
||||
wrong distro 2
|
||||
{{%- endif %}}
|
||||
{{%- if YADM_DISTRO_FAMILY == "wrongfamily1" %}}
|
||||
wrong family 1
|
||||
{{%- endif %}}
|
||||
{{%- if YADM_DISTRO_FAMILY == "{LOCAL_DISTRO_FAMILY}" %}}
|
||||
Included section for distro_family = \
|
||||
{{{{YADM_DISTRO_FAMILY}}}} ({{{{YADM_DISTRO_FAMILY}}}} again)
|
||||
{{%- endif %}}
|
||||
{{%- if YADM_DISTRO_FAMILY == "wrongfamily2" %}}
|
||||
wrong family 2
|
||||
{{%- endif %}}
|
||||
end of template
|
||||
'''
|
||||
EXPECTED = f'''
|
||||
|
@ -82,12 +94,15 @@ j2 os = >{LOCAL_SYSTEM}<
|
|||
j2 host = >{LOCAL_HOST}<
|
||||
j2 user = >{LOCAL_USER}<
|
||||
j2 distro = >{LOCAL_DISTRO}<
|
||||
j2 distro_family = >{LOCAL_DISTRO_FAMILY}<
|
||||
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
|
||||
Included section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
|
||||
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
|
||||
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
|
||||
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
|
||||
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
|
||||
Included section for distro_family = \
|
||||
{LOCAL_DISTRO_FAMILY} ({LOCAL_DISTRO_FAMILY} again)
|
||||
end of template
|
||||
'''
|
||||
|
||||
|
@ -115,6 +130,7 @@ def test_template_j2(runner, yadm, tmpdir, processor):
|
|||
local_host="{LOCAL_HOST}"
|
||||
local_user="{LOCAL_USER}"
|
||||
local_distro="{LOCAL_DISTRO}"
|
||||
local_distro_family="{LOCAL_DISTRO_FAMILY}"
|
||||
template_{processor} "{input_file}" "{output_file}"
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
|
|
34
yadm
34
yadm
|
@ -210,23 +210,30 @@ function score_file() {
|
|||
score=0
|
||||
return
|
||||
fi
|
||||
elif [[ "$label" =~ ^(f|distro_family)$ ]]; then
|
||||
if [ "$value" = "$local_distro_family" ]; then
|
||||
score=$((score + 8))
|
||||
else
|
||||
score=0
|
||||
return
|
||||
fi
|
||||
elif [[ "$label" =~ ^(c|class)$ ]]; then
|
||||
if [ "$value" = "$local_class" ]; then
|
||||
score=$((score + 8))
|
||||
score=$((score + 16))
|
||||
else
|
||||
score=0
|
||||
return
|
||||
fi
|
||||
elif [[ "$label" =~ ^(h|hostname)$ ]]; then
|
||||
if [ "$value" = "$local_host" ]; then
|
||||
score=$((score + 16))
|
||||
score=$((score + 32))
|
||||
else
|
||||
score=0
|
||||
return
|
||||
fi
|
||||
elif [[ "$label" =~ ^(u|user)$ ]]; then
|
||||
if [ "$value" = "$local_user" ]; then
|
||||
score=$((score + 32))
|
||||
score=$((score + 64))
|
||||
else
|
||||
score=0
|
||||
return
|
||||
|
@ -370,6 +377,7 @@ BEGIN {
|
|||
c["hostname"] = host
|
||||
c["user"] = user
|
||||
c["distro"] = distro
|
||||
c["distro_family"] = distro_family
|
||||
c["source"] = source
|
||||
ifs = "^{%" blank "*if"
|
||||
els = "^{%" blank "*else" blank "*%}$"
|
||||
|
@ -437,6 +445,7 @@ EOF
|
|||
-v host="$local_host" \
|
||||
-v user="$local_user" \
|
||||
-v distro="$local_distro" \
|
||||
-v distro_family="$local_distro_family" \
|
||||
-v source="$input" \
|
||||
-v source_dir="$(dirname "$input")" \
|
||||
"$awk_pgm" \
|
||||
|
@ -456,6 +465,7 @@ function template_j2cli() {
|
|||
YADM_HOSTNAME="$local_host" \
|
||||
YADM_USER="$local_user" \
|
||||
YADM_DISTRO="$local_distro" \
|
||||
YADM_DISTRO_FAMILY="$local_distro_family" \
|
||||
YADM_SOURCE="$input" \
|
||||
"$J2CLI_PROGRAM" "$input" -o "$temp_file"
|
||||
|
||||
|
@ -473,6 +483,7 @@ function template_envtpl() {
|
|||
YADM_HOSTNAME="$local_host" \
|
||||
YADM_USER="$local_user" \
|
||||
YADM_DISTRO="$local_distro" \
|
||||
YADM_DISTRO_FAMILY="$local_distro_family" \
|
||||
YADM_SOURCE="$input" \
|
||||
"$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file"
|
||||
|
||||
|
@ -491,6 +502,7 @@ function template_esh() {
|
|||
YADM_HOSTNAME="$local_host" \
|
||||
YADM_USER="$local_user" \
|
||||
YADM_DISTRO="$local_distro" \
|
||||
YADM_DISTRO_FAMILY="$local_distro_family" \
|
||||
YADM_SOURCE="$input"
|
||||
|
||||
move_file "$input" "$output" "$temp_file"
|
||||
|
@ -526,6 +538,7 @@ function alt() {
|
|||
local local_host
|
||||
local local_user
|
||||
local local_distro
|
||||
local local_distro_family
|
||||
set_local_alt_values
|
||||
|
||||
# only be noisy if the "alt" command was run directly
|
||||
|
@ -644,6 +657,7 @@ function set_local_alt_values() {
|
|||
fi
|
||||
|
||||
local_distro="$(query_distro)"
|
||||
local_distro_family="$(query_distro_family)"
|
||||
|
||||
}
|
||||
|
||||
|
@ -1486,6 +1500,20 @@ function query_distro() {
|
|||
echo "$distro"
|
||||
}
|
||||
|
||||
function query_distro_family() {
|
||||
family=""
|
||||
if [ -f "$OS_RELEASE" ]; then
|
||||
while IFS='' read -r line || [ -n "$line" ]; do
|
||||
if [[ "$line" = ID_LIKE=* ]]; then
|
||||
family="${line#ID_LIKE=}"
|
||||
family="${family//\"}"
|
||||
break
|
||||
fi
|
||||
done < "$OS_RELEASE"
|
||||
fi
|
||||
echo "$family"
|
||||
}
|
||||
|
||||
function process_global_args() {
|
||||
|
||||
# global arguments are removed before the main processing is done
|
||||
|
|
Loading…
Reference in a new issue