Skip to content

Commit

Permalink
Merge pull request #543 from ucb-rit/develop
Browse files Browse the repository at this point in the history
v3.4.1 Changes
  • Loading branch information
matthew-li authored May 31, 2023
2 parents f31da37 + b34f7c6 commit 273f09e
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 167 deletions.
3 changes: 0 additions & 3 deletions .env

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dist
build
*._*
*.DS_Store
.env
*.swp
env/
venv/
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile.email
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3

LABEL description="coldfront email"

RUN apt update && apt install -y sudo net-tools
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,42 @@ multiple files or directories to omit.
- Open `htmlcov/index.html` in a browser to view which lines of
code were covered by the tests and which were not.
## Docker - Quick Install (Recommend)
1. Generate configuration (`dev_settings.py`): have Python with the `jinja2` and `pyyaml` libraries installed, and then run `bootstrap/development/gen_config.sh`
2. Build Images: In the base directory, run `docker build . -t coldfront` and `docker build . -f Dockerfile.db -t coldfront_db`. `--build-arg PORTAL=mylrc` can be added to the build command to build for mylrc.
3. If needed, modify `.env` to customize the web server port and the database name (e.g from `cf_mybrc_db` to `cf_mylrc_db`)
4. To run: In the base directory, run `docker compose up`
5. To enter the coldfront container (similar to `vagrant ssh`): run `docker exec -it coldfront-coldfront-1 bash`
6. To load a database backup: run `bootstrap/development/docker_load_database_backup.sh ${DB_NAME} ${PATH_TO_DUMP}`
7. To start from scratch (delete volumes): In the base directory, run `docker compose down --volumes`
## Docker - Quick Install (Recommended)
1. Create and customize `main.yml`, as described above.
- In the Vagrant VM setup and in production, this file is used to configure Ansible. In particular, variables in this file are used to generate a deployment-specific Django settings file.
- However, Ansible is not used in the Docker setup. Variables are manually parsed to generate the configuration file (`dev_settings.py`).
2. Manually generate a deployment-specific Django settings file from `main.yml`.
```bash
# This can be done in a Python virtual environment.
pip install jinja2 pyyaml
sh bootstrap/development/gen_config.sh
```
3. Build images. In the base directory, run:
```bash
docker build . -t coldfront
docker build . -f Dockerfile.db -t coldfront_db
docker build . -f Dockerfile.email -t coldfront_email
```
Note: The above commands build images meant for a MyBRC instance. To build MyLRC images, include `--build-arg PORTAL=mylrc`.
4. Configure environment variables to be injected into containers by creating a `.env` file in the root directory (ignored by Git) or by setting them manually.
- `DB_NAME=cf_brc_db`: The name of the database can be customized (e.g., for a MyLRC instance, change it to `cf_lrc_db`).
- `COLDFRONT_PORT=8880`: The port can be customized so that multiple instances can be run without conflicting.
5. Bring up the stack. In the root directory, run:
```bash
docker compose up
```
6. To SSH into the `coldfront` container, run:
```bash
docker exec -it coldfront-coldfront-1 bash
```
7. To load a database backup, run:
```bash
sh bootstrap/development/docker_load_database_backup.sh ${DB_NAME} ${PATH_TO_DUMP}
```
8. To start from scratch, deleting created volumes, run:
```bash
docker compose down --volumes
```

## Local Machine - Quick Install (Not Recommended)

Expand Down
3 changes: 3 additions & 0 deletions bootstrap/ansible/main.copyme
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ sentry_dsn: ""
# debug_toolbar_ips: []

# # Email settings.
# email_host: localhost
# email_port: 25
# # TODO: Set these addresses to yours.
# from_email: [email protected]
Expand Down Expand Up @@ -229,6 +230,7 @@ sentry_dsn: ""
# debug_toolbar_ips: []

# # Email settings.
# email_host: localhost
# email_port: 25
# # TODO: Set these addresses to yours.
# from_email: [email protected]
Expand Down Expand Up @@ -281,6 +283,7 @@ sentry_dsn: ""
# debug_toolbar_ips: ['10.0.2.2'] # 10.0.2.2 is the vagrant host.

# # Email settings.
# email_host: localhost
# email_port: 1025
# # TODO: Set these addresses to yours.
# from_email: [email protected]
Expand Down
1 change: 1 addition & 0 deletions bootstrap/ansible/settings_template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CENTER_BASE_URL = '{{ full_host_path }}'
CENTER_HELP_URL = CENTER_BASE_URL + '/help'
CENTER_PROJECT_RENEWAL_HELP_URL = CENTER_BASE_URL + '/help'

