Created
November 7, 2014 00:49
-
-
Save mnunberg/8310a00738db208d7c38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
### Repositories: | |
my $REPOS = { | |
RPM => { | |
5 => { | |
'32bit' => "http://packages.couchbase.com/rpm/5.5/i386", | |
'64bit' => "http://packages.couchbase.com/rpm/5.5/x86_64", | |
}, | |
6 => { | |
'32bit' => "http://packages.couchbase.com/rpm/6.2/i686", | |
'64bit' => "http://packages.couchbase.com/rpm/6.2/x86_64", | |
}, | |
7 => { | |
'64bit' => "http://packages.couchbase.com/rpm/7/x86_64" | |
} | |
}, | |
DEB => { | |
'trusty' => 'deb http://packages.couchbase.com/ubuntu trusty trusty/main', | |
'precise' => 'deb http://packages.couchbase.com/ubuntu precise precise/main', | |
'lucid' => 'deb http://packages.couchbase.com/ubuntu lucid lucid/main', | |
'wheezy' => 'deb http://packages.couchbase.com/ubuntu wheezy wheezy/main' | |
} | |
}; | |
# Figure out the kind of operating system we're on | |
sub print_supported { | |
print STDERR "Supported RPM (RedHat Enterprise Linux, CentOS) platforms:\n"; | |
foreach my $vnum ( keys %{$REPOS->{RPM}} ) { | |
foreach my $arch (keys %{$REPOS->{RPM}->{$vnum}}) { | |
print STDERR " EL $vnum.x - $arch\n"; | |
} | |
} | |
print STDERR "\n"; | |
print STDERR "Supported DEB (Debian, Ubuntu) platforms:\n"; | |
foreach my $key (keys %{$REPOS->{DEB}}) { | |
if ($key eq 'trusty') { $key = "Ubuntu 14.04 LTS (Trusty)" } | |
elsif ($key eq 'precise') { $key = "Ubuntu 12.04 LTS (Precise)" } | |
elsif ($key eq 'lucid') { $key = "Ubuntu 10.04 LTS (Lucid)" } | |
elsif ($key eq 'wheezy') { $key = "Debian 7 (Wheezy: Stable)" } | |
print STDERR " $key\n"; | |
} | |
print STDERR "\n"; | |
print STDERR "NOTE: You may also try to manually install packages for other\n"; | |
print STDERR "similar platforms.\n"; | |
} | |
$| = 1; | |
my $REPLY; | |
sub user_terminated { | |
print STDERR "Setup will exit because you have requested termination\n"; | |
exit(1); | |
} | |
if ($< != 0) { | |
print STDERR "I'm detecting you are not the root user. If I continue, I will\n"; | |
print STDERR "not be able to make any changes and setup the repositories.\n"; | |
print STDERR "\n"; | |
print STDERR "Continue? If yes, setup will display the necessary files and modifications\n"; | |
print STDERR "needed, but not actually make them: [Y/n] "; | |
$REPLY = <STDIN>; | |
if ($REPLY =~ /n/) { | |
user_terminated(); | |
} | |
print STDERR "\n"; | |
} | |
print STDERR "Will determine the kind of system you are using\n"; | |
my $ARCH = qx(uname -m); | |
if ($ARCH =~ /64/) { | |
$ARCH = '64bit'; | |
} else { | |
$ARCH = '32bit'; | |
} | |
my $RELTYPE; | |
my $VERSION; | |
sub get_lsb_field { | |
my $name = shift; | |
$name = "--$name"; | |
my $rv = qx(lsb_release $name); | |
return (split(' ', $rv))[1]; | |
} | |
sub prompt_replace_file { | |
my $fname = shift; | |
if (! -e $fname) { | |
return; | |
} | |
print STDERR "The file '$fname' already exists. Its contents are:\n"; | |
open my $fp, "<", $fname; | |
print STDERR "-- BEGIN --\n"; | |
foreach my $line (<$fp>) { | |
print STDERR $line; | |
} | |
print STDERR "-- END --\n"; | |
close($fp); | |
print STDERR "\n"; | |
print STDERR "Should I replace it? [y/N] "; | |
$REPLY = <STDIN>; | |
unless ($REPLY =~ /y/i) { | |
user_terminated(); | |
} | |
} | |
if (-e '/etc/redhat-release') { | |
# RPM Based system | |
$VERSION = get_lsb_field('release'); | |
$VERSION =~ s,\..*,,g; | |
$RELTYPE = "RPM"; | |
} elsif (-e '/etc/debian_version') { | |
# Debian based system | |
$VERSION = get_lsb_field('codename'); | |
$RELTYPE = "DEB"; | |
} else { | |
print_supported(); | |
exit(1); | |
} | |
print STDERR "Architecture: $ARCH. Package Type: $RELTYPE. Version: $VERSION\n"; | |
print STDERR "Is this correct? [Y/n]"; | |
$REPLY = <STDIN>; | |
if ($REPLY =~ /n/) { | |
user_terminated(); | |
} | |
print STDERR "\n"; | |
if ($RELTYPE eq 'DEB') { | |
if (!-d '/etc/apt/sources.list.d') { | |
print STDERR "Creating /etc/apt/sources.list.d directory.. \n"; | |
mkdir('/etc/apt/sources.list.d'); | |
if ($! && $< == 0) { | |
die($!); | |
} | |
} | |
my $repofile = '/etc/apt/sources.list.d/couchbase.list'; | |
prompt_replace_file($repofile); | |
my $line = $REPOS->{DEB}->{$VERSION}; | |
print STDERR "Will insert '$line' into '$repofile'\n"; | |
my $status = open my $fp, ">", $repofile; | |
if ($status) { | |
print $fp $line."\n"; | |
close($fp); | |
} elsif ($< == 0) { | |
die("$repofile: $!"); | |
} | |
# Now, install the GPG key: | |
my $gpgtxt = get_deb_gpg(); | |
print STDERR "\n"; | |
print STDERR "Adding GPG Key. Piping GPG key data to `apt-key add -`\n"; | |
open $fp, "|-", "apt-key", ("add", "-") or die "$!"; | |
print $fp ($gpgtxt); | |
close($fp); | |
if ($? >> 8 != 0 && $< == 0) { | |
die("Couldn't add apt-key ($?)!\n"); | |
} | |
my $cmd; | |
print STDERR "\n"; | |
print STDERR "Running apt-get -qq update..\n"; | |
$cmd = "apt-get -qq update"; | |
if (system($cmd) != 0 && $< == 0) { | |
die("Couldn't run $cmd update"); | |
} | |
$cmd = "apt-get -q -y install "; | |
$cmd .= "libcouchbase2-core libcouchbase2-libevent libcouchbase2-bin libcouchbase-dev"; | |
print STDERR "\n"; | |
print STDERR "Running: $cmd\n"; | |
if (system($cmd) != 0 && $< == 0) { | |
die("Couldn't install!"); | |
} | |
} elsif ($RELTYPE eq 'RPM') { | |
# Figure out the architecture and the repository to search for.. | |
my $repofile = '/etc/yum.repos.d/couchbase.repo'; | |
my $repoline = $REPOS->{RPM}->{$VERSION}->{$ARCH}; | |
if (!$repoline) { | |
print STDERR "Your platform does not seem to be supported!\n"; | |
print_supported(); | |
exit(1); | |
} | |
prompt_replace_file($repofile); | |
my $status = open my $fp, ">", $repofile; | |
if ($status) { | |
print $fp <<"EOF"; | |
[couchbase] | |
enabled = 1 | |
name = Couchbase package repository | |
baseurl = $repoline | |
gpgcheck = 1 | |
gpgkey = http://packages.couchbase.com/rpm/couchbase-rpm.key | |
EOF | |
close($fp); | |
} elsif ($< == 0) { | |
die("$repofile: $!"); | |
} | |
# Now, simply do the yum dance: | |
my $cmd = "yum -y install libcouchbase2-libevent libcouchbase-devel libcouchbase2-bin"; | |
print STDERR "\n"; | |
print STDERR "Running: $cmd\n"; | |
if (system($cmd) != 0 && $< == 0) { | |
die("Couldn't execute Yum!\n"); | |
} | |
} | |
print STDERR "\n"; | |
print STDERR "Will verify your install..\n"; | |
system("cbc version") == 0 or die("Couldn't verify installation!"); | |
sub get_deb_gpg { | |
return <<'EOG'; | |
-----BEGIN PGP PUBLIC KEY BLOCK----- | |
Version: GnuPG v1.4.12 (GNU/Linux) | |
mQGiBFBNzokRBADBz7qXEhIMXl7c0JVfdUcrQnfz5KAKS5+XSt0YSHYpXxbrwuwZ | |
epfJfAgqT3d5/qgHAKrOd0wFLh7h4QqAVA/EqEnBFM8Qsg+ng4CbzS9Z6LnQIbFc | |
1za+Ax2vxmr6NXEX4vaE4T4jCtexW4QlZ4OoSMxDhiH287Q+nHBg7C9xAwCg2OoR | |
Wgq03egTMboMtX1OZXZqvUkD/iBl9kY9QjO3n/4/+wKvYwgYJgWm8AL6oCbXM4ik | |
kNpHIfr46+ijbS8o1vKRjXHlGC/rhYIwqtRpJWvH1CV87ggTIJ+2Gef9WLuQkDUp | |
RN93S/tW+UjHuebdK406NgBB0qE+f07NNdA3a/VgkSe1KL8XG/K3StaEPvGk/wrB | |
RRFdA/wK5Z+tdeJwu5cFPpzqcJXnobtPJ31WgCggxLibmzbDMSPzeq7e0eMj0xlo | |
nnMRTlyKRhzOSy9t29aOJUvrcDhJ0xS6/mu3kPDhw2yK/8q8lfmT4U9XgJiyV47V | |
ambQmI4CfMaAy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kjZIj7av80H/jYckV5QafnlFtNeFxypAfc8+uVG | |
dym9bGLmx6hd2YPXRMZ5mXrNliNqXjCA/pkV8VKvN8sYa4W+x6hXTmLi5/MiTGky | |
Q5t5qtxlRWL2zO/fU4L8QgkACysJbNcfPAJb8zkx4GKUZbmzimpXA29I4BrxE+WI | |
9rgfHddoPXt9x49tl9nloxtszZpm6SDG+gqGi2Da8lcpQx/rHGnfwwfPvVZM1z08 | |
0Kc3wOZd3vSUjt6l0YNAcXwxD8k6q1gSCCEtRXDO/hAsowYJC9lDpkpv4Lh7wq0W | |
waVofgPSQjccQDBYdfyzK4W8NlJXlbFu8qu54qFOPd7jD8aQCozWE722VJ8= | |
=y1S/ | |
-----END PGP PUBLIC KEY BLOCK----- | |
EOG | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment