Gmail2PubSub is an application that allows monitoring a user's emails via the Gmail API and publishing extracted information (e.g., client appointments) to Google Cloud Pub/Sub. This project supports two types of authentication: OAuth2 authentication for accessing a user's emails, and service account authentication for server-to-server interactions (Pub/Sub). The application is designed to be deployed on Kubernetes, making it easy to manage and scale.
Here is an overview of the important files and folders in the project:
.
├── gmail2pubsub/
│ ├── __init__.py # Package initialization file
│ ├── auth.py # OAuth2 authentication for the Gmail API
│ ├── email_parser.py # Extracts information from emails
│ ├── gmail_manager.py # Manages interactions with the Gmail API (labels, messages, etc.)
│ ├── main.py # Main script to orchestrate actions (configure watch, listen to Pub/Sub notifications, and publish information)
│ ├── main_watch.py # Handles received Pub/Sub messages and processes related emails (callback for Pub/Sub events)
│ ├── pubsub_manager.py # Manages publishing information to Pub/Sub
│ ├── utils.py # Utility functions
│ ├── watch.py # Configures push notifications on the Gmail API
├── config/
│ ├── settings.py # Global project configuration (file paths, constants)
├── secrets/ # Directory for secret files (service-account.json, token.json, credentials.json)
├── tests/ # Unit tests for the project
│ ├── test_auth.py # Tests for auth.py
│ ├── test_email_parser.py # Tests for email_parser.py
│ ├── test_gmail_manager.py # Tests for gmail_manager.py
│ ├── test_main.py # Tests for main.py
│ ├── test_pubsub_manager.py # Tests for pubsub_manager.py
│ ├── test_utils.py # Tests for utils.py
│ ├── test_watch.py # Tests for watch.py
├── Dockerfile # Configuration file for building the Docker image
├── deployment.yaml # Configuration file for Kubernetes deployment
├── requirements.txt # List of Python dependencies for the project
├── README.md # Project documentation- Python 3.12
- Google Cloud SDK
- Docker
- Kubernetes
-
Clone the repository:
git clone https://github.com/yourusername/Gmail2PubSub.git cd Gmail2PubSub -
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate -
Install the required dependencies:
pip install -r requirements.txt
-
Create a
secretsdirectory and place yourservice-account.json,token.json, andcredentials.jsonfiles inside it. -
Update the environment variables in the
Dockerfileanddeployment.yamlas needed. -
Create Kubernetes secrets:
kubectl create namespace dev kubectl create secret generic gmail-service-account \ --from-file=service-account.json=secrets/service-account.json \ --namespace=dev kubectl create secret generic gmail-token \ --from-file=token.json=secrets/token.json \ --namespace=dev kubectl create secret generic gmail-credentials \ --from-file=credentials.json=secrets/credentials.json \ --namespace=dev
The application supports the following command-line arguments:
--watch: Configures Gmail to watch for new emails.--listen: Listens to Pub/Sub and processes incoming messages.--reset-cache: Resets the cache of thehistory_id.
Example usage:
python -m gmail2pubsub.main --watch
python -m gmail2pubsub.main --listen
python -m gmail2pubsub.main --reset-cache-
Activate the virtual environment:
source venv/bin/activate -
Run the application:
python -m gmail2pubsub.main --watch
-
Build the Docker image:
docker build -t gmail2pubsub-app . -
Push the Docker Image to a Registry:
docker tag gmail2pubsub-app your-dockerhub-username/gmail2pubsub-app docker push your-dockerhub-username/gmail2pubsub-app
-
Run the Docker container in the background:
docker run -d \ -e PROJECT_ID="smshttp-436212" \ -e SUBSCRIPTION_ID="gmail-getmessages" \ -e GMAIL_TOPIC="GmailTopic" \ -e NEW_RDV_TOPIC="NewRdvTopic" \ -e LABEL_NAME="RESALIB" \ -v $(pwd)/secrets/service-account.json:/run/secrets/service-account.json \ -v $(pwd)/secrets/token.json:/run/secrets/token.json \ -v $(pwd)/secrets/credentials.json:/run/secrets/credentials.json \ --name gmail2pubsub-container gmail2pubsub-app python -m gmail2pubsub.main --listen
-
Run the Docker container interactively:
docker run -it \ -e PROJECT_ID="smshttp-436212" \ -e SUBSCRIPTION_ID="gmail-getmessages" \ -e GMAIL_TOPIC="GmailTopic" \ -e NEW_RDV_TOPIC="NewRdvTopic" \ -e LABEL_NAME="RESALIB" \ -v $(pwd)/secrets/service-account.json:/run/secrets/service-account.json \ -v $(pwd)/secrets/token.json:/run/secrets/token.json \ -v $(pwd)/secrets/credentials.json:/run/secrets/credentials.json \ --name gmail2pubsub-container gmail2pubsub-app python -m gmail2pubsub.main --listen
-
To remove the Docker container:
docker rm -f gmail2pubsub-container
- Apply the ConfigMap and Deployment:
kubectl apply -f configmap.yaml -n dev kubectl apply -f deployment.yaml -n dev kubectl get deployments -n dev
-
Install
sqlite3:sudo apt-get install sqlite3
-
Run the tests:
pytest tests/
This project is licensed under the MIT License.