Prevent Debian package upgrades from prompting.
[sliver-openvswitch.git] / debian / ofp-switch-setup
index 275ddfc..5a999ab 100755 (executable)
@@ -8,9 +8,13 @@ use Digest::SHA1 'sha1_hex';
 use strict;
 use warnings;
 
+# XXX should support configuring SWITCH_NETMASK and SWITCH_GATEWAY
+# when the mode is in-band.
+
 my $debconf_owner = 'openflow-switch';
 
 my $default = '/etc/default/openflow-switch';
+my $template = '/usr/share/openflow/switch/default.template';
 my $etc = '/etc/openflow-switch';
 my $rundir = '/var/run';
 my $privkey_file = "$etc/of0-privkey.pem";
@@ -35,8 +39,9 @@ db_subst('netdevs', 'choices',
          join(', ', map($netdevs{$_}, sort(keys(%netdevs)))));
 db_set('netdevs', join(', ', grep(!/IP/, values(%netdevs))));
 
+my %oldconfig;
 if (-e $default) {
-    my (%config) = load_config($default);
+    %oldconfig = load_config($default);
 
     my (%map) =
       (NETDEVS => sub {
@@ -55,9 +60,11 @@ if (-e $default) {
       );
 
     for my $key (keys(%map)) {
-        local $_ = $config{$key};
+        local $_ = $oldconfig{$key};
         &{$map{$key}}() if defined && !/^\s*$/;
     }
+} elsif (-e $template) {
+    %oldconfig = load_config($template);
 }
 
 my $cacert_preverified = -e $cacert_file;
@@ -122,6 +129,8 @@ my (@states) =
                        $value =~ s/\\([0-7][0-7][0-7])/chr($1)/ge;
                    } else {
                        $value =~ s/^(0x[[:xdigit:]]+)$/hex($1)/e;
+                       $value = '' if $value eq 'empty';
+                       next if $value eq 'null'; # Shouldn't happen.
                    }
                    $options{$name} = $value;
                }
@@ -331,7 +340,7 @@ for (;;) {
     $state += $direction;
 }
 
-my %config;
+my %config = %oldconfig;
 $config{NETDEVS} = join(' ', netdev_names());
 $config{MODE} = db_get('mode');
 if (db_get('mode') eq 'in-band') {
@@ -476,10 +485,25 @@ sub find_netdevs {
 
 sub load_config {
     my ($file) = @_;
-    my ($cmd) = "set -a && . $file && env";
+
+    # Get the list of the variables that the shell sets automatically.
+    my (%auto_vars) = read_vars("set -a && env");
+
+    # Get the variables from $default.
+    my (%config) = read_vars("set -a && . '$default' && env");
+
+    # Subtract.
+    delete @config{keys %auto_vars};
+
+    return %config;
+}
+
+sub read_vars {
+    my ($cmd) = @_;
+    local @ENV;
     if (!open(VARS, '-|', $cmd)) {
         print STDERR "$cmd: failed to execute: $!\n";
-        return;
+        return ();
     }
     my (%config);
     while (<VARS>) {