Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate all Agama json profiles using jsonnet profiles #21108

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions data/yam/agama/auto/lib/base.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
bootloader: {
stopOnBootMenu: true
},
user: {
fullName: 'Bernhard M. Wiedemann',
password: '$6$vYbbuJ9WMriFxGHY$gQ7shLw9ZBsRcPgo6/8KmfDvQ/lCqxW8/WnMoLCoWGdHO6Touush1nhegYfdBbXRpsQuy/FTZZeg7gQL50IbA/',
hashedPassword: true,
userName: 'bernhard'
},
root: {
password: '$6$vYbbuJ9WMriFxGHY$gQ7shLw9ZBsRcPgo6/8KmfDvQ/lCqxW8/WnMoLCoWGdHO6Touush1nhegYfdBbXRpsQuy/FTZZeg7gQL50IbA/',
hashedPassword: true
}
}
10 changes: 10 additions & 0 deletions data/yam/agama/auto/lib/scripts_post.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
enable_root_login: {
name: 'enable root login',
chroot: true,
body: |||
#!/usr/bin/env bash
echo 'PermitRootLogin yes' > /etc/ssh/sshd_config.d/root.conf
|||
}
}
24 changes: 24 additions & 0 deletions data/yam/agama/auto/lib/scripts_pre.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
activate_multipath: {
name: 'activate multipath',
body: |||
#!/bin/bash
if ! systemctl status multpathd ; then
echo 'Activating multipath'
systemctl start multipathd.socket
systemctl start multipathd
fi
|||
},
wipe_filesystem: {
name: 'wipefs',
body: |||
#!/usr/bin/env bash
for i in `lsblk -n -l -o NAME -d -e 7,11,254`
do wipefs -af /dev/$i
sleep 1
sync
done
|||
},
}
45 changes: 45 additions & 0 deletions data/yam/agama/auto/lib/storage.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local get_root_filesystem(filesystem) = {
drives: [
{
partitions: [
{ search: "*", delete: true },
{ generate: 'default' },
{ filesystem: { path: '/', type: filesystem } },
],
},
],
};

{
lvm(encrypted=false): {
drives: [
{
alias: 'pvs-disk',
partitions: [
{ search: "*", delete: true }
]
},
],
volumeGroups: [
{
name: 'system',
physicalVolumes: [
{
[if encrypted == true then 'generate']: {
targetDevices: ['pvs-disk'],
encryption: {
luks2: { password: "nots3cr3t" }
}
},
[if encrypted == false then 'generate']: ['pvs-disk'],
},
],
logicalVolumes: [
{ generate: 'default' },
],
},
]
},
root_filesystem_ext4: get_root_filesystem('ext4'),
root_filesystem_xfs: get_root_filesystem('xfs'),
}
20 changes: 20 additions & 0 deletions data/yam/agama/auto/template.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local base_lib = import 'lib/base.libsonnet';
local scripts_pre_lib = import 'lib/scripts_pre.libsonnet';
local scripts_post_lib = import 'lib/scripts_post.libsonnet';
local storage_lib = import 'lib/storage.libsonnet';

function(bootloader=true, user=true, root=true, storage='', product='', scripts_pre='',
scripts_post='', encrypted=false) {
[if product != '' then 'product']: { id: product },
[if bootloader == true then 'bootloader']: base_lib['bootloader'],
[if user == true then 'user']: base_lib['user'],
[if root == true then 'root']: base_lib['root'],
[if scripts_pre != '' || scripts_post != '' then 'scripts']: {
[if scripts_pre != '' then 'pre']: [ scripts_pre_lib[x] for x in std.split(scripts_pre, ',') ],
[if scripts_post != '' then 'post']: [ scripts_post_lib[x] for x in std.split(scripts_post, ',') ],
},

[if storage == 'lvm' then 'storage']: storage_lib.lvm(encrypted),
[if storage == 'root_filesystem_ext4' then 'storage']: storage_lib['root_filesystem_ext4'],
[if storage == 'root_filesystem_xfs' then 'storage']: storage_lib['root_filesystem_xfs'],
}
47 changes: 47 additions & 0 deletions lib/autoyast.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use File::Copy 'copy';
use File::Find qw(finddepth);
use File::Path 'make_path';
use LWP::Simple 'head';
use Mojo::Util 'trim';
use Socket;

