Skip to content

Commit

Permalink
Hacky fix for config upgrading
Browse files Browse the repository at this point in the history
  • Loading branch information
ichorid committed Feb 21, 2020
1 parent 05cc9cc commit be1b076
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Tribler/Core/Config/tribler_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TriblerConfig(object):
their allowed values and default value in `config.spec`.
"""

def __init__(self, config=None):
def __init__(self, config=None, root_state_dir=None):
"""
Create a new TriblerConfig instance.
Expand All @@ -42,7 +42,7 @@ def __init__(self, config=None):
"""
self._logger = logging.getLogger(self.__class__.__name__)

self._root_state_dir = self.get_default_root_state_dir()
self._root_state_dir = root_state_dir or self.get_default_root_state_dir()
self._state_dir = os.path.join(self._root_state_dir, tribler_version.version_id)

if config is None:
Expand Down
3 changes: 1 addition & 2 deletions Tribler/Core/Session.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ def __init__(self, config=None, autoload_discovery=True):
self.get_ports_in_config()
self.create_state_directory_structure()

self.selected_ports = self.config.selected_ports

self.lm = TriblerLaunchMany()
self.notifier = Notifier()

Expand Down Expand Up @@ -422,6 +420,7 @@ def start(self):

def after_upgrade(_):
self.upgrader = None
self.selected_ports = self.config.selected_ports

def log_upgrader_error(failure):
self._logger.error("Error in Upgrader callback chain: %s", failure)
Expand Down
9 changes: 7 additions & 2 deletions Tribler/Core/Upgrade/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from twisted.internet.defer import succeed

from Tribler.Core.Category.l2_filter import is_forbidden
from Tribler.Core.Config.tribler_config import TriblerConfig
from Tribler.Core.Modules.MetadataStore.OrmBindings.channel_metadata import CHANNEL_DIR_NAME_LENGTH
from Tribler.Core.Modules.MetadataStore.store import MetadataStore
from Tribler.Core.Upgrade.config_converter import convert_config_to_tribler71, convert_config_to_tribler74
Expand Down Expand Up @@ -90,8 +91,12 @@ def run(self):
"""
# Before any upgrade, prepare a separate state directory for the update version so it does not affect the
# older version state directory. This allows for safe rollback.
self.version_manager.setup_state_directory_for_upgrade()
self.session.init_keypair()
upgraded_dir = self.version_manager.setup_state_directory_for_upgrade()
# This is a horrible, horrible way to fix the problem of upgrading triblerd.conf
# It effectively creates a second TriblerConfig object and writes it over the first one
if upgraded_dir:
self.session.config = TriblerConfig(root_state_dir=self.session.config.get_root_state_dir())
self.session.init_keypair()

d = self.upgrade_72_to_pony()
d.addCallback(self.upgrade_pony_db_6to7)
Expand Down
5 changes: 4 additions & 1 deletion Tribler/Core/Upgrade/version_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@ def setup_state_directory_for_upgrade(self, version_id=None):

# If upgrade is possible, fork the state directory for the new code version
if upgrade_possible:

self.fork_state_directory(code_version, self.get_state_directory(last_usage_version))

# Update the history file
if last_usage_version != code_version:
self.update_version_history(code_version)

return upgrade_possible


def get_state_directory(self, version_id):
"""
Returns versioned state directory for a given version else returns .Tribler directory.
Expand Down
2 changes: 2 additions & 0 deletions Tribler/Test/Community/Tunnel/FullSession/test_tunnel_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def create_proxy(self, index, exitnode=False):
config.set_tunnel_community_socks5_listen_ports(self.get_ports(5))

session = Session(config)
session.upgrader_enabled = False
yield session.start()
self.sessions.append(session)

Expand All @@ -159,6 +160,7 @@ def setup_tunnel_seeder(self, hops):
self.seed_config.set_tunnel_community_socks5_listen_ports(self.get_ports(5))
if self.session2 is None:
self.session2 = Session(self.seed_config)
self.session2.upgrader_enabled = False
self.session2.start()

tdef = TorrentDef()
Expand Down
6 changes: 4 additions & 2 deletions Tribler/Test/Core/Upgrade/test_upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def test_upgrade_72_to_pony(self):
OLD_DB_SAMPLE = os.path.abspath(os.path.join(os.path.abspath(
os.path.dirname(os.path.realpath(__file__))), '..', 'data', 'upgrade_databases', 'tribler_v29.sdb'))
old_database_path = os.path.join(self.session.config.get_state_dir(), 'sqlite', 'tribler.sdb')
new_database_path = os.path.join(self.session.config.get_state_dir(), 'sqlite', 'metadata.db')
shutil.copyfile(OLD_DB_SAMPLE, old_database_path)

yield self.upgrader.run()
new_database_path = os.path.join(self.session.config.get_state_dir(), 'sqlite', 'metadata.db')
channels_dir = os.path.join(self.session.config.get_chant_channels_dir())
mds = MetadataStore(new_database_path, channels_dir, self.session.trustchain_keypair)
with db_session:
Expand All @@ -67,8 +67,10 @@ def test_upgrade_pony_db_6to7(self):
old_database_path = os.path.join(self.session.config.get_state_dir(), 'sqlite', 'metadata.db')
shutil.copyfile(OLD_DB_SAMPLE, old_database_path)

print(self.session.config.get_chant_channels_dir())
yield self.upgrader.run()
channels_dir = os.path.join(self.session.config.get_chant_channels_dir())
print(self.session.config.get_chant_channels_dir())
mds = MetadataStore(old_database_path, channels_dir, self.session.trustchain_keypair)
with db_session:
self.assertEqual(mds.TorrentMetadata.select().count(), 23)
Expand All @@ -82,13 +84,13 @@ def test_skip_upgrade_72_to_pony(self):
OLD_DB_SAMPLE = os.path.abspath(os.path.join(os.path.abspath(
os.path.dirname(os.path.realpath(__file__))), '..', 'data', 'upgrade_databases', 'tribler_v29.sdb'))
old_database_path = os.path.join(self.session.config.get_state_dir(), 'sqlite', 'tribler.sdb')
new_database_path = os.path.join(self.session.config.get_state_dir(), 'sqlite', 'metadata.db')
channels_dir = os.path.join(self.session.config.get_chant_channels_dir())

shutil.copyfile(OLD_DB_SAMPLE, old_database_path)

self.upgrader.skip()
yield self.upgrader.run()
new_database_path = os.path.join(self.session.config.get_state_dir(), 'sqlite', 'metadata.db')
mds = MetadataStore(new_database_path, channels_dir, self.session.trustchain_keypair)
with db_session:
self.assertEqual(mds.TorrentMetadata.select().count(), 0)
Expand Down

0 comments on commit be1b076

Please sign in to comment.