You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2017/04/07 03:20:21 UTC

[1/3] incubator-trafficcontrol git commit: tenancy-init script improvements

Repository: incubator-trafficcontrol
Updated Branches:
  refs/heads/master bf2f8a63b -> 7aa3abc66


tenancy-init script improvements


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/55391414
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/55391414
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/55391414

Branch: refs/heads/master
Commit: 55391414163ac0573169fd50dac2df94a5375f7a
Parents: 569d223
Author: nir-sopher <ni...@gmail.com>
Authored: Thu Apr 6 16:37:07 2017 +0300
Committer: Jeremy Mitchell <mi...@gmail.com>
Committed: Thu Apr 6 21:20:15 2017 -0600

----------------------------------------------------------------------
 traffic_ops/app/db/tools/tenant-init | 135 ++++++++++++++++++------------
 1 file changed, 82 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/55391414/traffic_ops/app/db/tools/tenant-init
----------------------------------------------------------------------
diff --git a/traffic_ops/app/db/tools/tenant-init b/traffic_ops/app/db/tools/tenant-init
index eb83772..9a7d6a1 100755
--- a/traffic_ops/app/db/tools/tenant-init
+++ b/traffic_ops/app/db/tools/tenant-init
@@ -33,37 +33,41 @@ my $default_environment = '';
 if ($default_environment eq "") {
 	$default_environment = $ENV{'MOJO_MODE'};
 }
-
-# The tenant user to be created: If not set by command-line, the below variable setting will take effect. 
-my $default_tenant_name = 'root';
 #################################################################################################
 #DONE TEMPLATE TO BE CHANGED BY THE USER
 
 my $usage = "\n"
-	. "Usage:  $PROGRAM_NAME [--env <environment>] --username <username> [--tenant-name <tenant-name>]\t\n\n"
-	. "Example:  $PROGRAM_NAME  --env production --username admin1 --tenant-name root\n\n"
+	. "Usage:  $PROGRAM_NAME (--assign-user | --list-root-tenants) [--env <environment>] [--tenant-name <tenant-name>] username1 [username2 ...]\t\n\n"
+	. "Examples:  $PROGRAM_NAME --assign-user admin1\n"
+	. "           $PROGRAM_NAME --list-root-tenants --env production\n\n"
 	. "Purpose:  This script allows the assignment a user tenant.\n"
 	. "          It is required in order to set the tenancy of the initial admin user of a root tenant\n"
-	. "          (If no such user exists the tenant practically cannot be used).\n\n"
+	. "          (If no such user exists the tenant practically cannot be used).\n"
+	. "          It additionally can provide a list of all root tenants.\n\n"
+	. "Operations:   \n"
+	. "assign-user  - Assign a user to tenant acording to below options.\n"
+	. "list-root-tenants  - list all root tenants.\n\n"
 	. "Options:   \n"
 	. "env  - The environment (development|test|production|integration) to execute the operation on.\n"
 	. "       If not set, '$default_environment' is used (value is 'MOJO_MODE' env var dependent).\n"
-	. "username  - The username of the tenant to which tenancy is set. This user must be already exists.\n"
 	. "tenant-name  - The tenant name of the tenant to assign the user to.\n"
-	. "               If not set, '$default_tenant_name' is used.\n"
-	. "               If the tenant does not exists, a new root tenant is created with the specified name.\n"
+	. "               If not set, use the single root tenant defined.\n"
+	. "               If set, and no tenant exists, create the tenant as a root tenant.\n\n"
+	. "Arguments:   \n"
+	. "List of usernames of users to be assign to the tenant.\n"
 	. "\n\n";
 
 
 #parameters retrival
 my $help = 0;
+my $list_root_tenants = 0;
+my $assign_user = 0;
 my $environment = '';
-my $user_name = '';
 my $tenant_name = '';
 
-GetOptions( "help|?", \$help, "env:s", \$environment, "username:s", \$user_name, "tenant-name:s", \$tenant_name );
+GetOptions( "help|?", \$help, "list-root-tenants", \$list_root_tenants, "assign-user", \$assign_user, "env:s", \$environment, "tenant-name:s", \$tenant_name );
 
-if ($help) {
+if ($help || (!$list_root_tenants && !$assign_user)) {
 	print $usage and exit(0);
 }
 
@@ -77,59 +81,84 @@ if ($environment eq "") {
 }
 $ENV{'MOJO_MODE'} = $environment;
 
-if ($user_name eq "") {
-	print STDERR $usage;
-	print STDERR "No username specified!\n" and exit(1);
-}
-
-# get tenant name
-if ($tenant_name eq "") {
-	$tenant_name =  $default_tenant_name;
-}
-if ($tenant_name eq "") {
-	print STDERR $usage;
-	print STDERR "No tenant-name specified!\n" and exit(1);
-}
-
 #DB connection
 my $dbh    = Schema->database_handle;
 my $schema = Schema->connect_to_database;
 
-
-#validity checks
-my $user_data = $schema->resultset('TmUser')->search({ username => $user_name })->single;
-if (!defined($user_data)) {
-	print STDERR "User '$user_name' does not exists!\n" and exit(1);
+if ($list_root_tenants) {
+	my $tenants = $schema->resultset('Tenant')->search({ parent_id => undef });
+	while ( my $row = $tenants->next ) {
+		my $tenant_id =  $row->id;
+		my $tenant_name =  $row->name;
+		print "ID: $tenant_id NAME: $tenant_name\n";
+	}
+	exit(0);
 }
 
-#actual work
+# assign-user operation
 
-#Create the tenant if needed
-my $tenant = $schema->resultset('Tenant')->search( {name =>  $tenant_name} )->single;
-if (defined($tenant)){
-	print STDERR "Tenant '$tenant_name' already exists. No need to create.\n";
+if (!@ARGV){
+	print STDERR $usage;
+	print STDERR "No username was specified!\n" and exit(1);
+}
+
+my $root_tenants = $schema->resultset('Tenant')->search({ parent_id => undef });
+	
+# get tenant name
+if ($tenant_name eq "") {
+	#find the root tenant	
+	if ($root_tenants->count > 1) {
+		print STDERR "More than 1 root tenants, please specify tenant name\n" and exit(1);
+	}
+	my $tenant = $root_tenants->single;
+	if (!defined($tenant)) {
+		print STDERR "No root tenant, please specify tenant name one for creation\n" and exit(1);
+	}
+	$tenant_name = $tenant->name;
 }
-else{
-	my $tenant_values = {
-			name => $tenant_name,
-			active => 1,
-			parent_id => undef
-			};
+	
+# Create tenant if needed
+my $tenant = $schema->resultset('Tenant')->search( {name =>  $tenant_name} )->single;
+if (!defined($tenant)){
+	if ($root_tenants->count == 0){
+		#no tenants, lest create a tenant as requested
+		my $tenant_values = {
+				name => $tenant_name,
+				active => 1,
+				parent_id => undef
+				};
 		
-	my $insert = $schema->resultset('Tenant')->create($tenant_values)->insert();
-	if (! $insert) {
-		print STDERR "Failed creating tenant '$tenant_name'!\n" and exit;
+		my $insert = $schema->resultset('Tenant')->create($tenant_values)->insert();
+		if (! $insert) {
+			print STDERR "Failed creating tenant '$tenant_name'!\n" and exit(1);
+		}
+		print STDERR "Tenant '$tenant_name' was created.\n";
+		$tenant = $schema->resultset('Tenant')->search( {name =>  $tenant_name} )->single;
+	}
+	else {
+		print STDERR "Tenant '$tenant_name' does not exists.\n" and exit(1);
 	}
-	print STDERR "Tenant '$tenant_name' was created.\n";
-	$tenant = $schema->resultset('Tenant')->search( {name =>  $tenant_name} )->single;
 }
 
+
 #update the user tenancy
-my $user = $schema->resultset('TmUser')->find( { id => $user_data->id } );
-my $rc = $user->update({tenant_id=>$tenant->id});
+my $rc = 0;
+foreach $user_name (@ARGV) {
+	my $user_data = $schema->resultset('TmUser')->search({ username => $user_name })->single;
+	if (!defined($user_data)) {
+		print STDERR "User '$user_name' does not exists!\n";
+		$rc = 1;
+		next;
+	}
+	
+	my $user = $schema->resultset('TmUser')->find( { id => $user_data->id } );
+	my $rc = $user->update({tenant_id=>$tenant->id});
 
-if (! $rc) {
-	print STDERR "Failed setting tenant '$tenant_name' to user '$user_name'!\n" and exit(1);
+	if (! $rc) {
+		print STDERR "Failed setting tenant '$tenant_name' to user '$user_name'!\n";
+		$rc = 1;
+	}
+	print STDERR "User '$user_name' tenancy was set.\n";
 }
-print STDERR "User '$user_name' tenancy was set.\n";
-exit(0);
+
+exit($rc);


[2/3] incubator-trafficcontrol git commit: Org tenancy - initial configuration

Posted by mi...@apache.org.
Org tenancy - initial configuration


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/569d223b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/569d223b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/569d223b

Branch: refs/heads/master
Commit: 569d223b4b7b5407f141c33e6d086dd591a15137
Parents: bf2f8a6
Author: nir-sopher <ni...@gmail.com>
Authored: Wed Apr 5 00:06:17 2017 +0300
Committer: Jeremy Mitchell <mi...@gmail.com>
Committed: Thu Apr 6 21:20:15 2017 -0600

----------------------------------------------------------------------
 traffic_ops/app/db/seeds.sql         |   3 +
 traffic_ops/app/db/tools/tenant-init | 135 ++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/569d223b/traffic_ops/app/db/seeds.sql
----------------------------------------------------------------------
diff --git a/traffic_ops/app/db/seeds.sql b/traffic_ops/app/db/seeds.sql
index bef8211..4989b25 100644
--- a/traffic_ops/app/db/seeds.sql
+++ b/traffic_ops/app/db/seeds.sql
@@ -888,6 +888,9 @@ insert into profile_parameter (profile, parameter) values ( (select id from prof
 -- servers
 update server set https_port = 443 where https_port is null;
 
+-- root tenant
+insert into tenant (name, active, parent_id) values ('root', true, null) ON CONFLICT DO NOTHING;
+
 -- users
 insert into tm_user (username, role,full_name) values ('portal',(select id from role where name='portal'), 'Portal User') ON CONFLICT DO NOTHING;
 insert into tm_user (username, role, full_name, token) values ('extension', 3, 'Extension User, DO NOT DELETE', '91504CE6-8E4A-46B2-9F9F-FE7C15228498') ON CONFLICT DO NOTHING;

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/569d223b/traffic_ops/app/db/tools/tenant-init
----------------------------------------------------------------------
diff --git a/traffic_ops/app/db/tools/tenant-init b/traffic_ops/app/db/tools/tenant-init
new file mode 100755
index 0000000..eb83772
--- /dev/null
+++ b/traffic_ops/app/db/tools/tenant-init
@@ -0,0 +1,135 @@
+#!/usr/bin/perl
+
+package main;
+#
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+use English;
+use Getopt::Long;
+use DBI;
+use Schema;
+
+use YAML;
+use YAML qw(LoadFile);
+use DBIx::Class::Schema::Loader qw/make_schema_at/;
+
+
+#TEMPLATE TO BE CHANGED BY THE USER
+#################################################################################################
+# Environment: If not set by command-line, the below variable setting will take effect. 
+my $default_environment = '';
+if ($default_environment eq "") {
+	$default_environment = $ENV{'MOJO_MODE'};
+}
+
+# The tenant user to be created: If not set by command-line, the below variable setting will take effect. 
+my $default_tenant_name = 'root';
+#################################################################################################
+#DONE TEMPLATE TO BE CHANGED BY THE USER
+
+my $usage = "\n"
+	. "Usage:  $PROGRAM_NAME [--env <environment>] --username <username> [--tenant-name <tenant-name>]\t\n\n"
+	. "Example:  $PROGRAM_NAME  --env production --username admin1 --tenant-name root\n\n"
+	. "Purpose:  This script allows the assignment a user tenant.\n"
+	. "          It is required in order to set the tenancy of the initial admin user of a root tenant\n"
+	. "          (If no such user exists the tenant practically cannot be used).\n\n"
+	. "Options:   \n"
+	. "env  - The environment (development|test|production|integration) to execute the operation on.\n"
+	. "       If not set, '$default_environment' is used (value is 'MOJO_MODE' env var dependent).\n"
+	. "username  - The username of the tenant to which tenancy is set. This user must be already exists.\n"
+	. "tenant-name  - The tenant name of the tenant to assign the user to.\n"
+	. "               If not set, '$default_tenant_name' is used.\n"
+	. "               If the tenant does not exists, a new root tenant is created with the specified name.\n"
+	. "\n\n";
+
+
+#parameters retrival
+my $help = 0;
+my $environment = '';
+my $user_name = '';
+my $tenant_name = '';
+
+GetOptions( "help|?", \$help, "env:s", \$environment, "username:s", \$user_name, "tenant-name:s", \$tenant_name );
+
+if ($help) {
+	print $usage and exit(0);
+}
+
+# get environment name
+if ($environment eq "") {
+	$environment =  $default_environment;
+}
+if ($environment eq "") {
+	print STDERR $usage;
+	print STDERR "No environment specified!\n" and exit(1);
+}
+$ENV{'MOJO_MODE'} = $environment;
+
+if ($user_name eq "") {
+	print STDERR $usage;
+	print STDERR "No username specified!\n" and exit(1);
+}
+
+# get tenant name
+if ($tenant_name eq "") {
+	$tenant_name =  $default_tenant_name;
+}
+if ($tenant_name eq "") {
+	print STDERR $usage;
+	print STDERR "No tenant-name specified!\n" and exit(1);
+}
+
+#DB connection
+my $dbh    = Schema->database_handle;
+my $schema = Schema->connect_to_database;
+
+
+#validity checks
+my $user_data = $schema->resultset('TmUser')->search({ username => $user_name })->single;
+if (!defined($user_data)) {
+	print STDERR "User '$user_name' does not exists!\n" and exit(1);
+}
+
+#actual work
+
+#Create the tenant if needed
+my $tenant = $schema->resultset('Tenant')->search( {name =>  $tenant_name} )->single;
+if (defined($tenant)){
+	print STDERR "Tenant '$tenant_name' already exists. No need to create.\n";
+}
+else{
+	my $tenant_values = {
+			name => $tenant_name,
+			active => 1,
+			parent_id => undef
+			};
+		
+	my $insert = $schema->resultset('Tenant')->create($tenant_values)->insert();
+	if (! $insert) {
+		print STDERR "Failed creating tenant '$tenant_name'!\n" and exit;
+	}
+	print STDERR "Tenant '$tenant_name' was created.\n";
+	$tenant = $schema->resultset('Tenant')->search( {name =>  $tenant_name} )->single;
+}
+
+#update the user tenancy
+my $user = $schema->resultset('TmUser')->find( { id => $user_data->id } );
+my $rc = $user->update({tenant_id=>$tenant->id});
+
+if (! $rc) {
+	print STDERR "Failed setting tenant '$tenant_name' to user '$user_name'!\n" and exit(1);
+}
+print STDERR "User '$user_name' tenancy was set.\n";
+exit(0);


[3/3] incubator-trafficcontrol git commit: This closes #429

Posted by mi...@apache.org.
This closes #429


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/7aa3abc6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/7aa3abc6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/7aa3abc6

Branch: refs/heads/master
Commit: 7aa3abc6619ff201f9bca99ecf37543d81fc7556
Parents: 5539141
Author: Jeremy Mitchell <mi...@gmail.com>
Authored: Thu Apr 6 21:20:35 2017 -0600
Committer: Jeremy Mitchell <mi...@gmail.com>
Committed: Thu Apr 6 21:20:35 2017 -0600

----------------------------------------------------------------------

----------------------------------------------------------------------