[X2Go-Commits] [x2goserver] 01/04: Provide --updatedb feature in x2godbadmin if DB backend is PostgreSQL.

git-admin at x2go.org git-admin at x2go.org
Tue Jun 17 20:31:17 CEST 2014


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository x2goserver.

commit 370c2bf6e459d06d170a4becde67831c207c69ea
Author: Mike Gabriel <mike.gabriel at das-netzwerkteam.de>
Date:   Tue Jun 17 20:27:16 2014 +0200

    Provide --updatedb feature in x2godbadmin if DB backend is PostgreSQL.
---
 debian/changelog            |    1 +
 x2goserver/sbin/x2godbadmin |   68 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 18eccbb..2a306f2 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -46,6 +46,7 @@ x2goserver (4.1.0.0-0x2go1.1) UNRELEASED; urgency=low
       where possible, avoid backticks, use more quotes in shell scripts.
     - Add tunneling port allocation for TeleKinesis framework.
     - Only use teki_* fields with PostgreSQL DB backend if TeKi is enabled.
+    - Provide --updatedb feature in x2godbadmin if DB backend is PostgreSQL.
     - Execute post-start.d extension scripts _after_ the session information has
       been submitted to the session DB. This allows execution of x2golistsessions
       and other X2Go commands in post-start.d extension scripts.
diff --git a/x2goserver/sbin/x2godbadmin b/x2goserver/sbin/x2godbadmin
index 50597b6..f0053af 100755
--- a/x2goserver/sbin/x2godbadmin
+++ b/x2goserver/sbin/x2godbadmin
@@ -25,7 +25,7 @@ use Sys::Syslog qw( :standard :macros );
 use File::Path qw( make_path );
 use Getopt::Long;
 use DBI;
-use Try::Tiny;
+use TryCatch;
 
 use X2Go::Config qw( get_sqlconfig );
 use X2Go::Log qw( loglevel );
@@ -262,27 +262,71 @@ my $dbadminpass=<FL>;
 close(FL);
 chomp($dbadminpass);
 
-my $dbh;
+if ($updatedb)
+{
+	# check if the DB already exists, if not, create it...
+	my $dbh;
+	try {
+		$dbh = DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;sslmode=$sslmode", "$dbadmin", "$dbadminpass",{AutoCommit => 1, RaiseError => 1, PrintError => 0});
+	} catch {
+		$createdb = 1;
+	};
+	if (!$createdb) {
+		if ($dbh) {
+			my $sth_tekictrl;
+			my $sth_tekidata;
+			try {
+				$sth_tekictrl = $dbh->prepare("
+				            select tekictrl_port from sessions
+				              ");
+				$sth_tekictrl->execute();
+			} catch {
+				print "ADDING: tekictrl_port column to table sessions\n";
+				$sth_tekictrl = $dbh->prepare("
+				                  alter table sessions
+				                  add column tekictrl_port int
+				                  ");
+				$sth_tekictrl->execute() or die;
+				$sth_tekictrl->finish();
+			}
+			try {
+				$sth_tekidata = $dbh->prepare("
+				              select tekidata_port from sessions
+				              ");
+				$sth_tekidata->execute();
+			} catch {
+				print "ADDING: tekidata_port column to table sessions\n";
+				$sth_tekidata=$dbh->prepare("
+				                  alter table sessions
+				                  add column tekidata_port int
+				                  ");
+				$sth_tekidata->execute() or die;
+				$sth_tekidata->finish();
+			}
+		}
+		if ($dbh) {
+			$dbh->disconnect();
+		}
+		exit(0);
+	} else {
+		print "No session DB found. Use --createdb instead of --updatedb.\n";
+	}
+}
+
 if ($createdb)
 {
-	$dbh=DBI->connect("dbi:Pg:dbname=postgres;host=$host;port=$port;sslmode=$sslmode", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
 	create_database();
-	$dbh->disconnect();
-	$dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;sslmode=$sslmode", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
 	create_tables();
-	$dbh->disconnect();
 	exit(0);
 }
 
 if ($listusers)
 {
-	$dbh=DBI->connect("dbi:Pg:dbname=postgres;host=$host;port=$port;sslmode=$sslmode", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
 	list_users();
-	$dbh->disconnect();
 	exit(0);
 }
 
-$dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;sslmode=$sslmode", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
+my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;sslmode=$sslmode", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
 if ($adduser)
 {
 	add_user($adduser);
@@ -318,6 +362,7 @@ $dbh->disconnect();
 
 sub list_users()
 {
+	my $dbh=DBI->connect("dbi:Pg:dbname=postgres;host=$host;port=$port;sslmode=$sslmode", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
 	my $sth=$dbh->prepare("select rolname from pg_roles where rolname like 'x2gouser_%'");
 	$sth->execute()or die;
 	printf ("%-20s DB user\n","UNIX user");
@@ -329,6 +374,7 @@ sub list_users()
 		printf ("%-20s x2gouser_ at data[0]\n", at data[0]);
 	}
 	$sth->finish();
+	$dbh->disconnect();
 }
 
 sub rm_user()
@@ -411,6 +457,7 @@ sub add_user()
 
 sub create_tables()
 {
+	$dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;sslmode=$sslmode", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
 	my $sth=$dbh->prepare("
 	                      create table sessions(
 	                      session_id text primary key,
@@ -587,10 +634,12 @@ sub create_tables()
 	$sth=$dbh->prepare("GRANT ALL PRIVILEGES ON sessions, messages, user_messages, used_ports, mounts TO $x2goadmin");
 	$sth->execute() or die;
 	$sth->finish();
+	$dbh->disconnect();
 }
 
 sub create_database
 {
+	my $dbh=DBI->connect("dbi:Pg:dbname=postgres;host=$host;port=$port;sslmode=$sslmode", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
 	#drop db if exists
 	my $sth=$dbh->prepare("drop database if exists x2go_sessions");
 	$sth->execute();
@@ -608,4 +657,5 @@ sub create_database
 	print FL $x2goadminpass;
 	close(FL);
 	$sth->finish();
+	$dbh->disconnect();
 }

--
Alioth's /srv/git/_hooks_/post-receive-email on /srv/git/code.x2go.org/x2goserver.git


More information about the x2go-commits mailing list