Skip to content

Commit

Permalink
fix object comparisson
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim Fehrs committed May 22, 2024
1 parent 89a3e08 commit 2d03b30
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
6 changes: 5 additions & 1 deletion node-runner-cli/commands/dockercommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ def install(args):

########## Update existing Config
docker_config: DockerConfig = DockerSetup.load_settings(argument_object.config_file)

original_config_dict = docker_config.to_dict()

docker_config_updated_versions = (
DockerSetup.update_versions(docker_config, argument_object.autoapprove)
if argument_object.update
Expand All @@ -251,8 +254,9 @@ def install(args):
docker_config_updated_versions = DockerSetup.check_set_passwords(
docker_config_updated_versions
)

DockerSetup.confirm_config_changes(
argument_object, docker_config, docker_config_updated_versions
argument_object, original_config_dict, docker_config_updated_versions
)

########## Install dependent services
Expand Down
12 changes: 12 additions & 0 deletions node-runner-cli/config/BaseConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import yaml

from deepdiff import DeepDiff


class BaseConfig:
def __iter__(self):
Expand Down Expand Up @@ -80,6 +82,16 @@ def to_file(self, config_file):
yaml.dump(config_to_dump, f, sort_keys=True, default_flow_style=False)


def compare_to_dict(self, config_as_dict: dict) -> dict:
return dict(
DeepDiff(config_as_dict, self.to_dict())
)

def compare_to_object(self, config_object: BaseConfig) -> dict:
return dict(
DeepDiff(config_object.to_dict(), self.to_dict())
)

class SetupMode:
_instance = None
mode = None
Expand Down
27 changes: 13 additions & 14 deletions node-runner-cli/setup/DockerSetup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import getpass
import json
import os
import sys

Expand Down Expand Up @@ -35,7 +34,9 @@ def print_questionary_header(config_file):
)



class DockerSetup(BaseSetup):

@staticmethod
def save_config(config: DockerConfig, config_file: str, autoapprove=False):
to_update = ""
Expand Down Expand Up @@ -372,11 +373,12 @@ def compare_config_file_with_config_object(
if os.path.exists(config_file):
old_config: DockerConfig = DockerSetup.load_settings(config_file)
if old_config is not None:
differences = config_object.compare_to_object(old_config)
logger.info(
f"""
{Helpers.section_headline("Differences")}
Difference between existing config file and new config that you are creating
{dict(DeepDiff(old_config, config_object.to_dict()))}
{differences}
"""
)

Expand All @@ -400,23 +402,20 @@ def render_docker_compose(docker_config: DockerConfig):
@staticmethod
def confirm_config_changes(
argument_object: DockerInstallArguments,
docker_config,
docker_config_updated_versions,
original_config_dict: dict,
updated_config_object: DockerConfig ,
):
config_differences = dict(
DeepDiff(docker_config, docker_config_updated_versions)
)
config_differences = updated_config_object.compare_to_dict(original_config_dict)

if len(config_differences) != 0:
print(
f"""
{Helpers.section_headline("Differences in config file with updated software versions")}
Difference between existing config file and new config that you are creating
{config_differences}
"""
{config_differences} """
)
DockerSetup.save_config(
docker_config_updated_versions,
updated_config_object,
argument_object.config_file,
argument_object.autoapprove,
)
Expand All @@ -425,13 +424,13 @@ def confirm_config_changes(
def confirm_docker_compose_file_changes(
docker_config: DockerConfig, autoapprove: bool
):
docker_compose_yaml: yaml = DockerSetup.render_docker_compose(docker_config)
docker_compose_yaml_rendered: yaml = DockerSetup.render_docker_compose(docker_config)
backup_time = Helpers.get_current_date_time()
compose_file_yaml = DockerSetup.get_existing_compose_file(
docker_compose_yaml_from_file = DockerSetup.get_existing_compose_file(
docker_config.common_config.docker_compose
)
compose_file = docker_config.common_config.docker_compose
compose_file_difference = dict(DeepDiff(compose_file_yaml, docker_compose_yaml))
compose_file_difference = dict(DeepDiff(docker_compose_yaml_from_file, docker_compose_yaml_rendered))
if len(compose_file_difference) != 0:
logger.info(
f"""
Expand All @@ -451,7 +450,7 @@ def confirm_docker_compose_file_changes(
if Helpers.check_Yes(to_update) or autoapprove:
if os.path.exists(compose_file):
Helpers.backup_file(compose_file, f"{compose_file}_{backup_time}")
DockerSetup.save_compose_file(compose_file, docker_compose_yaml)
DockerSetup.save_compose_file(compose_file, docker_compose_yaml_rendered)
run_shell_command(f"cat {compose_file}", shell=True)
return compose_file

Expand Down
4 changes: 2 additions & 2 deletions node-runner-cli/setup/SystemDSetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,14 @@ def load_settings(config_file) -> SystemDConfig:
def compare_old_and_new_config(config_file: str, systemd_config: SystemDConfig):
old_config_object = SystemDSetup.load_settings(config_file)
old_config = old_config_object.to_dict()
config_to_dump = systemd_config.to_dict()
if old_config is not None:
if len(old_config) != 0:
differences = systemd_config.compare_to_dict(old_config)
print(
f"""
{Helpers.section_headline("Differences")}
Difference between existing config file and new config that you are creating
{dict(DeepDiff(old_config, config_to_dump))}
{differences}
"""
)

Expand Down
29 changes: 29 additions & 0 deletions node-runner-cli/tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@

import yaml
from yaml import UnsafeLoader
from deepdiff import DeepDiff

import copy

from config.DockerConfig import DockerConfig
from config.Nginx import SystemdNginxConfig
from config.SystemDConfig import SystemDConfig
from setup.DockerCommandArguments import DockerInstallArguments
from setup.DockerSetup import DockerSetup
from utils.Network import Network


Expand Down Expand Up @@ -40,6 +46,29 @@ def test_network_id_can_be_parsed(self):
self.assertEqual(Network.validate_network_id("stokenet"), 2)


def test_config_is_changed_and_compared_properly(self):
config: DockerConfig = DockerConfig({})
config_dict = config.to_dict()
config_differences = dict(
DeepDiff(config_dict, config_dict)
)
# Compares to 0 differences
self.assertEqual(len(config_differences),0)

config.core_node.core_release = "otherrelease"
updated_config_dict = config.to_dict()
config_differences = dict(
DeepDiff(config_dict, updated_config_dict)
)
# Compares to 0 differences
self.assertEqual(len(config_differences), 1)

def test_compare_to_dict(self):
config: DockerConfig = DockerConfig({})
config_dict = config.to_dict()
config.core_node.core_release = "randomvalue"
self.assertTrue(len(config.compare_to_dict(config_dict)) != 0)

def suite():
"""This defines all the tests of a module"""
suite = unittest.TestSuite()
Expand Down

0 comments on commit 2d03b30

Please sign in to comment.