EMAIL_HOST = '{{ email_host}}'
EMAIL_PORT = {{ email_port }}
EMAIL_SUBJECT_PREFIX = '{{ email_subject_prefix }}'
# A list of admin email addresses to be notified about new requests and other
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/development/docker_load_database_backup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
# $1 = database name
# $2 = dump file
docker exec -i coldfront-db-1 pg_restore --verbose --clean -U admin -d $1 < $2
docker exec -i coldfront-db-1 pg_restore --verbose --clean -U admin -d $1<$2
14 changes: 14 additions & 0 deletions bootstrap/development/gen_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from jinja2 import Environment
from jinja2 import FileSystemLoader
import yaml


env = Environment(loader=FileSystemLoader('bootstrap/ansible/'))
env.filters['bool'] = lambda x: str(x).lower() in ['true', 'yes', 'on', '1']
options = yaml.safe_load(open('main.yml').read())
options.update({
'db_host': 'db',
'email_host': 'email',
'redis_host': 'redis',
})
print(env.get_template('settings_template.tmpl').render(options))
16 changes: 3 additions & 13 deletions bootstrap/development/gen_config.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
#!/usr/bin/env bash
cp coldfront/config/local_settings.py.sample \
coldfront/config/local_settings.py
cp coldfront/config/local_strings.py.sample \
coldfront/config/local_strings.py
python -c \
"from jinja2 import Template, Environment, FileSystemLoader; \
import yaml; \
env = Environment(loader=FileSystemLoader('bootstrap/ansible/')); \
env.filters['bool'] = lambda x: str(x).lower() in ['true', 'yes', 'on', '1']; \
options = yaml.safe_load(open('main.yml').read()); \
options.update({'redis_host': 'redis', 'db_host': 'db'}); \
print(env.get_template('settings_template.tmpl').render(options))" \
> coldfront/config/dev_settings.py
cp coldfront/config/local_settings.py.sample coldfront/config/local_settings.py
cp coldfront/config/local_strings.py.sample coldfront/config/local_strings.py
python bootstrap/development/gen_config.py > coldfront/config/dev_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from coldfront.core.utils.common import display_time_zone_current_date
from coldfront.core.utils.common import display_time_zone_date_to_utc_datetime
from coldfront.core.utils.common import utc_now_offset_aware
from coldfront.core.utils.email.email_strategy import DropEmailStrategy
from coldfront.core.utils.email.email_strategy import SendEmailStrategy


"""An admin command that approves AllocationRenewalRequests for a
Expand All @@ -34,12 +36,18 @@ def add_arguments(self, parser):
'allocation_period_id',
help='The ID of the AllocationPeriod.',
type=int)
parser.add_argument(
'--skip_emails',
action='store_true',
default=False,
help='Skip sending notification emails to requesters and PIs.')
add_argparse_dry_run_argument(parser)

def handle(self, *args, **options):
"""Approve eligible requests if the AllocationPeriod is valid
and eligible."""
dry_run = options['dry_run']
skip_emails = options['skip_emails']

allocation_period_id = options['allocation_period_id']
try:
Expand Down Expand Up @@ -83,8 +91,11 @@ def handle(self, *args, **options):

message_template = (
f'{{0}} AllocationRenewalRequest {{1}} for PI {{2}}, scheduling '
f'{{3}} to be granted to {{4}} on {allocation_period_start_date}, '
f'and emailing the requester and/or PI.')
f'{{3}} to be granted to {{4}} on {allocation_period_start_date}')
if skip_emails:
message_template += '.'
else:
message_template += ', and emailing the requester and/or PI.'
for request in requests:
num_service_units = num_service_units_by_allowance_name[
request.computing_allowance.name]
Expand All @@ -99,7 +110,8 @@ def handle(self, *args, **options):
try:
self.update_request_state(request)
request.refresh_from_db()
self.approve_request(request, num_service_units)
self.approve_request(
request, num_service_units, skip_emails=skip_emails)
except Exception as e:
message = (
f'Failed to approve AllocationRenewalRequest '
Expand All @@ -123,9 +135,11 @@ def update_request_state(request):
request.save()

@staticmethod
def approve_request(request, num_service_units):
"""Instantiate and run tne approval runner for the given request
and number of service units, sending emails."""
def approve_request(request, num_service_units, skip_emails=False):
"""Instantiate and run the approval runner for the given request
and number of service units. Optionally skip sending email."""
email_strategy = (
DropEmailStrategy() if skip_emails else SendEmailStrategy())
approval_runner = AllocationRenewalApprovalRunner(
request, num_service_units)
request, num_service_units, email_strategy=email_strategy)
approval_runner.run()
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from django.core.management.base import CommandError
from flags.state import flag_enabled

import json
import logging
import os

"""An admin command that creates AllocationPeriods."""

Expand Down Expand Up @@ -56,6 +58,9 @@ def handle(self, *args, **options):
else:
prev_start_date = allocation_period.start_date
prev_end_date = allocation_period.end_date
if start_date == prev_start_date and end_date == prev_end_date:
# No update needed.
continue
message_template = (
f'{{0}} AllocationPeriod {allocation_period.pk} with '
f'name "{name}" from ({prev_start_date}, {prev_end_date}) '
Expand Down Expand Up @@ -94,126 +99,17 @@ def get_allocation_periods():
based on the current deployment."""
periods = []

