Skip to content

Commit

Permalink
Deployment script fix (#83)
Browse files Browse the repository at this point in the history
* look for `main`

* test

* _0_3_9

* Update README.md
  • Loading branch information
dereknop authored Dec 6, 2024
1 parent aed4abc commit f94d886
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ The probe matcher will output two json files for each input json found: one for
To utilize this function, you will need to manually run the deployment script.
We will eventually have a Jenkins job that will run the script whenever ingest is updated.
To run a new script, add it to the `/scripts` folder, following the naming convention. The deployment script will check the current database version in mongo to determine which scripts need to be run.

**Important:** the script must have a main function (`def main(mongo_db):`)
The script will only run if the version is a newer version of the db, the script also create the collection for the version if it doesn't exist yet. Set your .env file to point to the instances of the TA1 servers you have running locally.
26 changes: 10 additions & 16 deletions deployment_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def __init__(self, mongo_url, db_name, scripts_dir = 'scripts'):
self.db = self.client[db_name]
self.scripts_dir = scripts_dir
self.version_collection = "itm_version"

def get_current_db_version(self):
collection = self.db[self.version_collection]
version_obj = collection.find_one()
return version_obj['version'] if version_obj else "0.0.0"

def update_db_version(self, new_version: str):
collection = self.db[self.version_collection]
version_obj = collection.find_one()
Expand All @@ -41,20 +41,15 @@ def discover_update_scripts(self):

# convert filename for import
module_name = f"{self.scripts_dir}.{filename[:-3]}"

'''
Note: it is going to call the first function found in the script.
We should standardize the main function of each script (if there is more than one)
to be at the beginning of the file.
Note: Your script MUST have 'def main', this function
should take mongo_db as a parameter
'''
try:
module = importlib.import_module(module_name)
main_functions = [
name for name in dir(module)
if callable(getattr(module, name)) and not name.startswith('_')
]
if main_functions:
updates.append((version_str, module_name, main_functions[0]))
main_function = getattr(module, 'main', None)
if main_function:
updates.append((version_str, module_name, main_function))
except ImportError as e:
print(f"Warning: Could not import {module_name}: {e}")

Expand All @@ -78,18 +73,17 @@ def run_updates(self):

print(f"Found {len(pending_scripts)} pending updates")

for version_str, module_name, function_name in pending_scripts:
print(f"Running update {version_str} from {module_name}.{function_name}")
for version_str, module_name, update_function in pending_scripts:
print(f"Running update {version_str} from {module_name}")
try:
module = importlib.import_module(module_name)
update_function = getattr(module, function_name)
# mongo instance passed
update_function(self.db)
self.update_db_version(version_str)
print(f"Successfully completed script {version_str}")
except Exception as e:
print(f"Error running script {version_str}: {e}")
raise # stops the update process on err

final_version = self.get_current_db_version()
print(f"\nDatabase has been updated to version {final_version}")

Expand Down
5 changes: 5 additions & 0 deletions scripts/_0_3_9_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def another_function(mongo_db):
print('dont print me')

def main(mongo_db):
print('printing from _0_3_8_')

0 comments on commit f94d886

Please sign in to comment.