This project simulates the control of Mars Rovers based on NASA specifications. It allows you to navigate rovers on a plateau on Mars, providing a simple interface to move and rotate the rovers.
- Prerequisites
- Installation
- Usage
- Configuration
- Development
- Docker Support
- CI/CD and Infrastructure
- Project Structure
- Contributing
- License
-
Clone the repository:
git clone https://github.com/barclayd/mars-rover.git cd mars-rover
-
Install dependencies:
bun install
To run the Mars Rover simulation in production mode:
bun start
This will output the final positions of the rovers to the file specified in the DEFAULT_OUTPUT_FILE_PATH
environment variable, upon success.
The DEFAULT_OUTPUT_FILE_PATH
is deleted if it exists before the application starts to ensure an accurate output for every run.
To run the Mars Rover simulation in development mode:
bun dev
This will output a representation of the plateau to the console:
(0,5) | (1,5) | (2,5) | (3,5) | (4,5) | (5,5) |
---|---|---|---|---|---|
(0,4) | (1,4) | (2,4) | (3,4) | (4,4) | (5,4) |
(0,3) | N | (2,3) | (3,3) | (4,3) | (5,3) |
(0,2) | (1,2) | (2,2) | (3,2) | (4,2) | (5,2) |
(0,1) | (1,1) | (2,1) | (3,1) | (4,1) | E |
(0,0) | (1,0) | (2,0) | (3,0) | (4,0) | (5,0) |
This can be used to visualize the plateau and the rovers' positions and for debugging purposes.
The input file should follow this format:
5 5 # Plateau size (width height)
1 2 N # Rover 1 initial position (x y direction)
LMLMLMLMM # Rover 1 movement instructions
3 3 E # Rover 2 initial position
MMRMMRMRRM # Rover 2 movement instructions
Valid directions are: N (North), E (East), S (South), W (West) Valid instructions are: L (turn left), R (turn right), M (move forward)
The application accepts the following command line arguments:
filePath
: The path to the input fileisDev
: A boolean flag to indicate if the application is running in development mode
The application uses the following environment variables:
OUTPUT_FILE_PATH
: The path to the output fileDEFAULT_INPUT_FILE_PATH
: The path to the default input file
This project uses Bun's built-in test runner for unit and integration testing.
To run all tests:
bun test
This project uses Biome for linting and formatting. Biome is a fast, modern linter and formatter for JavaScript and TypeScript projects.
To run the linter:
bun lint
This project includes Docker support for containerized deployment. The provided Dockerfile uses the official Bun Docker image as a base.
To build the Docker image locally:
docker build -t mars-rover .
This project uses GitHub Actions for continuous integration and deployment, along with Terraform for infrastructure as code. The pipeline automatically builds, tests, and deploys the application to Google Cloud Run.
The CI/CD workflow consists of the following stages:
-
Build & Test
- Runs on every push and pull request
- Installs dependencies
- Runs linting checks
- Executes test suite
-
Deploy
- Triggers on merges to main branch
- Authenticates with Google Cloud
- Builds and pushes container to Artifact Registry
- Deploys to Cloud Run
The workflow is defined in .github/workflows/ci.yaml
.
The project includes an automated deployment process configured in deploy/main.tf
. A Cloud Build trigger is set up to automatically run whenever code is pushed to the main branch. Specifically:
- The GitHub trigger monitors the main branch for any pushes
- When code is pushed, it automatically triggers Cloud Build using the
cloudbuild.yaml
configuration - Cloud Build executes the following steps:
- Builds a new Docker container from the latest code
- Pushes the container to Artifact Registry
- Updates the Cloud Run service with the new container
- The Cloud Run service is then automatically updated to run the latest version of the code
This creates a seamless deployment pipeline where code changes pushed to main are automatically built, tested, and deployed without manual intervention. The Cloud Run service URL remains constant while the underlying container is updated.
The infrastructure is managed using Terraform, with configurations stored in the terraform/
directory:
terraform/main.tf
: Core infrastructure componentsterraform/variables.tf
: Variable definitionsterraform/outputs.tf
: Output configurationsterraform/providers.tf
: Provider configurations
Key infrastructure components include:
- Google Cloud Run service
- Artifact Registry repository
- IAM roles and permissions
- Cloud Build triggers
To work with the infrastructure:
- Install Terraform
- Configure Google Cloud credentials:
gcloud auth application-default login
- Initialize Terraform:
cd terraform terraform init
The deployment process is automated through GitHub Actions, but can also be performed manually:
-
Build and tag the Docker image:
docker build -t gcr.io/[PROJECT_ID]/mars-rover .
-
Push to Artifact Registry:
docker push gcr.io/[PROJECT_ID]/mars-rover
-
Deploy to Cloud Run:
gcloud run deploy mars-rover \ --image gcr.io/[PROJECT_ID]/mars-rover \ --platform managed \ --region [REGION]
The application is currently deployed and accessible at https://mars-rover-282376634786.europe-west1.run.app/.
The following improvements are planned for future releases:
- Integration with Google Cloud Storage (GCS) to persist rover movement outputs and enable historical tracking
- Enhanced file input capabilities allowing users to:
- Upload custom input files through a web interface
- Select from a library of predefined rover movement patterns
- Save and share rover movement configurations
The following environment variables are required for CI/CD:
GCP_PROJECT_ID
: Google Cloud project IDGCP_SA_KEY
: Service account key for GCP authenticationTF_VAR_project_id
: Project ID for TerraformDOCKER_USERNAME
: Docker registry usernameDOCKER_PASSWORD
: Docker registry password
These should be configured as GitHub repository secrets.
The project is organized as follows:
src/
: Contains the main source codeindex.ts
: Entry point of the applicationplateau.ts
: Defines the plateau and related functionsrover.ts
: Implements rover movement and placement logicutils.ts
: Utility functions for file reading and parsinghelpers/
: Helper functions for output formattingschemas/
: Zod schemas for input validationtypes/
: TypeScript type definitions
test/
: Contains test filesmanual/
: Manual test dataintegration/
: Integration tests for the application
biome.json
: Configuration file for Biome (linter and formatter)package.json
: Project metadata and dependenciestsconfig.json
: TypeScript configurationLICENSE
: MIT License file
This structure separates concerns, making the codebase modular and easy to navigate. The src
directory contains the core logic, while test
houses both manual test data and automated unit tests.
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a new branch (
git checkout -b feature/improvement
) - Make your changes
- Run tests to ensure everything works (
bun test
) - Run the linter (
bun lint
) - Commit your changes (
git commit -am 'Add new feature'
) - Push to the branch (
git push origin feature/improvement
) - Create a Pull Request
Please make sure your PR:
- Includes tests for new functionality
- Passes all existing tests
- Follows the project's code style (using Biome)
- Includes appropriate documentation updates
- Install Bun if you haven't already
- Clone your fork of the repository
- Install dependencies:
bun install
- Start developing!
For major changes, please open an issue first to discuss what you would like to change.