relative_json_path = None
if flag_enabled('BRC_ONLY'):
periods.extend(
[
{
"name": "Allowance Year 2020 - 2021",
"start_date": "2020-06-01",
"end_date": "2021-05-31"
},
{
"name": "Allowance Year 2021 - 2022",
"start_date": "2021-06-01",
"end_date": "2022-05-31"
},
{
"name": "Allowance Year 2022 - 2023",
"start_date": "2022-06-01",
"end_date": "2023-05-31"
},
{
"name": "Allowance Year 2023 - 2024",
"start_date": "2023-06-01",
"end_date": "2024-05-31"
},
{
"name": "Fall Semester 2021",
"start_date": "2021-08-18",
"end_date": "2021-12-17"
},
{
"name": "Spring Semester 2022",
"start_date": "2022-01-11",
"end_date": "2022-05-13"
},
{
"name": "Summer Sessions 2022 - Session A",
"start_date": "2022-05-23",
"end_date": "2022-07-01"
},
{
"name": "Summer Sessions 2022 - Session B",
"start_date": "2022-06-06",
"end_date": "2022-08-12"
},
{
"name": "Summer Sessions 2022 - Session C",
"start_date": "2022-06-21",
"end_date": "2022-08-12"
},
{
"name": "Summer Sessions 2022 - Session D",
"start_date": "2022-07-05",
"end_date": "2022-08-12"
},
{
"name": "Summer Sessions 2022 - Session E",
"start_date": "2022-07-25",
"end_date": "2022-08-12"
},
{
"name": "Summer Sessions 2022 - Session F",
"start_date": "2022-07-05",
"end_date": "2022-07-22"
},
{
"name": "Fall Semester 2022",
"start_date": "2022-08-17",
"end_date": "2022-12-16"
},
{
"name": "Spring Semester 2023",
"start_date": "2023-01-10",
"end_date": "2023-05-12"
},
{
"name": "Summer Sessions 2023 - Session A",
"start_date": "2023-05-22",
"end_date": "2023-06-30"
},
{
"name": "Summer Sessions 2023 - Session B",
"start_date": "2023-06-05",
"end_date": "2023-08-11"
},
{
"name": "Summer Sessions 2023 - Session C",
"start_date": "2023-06-20",
"end_date": "2023-08-11"
},
{
"name": "Summer Sessions 2023 - Session D",
"start_date": "2023-07-03",
"end_date": "2023-08-11"
},
{
"name": "Summer Sessions 2023 - Session E",
"start_date": "2023-07-24",
"end_date": "2023-08-11"
},
{
"name": "Summer Sessions 2023 - Session F",
"start_date": "2023-07-03",
"end_date": "2023-07-21"
}
]
)

relative_json_path = 'data/brc_allocation_periods.json'
if flag_enabled('LRC_ONLY'):
periods.extend(
[
{
"name": "Allowance Year 2021 - 2022",
"start_date": "2021-10-01",
"end_date": "2022-09-30"
},
{
"name": "Allowance Year 2022 - 2023",
"start_date": "2022-10-01",
"end_date": "2023-09-30"
}
]
)
relative_json_path = 'data/lrc_allocation_periods.json'

if relative_json_path is not None:
absolute_json_path = os.path.join(
os.path.dirname(__file__), relative_json_path)
with open(absolute_json_path, 'r') as f:
period_list = json.load(f)
periods.extend(period_list)

return periods
Loading

0 comments on commit 273f09e

Please sign in to comment.