use xml_utils;
Expand All @@ -41,6 +42,7 @@ our @EXPORT = qw(
expand_variables
adjust_user_password
upload_profile
generate_json_profile
inject_registration
init_autoyast_profile
test_ayp_url
Expand Down Expand Up @@ -783,6 +785,51 @@ sub expand_agama_profile {
return $profile_url;
}

=head2 workaround_install_jsonnet

workaround_install_jsonnet();

Workaround Install via zypper golang-github-google-jsonnet package in the worker.
We need this meanwhile the package is built for all other architectures (s390x/aarch64/ppc64le).
After that it should be in the Salt repository.

=cut

sub workaround_install_jsonnet {
if (system("sudo", "zypper", "-n", "repos", "systemsmanagement_Agama_Devel") != 0) {
system("sudo", "zypper", "-n", "addrepo", "-f", "-G",
"https://download.opensuse.org/repositories/systemsmanagement:Agama:Devel/15.6/systemsmanagement:Agama:Devel.repo");
}

if (system("which", "jsonnet") != 0) {
system("sudo", "zypper", "-n", "install", "-f", "golang-github-google-jsonnet");
}
}

=head2 generate_json_profile

generate_json_profile();

Return the URL of generated JSON profile

=cut

sub generate_json_profile {
my $profile_name = "generated_profile.json";
my $casedir = get_required_var('CASEDIR');

workaround_install_jsonnet();
my $profile_options = map { " --tla-" . (/true|false/ ? "code" : "str") . " $_" } split(' ', trim(get_var('AGAMA_PROFILE_OPTIONS')));
my $profile_content = `jsonnet $profile_options $casedir/data/yam/agama/auto/template.jsonnet`;
record_info("Profile", $profile_content);

save_tmp_file($profile_name, $profile_content);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this extra line is not needed, the whole thing could be logic block, kind of blocks (1) preparation (2) execution (3) post-stuff and no need for comment in each block because it is obvious that is the structure.

my $profile_url = autoinst_url . "/files/$profile_name";
upload_profile(path => $profile_name, profile => $profile_content);
Copy link
Contributor

@jknphy jknphy Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case of failure we might not have the uploaded json file, so I think we should also diag for example the generated json file to display it in the screen, otherwise could be hard to dissect.

return $profile_url;
}

=head2 upload_profile

upload_profile(profile => $profile, path => $path)
Expand Down
13 changes: 9 additions & 4 deletions tests/yam/agama/boot_agama.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use strict;
use warnings;

use testapi;
use autoyast qw(expand_agama_profile);
use autoyast qw(expand_agama_profile generate_json_profile);
use Utils::Architectures;
use Utils::Backends;

Expand Down Expand Up @@ -55,9 +55,14 @@ sub run {

# prepare kernel parameters
if (my $agama_auto = get_var('AGAMA_AUTO')) {
my $path = expand_agama_profile($agama_auto);
set_var('AGAMA_AUTO', $path);
set_var('EXTRABOOTPARAMS', get_var('EXTRABOOTPARAMS', '') . " agama.auto=\"$path\"");
if (is_x86_64()) {
my $profile_url = generate_json_profile();
set_var('EXTRABOOTPARAMS', get_var('EXTRABOOTPARAMS', '') . " agama.auto=\"$profile_url\"");
} else {
my $path = expand_agama_profile($agama_auto);
set_var('AGAMA_AUTO', $path);
set_var('EXTRABOOTPARAMS', get_var('EXTRABOOTPARAMS', '') . " agama.auto=\"$path\"");
}
}
my @params = split ' ', trim(get_var('EXTRABOOTPARAMS', ''));

Expand Down