From 81a20e11cec28f7e76ff8413dd4d3400ad76d843 Mon Sep 17 00:00:00 2001 From: DaMandal0rian Date: Thu, 26 Sep 2024 12:21:49 +0200 Subject: [PATCH] use sed and ssh instead of sftp for .env modifying --- scripts/launch-nodes/manage_subspace.py | 61 +++++++++++++------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/scripts/launch-nodes/manage_subspace.py b/scripts/launch-nodes/manage_subspace.py index 8399ad5..1158d56 100755 --- a/scripts/launch-nodes/manage_subspace.py +++ b/scripts/launch-nodes/manage_subspace.py @@ -1,3 +1,4 @@ +import os import paramiko import argparse import tomli @@ -63,37 +64,37 @@ def docker_compose_down(client, subspace_dir): raise def modify_env_file(client, subspace_dir, release_version, genesis_hash=None, pot_external_entropy=None): - """Modify the .env file to update the Docker tag and optionally the Genesis Hash and POT_EXTERNAL_ENTROPY.""" - env_file = f'{subspace_dir}/.env' - + """Modify the .env file to update the Docker tag, Genesis Hash, and POT_EXTERNAL_ENTROPY using sed.""" try: - sftp = client.open_sftp() - with sftp.open(env_file, 'r') as f: - env_data = f.readlines() - - pot_entropy_found = False - - # Modify the Docker tag, Genesis hash, and POT_EXTERNAL_ENTROPY - with sftp.open(env_file, 'w') as f: - for line in env_data: - if line.startswith('DOCKER_TAG='): - logger.debug(f"Writing DOCKER_TAG={release_version}") - f.write(f'DOCKER_TAG={release_version}\n') - elif genesis_hash and line.startswith('GENESIS_HASH='): - logger.debug(f"Writing GENESIS_HASH={genesis_hash}") - f.write(f'GENESIS_HASH={genesis_hash}\n') - elif pot_external_entropy and line.startswith('POT_EXTERNAL_ENTROPY='): - f.write(f'POT_EXTERNAL_ENTROPY={pot_external_entropy}\n') - pot_entropy_found = True - else: - f.write(line) - - # If POT_EXTERNAL_ENTROPY was not found, append it - if pot_external_entropy and not pot_entropy_found: - logger.debug(f"Writing POT_EXTERNAL_ENTROPY={pot_external_entropy}") - f.write(f'POT_EXTERNAL_ENTROPY={pot_external_entropy}\n') - - logger.info(f"Modified .env file in {env_file}") + # Command to update DOCKER_TAG + commands = [ + f"sed -i 's/^DOCKER_TAG=.*/DOCKER_TAG={release_version}/' {subspace_dir}/.env" + ] + + # Command to update GENESIS_HASH if provided + if genesis_hash: + commands.append(f"sed -i 's/^GENESIS_HASH=.*/GENESIS_HASH={genesis_hash}/' {subspace_dir}/.env") + + # Command to update POT_EXTERNAL_ENTROPY if provided + if pot_external_entropy: + # If POT_EXTERNAL_ENTROPY exists, replace it, otherwise append it + commands.append(f"grep -q '^POT_EXTERNAL_ENTROPY=' {subspace_dir}/.env && " + f"sed -i 's/^POT_EXTERNAL_ENTROPY=.*/POT_EXTERNAL_ENTROPY={pot_external_entropy}/' {subspace_dir}/.env || " + f"echo 'POT_EXTERNAL_ENTROPY={pot_external_entropy}' >> {subspace_dir}/.env") + + # Execute the commands over SSH + for command in commands: + logger.debug(f"Executing command: {command}") + stdin, stdout, stderr = client.exec_command(command) + stdout_text = stdout.read().decode() + stderr_text = stderr.read().decode() + + if stderr_text: + logger.error(f"Error modifying .env file with command: {command}, error: {stderr_text}") + raise Exception(f"Error modifying .env file: {stderr_text}") + else: + logger.info(f"Successfully executed command: {command}") + except Exception as e: logger.error(f"Failed to modify .env file: {e}") raise