From 3d81fd12e34a9f24a40636cdbab7a3dca3e72b03 Mon Sep 17 00:00:00 2001
From: paspo <paolo@asperti.com>
Date: Sun, 24 Jul 2016 00:27:49 +0200
Subject: [PATCH] too late for a commit message

---
 doraemon.spec                                 | 89 +++++++++++++++++--
 .../migrate/migrate_from_doraemon_120         | 33 -------
 .../events/actions/doraemon-asyncreload       |  3 -
 .../e-smith/events/actions/doraemon-migrate   | 56 ------------
 .../doraemon-update/services2adjust/doraemon  |  1 -
 .../etc/httpd/doraemon/httpd.conf             |  0
 .../templates2expand/etc/init/doraemon.conf   |  0
 .../templates2expand/etc/sudoers              |  0
 .../migration-import/S99doraemon-migrate      |  1 -
 root/etc/httpd/doraemon/.gitignore            |  0
 10 files changed, 80 insertions(+), 103 deletions(-)
 delete mode 100644 root/etc/e-smith/db/configuration/migrate/migrate_from_doraemon_120
 delete mode 100755 root/etc/e-smith/events/actions/doraemon-asyncreload
 delete mode 100755 root/etc/e-smith/events/actions/doraemon-migrate
 delete mode 120000 root/etc/e-smith/events/doraemon-update/services2adjust/doraemon
 delete mode 100644 root/etc/e-smith/events/doraemon-update/templates2expand/etc/httpd/doraemon/httpd.conf
 delete mode 100644 root/etc/e-smith/events/doraemon-update/templates2expand/etc/init/doraemon.conf
 delete mode 100644 root/etc/e-smith/events/doraemon-update/templates2expand/etc/sudoers
 delete mode 120000 root/etc/e-smith/events/migration-import/S99doraemon-migrate
 delete mode 100644 root/etc/httpd/doraemon/.gitignore

diff --git a/doraemon.spec b/doraemon.spec
index f7b4a52..c030b11 100644
--- a/doraemon.spec
+++ b/doraemon.spec
@@ -28,39 +28,110 @@ rm -f %{name}-%{version}-%{release}-filelist
 mkdir -p %{buildroot}%{_localstatedir}/log/doraemon
 mkdir -p %{buildroot}%{_localstatedir}/cache/doraemon
 
-# Copy the webroot dir
-# mkdir -p %{buildroot}%{_datarootdir}/%{name}
-# cp -av doraemon/* %{buildroot}%{_datadir}/%{name}
+
+%pre
+#if [ "$1" = 1 ]; then
+#  #installation
+#fi
+if [ "$1" = 2 ]; then
+  #upgrade
+  /sbin/stop %{name} >/dev/null 2>&1 || :
+  DATABASE=/var/lib/doraemon/doraemon.db
+  INI=/etc/doraemon.ini
+  if [ -f $INI ]; then
+    DATABASE=$(awk -F "=" '/Database/ {print $2}' $INI)
+    PORT=$(awk -F "=" '/Port/ {print $2}' $INI)
+    BASE=$(awk -F "=" '/Base/ {print $2}' $INI)
+    ROLE=$(awk -F "=" '/Role/ {print $2}' $INI)
+    DIGITS=$(awk -F "=" '/Digits/ {print $2}' $INI)
+    DOMAIN=$(awk -F "=" '/Domain/ {print $2}' $INI)
+    MGMTKEY=$(awk -F "=" '/MgmtKey/ {print $2}' $INI)
+    VAULTPASSFILE=$(awk -F "=" '/VaultPassFile/ {print $2}' $INI)
+    rm $INI
+    /sbin/e-smith/db configuration set %{name} service \
+      status enabled \
+      TCPPort $PORT \
+      access private \
+      DefaultRole $ROLE \
+      DomainFile $DOMAIN \
+      ManagementKeyFile $MGMTKEY \
+      NamingBase $BASE \
+      NamingDigits $DIGITS \
+      VaultPassFile $VAULTPASSFILE
+  else
+    /sbin/e-smith/db configuration set %{name} service \
+      status enabled \
+      TCPPort 3000 \
+      access private \
+      DefaultRole client \
+      DomainFile /etc/domain.yml \
+      ManagementKeyFile /home/amgmt/.ssh/id_rsa.pub \
+      NamingBase lab \
+      NamingDigits 2 \
+      VaultPassFile /home/amgmt/.ansible/vault.txt
+  fi
+  if [ -f $DATABASE ]; then
+    sqlite3 -separator " " $DATABASE \
+      "select * from client" | while read id hostname mac role; do
+      /sbin/e-smith/db hosts set $hostname remote \
+        MacAddress $mac Role $role
+    done
+    rm $DATABASE
+  fi
+fi
 
 %post
 if [ "$1" = 1 ]; then
