Skip to content

Core: Services: cable-guy: Complete migration from old settings to Pydantic to ensure migrations#3232

Merged
patrickelectric merged 6 commits intobluerobotics:masterfrom
joaomariolago:fix-cable-guy-settings
Mar 31, 2025
Merged

Core: Services: cable-guy: Complete migration from old settings to Pydantic to ensure migrations#3232
patrickelectric merged 6 commits intobluerobotics:masterfrom
joaomariolago:fix-cable-guy-settings

Conversation

@joaomariolago
Copy link
Collaborator

@joaomariolago joaomariolago commented Mar 27, 2025

Fix #3231
Depends on #3235

When updating to one of 1.4.0-beta.17 or 1.4.0-beta.18 and after downgrading it to other lower version it makes cable-guy break due to invalid settings and not configure network properly.

NOTE: Tested on PI4, but aditional test would be good before merge, mainly to make sure cable-guy functionality was not affected.

Summary by Sourcery

Migrate cable-guy service settings to Pydantic to improve settings management and ensure compatibility across different versions

Bug Fixes:

  • Resolve settings migration issues that previously caused network configuration problems when downgrading BlueOS versions

Enhancements:

  • Refactor settings management to use Pydantic for more robust configuration handling
  • Improve settings migration mechanism to handle version changes

Chores:

  • Create a new config.py file to centralize service configuration
  • Simplify settings loading and saving logic

Summary by Sourcery

Migrate cable-guy service settings to Pydantic for improved configuration management and version compatibility

New Features:

  • Implement a new settings migration mechanism using Pydantic

Bug Fixes:

  • Resolve settings migration issues that caused network configuration problems when downgrading BlueOS versions

Enhancements:

  • Refactor settings management to use Pydantic for more robust configuration handling
  • Improve settings migration mechanism to handle version changes

Chores:

  • Create a centralized configuration file for service settings
  • Simplify settings loading and saving logic

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 27, 2025

Reviewer's Guide by Sourcery

This pull request migrates the cable-guy service settings to Pydantic to improve settings management and ensure compatibility across different BlueOS versions. It refactors the settings loading and saving logic, centralizes service configuration, and resolves settings migration issues that previously caused network configuration problems when downgrading BlueOS versions.

Sequence diagram for loading settings during initialization

sequenceDiagram
    participant EM as EthernetManager
    participant PM as PydanticManager
    participant S as SettingsV1

    EM->>PM: _settings (property)
    activate PM
    PM->>S: Returns settings
    deactivate PM
    EM->>EM: Load previous settings
    loop For each item in settings.content
        EM->>EM: set_configuration(item)
    end
    EM->>PM: save()
    activate PM
    PM->>S: save()
    deactivate PM
Loading

Sequence diagram for updating interface settings

sequenceDiagram
    participant EM as EthernetManager
    participant S as SettingsV1
    participant PM as PydanticManager

    EM->>EM: _update_interface_settings(interface_name, updated_interface)
    EM->>S: _settings.content
    S-->>EM: Returns content
    EM->>EM: Filter out old interface
    EM->>S: _settings.content.append(updated_interface)
    EM->>PM: _manager.save()
    activate PM
    PM->>S: save()
    deactivate PM
Loading

Updated class diagram for SettingsV1

classDiagram
    class SettingsV1 {
        - content: List~NetworkInterface~
        + migrate(data: Dict~str, Any~): None
        + on_settings_created(file_path: pathlib.Path): None
    }
    class PydanticSettings {
    }
    SettingsV1 --|> PydanticSettings
    class NetworkInterface {
        - name: str
        - addresses: List~InterfaceAddress~
        - info: Optional~InterfaceInfo~
        - priority: Optional~int~
    }
    SettingsV1 --  NetworkInterface : contains
Loading

File-Level Changes

