mirror of
https://github.com/pragma-/pbot.git
synced 2025-01-10 20:12:35 +01:00
Rename Migration to Updater
This commit is contained in:
parent
340f9d9fd7
commit
f6ac46956f
@ -1,89 +0,0 @@
|
|||||||
# File: Migration.pm
|
|
||||||
# Author: pragma_
|
|
||||||
#
|
|
||||||
# Purpose: Migrates data/configration files to new locations/formats based
|
|
||||||
# on versioning information. Ensures data/configuration files are in the
|
|
||||||
# proper location and using the latest data structure.
|
|
||||||
|
|
||||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
||||||
|
|
||||||
package PBot::Migration;
|
|
||||||
use parent 'PBot::Class';
|
|
||||||
|
|
||||||
use warnings; use strict;
|
|
||||||
use feature 'unicode_strings';
|
|
||||||
|
|
||||||
use File::Basename;
|
|
||||||
|
|
||||||
sub initialize {
|
|
||||||
my ($self, %conf) = @_;
|
|
||||||
$self->{data_dir} = $conf{data_dir};
|
|
||||||
$self->{migration_dir} = $conf{migration_dir};
|
|
||||||
}
|
|
||||||
|
|
||||||
sub migrate {
|
|
||||||
my ($self) = @_;
|
|
||||||
|
|
||||||
$self->{pbot}->{logger}->log("Checking if migration needed...\n");
|
|
||||||
|
|
||||||
my $current_version = $self->get_current_version;
|
|
||||||
my $last_migration_version = $self->get_last_migration_version;
|
|
||||||
|
|
||||||
$self->{pbot}->{logger}->log("Current version: $current_version; last migration version: $last_migration_version\n");
|
|
||||||
|
|
||||||
if ($last_migration_version >= $current_version) {
|
|
||||||
$self->{pbot}->{logger}->log("No migration necessary.\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
my @migrations = $self->get_available_migrations($last_migration_version);
|
|
||||||
|
|
||||||
if (not @migrations ) {
|
|
||||||
$self->{pbot}->{logger}->log("No migrations available.\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach my $migration (@migrations) {
|
|
||||||
$self->{pbot}->{logger}->log("Executing migration script: $migration\n");
|
|
||||||
my $output = `$migration $self->{data_dir}`;
|
|
||||||
my $exit = $? >> 8;
|
|
||||||
$self->{pbot}->{logger}->log("Script completed. Exit $exit. Output: $output");
|
|
||||||
return $exit if $exit != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $self->put_last_migration_version($current_version);
|
|
||||||
}
|
|
||||||
|
|
||||||
sub get_available_migrations {
|
|
||||||
my ($self, $last_migration_version) = @_;
|
|
||||||
my @migrations = sort glob "$self->{migration_dir}/*";
|
|
||||||
return grep { my ($version) = split /_/, basename $_; $version > $last_migration_version ? 1 : 0 } @migrations;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub get_current_version {
|
|
||||||
return PBot::VERSION::BUILD_REVISION;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub get_last_migration_version {
|
|
||||||
my ($self) = @_;
|
|
||||||
open(my $fh, '<', "$self->{data_dir}/last_migration") or return 0;
|
|
||||||
chomp(my $last_migration = <$fh>);
|
|
||||||
close $fh;
|
|
||||||
return $last_migration;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub put_last_migration_version {
|
|
||||||
my ($self, $version) = @_;
|
|
||||||
if (open(my $fh, '>', "$self->{data_dir}/last_migration")) {
|
|
||||||
print $fh "$version\n";
|
|
||||||
close $fh;
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
$self->{pbot}->{logger}->log("Could not save last migration to $self->{data_dir}/last_migration: $!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
1;
|
|
56
PBot/PBot.pm
56
PBot/PBot.pm
@ -50,7 +50,7 @@ use PBot::Plugins;
|
|||||||
use PBot::Functions;
|
use PBot::Functions;
|
||||||
use PBot::Modules;
|
use PBot::Modules;
|
||||||
use PBot::ProcessManager;
|
use PBot::ProcessManager;
|
||||||
use PBot::Migration;
|
use PBot::Updater;
|
||||||
|
|
||||||
sub new {
|
sub new {
|
||||||
my ($proto, %conf) = @_;
|
my ($proto, %conf) = @_;
|
||||||
@ -64,26 +64,26 @@ sub initialize {
|
|||||||
my ($self, %conf) = @_;
|
my ($self, %conf) = @_;
|
||||||
$self->{startup_timestamp} = time;
|
$self->{startup_timestamp} = time;
|
||||||
|
|
||||||
my $data_dir = $conf{data_dir};
|
my $data_dir = $conf{data_dir};
|
||||||
my $module_dir = $conf{module_dir};
|
my $module_dir = $conf{module_dir};
|
||||||
my $plugin_dir = $conf{plugin_dir};
|
my $plugin_dir = $conf{plugin_dir};
|
||||||
my $migration_dir = $conf{migration_dir};
|
my $update_dir = $conf{update_dir};
|
||||||
|
|
||||||
# check command-line arguments for directory overrides
|
# check command-line arguments for directory overrides
|
||||||
foreach my $arg (@ARGV) {
|
foreach my $arg (@ARGV) {
|
||||||
if ($arg =~ m/^-?(?:general\.)?((?:data|module|plugin|migration)_dir)=(.*)$/) {
|
if ($arg =~ m/^-?(?:general\.)?((?:data|module|plugin|update)_dir)=(.*)$/) {
|
||||||
my $override = $1;
|
my $override = $1;
|
||||||
my $value = $2;
|
my $value = $2;
|
||||||
$data_dir = $value if $override eq 'data_dir';
|
$data_dir = $value if $override eq 'data_dir';
|
||||||
$module_dir = $value if $override eq 'module_dir';
|
$module_dir = $value if $override eq 'module_dir';
|
||||||
$plugin_dir = $value if $override eq 'plugin_dir';
|
$plugin_dir = $value if $override eq 'plugin_dir';
|
||||||
$migration_dir = $value if $override eq 'migration_dir';
|
$update_dir = $value if $override eq 'update_dir';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# check command-line arguments for registry overrides
|
# check command-line arguments for registry overrides
|
||||||
foreach my $arg (@ARGV) {
|
foreach my $arg (@ARGV) {
|
||||||
next if $arg =~ m/^-?(?:general\.)?(?:config|data|module|plugin|migration)_dir=.*$/; # already processed
|
next if $arg =~ m/^-?(?:general\.)?(?:config|data|module|plugin|update)_dir=.*$/; # already processed
|
||||||
my ($item, $value) = split /=/, $arg, 2;
|
my ($item, $value) = split /=/, $arg, 2;
|
||||||
|
|
||||||
if (not defined $item or not defined $value) {
|
if (not defined $item or not defined $value) {
|
||||||
@ -125,16 +125,16 @@ sub initialize {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (not -d $migration_dir) {
|
if (not -d $update_dir) {
|
||||||
$self->{logger}->log("Migration directory ($migration_dir) does not exist; aborting...\n");
|
$self->{logger}->log("Updates directory ($update_dir) does not exist; aborting...\n");
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
# migrate/update any data files to new locations/formats
|
$self->{updater} = PBot::Updater->new(pbot => $self, data_dir => $data_dir, update_dir => $update_dir);
|
||||||
$self->{migrator} = PBot::Migration->new(pbot => $self, data_dir => $data_dir, migration_dir => $migration_dir);
|
|
||||||
|
|
||||||
if ($self->{migrator}->migrate) {
|
# update any data files to new locations/formats
|
||||||
$self->{logger}->log("Migration failed.\n");
|
if ($self->{updater}->update) {
|
||||||
|
$self->{logger}->log("Update failed.\n");
|
||||||
exit 0;
|
exit 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,7 +163,7 @@ sub initialize {
|
|||||||
$self->{logger}->log("module_dir: $module_dir\n");
|
$self->{logger}->log("module_dir: $module_dir\n");
|
||||||
$self->{logger}->log("plugin_dir: $plugin_dir\n");
|
$self->{logger}->log("plugin_dir: $plugin_dir\n");
|
||||||
$self->{logger}->log("data_dir: $data_dir\n");
|
$self->{logger}->log("data_dir: $data_dir\n");
|
||||||
$self->{logger}->log("migration_dir: $migration_dir\n");
|
$self->{logger}->log("update_dir: $update_dir\n");
|
||||||
|
|
||||||
|
|
||||||
$self->{timer} = PBot::Timer->new(pbot => $self, timeout => 10, name => 'PBot Timer', %conf);
|
$self->{timer} = PBot::Timer->new(pbot => $self, timeout => 10, name => 'PBot Timer', %conf);
|
||||||
@ -174,10 +174,10 @@ sub initialize {
|
|||||||
# create registry and set some defaults
|
# create registry and set some defaults
|
||||||
$self->{registry} = PBot::Registry->new(pbot => $self, filename => "$data_dir/registry", %conf);
|
$self->{registry} = PBot::Registry->new(pbot => $self, filename => "$data_dir/registry", %conf);
|
||||||
|
|
||||||
$self->{registry}->add_default('text', 'general', 'data_dir', $data_dir);
|
$self->{registry}->add_default('text', 'general', 'data_dir', $data_dir);
|
||||||
$self->{registry}->add_default('text', 'general', 'module_dir', $module_dir);
|
$self->{registry}->add_default('text', 'general', 'module_dir', $module_dir);
|
||||||
$self->{registry}->add_default('text', 'general', 'plugin_dir', $plugin_dir);
|
$self->{registry}->add_default('text', 'general', 'plugin_dir', $plugin_dir);
|
||||||
$self->{registry}->add_default('text', 'general', 'migration_dir', $migration_dir);
|
$self->{registry}->add_default('text', 'general', 'update_dir', $update_dir);
|
||||||
$self->{registry}->add_default('text', 'general', 'trigger', $conf{trigger} // '!');
|
$self->{registry}->add_default('text', 'general', 'trigger', $conf{trigger} // '!');
|
||||||
|
|
||||||
$self->{registry}->add_default('text', 'irc', 'debug', $conf{irc_debug} // 0);
|
$self->{registry}->add_default('text', 'irc', 'debug', $conf{irc_debug} // 0);
|
||||||
@ -202,10 +202,10 @@ sub initialize {
|
|||||||
if (-e $self->{registry}->{registry}->{filename}) { $self->{registry}->load; }
|
if (-e $self->{registry}->{registry}->{filename}) { $self->{registry}->load; }
|
||||||
|
|
||||||
# update important paths
|
# update important paths
|
||||||
$self->{registry}->set('general', 'data_dir', 'value', $data_dir, 0, 1);
|
$self->{registry}->set('general', 'data_dir', 'value', $data_dir, 0, 1);
|
||||||
$self->{registry}->set('general', 'module_dir', 'value', $module_dir, 0, 1);
|
$self->{registry}->set('general', 'module_dir', 'value', $module_dir, 0, 1);
|
||||||
$self->{registry}->set('general', 'plugin_dir', 'value', $plugin_dir, 0, 1);
|
$self->{registry}->set('general', 'plugin_dir', 'value', $plugin_dir, 0, 1);
|
||||||
$self->{registry}->set('general', 'migration_dir', 'value', $migration_dir, 0, 1);
|
$self->{registry}->set('general', 'update_dir', 'value', $update_dir, 0, 1);
|
||||||
|
|
||||||
# override registry entries with command-line arguments, if any
|
# override registry entries with command-line arguments, if any
|
||||||
foreach my $override (keys %{$self->{overrides}}) {
|
foreach my $override (keys %{$self->{overrides}}) {
|
||||||
|
@ -27,11 +27,11 @@ sub initialize {
|
|||||||
sub refresh {
|
sub refresh {
|
||||||
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
||||||
|
|
||||||
my $last_migration = $self->{pbot}->{migrator}->get_last_migration_version;
|
my $last_update = $self->{pbot}->{updater}->get_last_update_version;
|
||||||
my @migrations = $self->{pbot}->{migrator}->get_available_migrations($last_migration);
|
my @updates = $self->{pbot}->{updater}->get_available_updates($last_update);
|
||||||
|
|
||||||
if (@migrations) {
|
if (@updates) {
|
||||||
return "Migration available; cannot refresh. Please restart PBot to begin migration of " . join(', ', map { basename $_ } @migrations);
|
return "Update available; cannot refresh. Please restart PBot to begin update of " . join(', ', map { basename $_ } @updates);
|
||||||
}
|
}
|
||||||
|
|
||||||
my $refresh_error;
|
my $refresh_error;
|
||||||
|
89
PBot/Updater.pm
Normal file
89
PBot/Updater.pm
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
# File: Updater.pm
|
||||||
|
# Author: pragma_
|
||||||
|
#
|
||||||
|
# Purpose: Updates data/configration files to new locations/formats based
|
||||||
|
# on versioning information. Ensures data/configuration files are in the
|
||||||
|
# proper location and using the latest data structure.
|
||||||
|
|
||||||
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package PBot::Updater;
|
||||||
|
use parent 'PBot::Class';
|
||||||
|
|
||||||
|
use warnings; use strict;
|
||||||
|
use feature 'unicode_strings';
|
||||||
|
|
||||||
|
use File::Basename;
|
||||||
|
|
||||||
|
sub initialize {
|
||||||
|
my ($self, %conf) = @_;
|
||||||
|
$self->{data_dir} = $conf{data_dir};
|
||||||
|
$self->{update_dir} = $conf{update_dir};
|
||||||
|
}
|
||||||
|
|
||||||
|
sub update {
|
||||||
|
my ($self) = @_;
|
||||||
|
|
||||||
|
$self->{pbot}->{logger}->log("Checking if update needed...\n");
|
||||||
|
|
||||||
|
my $current_version = $self->get_current_version;
|
||||||
|
my $last_update_version = $self->get_last_update_version;
|
||||||
|
|
||||||
|
$self->{pbot}->{logger}->log("Current version: $current_version; last update version: $last_update_version\n");
|
||||||
|
|
||||||
|
if ($last_update_version >= $current_version) {
|
||||||
|
$self->{pbot}->{logger}->log("No update necessary.\n");
|
||||||
|
return $self->put_last_update_version($current_version);
|
||||||
|
}
|
||||||
|
|
||||||
|
my @updates = $self->get_available_updates($last_update_version);
|
||||||
|
|
||||||
|
if (not @updates ) {
|
||||||
|
$self->{pbot}->{logger}->log("No updates available.\n");
|
||||||
|
return $self->put_last_update_version($current_version);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach my $update (@updates) {
|
||||||
|
$self->{pbot}->{logger}->log("Executing update script: $update\n");
|
||||||
|
my $output = `$update $self->{data_dir}`;
|
||||||
|
my $exit = $? >> 8;
|
||||||
|
$self->{pbot}->{logger}->log("Script completed. Exit $exit. Output: $output");
|
||||||
|
return $exit if $exit != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $self->put_last_update_version($current_version);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub get_available_updates {
|
||||||
|
my ($self, $last_update_version) = @_;
|
||||||
|
my @updates = sort glob "$self->{update_dir}/*.pl";
|
||||||
|
return grep { my ($version) = split /_/, basename $_; $version > $last_update_version } @updates;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub get_current_version {
|
||||||
|
return PBot::VERSION::BUILD_REVISION;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub get_last_update_version {
|
||||||
|
my ($self) = @_;
|
||||||
|
open(my $fh, '<', "$self->{data_dir}/last_update") or return 0;
|
||||||
|
chomp(my $last_update = <$fh>);
|
||||||
|
close $fh;
|
||||||
|
return $last_update;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub put_last_update_version {
|
||||||
|
my ($self, $version) = @_;
|
||||||
|
if (open(my $fh, '>', "$self->{data_dir}/last_update")) {
|
||||||
|
print $fh "$version\n";
|
||||||
|
close $fh;
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
$self->{pbot}->{logger}->log("Could not save last update version to $self->{data_dir}/last_update: $!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
1;
|
8
pbot
8
pbot
@ -15,10 +15,10 @@ BEGIN {
|
|||||||
# configuration is overridden via command-line arguments, do not modify
|
# configuration is overridden via command-line arguments, do not modify
|
||||||
# see doc/QuickStart.md
|
# see doc/QuickStart.md
|
||||||
my %config = (
|
my %config = (
|
||||||
data_dir => "$bothome/data",
|
data_dir => "$bothome/data",
|
||||||
module_dir => "$bothome/modules",
|
module_dir => "$bothome/modules",
|
||||||
plugin_dir => "$bothome/Plugins",
|
plugin_dir => "$bothome/Plugins",
|
||||||
migration_dir => "$bothome/migration",
|
update_dir => "$bothome/updates",
|
||||||
);
|
);
|
||||||
|
|
||||||
use PBot::PBot;
|
use PBot::PBot;
|
||||||
|
Loading…
Reference in New Issue
Block a user