Skip to content

Commit

Permalink
Merge pull request #1023 from massenergize/main
Browse files Browse the repository at this point in the history
Release: Merge dev into canary
  • Loading branch information
archx3 authored May 30, 2024
2 parents ec7389f + 6ff80d3 commit fe500e0
Show file tree
Hide file tree
Showing 68 changed files with 1,970 additions and 384 deletions.
27 changes: 1 addition & 26 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,26 +1 @@
src/_main_/config/*json
**/**/__pycache__
**/__pycache__
*.pyc
timelog.csv
**/.DS_Store

src/*.png
src/*.jpg
src/*.jpeg

\.vscode/

*.env
env
src/Dockerrun.aws.json
.idea
src/api/tests/TestCases.json
src/api/store/ZIPCODE_FIXES.json
src/_main_/utils/GeoLite2-City/*
*GeoLite2-City.mmdb
src/celerybeat-schedule.db

src/~Downloads
*.coverage
src/.coverage
.vscode/
93 changes: 81 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,90 @@
# MassEnergize API
<p align="center">
<img width="100px" src="https://www.massenergize.org/wp-content/uploads/2023/03/MassEnergize-logo.png" align="center" alt="MassEnergize Logo" />
<h2 align="center">MassEnergize API</h2>
<i align="center">This repo contains the code for the massenergize API that powers all the massenergize websites and mobile apps.</p>
</i>
<p align="center">
<a href="https://github.com/massenergize/api/issues/new?assignees=&labels=API%2C+bug%2C+enhancement&projects=&template=bug_report.md&title=">Report Bug</a>
·
<a href="https://github.com/massenergize/api/issues/new?assignees=&labels=API%2C+enhancement&projects=&template=feature.md&title=%5BFeature%5D">Request Feature</a>
·
<a href="src/FAQs.md">FAQ</a>
·
<a href="https://github.com/massenergize/api/discussions/new?category=q-a">Ask Question</a>
</p>
</p>

## Maintainers
---

This repository is managed by
## Welcome to MassEnergize's API!

* [Samuel Opoku-Agyemang](http://samuelopokuagyemang.com])
* [Satrajit Ghosh](https://satra.cogitatum.org/)
* [Brad Hubbard-Nelson](http://www.hubbardnelson.org/)
🌍 **MassEnergize** is on a mission to tackle the climate emergency head-on! We work with community organizers and local leaders to supercharge household and community-level climate actions. Our open-source API is a crucial part of this mission, and we need YOU to join our team of eco-warriors!

## Contributors
**The Situation:** Mother Earth's throwing some serious shade our way (literally, with all this heat). We need a code-powered climate crusade!

The following individuals have made signifigant contributions to this repository, as [MassCEC interns](https://www.masscec.com/clean-energy-internship-program) or through other means
**The Backend Team's Mission:** We're like climate action superheroes, except instead of capes, we rock awesome APIs! We empower everyday people to fight climate change with household-level solutions. Why? Because a million tiny changes add up to a HUGE impact

* [Kieran O'Day](https://github.com/ki3ranoday)
* [Frimpong Opoku-Agyemang](https://github.com/frimpongopoku)
* [Josh Katofsky](https://www.linkedin.com/in/josh-katofsky/)
* [Derek Zheng](https://dereknzheng.com/)
# Table of Contents

- [Contribution]()

- [Why Contribute?](#contribution)
- [How to Contribute](#how-to-contribute)
- [Contribution Guidelines](#contribution-guidelines)

- [How to run the API](src/README.md)
- [Support Massenergize](https://massenergize.app.neoncrm.com/forms/donation)

### Why Contribute? <a id="contribution"></a>

- **Fight Climate Change**: Use your coding skills to help save the planet. How cool is that?
- **Empower Communities**: Your contributions will directly help communities adopt climate-friendly practices.
- **Be a Hero**: Gain recognition and respect in the tech and environmental communities.

### How to Contribute <a id="how-to-contribute"></a>

Ready to make a difference? Follow these simple steps to join our cause:

1. **Fork the Repository**: Click the 'Fork' button at the top of this page to create your own copy of this repository. Fork it like a pro!
2. **Clone the Repository**: Use `git clone` to bring the repository to your local machine. It’s like bringing the fight against climate change to your backyard.
3. **Create a New Branch**: Create a new branch with `git checkout -b your-branch-name`. Fun branch names encouraged!
4. **Make Your Changes**: Add features, fix bugs, write tests, and update documentation. Channel your inner eco-warrior.
5. **Commit Your Changes**: Use meaningful and fun commit messages. “Fixed bug” is good; “Squashed the carbon bug” is better.
6. **Push to GitHub**: Push your changes to your forked repository. Ready, set, push!
7. **Create a Pull Request**: Submit a pull request to the `main` branch of this repository. Tell us about your changes and link any relevant issues.

### Contribution Guidelines <a id="contribution-guidelines"></a>

To keep our mission on track, please follow these guidelines:

#### Do's

- **Write Clean, Green Code**: Follow our [coding standards](link-to-coding-standards) to keep your code efficient and effective.
- **Document Thoroughly**: Make sure your code is well-documented with clear comments and README updates.
- **Test Your Code**: All new features and bug fixes should include tests. Quality is key!
- **Be Respectful**: Engage with other contributors kindly. We’re all in this together!
- **Review Open PRs**: If you have the time, review open pull requests and provide constructive feedback. Every little bit helps!

#### Don'ts

- **Submit Unfinished Work**: Ensure your code is complete and tested before submitting a pull request.
- **Ignore Guidelines**: Stick to the project’s coding standards and contribution guidelines to ensure consistency.
- **Be a Naysayer**: Maintain a positive and supportive environment. Negativity won’t save the planet!

### Get in Touch

Need help or just want to chat with fellow eco-warriors? Here’s how you can reach us:

- **GitHub Issues**: [Open an issue](https://github.com/massenergize/api/issues/new?assignees=&labels=API%2C+bug%2C+enhancement&projects=&template=bug_report.md&title=) for questions, bugs, or suggestions.
<!-- - **Discord**: Join our [Discord community](link-to-discord) for real-time support, discussion, and climate memes. -->
- **Email**: Drop us a line at [massenergize mail](mailto:[email protected]) if you prefer traditional communication.

We’re excited to see your contributions and work together to create a greener, more resilient world! 🌱

Happy Coding and Keep it Green!

---

</p>

[![codecov](https://codecov.io/gh/massenergize/api/graph/badge.svg?token=KYFAUXOAP4)](https://codecov.io/gh/massenergize/api)
25 changes: 25 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 0.0
os: linux
files:
- source: src # copy all files in this folder
destination: /webapps/massenergize/api # and put them here in the server
hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/load_env.sh
timeout: 300
runas: root
- location: scripts/after_install.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/start_application.sh
timeout: 300
runas: root
ValidateService:
- location: scripts/validate_service.sh
timeout: 300
runas: root
49 changes: 49 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Overview of the different scripts

### BeforeInstall:
The BeforeInstall script runs before any application files are copied to the instances. This script is typically used for preparing the instances for the deployment, such as backing up data, stopping services, or performing any necessary pre-installation tasks.

Example:
```
hooks:
BeforeInstall:
- location: scripts/backup.sh
timeout: 300
runas: root
```

### AfterInstall:
The AfterInstall script runs after the application files have been copied to the instances but before the application starts. This script is commonly used for tasks like installing dependencies, running database migrations, or performing any necessary post-installation steps.

Example:
```
hooks:
AfterInstall:
- location: scripts/install_dependencies.sh
timeout: 600
runas: root
```

### ApplicationStart:
The ApplicationStart script runs after the AfterInstall script finishes and is responsible for starting or restarting the application or any required services. This script is specific to your application and should include the necessary commands to start the application.

Example:
```
hooks:
ApplicationStart:
- location: scripts/start_server.sh
timeout: 300
runas: root
```

### ValidationTest:
The ValidationTest script runs after the ApplicationStart script and is used to validate that the application deployment was successful. This script should include tests or checks to ensure the application is running correctly. If the validation fails, CodeDeploy will roll back the deployment.

Example:
```
hooks:
ValidationTest:
- location: scripts/run_tests.sh
timeout: 600
runas: root
```
13 changes: 13 additions & 0 deletions scripts/after_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

cd /webapps/massenergize/api

sudo docker-compose down
sudo docker-compose down --remove-orphans
sudo docker stop $(docker ps -q) && sudo docker rm $(docker ps -aq)
sudo service docker restart
sudo docker image prune -f
sudo docker builder prune -f

# Build the new Docker images
sudo docker-compose build
6 changes: 6 additions & 0 deletions scripts/before_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

while [ ! -f /webapps/server_is_ready ]; do
sleep 5
done

63 changes: 63 additions & 0 deletions scripts/load_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

cd /webapps/massenergize/api


# File to store environment variables
ENV_FILE=".env"

# Remove existing .env file if it exists
if [ -f "$ENV_FILE" ]; then
rm "$ENV_FILE"
fi

sudo touch $ENV_FILE

# Run AWS Secrets Manager command to get secret value and store it in a variable
source /etc/profile

secret_json=$(aws secretsmanager get-secret-value --secret-id "$SECRETS_ID" --query SecretString --output text --region "$SECRETS_TARGET_REGION")

# Check if the response is valid JSON
if [ $? -eq 0 ]; then
# Parse JSON and extract key-value pairs
env_lines=($(echo "$secret_json" | jq -r 'to_entries | .[] | select(.key != "FIREBASE_SERVICE_ACCOUNT_PRIVATE_KEY") | "\(.key)=\"\(.value | gsub("\"";"\"\""))\""'))
# Write to .env file
for line in "${env_lines[@]}"; do
echo "$line" >> .env
done

echo "DJANGO_ENV=$BUILD_ENV" >> .env
echo "DOCKER_MODE=true" >> .env
echo "HOST_IP_ADDRESSES=\"$(hostname -I)\"" >> .env
echo "Secrets successfully written to .env file."
else
echo "Error: Failed to retrieve secrets from AWS Secrets Manager."
fi

get_and_write_public_ip() {
token=$(curl -X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 300" -s http://169.254.169.254/latest/api/token)
public_ipv4=$(curl -H "X-aws-ec2-metadata-token: $token" -s http://169.254.169.254/latest/meta-data/public-ipv4)
public_ipv6=$(curl -H "X-aws-ec2-metadata-token: $token" -s http://169.254.169.254/latest/meta-data/public-ipv6)

if [ -z "$public_ipv4" ] && [ -z "$public_ipv6" ]; then
echo "Error: No public IP address assigned to the instance."
exit 0
fi

if [ ! -z "$public_ipv4" ] && [ ${#public_ipv4} -le 16 ]; then
echo "PUBLIC_IPV4=\"$public_ipv4\"" >> .env
echo "PUBLIC_IPV4 Address written to .env file: $public_ipv4"
fi

if [ ! -z "$public_ipv6" ] && [ ${#public_ipv6} -le 40 ]; then
echo "PUBLIC_IPV6=\"$public_ipv6\"" >> .env
echo "PUBLIC_IPV6 Address written to .env file: $public_ipv6"
fi

if [ -z "$public_ipv4" ] && [ -z "$public_ipv6" ]; then
echo "Error: No suitable public IP address(es) found."
fi
}

get_and_write_public_ip
9 changes: 9 additions & 0 deletions scripts/start_application.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

cd /webapps/massenergize/api

# Start the new Docker containers
sudo docker-compose up -d

# Wait for the containers to be ready
sleep 30
14 changes: 14 additions & 0 deletions scripts/validate_service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Check if the Django API is running and responding
STATUS_CODE=$(curl --write-out %{http_code} --silent --output /dev/null http://localhost:8000/health_check)
echo $STATUS_CODE

if [ "$STATUS_CODE" -eq 200 ]; then
echo "Deployment successful"
exit 0 # Deployment successful
else
cd /webapps/massenergize/api
echo "Deployment failed"
exit 1 # Deployment failed
fi
22 changes: 14 additions & 8 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
.env
dev.env
prod.env
.ebextensions/securelistener-clb.config
.elasticbeanstalk
.ebextensions
prepare-for-deploy.py
deployment/aws/
*.sqlite3*
test_data/
celerybeat-schedule.db
*.db
*.rdb*
*.venv
venv
.hide
.logs
__pycache__
.coverage
Dockerrun.aws.json
*.env
passport.key.json
_main_/config/*json
.vscode
.massenergize
settings.json
4 changes: 2 additions & 2 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM python:3.8.8
FROM python:3.9.19
ENV PYTHONUNBUFFERED 1
RUN mkdir /src

WORKDIR /src
COPY . /src
RUN pip install -r requirements.txt
RUN make init
13 changes: 13 additions & 0 deletions src/FAQs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<p align="center">
<img width="100px" src="https://www.massenergize.org/wp-content/uploads/2023/03/MassEnergize-logo.png" align="center" alt="MassEnergize Logo" />
<h2 align="center">Masenergize API </h2>
</i>
<p align="center">
<a href="./README.md">How to set up the API</a>
·
</p>
</p>

---

<h2 align="center"> Frequently Asked Questions (FAQs)</h2>
Loading

0 comments on commit fe500e0

Please sign in to comment.