#!/usr/bin/perl
#
# Copyright (C) 2017 Thorsten Kukuk
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# in Version 2 or later as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see .
#
=head1 NAME
create_autoyast_profile - Create autoyast profile for SUSE CaaSP
=head1 SYNOPSIS
create_autoyast_profile [options]
=head1 DESCRIPTION
Create an autoyast profile for fully automatic installation of
SUSE Container as a Service Platform Cluster Node.
=head1 OPTIONS
-o|--output file Write autoyast profile as 'file' to disk
--salt-master Specify the name of the salt master server
--ntp-server Specify name of ntp server
--smt-url Specify url of SMT server
--regcode Specify registration code for SUSE CaaSP
--reg-email Specify email address for registration
--usage Print usage
-h|-?|--help Help
=cut
use strict;
use warnings;
use locale;
use Pod::Usage;
use Getopt::Long;
use Net::Domain qw(hostname hostfqdn);
use JSON qw(decode_json);
my $outputfile = "-";
my $saltmaster = hostfqdn();
my $ntp_server = "";
my $smturl = "";
my $reg_email = "";
my $regcode = "";
my $help = 0;
my $man = 0;
my $usage = 0;
GetOptions('o|output=s' => \$outputfile,
'salt-master=s' => \$saltmaster,
'smt-url=s' => \$smturl,
'reg-email=s' => \$reg_email,
'regcode=s' => \$regcode,
'ntp-server=s'=>\$ntp_server,
'man' => \$man,
'u|usage' => \$usage,
'help|h|?' => \$help) or pod2usage(2);
pod2usage(0) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
pod2usage(-exitstatus => 0, -verbose => 0) if $usage;
open(OUTPUT,">$outputfile") || die("Can't open output file $outputfile: $!.");
print_header();
print_bootloader();
print_general_section();
print_languages();
setup_networking();
setup_ntp();
print_software();
print_services();
print_scripts();
set_root_password();
setup_registration();
setup_salt_minion();
print_footer();
close(OUTPUT);
#------------------------------------------------------------------------------
sub print_header {
print OUTPUT <<"HeaderText";
HeaderText
}
#------------------------------------------------------------------------------
sub print_footer {
print OUTPUT <<"EOT";
EOT
}
#------------------------------------------------------------------------------
sub print_bootloader {
print OUTPUT <<"EOT";
true
auto
false
false
gfxterm
8
true
EOT
}
#------------------------------------------------------------------------------
sub print_general_section {
print OUTPUT <<"EOT";
false
false
false
align_optimal
false
/boot/efi
200mb
1
vfat
/
30gb
btrfs
/var/lib/docker
max
false
false
EOT
}
#------------------------------------------------------------------------------
sub print_languages {
print OUTPUT <<"EOT";
english-us
en_US
UTC
Etc/GMT
EOT
}
#------------------------------------------------------------------------------
sub set_root_password {
my $password = "!";
my $encrypted = "true";
open(PASSWD, '/etc/passwd');
while () {
chomp;
my($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(/:/);
if ($login eq "root") {
if ($passwd eq "x") {
if (open(SHADOW, '/etc/shadow')) {
while () {
chomp;
my($slogin, $spasswd, $sp_lstchg, $sp_min, $sp_max,
$sp_warn, $sp_inact, $sp_expire, $sp_flag) = split(/:/);
if ($slogin eq "root") {
$password = $spasswd;
$encrypted = "true";
}
}
close(SHADOW);
}
} else {
$password = $passwd;
}
}
}
close(PASSWD);
print OUTPUT <<"EOT";
root
EOT
print OUTPUT " $password\n";
print OUTPUT " $encrypted\n";
print OUTPUT <<"EOT"
EOT
}
#------------------------------------------------------------------------------
sub print_software {
print OUTPUT <<"EOT";
false
SUSE-MicroOS
SUSE-MicroOS-hardware
SUSE-MicroOS-apparmor
SUSE-CaaSP-Stack
EOT
}
#------------------------------------------------------------------------------
sub print_services {
print OUTPUT <<"EOT";
multi-user
purge-kernels
sshd
cloud-init-local
cloud-init
cloud-config
cloud-final
issue-generator
issue-add-ssh-keys
docker
container-feeder
EOT
print OUTPUT " salt-minion\n" if ($saltmaster ne "");
print OUTPUT " systemd-timesyncd\n" if ($ntp_server eq "");
print OUTPUT <<"EOT";
EOT
}
#------------------------------------------------------------------------------
sub print_scripts {
if ($saltmaster ne "" || $ntp_server eq "") {
print OUTPUT <<"EOT";
EOT
if ($saltmaster ne "") {
print OUTPUT <<"EOT";
EOT
}
if ($ntp_server eq "") {
print OUTPUT <<"EOT";
EOT
}
print OUTPUT " \n";
print OUTPUT " \n";
}
}
#------------------------------------------------------------------------------
sub find_smturl {
if (open(INPUTFILE, ") {
chomp;
if ( $_ =~ m/^url:/ ) {
$_ =~ s/url: //;
close (INPUTFILE);
return $_;
}
}
}
close (INPUTFILE);
return "";
}
sub setup_registration {
my $is_active = 0;
if ($smturl ne "" || $regcode ne "") {
$is_active = 1;
} else {
my $connectoutput = `/usr/sbin/SUSEConnect -s 2>/dev/null`;
if ($? == 0) {
my $decoded = decode_json($connectoutput);
foreach my $prod ( @{$decoded} ) {
if ($prod->{"identifier"} eq "CAASP") {
$regcode = $prod->{"regcode"} if ($regcode eq "");
$is_active = 1 if ($prod->{"status"} eq "Registered");
}
}
}
}
print OUTPUT " \n";
if ($is_active) {
$smturl = find_smturl() if ($smturl eq "");
print OUTPUT " true\n";
print OUTPUT " $reg_email\n" unless ($reg_email eq "");
print OUTPUT " $regcode\n" if (defined $regcode && $regcode ne "");
print OUTPUT " true\n";
print OUTPUT " false\n";
print OUTPUT " $smturl\n" if ($smturl ne "");
} else {
print OUTPUT " false\n";
}
print OUTPUT " \n";
}
#------------------------------------------------------------------------------
sub setup_salt_minion {
if ($saltmaster ne "") {
print OUTPUT <<"EOT";
/etc/salt/minion.d/master.conf
root.root
640
EOT
}
}
#------------------------------------------------------------------------------
sub setup_networking {
print OUTPUT <<"EOT";
AUTO
true
auto
false
dhcp
eth0
yes
auto
static
lo
no
127.0.0.1
255.0.0.0
127.0.0.0
8
nfsroot
no
true
true
true
false
false
false
EOT
}
#------------------------------------------------------------------------------
sub setup_ntp {
return if ($ntp_server eq "");
print OUTPUT <<"EOT";
false
EOT
print OUTPUT " $ntp_server\n";
print OUTPUT <<"EOT";
iburst
server
true
false
EOT
}
#------------------------------------------------------------------------------