Change Details Files
Migrated settings management to Pydantic for improved robustness and version compatibility.
  • Replaced the custom Settings class with SettingsV1 based on PydanticSettings.
  • Implemented settings migration logic within the SettingsV1 class to handle version upgrades.
  • Utilized Pydantic models for defining network interface configurations.
  • Implemented a mechanism to sanitize old settings files to prevent issues when downgrading BlueOS versions.
  • Leveraged PydanticManager for settings loading and saving.
core/services/cable_guy/api/settings.py
core/services/cable_guy/api/manager.py
Refactored the EthernetManager to use Pydantic settings and simplified settings loading/saving.
  • Removed the direct usage of the custom Settings class.
  • Introduced PydanticManager to handle settings persistence.
  • Modified the EthernetManager to use the SettingsV1 model.
  • Simplified the save method to use PydanticManager.
  • Updated the _update_interface_settings method to directly modify the settings content.
  • Updated the get_saved_interface_by_name method to directly fetch from settings content.
core/services/cable_guy/api/manager.py
Created a new config.py file to centralize service configuration.
  • Created a new config.py file.
  • Moved SERVICE_NAME and DEFAULT_NETWORK_INTERFACES to config.py.
core/services/cable_guy/config.py
core/services/cable_guy/api/settings.py
core/services/cable_guy/main.py
Updated the NetworkInterface model to have optional info and priority fields.
  • Set info and priority fields in NetworkInterface model to optional with default value None.
core/services/cable-guy/typedefs.py

Assessment against linked issues

Issue Objective Addressed Explanation
#3231 The application should not break on downgrading from beta 17 or beta 18 to a previous version due to incompatible settings.
#3231 The application should migrate settings from older versions to newer versions.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@joaomariolago joaomariolago force-pushed the fix-cable-guy-settings branch from 903508a to 19a30a0 Compare March 27, 2025 19:21
@joaomariolago joaomariolago force-pushed the fix-cable-guy-settings branch 3 times, most recently from 0806396 to 4590b02 Compare March 28, 2025 19:34
@joaomariolago joaomariolago force-pushed the fix-cable-guy-settings branch 2 times, most recently from f7388b7 to 965a604 Compare March 28, 2025 22:15
@joaomariolago joaomariolago marked this pull request as ready for review March 31, 2025 13:20
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @joaomariolago - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a unit test for the sanitize_old_settings_file function to ensure it correctly handles the settings migration.
  • The on_settings_created method includes a try/except block that simply passes on exception - consider logging the exception to provide better visibility into potential issues.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

"""
# We don't want to write in disk if there is nothing different to write
if self.root["content"] == content:
def on_settings_created(self, file_path: pathlib.Path) -> None:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the migration logic and file parsing within on_settings_created into a separate helper function to reduce nesting and improve readability

Consider extracting migration logic and file parsing into a separate helper to reduce nesting. For example, you can move the inner logic of on_settings_created to a dedicated function:

def migrate_old_settings(file_path: pathlib.Path, settings: SettingsV1) -> None:
    old_settings_file_path = file_path.parent / "settings.json"
    try:
        with open(old_settings_file_path, "r", encoding="utf-8") as file:
            settings.content = [NetworkInterface.parse_obj(iface) for iface in json.load(file)["content"]]
        sanitize_old_settings_file(old_settings_file_path)
    except Exception:
        pass

Then call it in your method:

def on_settings_created(self, file_path: pathlib.Path) -> None:
    if self.VERSION != SettingsV1.STATIC_VERSION:
        return
    migrate_old_settings(file_path, self)

This refactoring isolates migration details, reducing complexity in the primary class while preserving all functionality.

Copy link
Member

@patrickelectric patrickelectric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on 1.3.1, 1.4.0-beta.16 and the PR BlueOS version itself.
Will wait for someone else to review and test before merging.

@patrickelectric patrickelectric merged commit 8f37778 into bluerobotics:master Mar 31, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[cable-guy] Completely broken when downgrading from beta 17 to previous

3 participants

Comments