+  #installation
   /sbin/e-smith/db configuration set %{name} service status enabled TCPPort 3000 access private DefaultRole client DomainFile /etc/domain.yml ManagementKeyFile /home/amgmt/.ssh/id_rsa.pub NamingBase lab NamingDigits 2 VaultPassFile /home/amgmt/.ansible/vault.txt
+  /sbin/e-smith/expand-template /etc/sudoers
+  /sbin/e-smith/expand-template /etc/httpd/doraemon/httpd.conf
+  /sbin/start %{name} >/dev/null 2>&1 || :
+  /sbin/e-smith/signal-event runlevel-adjust
+  /sbin/e-smith/signal-event firewall-adjust
+fi
+if [ "$1" = 2 ]; then
+  #upgrade
+  # TODO: this won't be needed in the next version
+  /sbin/e-smith/expand-template /etc/sudoers
+  /sbin/e-smith/expand-template /etc/httpd/doraemon/httpd.conf
   /sbin/start %{name} >/dev/null 2>&1 || :
   /sbin/e-smith/signal-event runlevel-adjust
   /sbin/e-smith/signal-event firewall-adjust
 fi
 
+
 %preun
+#if [ "$1" = 1 ]; then
+#  #upgrade
+#fi
 if [ "$1" = 0 ]; then
+  #uninstallation
   /sbin/stop %{name} >/dev/null 2>&1 || :
+fi
+
+
+%postun
+#if [ "$1" = 1 ]; then
+#  #upgrade
+#fi
+if [ "$1" = 0 ]; then
+  #uninstallation
   /sbin/e-smith/db configuration delete %{name}
+  /sbin/e-smith/expand-template /etc/sudoers
   /sbin/e-smith/signal-event runlevel-adjust
   /sbin/e-smith/signal-event firewall-adjust
 fi
 
+
 %clean
 rm -rf %{buildroot}
 
 %files -f %{name}-%{version}-%{release}-filelist
 %files
 %defattr(644,root,root,755)
-# %doc README.md LICENSE.txt
-# %attr(755,-,-) %{_initrddir}/%{name}
-# %{_sysconfdir}/logrotate.d/%{name}
-# %dir %{_sysconfdir}/httpd/doraemon
+%doc README.md LICENSE.txt
 %attr(0750,srvmgr,srvmgr) %dir %{_localstatedir}/cache/doraemon
-#  %attr(0644,root,root) %ghost %{_sysconfdir}/init/doraemon.conf
-#  %attr(0644,root,root) %ghost %{_sysconfdir}/httpd/doraemon/httpd.conf
 %attr(0700,root,root) %dir %{_localstatedir}/log/doraemon
 %attr(0644,root,root) %config %ghost %{_localstatedir}/log/doraemon/access_log
 %attr(0644,root,root) %config %ghost %{_localstatedir}/log/doraemon/error_log
