libTaintStates.pm 4.62 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2014-2016 University of Utah and the Flux Group.
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
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

#
# Common Emulab taint handling code.  These functions are meant to
# work with DB object abstractions, such as 'OSinfo' and 'Node'
# objects.  For an object abstraction to be compatible with this
# library, it needs to have a 'taint_states' DB column.  This column
# must then be accessible via a taint_states() method, and must be
# defined as a SQL set with the same set member options as in the
# "nodes" and "os_info" tables.  Finally, the object abstraction must
# have an Update() method, with the same semantics as those found in
# the Node and OSinfo objects.
#

package libTaintStates;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw();

use EmulabConstants;
use English;

# Function prototypes
sub GetTaintStates($);
sub IsTainted($;$);
sub SetTaintStates($@);
sub AddTaint($$);
sub RemoveTaint($;$);

#
# Return the current set of taint states as an array.
#
sub GetTaintStates($)
{
    my ($obj) = @_;

    if (!ref($obj)) {
	warn "First argument is not an object!\n";
	return undef;
    }

    my $taint_states = $obj->taint_states();
    return ()
	if (!defined($taint_states) || $taint_states eq "");

    return split(',', $taint_states);
}

#
# Check to see if the object is tainted, or tainted in a
# particular way.
#
sub IsTainted($;$)
{
    my ($obj, $taint) = @_;

    if (!ref($obj)) {
	warn "First argument is not an object!\n";
	return -1;
    }

87
    my @taint_states = GetTaintStates($obj);
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
    return 0
	if (!@taint_states);

    # Just looking to see if any taint is applied?
    return 1
	if (!defined($taint));

    # Looking for a specific taint.
    return grep {$_ eq $taint} @taint_states;
}

#
# Explicitly set the taint states based on an input array of states.
# Squash any duplicates or empty/undefined entries.
#
sub SetTaintStates($@)
{
    my ($obj, @taint_states) = @_;

    if (!ref($obj)) {
	warn "First argument is not an object!\n";
	return -1;
    }

    my %newtstates = ();
    my %validtstates = map {$_ => 1} TB_TAINTSTATE_ALL();

    foreach my $tstate (@taint_states) {
	next if (!$tstate);
	if (!exists($validtstates{$tstate})) {
	    warn "Invalid taint state: $tstate\n";
	    return -1;
	}
	$newtstates{$tstate} = 1;
    }

    my $upd_str = scalar(keys %newtstates) ? 
	join(',', keys %newtstates) : "NULL";
    return $obj->Update({"taint_states" => $upd_str});
}

#
# Add a taint state to the object.
#
132
sub AddTaintState($$)
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
{
    my ($obj, $taint) = @_;

    if (!ref($obj)) {
	warn "First argument is not an object!\n";
	return -1;
    }

    return -1
	if (!defined($taint));

    if (!grep {$_ eq $taint} TB_TAINTSTATE_ALL()) {
	warn "Invalid taint state: $taint\n";
	return -1;
    }

    return 0
	if (IsTainted($obj, $taint));

    my @taint_states = GetTaintStates($obj);
    push @taint_states, $taint;

    return SetTaintStates($obj, @taint_states);
}

#
# Remove a taint state (or all taint states).
#
161
sub RemoveTaintState($;$)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
{
    my ($obj, $taint) = @_;

    if (!ref($obj)) {
	warn "First argument is not an object!\n";
	return -1;
    }

    my @taint_states = GetTaintStates($obj);
    return 0
	if (!@taint_states);

    my @ntstates = ();
    if (defined($taint)) {
	@ntstates = grep {$_ ne $taint} @taint_states;
    }

    return SetTaintStates($obj, @ntstates);
}

182 183 184 185 186 187
#
# Inherit the taint states from an OS.  Take the union with whatever
# taint states are already set for the node.
#
sub InheritTaintStates($$)
{
188 189
    my ($obj, $osimage) = @_;
    require OSImage;
190

191 192
    if (!ref($osimage)) {
	my $tmp = OSImage->Lookup($osimage);
193
	if (!defined($tmp)) {
194
	    warn "Cannot lookup OSImage for $osimage\n";
195 196
	    return -1;
	}
197
	$osimage = $tmp;
198 199
    }

200
    my @taint_states = GetTaintStates($osimage);
201 202 203 204 205 206 207
    return 0
	if (!@taint_states);
    push @taint_states, GetTaintStates($obj);

    return SetTaintStates($obj, @taint_states);
}

208 209
# Next line required by perl for modules
1;