Skip to content

🤖 AI-powered CLI tool that automatically generates, builds, and tests Dockerfiles for scripts using LLMs

Notifications You must be signed in to change notification settings

nitzan-madar/jit-ai-script-wrapper

Repository files navigation

🤖 nitzanScriptWrap

AI-powered CLI tool that automatically generates, builds, and tests Dockerfiles for existing scripts using LLM providers (Default: OpenAI's GPT-4o-mini).

This project creates a generic tool that can wrap any script with Docker containers, automatically build and test them to ensure they work correctly.


📁 Project Structure

nitzanScriptWrap/
├── scripts/                          # Example scripts with documentation
│   ├── line_counter/
│   │   ├── line_counter.sh          # Bash script example
│   │   └── README.md                # Usage documentation
│   └── vowel_counter/
│       ├── vowel_counter.js         # Node.js script example
│       └── README.md                # Usage documentation
├── src/                             # Source code
│   ├── cli.ts                       # Main CLI interface
│   ├── config.ts                    # Generic LLM configuration
│   ├── llm-factory.ts              # LLM provider factory
│   ├── providers/                   # LLM provider implementations
│   ├── docker.ts                    # Docker operations
│   ├── security.ts                  # Input sanitization
│   └── test/                        # Test suite
├── .env.example                     # Environment configuration template
├── package.json                     # Dependencies and scripts
└── README.md                        # This file

🔧 Advanced Features# 🔧 ** project creates a generic tool that can wrap any script with Docker containers, automatically build and test them to ensure they work correctly.


  • ✅ AI Integration: Uses OpenAI GPT-4o-mini with optimized prompts
  • ✅ Generic Script Wrapper: Works with any script type (bash, Node.js, Python, etc.)
  • ✅ Dockerfile Generation: Creates production-ready Dockerfiles automatically
  • ✅ Build & Test Pipeline: Builds Docker images and tests execution with examples
  • ✅ Output Verification: Validates containerized scripts produce expected results
  • ✅ LLM Vendor-Agnostic: Extensible architecture for multiple AI providers
  • ✅ Comprehensive Testing: Automated test suite for the wrapping process
  • ✅ Security & Sanitization: Input validation and prompt injection protection

🚀 Quick Start

Prerequisites

  • Node.js 20+
  • Docker (installed and running)
  • LLM API Key (OpenAI, Anthropic, or local model)

Installation

# Clone or extract the project
cd nitzanScriptWrap

# Install dependencies
npm install

# Setup environment
cp .env.example .env
# Edit .env and configure your LLM provider:

Environment Configuration

# Generic LLM Configuration (Recommended)
LLM_API_KEY=your-api-key-here
LLM_PROVIDER=openai                # openai, anthropic, local
LLM_MODEL=gpt-4o-mini             # Provider-specific model
LLM_TEMPERATURE=0.1               # Creativity level (0-2)
LLM_MAX_TOKENS=2000              # Response length limit

# Legacy support (backward compatible)
OPENAI_API_KEY=your-openai-key-here

Basic Usage

# Full workflow (default) - Generate, Build, Test, Verify
npm start <script-path> <readme-path>

# Example with provided test files
npm start ./scripts/vowel_counter/vowel_counter.js ./scripts/vowel_counter/README.md
npm start ./scripts/line_counter/line_counter.sh ./scripts/line_counter/README.md

📋 Complete Usage Guide

Mode 1: Full Workflow (Default)

Generates Dockerfile, builds image, runs container, and verifies output:

npm start ./scripts/vowel_counter/vowel_counter.js ./scripts/vowel_counter/README.md

What happens:

  1. 🔒 Security scan of inputs
  2. 🤖 AI generates optimized Dockerfile
  3. 🔨 Builds Docker image
  4. 🚀 Runs container with example from README
  5. ✅ Verifies output matches expected result
  6. 📄 Outputs final Dockerfile

Mode 2: Custom Example & Verification

Specify your own example command and expected output:

npm start ./script.py ./README.md "python script.py test" "Expected output"

Mode 3: Generate Dockerfile Only

Just generate the Dockerfile without building/testing:

npm start ./script.sh ./README.md --generate-only

Mode 4: Configuration Override

Override LLM settings per command:

# Use higher creativity for more varied Dockerfiles
LLM_TEMPERATURE=0.8 npm start ./script.py ./README.md

# Use different model (if available)
LLM_MODEL=gpt-4 npm start ./script.py ./README.md

# Use different provider (when implemented)
LLM_PROVIDER=anthropic LLM_API_KEY=your-anthropic-key npm start ./script.py ./README.md

Security Bypass (Not Recommended)

Override security warnings:

npm start ./script.sh ./README.md --force

🧪 Testing

Run Test Suite

npm test

Test with Provided Examples

# Test Node.js script
npm start ./scripts/vowel_counter/vowel_counter.js ./scripts/vowel_counter/README.md

# Test Bash script  
npm start ./scripts/line_counter/line_counter.sh ./scripts/line_counter/README.md

Expected Test Results

  • ✅ Security scan passes
  • ✅ Dockerfile generated with appropriate base image
  • ✅ Docker image builds successfully
  • ✅ Container runs and produces expected output
  • ✅ Output verification passes

Usage Examples

Basic Workflow Examples

# Node.js script with full workflow
npm start ./scripts/vowel_counter/vowel_counter.js ./scripts/vowel_counter/README.md
# Output: Dockerfile + build + test + verify

# Bash script with full workflow  
npm start ./scripts/line_counter/line_counter.sh ./scripts/line_counter/README.md
# Output: Dockerfile + build + test + verify

# Python script (if you have one)
npm start ./my_script.py ./README.md
# Auto-detects Python and uses appropriate base image

Configuration Examples

# Default configuration (from .env file)
npm start ./script.sh ./README.md

# Override temperature for more creative Dockerfiles
LLM_TEMPERATURE=0.8 npm start ./script.sh ./README.md

# Use maximum creativity
LLM_TEMPERATURE=2.0 npm start ./script.sh ./README.md

# Override model (if you have access)
LLM_MODEL=gpt-4 npm start ./script.sh ./README.md

# Combine multiple overrides
LLM_MODEL=gpt-4 LLM_TEMPERATURE=0.5 npm start ./script.sh ./README.md

Output Examples

Command:

npm start ./scripts/line_counter/line_counter.sh ./scripts/line_counter/README.md

Output:

🤖 Using openai (gpt-4o-mini) with temperature 0.1
🔒 Performing security scan...
✅ Security scan passed
Building & Testing: line_counter.sh
Using example: ./scripts/line_counter/line_counter.sh 'Hello\nWorld'
Container command: /line_counter.sh Hello\nWorld
---
✅ Dockerfile generated successfully
🔨 Building Docker image...
✅ Docker image built successfully
🚀 Running container with example...
📤 Container output:
stdout: Line Count: 2
stderr: 
exit code: 0
✅ Container executed successfully
🎉 All tests passed! Dockerfile is working correctly.

FROM bash:latest
COPY line_counter.sh /line_counter.sh
RUN chmod +x /line_counter.sh
ENTRYPOINT ["bash", "/line_counter.sh"]
CMD ["Hello\nWorld\nTest"]

�🔧 Advanced Features

Generic LLM Provider System

The tool uses a vendor-agnostic configuration system that works with any LLM provider:

# OpenAI Configuration
LLM_PROVIDER=openai
LLM_MODEL=gpt-4o-mini                    # or gpt-4, gpt-3.5-turbo
LLM_API_KEY=sk-proj-your-openai-key

# Anthropic Configuration (template - implementation pending)
LLM_PROVIDER=anthropic
LLM_MODEL=claude-3-haiku-20240307        # or claude-3-sonnet-20240229
LLM_API_KEY=your-anthropic-key

# Local Model Configuration (template - implementation pending)
LLM_PROVIDER=local
LLM_MODEL=llama3.1
LLM_BASE_URL=http://localhost:11434

Runtime Configuration Display

The tool shows current configuration at startup:

🤖 Using openai (gpt-4o-mini) with temperature 0.1

Security Features

  • Script Analysis: Detects dangerous commands
  • Prompt Injection Protection: Prevents AI manipulation
  • Path Validation: Ensures safe file access
  • Input Sanitization: Cleans all user inputs

Supported Script Types

  • Bash/Shell: .sh files with #!/bin/bash
  • Node.js: .js files (automatically uses node:alpine)
  • Python: .py files (uses appropriate Python base)
  • Any Script: Generic support for any executable script

📊 Example Outputs

Bash Script Dockerfile

FROM bash:latest
COPY line_counter.sh /line_counter.sh
RUN chmod +x /line_counter.sh
ENTRYPOINT ["bash", "/line_counter.sh"]
CMD ["Hello\nWorld"]

Node.js Script Dockerfile

FROM node:alpine
COPY vowel_counter.js /vowel_counter.js
RUN chmod +x /vowel_counter.js
ENTRYPOINT ["node", "/vowel_counter.js"]
CMD ["Hello world"]

🐛 Troubleshooting

Common Issues

"LLM configuration is invalid" or "API key not found"

# Set up generic configuration (recommended)
echo "LLM_API_KEY=your-api-key-here" >> .env
echo "LLM_PROVIDER=openai" >> .env
echo "LLM_MODEL=gpt-4o-mini" >> .env

# Or use legacy configuration
echo "OPENAI_API_KEY=your-key-here" >> .env

"Unknown provider: anthropic"

# Anthropic is not fully implemented yet
# Use OpenAI provider instead
LLM_PROVIDER=openai npm start ./script.sh ./README.md

"Docker command not found"

# Install Docker and ensure it's running
docker --version

"Security scan failed"

# Use --force to bypass (not recommended)
npm start ./script.sh ./README.md --force

"Module not found"

# Reinstall dependencies
rm -rf node_modules package-lock.json
npm install

🏆 Assignment Achievement Summary

✅ Core Requirements Delivered

  1. Generic AI-Powered Tool: ✅ Works with any script type
  2. Dockerfile Generation: ✅ Smart, optimized containers
  3. Build & Test Pipeline: ✅ Fully automated workflow
  4. Output Verification: ✅ Ensures correctness
  5. Budget Optimization: ✅ Uses cost-effective GPT-4o-mini

🎁 Bonus Features Delivered

  1. LLM Vendor-Agnostic: ✅ Extensible provider system
  2. Comprehensive Testing: ✅ Automated test suite
  3. Security Features: ✅ Input validation & prompt injection protection

📈 Additional Value

  • Production Ready: Clean, documented, maintainable code
  • Developer Friendly: Clear usage examples and error messages
  • Extensible: Easy to add new script types and providers
  • Secure: Built-in protection against malicious inputs

🤝 Contributing

This project demonstrates advanced AI-powered DevOps automation. The architecture is designed to be:

  • Extensible: Easy to add new LLM providers
  • Maintainable: Clean separation of concerns
  • Testable: Comprehensive test coverage
  • Secure: Built-in safety measures

🎉 Ready to Use!

The project is production-ready and successfully demonstrates how AI can automate script containerization while maintaining security, reliability, and cost-effectiveness.

Try it now:

npm start ./scripts/vowel_counter/vowel_counter.js ./scripts/vowel_counter/README.md

About

🤖 AI-powered CLI tool that automatically generates, builds, and tests Dockerfiles for scripts using LLMs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published