omnitool-installer

 view release on metacpan or  search on metacpan

lib/omnitool/installer.pm  view on Meta::CPAN

sub create_config_files {
	my $self = shift;

	# the name of the config file template we are going to create
	my ($config_filename) = @_;
	# return if blank or non-existent
	return if !$config_filename || !(-e $self->{options}{othome}.'/distribution/configs/'.$config_filename);

	my ($destination_file, $output, $tt, $key, $new_key);

	# where is it going?
	$destination_file = $self->{options}{othome}.'/configs/'.$config_filename;
	$destination_file =~ s/.tt$//;

	# if it exists, abort
	if (-e 	$destination_file) {
		print "SKIPPED: Can not overwrite config file: $destination_file ; please delete and re-attempt or adjust by hand.\n";
		return;
	}

	# i hate dashes, but felt like they were standard in the args
	foreach $key ('database-server','omnitool-admin','os-username','init-vector','salt-phrase','ot-cookie-domain','ot-primary-hostname') {
		($new_key = $key) =~ s/-/_/g;
		$self->{options}{$new_key} = $self->{options}{$key};
	}

	# prepare template toolkit
	$output = ''; # where the text shall go
	$tt = Template->new({
		ENCODING => 'utf8',
		INCLUDE_PATH => $self->{options}{othome}.'/distribution/configs/',
		OUTPUT => \$output,
	});
	# process the template
	$tt->process($config_filename, $self, $output, {binmode => ':encoding(utf8)'});

	# save the new file under OTHOME/configs
	write_file($destination_file, $output);

	# if it's start_omnitool.bash.tt, make it executable
	if ($config_filename eq 'start_omnitool.bash.tt') {
		chmod 0744, $destination_file;
	}

	# symlink over code map
	symlink $self->{options}{othome}.'/distribution/configs/ot6_modules.yml', $self->{options}{othome}.'/configs/ot6_modules.yml';

	# report success
	print "Created config file $destination_file ; please verify and adjust.\n";
	return;
}

# method to set up the 'omnitool' and 'otstatedata' databases
sub setup_databases {
	my $self = shift;

	my ($dsn, $dbh, $db_name, $sth, $exists, $safe_to_modify);

	# try to make the connection
	$dsn = qq{DBI:mysql:database=information_schema;host=}.$self->{options}{'database-server'}.qq{;port=3306};
	$dbh = DBI->connect($dsn, $self->{options}{'db-username'}, $self->{options}{'db-password'},{ PrintError => 1, RaiseError=>1, mysql_enable_utf8=>8 });
	$dbh->{LongReadLen} = 1000000;

	# check to see if it has our databases
	foreach $db_name ('omnitool','otstatedata','omnitool_applications','omnitool_samples','sample_tools') {
		$sth = $dbh->prepare(qq{select CATALOG_NAME from SCHEMATA where SCHEMA_NAME='$db_name'});
		$sth->execute or print $dbh->errstr."\n";
		($exists) = $sth->fetchrow_array;
		if ($exists) { # if it exists, skip it
			print "SKIPPED: $db_name already exists and was not be overwritten.\n";
		} else { # otherwise, load it in
			$sth = $dbh->prepare(qq{create database $db_name});
			$sth->execute or print $dbh->errstr."\n";
			system('mysql -u'.$self->{options}{'db-username'}.' -p'.$self->{options}{'db-password'}.' -h'.$self->{options}{'database-server'}.' '.$db_name.' < '.$self->{options}{othome}.'/distribution/schema/'.$db_name.'.sql');
			print "Created $db_name from included schema.\n";
			# so we know we can update the databases below with out install tweaks
			$$safe_to_modify{$db_name} = 1;
		}
	}

	# start tweaking the omnitool(_*) DB's

	if ($$safe_to_modify{omnitool}) { # can modify core omnitool admin
		# fix the password for the 'omnitool_admin' user in all three databases
		foreach $db_name ('omnitool','omnitool_applications','omnitool_samples') {
			$sth = $dbh->prepare('update '.$db_name.qq{.omnitool_users set password=sha2(?,224) where username='omnitool_admin'});
			$sth->execute($self->{options}{'admin-ui-password'}) or print $dbh->errstr."\n";
		}

		print "Set Password for 'omnitool_admin' user in the OmniTool Admin Web UI.\n";

		# fix the name and hostname of the Apps for their domain
		$sth = $dbh->prepare(qq{update omnitool.instances set name=?, hostname=? where code='10'});
		$sth->execute('Admin for '.ucfirst($self->{options}{'ot-cookie-domain'}), 'apps-admin.'.$self->{options}{'ot-cookie-domain'}) or print $dbh->errstr."\n";

		# fix the name and hostname of the Sample Apps
		$sth = $dbh->prepare(qq{update omnitool.instances set hostname=? where code='9'});
		$sth->execute('sample-apps-admin.'.$self->{options}{'ot-cookie-domain'}) or print $dbh->errstr."\n";

		print "Updated Name, Hostname for Custom Applications Admin UI.\n";

	} else {

		print "SKIPPED: Could not modify 'omnitool_admin' password; Core 'omnitool' database already exists.\n";

	}

	# Update all database server records
	foreach $db_name ('omnitool','omnitool_applications','omnitool_samples') {
		if ($$safe_to_modify{$db_name}) {
			$sth = $dbh->prepare('update '.$db_name.qq{.database_servers set hostname=?});
			$sth->execute($self->{options}{'database-server'}) or print $dbh->errstr."\n";

			print "Set database server hostname for $db_name.\n";

		} else {
			print "SKIPPED: Could not set database server hostname for $db_name; that database already exists.\n";
		}
	}

}



( run in 0.738 second using v1.01-cache-2.11-cpan-6aa56a78535 )