summaryrefslogtreecommitdiffstats
path: root/lib/MDV/Draklive/Config.pm
blob: 3216c4d4f1b1c059a50cd251134d3a6259e7326d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package MDV::Draklive::Config;

use MDK::Common;
use Pod::Usage;
use Cwd 'getcwd';

#- we bless Media objects here
use MDV::Draklive::Media;
#- these modules can be used from config files
use MDV::Draklive::Mounts;
use MDV::Draklive::CustomMedia;

our $default_config_root = '/etc/draklive';
our $default_config_path = 'config/live.cfg';
our $default_settings_path = 'config/settings.cfg';

sub read_config {
    my ($live, $config_root, $config_path, $settings_path) = @_;

    if ($config_path && -e getcwd() . '/' . $config_path) {
        $config_root = getcwd();
    }
    print "using $config_root as directory root\n";

    add2hash($live->{settings} ||= {}, { getVarsFromSh($config_root . '/' . $settings_path) }) if $settings_path;
    if ($config_path) {
        #- don't use do(), since it can't see lexicals in the enclosing scope
        my $cfg = eval(cat_($config_root . '/' . $config_path)) or die "unable to load $config_path: $@\n";
        put_in_hash($live, $cfg);
        print "loaded $config_path as config file\n";
    }
    $live->{settings}{config_root} = $config_root;
}

sub check_config {
    my ($live) = @_;
    unless (keys(%$live)) {
        warn "no live definition\n";
        Pod::Usage::pod2usage();
    }
    #- check for minimum requirements
    ref $live->{media} && $live->{media}{storage} or die "no media storage definition\n";
    ref $live->{system} or die "no system definition\n";
}

sub complete_config {
    my ($live) = @_;

    my $default_prefix = {
        build => {
            boot => '/boot',
            files => '/files',
            dist => '/dist',
            initrd => '/initrd',
            loopbacks => '/loopbacks',
            modules => '/modules',
            scripts => '/scripts',
        },
        media => {
            boot => '/boot',
            hidden_boot => '/.boot',
            loopbacks => '/loopbacks',
            hidden_loopbacks => '/.loopbacks',
            mnt => '/media',
        },
        live => {
            mnt => '/live',
        },
    };

    #- set unsupplied config dirs
    add2hash($live->{prefix}{$_} ||= {}, $default_prefix->{$_}) foreach keys %$default_prefix;

    $live->{settings}{builddir} ||= '/var/lib/draklive/build';
    $live->{settings}{chroot} ||= '/var/lib/draklive/chroot';

    $live->{settings}{arch} ||= chomp_(`rpm --eval '%{_target_cpu}'`);
    $live->{media}{title} ||= "live";

    foreach ($live->{media}, if_($live->{replicator}{media}, $live->{replicator}{media}), if_($live->{oem_rescue}{media}, $live->{oem_rescue}{media})) {
        bless $_, 'MDV::Draklive::Media';
        $_->{partitions} ||= [ { mntpoint => '/' } ];
    }

    mkdir_p($live->get_builddir);
    mkdir_p($live->get_system_root);
    $live->{mnt} ||= $live->get_builddir . "/mnt";
}

sub dump_config {
    my ($live) = @_;
    use Data::Dumper;
    print Data::Dumper->Dump([ $live ], [ "live" ]);
}

1;