npm package: @john.klaumann/devops-deploy-cli
Description: CLI tool to generate Dockerfile, Terraform and deployment scripts for .NET microservices
Author: Your Name
Latest version: 1.1.0

# DevOps Deploy CLI

A CLI tool to automate the generation of deployment files for .NET microservices, eliminating dependency on DevOps team for basic deployment tasks.

## 🚀 Features

- **Automatic Dockerfile**: Generates optimized Dockerfile based on your .NET project
- **Complete Terraform**: Creates full configuration for AWS ECR + Kubernetes
- **Deploy scripts**: Ready-to-use bash scripts for AWS deployment
- **Auto-discovery**: Automatically finds .sln and launchSettings.json files
- **Multi-environment**: Support for dev, staging and production

## 📦 Installation

### Option 1: Global Installation (Recommended)

```bash
npm install -g @john.klaumann/devops-deploy-cli
```

### Option 2: Use with npx (No installation)

```bash
npx @john.klaumann/devops-deploy-cli generate --all
```

### Option 3: Install in Project

```bash
npm install @john.klaumann/devops-deploy-cli --save-dev
```

## 🛠️ Basic Usage

### 1. Generate all deployment files

```bash
# In your .NET project directory
devops-deploy generate --all
```

This will generate:
- `Dockerfile`
- `main.tf`, `variables.tf`, `outputs.tf`
- `terraform.tfvars.example`
- `deploy.sh`, `quick-deploy.sh`, `rollback.sh`, `status.sh`
- `README.md` with instructions

### 2. Generate only Dockerfile

```bash
devops-deploy generate --docker
```

### 3. Generate only Terraform files

```bash
devops-deploy generate --terraform
```

### 4. Generate only deployment scripts

```bash
devops-deploy generate --deploy
```

## 🎯 Available Commands

### `generate`

Generates deployment files based on your .NET project.

```bash
devops-deploy generate [options]
```

**Options:**
- `--docker`: Generate only Dockerfile
- `--terraform`: Generate only Terraform files
- `--deploy`: Generate only deployment scripts
- `--all`: Generate all files (default if no option specified)
- `--solution <path>`: Path to solution file (.sln) (auto-detected if not specified)
- `--settings <path>`: Path to launchSettings.json (auto-detected if not specified)
- `--output <path>`: Output directory (default: ./deploy-output)

**Examples:**
```bash
# Generate everything in current directory
devops-deploy generate --all

# Generate only Dockerfile in specific directory
devops-deploy generate --docker --output ./docker-files

# Specify files manually
devops-deploy generate --all --solution ./MySolution.sln --settings ./Properties/launchSettings.json
```

### `init`

Initialize project configuration with interactive wizard.

```bash
devops-deploy init
```

This will create a `devops-config.json` file with your project settings.

## 📁 Generated Files Structure

```
deploy-output/
├── Dockerfile                    # Docker multi-stage build
├── main.tf                      # Terraform main configuration
├── variables.tf                 # Terraform variables
├── outputs.tf                   # Terraform outputs
├── terraform.tfvars.example     # Example variables file
├── deploy.sh                    # Full deployment script
├── quick-deploy.sh              # Quick update script
├── rollback.sh                  # Rollback script
├── status.sh                    # Status check script
└── README.md                    # Deployment guide
```

## 🐳 Using the Generated Dockerfile

The generated Dockerfile is optimized for .NET with:
- Multi-stage build
- Non-root user for security
- Configured health check
- Optimized dependency caching
- Environment variables from launchSettings.json

```bash
# Build image
docker build -t my-microservice .

# Local run for testing
docker run -p 8080:8080 my-microservice
```

## ☁️ Deploy with Terraform

1. **Configure variables:**
   ```bash
   cp terraform.tfvars.example terraform.tfvars
   # Edit terraform.tfvars with your values
   ```

2. **Run Terraform:**
   ```bash
   terraform init
   terraform plan
   terraform apply
   ```

This will create:
- ECR Repository on AWS
- Kubernetes Deployment
- Kubernetes Service
- ConfigMaps and Secrets
- Horizontal Pod Autoscaler
- Health checks

## 🚀 Deploy with Bash Scripts

### Full Deploy (First time)

```bash
chmod +x deploy.sh
./deploy.sh
```

The script will:
1. Check prerequisites (AWS CLI, Docker, kubectl)
2. Create ECR repository if it doesn't exist
3. Build and push image
4. Apply Terraform configurations
5. Check deployment status

### Quick Update

For updates after code changes:

```bash
./quick-deploy.sh
```

### Check Status

```bash
./status.sh
```

### Rollback

If something goes wrong:

```bash
./rollback.sh
```

## 🔧 Advanced Configuration

### Environment Variables

You can configure script behavior:

```bash
export AWS_REGION="us-west-2"
export IMAGE_TAG="v1.2.3"
export KUBERNETES_NAMESPACE="my-namespace"
export ENVIRONMENT="production"

./deploy.sh
```

### Dockerfile Customization

If you need to customize the Dockerfile:

1. Generate base file:
   ```bash
   devops-deploy generate --docker
   ```

2. Edit the generated `Dockerfile` as needed

3. Use deployment scripts normally

### Custom Terraform Configuration

Most important Terraform variables:

```hcl
# terraform.tfvars
aws_region = "us-east-1"
environment = "production"
replicas = 3
cpu_limit = "1000m"
memory_limit = "1Gi"
auth_client_id = "your-client-id"
auth_client_secret = "your-client-secret"
```

## 🛡️ Security and Best Practices

The tool follows security best practices:

- **Dockerfile**: Non-root user, multi-stage build
- **Kubernetes**: Resource limits, health checks, secrets for credentials
- **AWS**: Restricted IAM policies, ECR image scanning
- **Variables**: Secrets separated from configs

## 🔍 Troubleshooting

### Problem: "No solution file found"

**Solution**: Specify path manually:
```bash
devops-deploy generate --solution ./path/to/your.sln --all
```

### Problem: "launchSettings.json not found"

**Solution**: Check if file exists in `Properties/launchSettings.json` or specify:
```bash
devops-deploy generate --settings ./Properties/launchSettings.json --all
```

### Problem: Deploy fails with AWS authentication error

**Solution**: Configure AWS CLI:
```bash
aws configure
# or use environment variables
export AWS_ACCESS_KEY_ID="..."
export AWS_SECRET_ACCESS_KEY="..."
```

### Problem: kubectl cannot connect

**Solution**: Configure kubeconfig:
```bash
aws eks update-kubeconfig --region us-east-1 --name my-cluster
```

## 📋 Complete Example

Let's say you have a project called "FlowchartService":

```bash
# 1. In project directory
cd FlowchartService

# 2. Generate deployment files
devops-deploy generate --all

# 3. Review generated files
ls deploy-output/

# 4. Configure Terraform
cp deploy-output/terraform.tfvars.example deploy-output/terraform.tfvars
# Edit terraform.tfvars with your credentials

# 5. Deploy
cd deploy-output
chmod +x *.sh
./deploy.sh

# 6. Check if it's working
./status.sh
```

## 🤝 Contributing

To improve the tool:

1. Clone the repository
2. Install dependencies: `npm install`
3. Make your changes
4. Test: `npm test`
5. Open a Pull Request

## 📄 License

MIT License - see LICENSE file for details.

---

**Tip**: Add the `deploy-output/` directory to your `.gitignore` if you don't want to version the generated files, or version only the `Dockerfile` if needed.