summaryrefslogtreecommitdiffstats
path: root/bin/sbuild-update
blob: 396b5268a3e752aeae4f045b647f3d6c51cef8e1 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/perl -w
#
# Copyright © 2006-2008 Roger Leigh <rleigh@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# 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
# <http://www.gnu.org/licenses/>.
#
#######################################################################

use strict;
use warnings;

use Sbuild::ChrootRoot;
use Sbuild::Resolver qw(get_resolver);

package Conf;

sub setup {
    my $conf = shift;

    my %update_keys = (
	'COMPAT'				=> {
	    DEFAULT => 1
	},
	'UPDATE'				=> {
	    DEFAULT => 0
	},
	'UPGRADE'				=> {
	    DEFAULT => 0
	},
	'DISTUPGRADE'				=> {
	    DEFAULT => 0
	},
	'CLEAN'				=> {
	    DEFAULT => 0
	},
	'AUTOCLEAN'			=> {
	    DEFAULT => 0
	},
	'AUTOREMOVE'			=> {
	    DEFAULT => 0
	},
	'CHROOT_MODE'				=> {
	    DEFAULT => 'schroot'
	},
    );

    $conf->set_allowed_keys(\%update_keys);
}

package Options;

use Sbuild::OptionsBase;
use Sbuild::Conf qw();

BEGIN {
    use Exporter ();
    our (@ISA, @EXPORT);

    @ISA = qw(Exporter Sbuild::OptionsBase);

    @EXPORT = qw();
}

sub set_options {
    my $self = shift;

    $self->add_options(
	"chroot-mode=s" => sub {
	    $self->set_conf('CHROOT_MODE', $_[1]);
	},
	"arch=s" => sub {
	    $self->set_conf('ARCH', $_[1]);
	    $self->set_conf('HOST_ARCH', $_[1]);
	    $self->set_conf('BUILD_ARCH', $_[1]);
	},
	"update|u" => sub {
	    $self->set_conf('UPDATE', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"upgrade|g" => sub {
	    $self->set_conf('UPGRADE', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"dist-upgrade|d" => sub {
	    $self->set_conf('DISTUPGRADE', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"clean|c" => sub {
	    $self->set_conf('CLEAN', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"autoclean|a" => sub {
	    $self->set_conf('AUTOCLEAN', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"autoremove|r" => sub {
	    $self->set_conf('AUTOREMOVE', 1);
	    $self->set_conf('COMPAT', 0);
	});
}

package main;

use Getopt::Long;
use Sbuild qw(help_text version_text usage_error check_group_membership);
use Sbuild::Utility qw(setup cleanup);

my $conf = Sbuild::Conf::new();
Conf::setup($conf);
exit 1 if !defined($conf);
my $options = Options->new($conf, "sbuild-update", "1");
exit 1 if !defined($options);
check_group_membership() if $conf->get('CHROOT_MODE') eq 'schroot';

if ($conf->get('COMPAT')) {
    my $msg = "$0 will perform apt-get command 'update' now, however this ";
    $msg .= "may change at a later revision.\n";
    print "$msg";
    $conf->set('UPDATE', 1);
}

if (@ARGV < 1) {
    usage_error("sbuild-update", "No chroot was specified");
}

my $status = 0;

my $host = Sbuild::ChrootRoot->new($conf);

foreach (@ARGV) {
    my $distribution = Sbuild::Utility::get_dist($_);

    my $session = setup('source', $distribution, $conf) or die "Chroot setup failed";
    if (!$host->begin_session()) {
	die "Chroot setup failed";
    }
    my $resolver = get_resolver($conf, $session, $host);

    if (!$session->lock_chroot('SBUILD_UPDATE', $$, $conf->get('USERNAME'))) {
	goto cleanup_unlocked;
    }

    $resolver->setup();

    if ($conf->get('UPDATE')) {
	print "$distribution: Performing update.\n";
	$status = $resolver->update($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from update with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('UPGRADE')) {
	print "$distribution: Performing upgrade.\n";
	my $status = $resolver->upgrade($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from upgrade with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('DISTUPGRADE')) {
	print "$distribution: Performing dist-upgrade.\n";
	my $status = $resolver->distupgrade($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from distupgrade with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('CLEAN')) {
	print "$distribution: Performing clean.\n";
	my $status = $resolver->clean($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from update with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('AUTOCLEAN')) {
	print "$distribution: Performing autoclean.\n";
	my $status = $resolver->autoclean($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from autoclean with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('AUTOREMOVE')) {
	print "$distribution: Performing autoremove.\n";
	my $status = $resolver->autoremove($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from autoremove with status $status.\n";
	    goto cleanup;
	}
    }

cleanup:
    $resolver->cleanup();
    # Unlock chroot now it's cleaned up and ready for other users.
    $session->unlock_chroot();

cleanup_unlocked:
    cleanup($conf);

    last if $status;
}

exit($status ? 1 : 0);