Page MenuHomeDevCentral

setup-bugzilla
No OneTemporary

setup-bugzilla

#!/usr/bin/env perl
# -------------------------------------------------------------
# Bugzilla - provide configuration from environment
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Created: 2018-10-05
# License: Trivial work, not eligible to copyright
# Description: To wrap Bugzilla into a 12-factor app or to
# package it as a Docker container, extract the
# configuration from the environment to provide
# to the checksetup.pl script.
# -------------------------------------------------------------
use Cwd 'abs_path';
use feature 'say';
use File::Basename;
use File::Path qw(make_path);
use POSIX qw/strftime/;
use strict;
use warnings;
# -------------------------------------------------------------
# Check environment
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
my @mandatory_variables = ("DB_HOST", "DB_USER", "DB_PASSWORD", "DB_DATABASE", "BUGZILLA_URL");
my $isEnviromentValid = 1;
foreach my $key (@mandatory_variables) {
if (!exists $ENV{$key}) {
say STDERR "Key missing: ", $key;
$isEnviromentValid = 0;
}
}
if ($isEnviromentValid eq 0) {
exit 1;
}
# -------------------------------------------------------------
# Helper methods
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub generate_random_password {
my @alphanumeric = ('a'..'z', 'A'..'Z', 0..9);
return join '', map $alphanumeric[rand @alphanumeric], 0..16;
}
sub run {
my $dir = shift;
my $command = shift;
chdir($dir);
system($command);
return $? >> 8;
}
sub get_value {
my $value = shift;
if ($value =~ /^\d+?$/) {
return $value;
}
return '\'' . $value . '\'';
}
sub create_directory_for_file {
my $filename = shift;
my $dir = dirname($filename);
if (-e $dir) {
if (!-d $dir) {
die "Can't create directory: this is currently a file -- " . $dir;
}
return;
}
say STDERR "Creating directory " . $dir;
make_path($dir);
}
sub save_configuration_hints {
my $filename = shift;
my $answers = shift;
create_directory_for_file($filename);
open(my $fd, '>', $filename) or die "Can't open the configuration hints file " . $filename;
say $fd "# Configurations hints for Bugzilla setup";
say $fd "# Autogenerated by ", abs_path($0);
say $fd "# at ", strftime("%Y-%m-%d %H:%M:%S", localtime);
say $fd "";
foreach my $key (keys %{ $answers }) {
say $fd '$answer{\'', $key, '\'} = ', get_value($answers->{$key}), ';';
}
}
# -------------------------------------------------------------
# Provide admin credentials if needed
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
my $admin_credentials_provided = 1;
my $username = "";
my $password = "";
if (exists $ENV{"ADMIN_EMAIL"} ) {
$username = $ENV{"ADMIN_EMAIL"};
} else {
$username = 'admin@domain.tld';
$admin_credentials_provided = 0;
}
if (exists $ENV{"ADMIN_PASSWORD"} ) {
$password = $ENV{"ADMIN_PASSWORD"};
} else {
$password = generate_random_password();
$admin_credentials_provided = 0;
}
# -------------------------------------------------------------
# Prepare configuration hints
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
my %answer = ();
$answer{'db_driver'} = 'mysql';
$answer{'db_host'} = $ENV{"DB_HOST"};
$answer{'db_port'} = 3306;
$answer{'db_user'} = $ENV{"DB_USER"};
$answer{'db_pass'} = $ENV{"DB_PASSWORD"};
$answer{'db_name'} = $ENV{"DB_DATABASE"};
$answer{'db_sock'} = '';
$answer{'db_check'} = 1;
$answer{'db_mysql_ssl_ca_file'} = '';
$answer{'db_mysql_ssl_ca_path'} = '';
$answer{'db_mysql_ssl_client_cert'} = '';
$answer{'db_mysql_ssl_client_key'} = '';
$answer{'urlbase'} = $ENV{"BUGZILLA_URL"};
$answer{'create_htaccess'} = 1;
$answer{'webservergroup'} = 'bugzilla';
$answer{'use_suexec'} = 0;
$answer{'index_html'} = 0;
$answer{'cvsbin'} = '/usr/bin/cvs';
$answer{'interdiffbin'} = '/usr/bin/interdiff';
$answer{'diffpath'} = '/usr/bin';
$answer{'ADMIN_OK'} = 'Y';
$answer{'ADMIN_EMAIL'} = $username;
$answer{'ADMIN_PASSWORD'} = $password;
$answer{'ADMIN_REALNAME'} = $ENV{"ADMIN_REALNAME"} || 'Admin';
$answer{'NO_PAUSE'} = 1;
save_configuration_hints('/usr/local/lib/bugzilla/provide_configuration.pl', \%answer);
# -------------------------------------------------------------
# Report extra information when needed
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (!$admin_credentials_provided) {
say STDERR "";
say STDERR "\e[31mAdministrator account has not been provided in the configuration.";
say STDERR "If no admin account is already defined in your database, this one will be created:";
say STDERR "";
say STDERR "\tE-mail ..... ", $username;
say STDERR "\tPassword ... ", $password;
say STDERR "\e[0m";
}
# -------------------------------------------------------------
# Run setup
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
my $exit_code = run("/opt/bugzilla", "perl checksetup.pl /usr/local/lib/bugzilla/provide_configuration.pl");
exit $exit_code;

File Metadata

Mime Type
text/x-perl
Expires
Fri, Nov 6, 13:02 (1 w, 6 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4223085
Default Alt Text
setup-bugzilla (5 KB)

Event Timeline