Skip to content

Commit

Permalink
fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanp8 committed Aug 12, 2024
1 parent 855617e commit d8d829e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 31 deletions.
13 changes: 13 additions & 0 deletions autodeploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Autodeployment Script

## Usage

### Starting caddy
Make sure that Caddy is running. If not, run it with `docker compose up caddy`

### Autodeploy
To run the script, run `python3 -m autodeploy`. This will search
ihr-archive to see if a dump has been pushed in the last week. If
so, a neo4j instance will be deployed using that dump. Alternatively,
running `python3 -m autodeploy [year]-[month]-[day]` will check if
there is a dump in the archive for the specified date and deploy it directly.
73 changes: 42 additions & 31 deletions autodeploy.py → autodeploy/autodeploy.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import datetime
import logging
import sys
import os
import docker
import logging
import requests
import sys
import time
import json

import arrow
import datetime
import docker
import requests

NEO4J_VERSION = '5.16.0'
today = arrow.utcnow()
Expand All @@ -23,28 +22,33 @@
logging.warning('Starting deployment')

client = docker.from_env()


def remove_deployment(month, day):
"""Checks if there is an active deployment for the given month and day. If there is,
remove it and the corresponding volume storing its data.
"""Checks if there is an active deployment for the given month and day.
If there is, remove it and the corresponding volume storing its data.
"""
try:
container = client.containers.get(f'deploy-{month}-{day}')
logging.warning(f'Removing active deployment for {month}-{day}')
container.stop()
# Wait a little bit after the container has been removed before deleting the volume
# Wait a little bit after the container has been removed
# before deleting the volume
while True:
try:
client.volumes.get(f'data-{month}-{day}').remove()
break
except:
except BaseException:
time.sleep(0.1)
except docker.errors.NotFound:
logging.warning(f'No existing deployment for {date}. Starting deployment')


def get_config_ports():
"""Makes a request to caddy config and returns the ports current being used (both active and previous)"""
with requests.get("http://sandbox.ihr.live:2019/config/") as response:
"""Makes a request to caddy config and returns the ports current being used (both
active and previous)"""
with requests.get('http://sandbox.ihr.live:2019/config/') as response:
body = json.loads(response.content.decode())
routes = body['apps']['http']['servers']['srv0']['routes']
active = {}
Expand All @@ -63,13 +67,14 @@ def get_config_ports():
'prev_http': prev_http_port,
'prev_bolt': prev_bolt_port
}
except:
except BaseException:
logging.warning('Unable to find currently active deployments')
return None


def check_log(year, month, day):
"""Makes a request to archive and checks if there is a valid dump for the specified date"""
"""Makes a request to archive and checks if there is a valid dump for the specified
date."""
date = f'{year}-{month}-{day}'
logging.warning(f'Downloading logs for {date}')
with requests.get(f'https://ihr-archive.iijlab.net/ihr-dev/iyp/{year}/{month}/{day}/iyp-{date}.log') as response:
Expand All @@ -82,25 +87,31 @@ def check_log(year, month, day):
return True
return False


def get_port_date(port):
"""Extracts the month and day from a port. Returns the tuple (month, day)"""
"""Extracts the month and day from a port.
Returns the tuple (month, day)
"""
month = port[-4:-2]
day = port[-2:]
return month, day

# If no date is provided when running the script, check if any dumps have been made within a week since the
# previous deployment. Otherwise, use the date provided in command line arg.

# If no date is provided when running the script, check if any dumps
# have been made within a week since the previous deployment. Otherwise,
# use the date provided in command line arg.
if len(sys.argv) < 2:
ports = get_config_ports()
if ports:
active_http = ports['active_http']
month, day = get_port_date(active_http)
start_date = datetime.date(int(today.year), int(month), int(day))
success = False

# Download logs from ihr archive each day in the next week since the previous release
for i in range(1,8):

# Download logs from ihr archive each day in the next week since
# the previous release
for i in range(1, 8):
date = start_date + datetime.timedelta(days=i)
date = date.strftime('%Y-%m-%d')
logging.warning(f'Checking archive for {date}')
Expand Down Expand Up @@ -142,17 +153,17 @@ def get_port_date(port):
# Load dump into volume
logging.warning('Load dump into neo4j db')
container = client.containers.run(
'neo4j/neo4j-admin:' + NEO4J_VERSION,
command='neo4j-admin database load neo4j --from-path=/dumps --verbose',
name='load',
tty=True,
stdin_open=True,
remove=True,
volumes={
'neo4j/neo4j-admin:' + NEO4J_VERSION,
command='neo4j-admin database load neo4j --from-path=/dumps --verbose',
name='load',
tty=True,
stdin_open=True,
remove=True,
volumes={
neo4j_volume: {'bind': '/data', 'mode': 'rw'},
dump_dir: {'bind': '/dumps', 'mode': 'rw'},
}
)
}
)


# Run neo4j based on data in volume just created
Expand Down

0 comments on commit d8d829e

Please sign in to comment.