-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.pl
executable file
·158 lines (135 loc) · 4.59 KB
/
publish.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/perl -w
#
# Publish www.mythtv.org code to /opt/www
#
# @url $URL$
# @date $Date$
# @version $Revision$
# @author $Author$
# @copyright MythTV
#
# @package mythtv.org
#
# Target directory
my $target = '/opt/www/www.mythtv.org';
# Current db schema version
my $cur_schema = 1;
###############################################################################
# Autoflush buffers
$|++;
# Add a couple of include paths so we can load the various export and gui modules
use English;
use File::Basename;
use File::Path;
use File::Copy;
use Cwd 'abs_path';
use DBI;
# Where are the files located?
my $src = dirname(abs_path($0 or $PROGRAM_NAME));
# Cleanup, and build shell-safe versions
$src =~ s#/*$#/#;
$target =~ s#/*$#/#;
my $safe_src = shell_escape($src);
my $safe_target = shell_escape($target);
# Make sure that we can understand the apache db config parameters
my ($db_file);
open DATA, $src . '/mythtv.conf.apache'
or die "Can't read mythtv.conf.apache: $!\n";
while (<DATA>) {
next unless (/setenv\s+db_file\s+"([^"]+)/);
$db_file = $1;
last;
}
close DATA;
die "Can't find db credentials\n" unless ($db_file);
# Connect to the database
my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file",'','');
die "Couldn't open/create $db_file SQLite database: $DBI::errstr\n" if ($DBI::errstr || !$dbh);
# Get the current db_vers from the database, but only query if we know the
# settings table exists.
my $db_schema = 0;
if ($cur_schema > 1) {
($db_schema) = $dbh->selectrow_array('SELECT data
FROM settings
WHERE value="db_schema"');
$db_schema ||= 0;
}
# Cleanup, since trailing slashes change rsync behavior
$target =~ s/\/+$//s;
# And make sure this one HAS a trailing slash, for rsync
$src =~ s#/*$#/#s;
# Make sure the target path exists
unless (-d $target) {
mkpath($target)
or die "Can't create $target\n";
}
# Take down the site, in case there is an error (in case rsync doesn't copy this
# file over fast enough).
# print "Disabling website for updates\n";
open DATA, ">$target/site_is_disabled"
or die "Can't create $target/htdocs/site_is_disabled: $!\n";
print DATA time();
close DATA;
# Copy
my $cmd = 'rsync -alvHS --exclude=.git --cvs-exclude --delete'
# Files we don't want to copy
.' --exclude publish.pl'
.' --exclude mythtv.conf.apache'
.' --exclude newpost.pl'
# Files we don't want the rsync to blow away
.' --exclude logs'
.' --exclude myth_svndocs'
.' --exclude site_is_disabled'
# src/dest
.' '.$safe_src
.' '.$safe_target;
print "$cmd\n";
system($cmd);
# Make sure the files are all owned properly
system('chown -R apache\:apache '.shell_escape($target));
# Make sure the files have the correct selinux context
system('restorecon -r '.shell_escape($target));
# Does the db schema need to be updated?
if ($db_schema < $cur_schema) {
print "Updating DB Schema\n";
# No version, no database
if ($db_schema == 0) {
$dbh->do('DROP TABLE IF EXISTS settings');
$dbh->do('CREATE TABLE settings (
value TEXT,
data TEXT
)');
$dbh->do('DROP TABLE IF EXISTS downloads');
$dbh->do('CREATE TABLE downloads (
date int,
version text,
module text,
count int
)');
$dbh->do('CREATE INDEX date_idx ON downloads (date, module, version)');
# Update the schema
update_schema(++$db_schema);
}
}
# Re-enable the website
unlink "$target/site_is_disabled"
or die "Can't unlink $target/site_is_disabled: $!\n";
print "Website has been re-enabled.\n";
# Done
exit;
###############################################################################
# Escape a parameter for safe use in a commandline call
sub shell_escape {
$str = shift;
$str =~ s/'/'\\''/sg;
return "'$str'";
}
# Update the db schema version
sub update_schema {
my $vers = shift;
$dbh->do('REPLACE INTO settings
(value, data)
VALUES ("db_schema", ?)',
undef,
$vers);
}