diff --git a/root/etc/e-smith/db/configuration/migrate/migrate_from_doraemon_120 b/root/etc/e-smith/db/configuration/migrate/migrate_from_doraemon_120
deleted file mode 100644
index 05df6ff..0000000
--- a/root/etc/e-smith/db/configuration/migrate/migrate_from_doraemon_120
+++ /dev/null
@@ -1,33 +0,0 @@
-{
-   #
-   # feature2956_ns6 -- Base: refactor Remote Access page
-   #
-   # The ValidFrom prop is migrated to AllowAccess
-   #
-
-   # TODO: gestire la migrazione dal doraemon precedente (sqlite + doraemon.ini -> e-smith)
-
-   use esmith::ConfigDB;
-   use esmith::util;
-
-   my $cdb = esmith::ConfigDB->open();
-
-   my $validFrom = $cdb->get_prop_and_delete('httpd-admin', 'ValidFrom') || '';
-
-   # 1) Ignore ValidFrom if AllowHosts is already set
-   if($cdb->get_prop('httpd-admin', 'AllowHosts')) {
-       return "";
-   }
-
-   # 2) Sanitize and import validFrom value if access is private
-   if($cdb->get_prop('httpd-admin', 'access') eq 'private') {
-       my $newValue = join(',', map {
-			       my ($ipaddr, $netmask) = (split(/\//, $_));
-			       $_ eq '0.0.0.0/0.0.0.0' ? () : esmith::util::computeLocalNetworkShortSpec($ipaddr, $netmask); 
-			   } split(/,/, $validFrom));
-       $cdb->set_prop('httpd-admin', 'AllowHosts', $newValue);
-   }
-
-
-   "";
-}
diff --git a/root/etc/e-smith/events/actions/doraemon-asyncreload b/root/etc/e-smith/events/actions/doraemon-asyncreload
deleted file mode 100755
index ae656e5..0000000
--- a/root/etc/e-smith/events/actions/doraemon-asyncreload
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/bash
-
-exec /sbin/start doraemon-reload
diff --git a/root/etc/e-smith/events/actions/doraemon-migrate b/root/etc/e-smith/events/actions/doraemon-migrate
deleted file mode 100755
index f4b9ca2..0000000
--- a/root/etc/e-smith/events/actions/doraemon-migrate
+++ /dev/null
@@ -1,56 +0,0 @@
-#!/usr/bin/perl
-
-#
-# Copyright (C) 2013 Nethesis S.r.l.
-# http://www.nethesis.it - support@nethesis.it
-# 
-# This script is part of NethServer.
-# 
-# NethServer 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 3 of the License,
-# or any later version.
-# 
-# NethServer 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 NethServer.  If not, see <http://www.gnu.org/licenses/>.
-#
-
-# TODO: manage migration
-
-use esmith::DB::db;
-use esmith::event;
-use strict;
-
-my $event = shift;
-my $sourceDir = shift;
-my $esmithDbDir = '/home/e-smith/db';
-my $errors = 0;
-if( ! -d $sourceDir) {
-    die("Could not find dir $sourceDir");
-}
-
-my $srcConfigDb = esmith::DB::db->open_ro(join('', $sourceDir, $esmithDbDir, '/configuration')) || die("Could not open source configuration database in $sourceDir \n");
-
-my $dstConfigDb = esmith::DB::db->open('configuration') || die("Could not open destination account db\n");
-
-
-my $service = 'httpd-admin';
-my $old = $srcConfigDb->get($service);
-my $new = $dstConfigDb->get($service) || $dstConfigDb->new_record($service);
-$new->set_prop('ValidFrom', $old->prop('ValidFrom') || '');
-$old = $srcConfigDb->get('modSSL');
-$new->set_prop('SSLCipherSuite', $old->prop('CipherSuite') || 'ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM');
-
-#
-# Apply configuration
-# 
-if( ! esmith::event::event_signal('nethserver-httpd-admin-update')) {
-    exit(1);
-}
-
-exit 0;
diff --git a/root/etc/e-smith/events/doraemon-update/services2adjust/doraemon b/root/etc/e-smith/events/doraemon-update/services2adjust/doraemon
deleted file mode 120000
index b7dc720..0000000
--- a/root/etc/e-smith/events/doraemon-update/services2adjust/doraemon
+++ /dev/null
@@ -1 +0,0 @@
-restart
\ No newline at end of file
diff --git a/root/etc/e-smith/events/doraemon-update/templates2expand/etc/httpd/doraemon/httpd.conf b/root/etc/e-smith/events/doraemon-update/templates2expand/etc/httpd/doraemon/httpd.conf
deleted file mode 100644
index e69de29..0000000
diff --git a/root/etc/e-smith/events/doraemon-update/templates2expand/etc/init/doraemon.conf b/root/etc/e-smith/events/doraemon-update/templates2expand/etc/init/doraemon.conf
deleted file mode 100644
index e69de29..0000000
diff --git a/root/etc/e-smith/events/doraemon-update/templates2expand/etc/sudoers b/root/etc/e-smith/events/doraemon-update/templates2expand/etc/sudoers
deleted file mode 100644
index e69de29..0000000
diff --git a/root/etc/e-smith/events/migration-import/S99doraemon-migrate b/root/etc/e-smith/events/migration-import/S99doraemon-migrate
deleted file mode 120000
index c6ee670..0000000
--- a/root/etc/e-smith/events/migration-import/S99doraemon-migrate
+++ /dev/null
@@ -1 +0,0 @@
-../actions/doraemon-migrate
\ No newline at end of file
diff --git a/root/etc/httpd/doraemon/.gitignore b/root/etc/httpd/doraemon/.gitignore
deleted file mode 100644
index e69de29..0000000