# TBI ACC Hotel Mapping Service - DevOps Configuration

This directory contains all CI/CD and deployment configurations for the `tbi-acc-hotel-mapping-service`.

## 📁 Directory Structure

```
dev/tbi-acc-hotel-mapping-service/
├── .circleci/
│   └── config.yml              # CircleCI pipeline configuration
├── .github/
│   └── workflows/
│       └── manage-secrets.yml  # GitHub Actions secrets management
├── docker/
│   └── Dockerfile              # Docker image build configuration
├── scripts/
│   ├── setup-kubectl.sh        # Kubernetes setup script
│   └── deploy-services.sh      # Main deployment script
├── charts/
│   └── tbi-acc-hotel-mapping-service/
│       ├── Chart.yaml          # Helm chart metadata
│       ├── values.yaml         # Default values
│       └── templates/          # Kubernetes manifests
│           ├── deployment.yaml
│           ├── service.yaml
│           ├── secrets.yaml
│           ├── ingress.yaml
│           ├── hpa.yaml
│           └── pdb.yaml
└── README.md                   # This file
```

## 🚀 Setup Instructions

### 1. Service Repository Setup

In the **tbi-acc-hotel-mapping-service** repository (application code):

#### a. Create VERSION file
```bash
echo "1.0.0" > VERSION
git add VERSION
git commit -m "Add VERSION file for deployment"
```

#### b. Copy GitHub Actions workflow
Copy the workflow from this repo to your service repo:
```bash
# From tbi-devops-environments
cp dev/tbi-acc-hotel-mapping-service/.github/workflows/manage-secrets.yml \
   /path/to/tbi-acc-hotel-mapping-service/.github/workflows/

cd /path/to/tbi-acc-hotel-mapping-service
git add .github/workflows/manage-secrets.yml
git commit -m "Add GitHub Actions secrets management workflow"
git push origin master
```

#### c. Add GitHub Secrets
Go to: `https://github.com/redlabelvacations/tbi-acc-hotel-mapping-service/settings/secrets/actions`

Add these secrets:
- `TBI_GITHUB_TOKEN` - Organization-level PAT with access to tbi-environments

### 2. tbi-environments Repository Setup

In the **tbi-environments** repository:

#### a. Create values file
Create `dev/values/tbi-acc-hotel-mapping-service.yaml`:
```yaml
image: IMAGE_TO_REPLACE
name: tbi-acc-hotel-mapping-service
replicas: 1
service_type: "ClusterIP"
port: "80"
targetPort: "3000"
node_env: "development"
group: "redtag"

# Optional: Enable ingress
# ingress:
#   enabled: true
#   host: tbi-acc-hotel-mapping-service.dev.tbidevelopment.ca

# Optional: Enable autoscaling
# autoscaling:
#   enabled: true
#   minReplicas: 1
#   maxReplicas: 3
#   targetCPUUtilizationPercentage: 70
#   targetMemoryUtilizationPercentage: 80
```

#### b. Create secrets file
Create `dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml`:
```yaml
configs:
    dotenv: |-
        NODE_ENV=development
        DB_HOST=your-db-host
        DB_PORT=5432
        DB_USERNAME=your-username
        DB_PASSWORD=your-password
        DB_NAME=your-database
        REDIS_HOST=your-redis-host
        REDIS_PORT=6379
        AWS_REGION=ca-central-1
        # Add all your environment variables here
```

**CRITICAL**: Use the `|-` format with actual newlines (not `\n` literals)!

#### c. Encrypt secrets
```bash
cd tbi-environments/dev
sops -e -i secrets/secrets.tbi-acc-hotel-mapping-service.yaml
```

#### d. Add to requirements.yaml
Add to `dev/requirements.yaml`:
```yaml
- name: tbi-acc-hotel-mapping-service
  version: 1.0.0-develop-1
  repository: https://tbi-charts.s3.amazonaws.com
```

#### e. Commit changes
```bash
git add dev/values/tbi-acc-hotel-mapping-service.yaml
git add dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml
git add dev/requirements.yaml
git commit -m "Add configuration for tbi-acc-hotel-mapping-service"
git push origin master
```

### 3. CircleCI Setup

#### a. Configure CircleCI project
1. Go to CircleCI: https://app.circleci.com/
2. Add project: `tbi-acc-hotel-mapping-service`
3. Use existing config from repo

#### b. Verify CircleCI contexts
Ensure the `devops-mgt` context has these variables:
- `AWS_ACCOUNT_ID` - AWS account ID (594765360358 for dev)
- `AWS_REGION` - ca-central-1
- `CLUSTER_NAME` - dev
- `GITHUB_TOKEN` - GitHub PAT for accessing tbi-devops-environments and tbi-environments
- `DEPLOY_ENV` - dev

### 4. Test Deployment

#### a. Push to develop branch
```bash
cd /path/to/tbi-acc-hotel-mapping-service
git checkout develop
git push origin develop
```

This will trigger:
1. **Lint and Test** - Run linting, tests, and build validation
2. **Build and Push Images** - Build Docker image and push to ECR
3. **Deploy to Kubernetes** - Deploy to dev cluster in redtag namespace

#### b. Monitor deployment
- CircleCI: https://app.circleci.com/pipelines/github/redlabelvacations/tbi-acc-hotel-mapping-service
- Kubernetes:
  ```bash
  kubectl get pods -n redtag -l app=tbi-acc-hotel-mapping-service
  kubectl logs -n redtag deployment/tbi-acc-hotel-mapping-service --tail=50
  ```

## 🔐 Secrets Management

### View Secrets
1. Go to GitHub Actions in service repo
2. Run "Manage Secrets" workflow
3. Select:
   - Environment: `dev`
   - Action: `view-secrets`
   - View output: `github-summary`

### Update Secrets
1. Run "Manage Secrets" workflow
2. Select:
   - Environment: `dev`
   - Action: `update-and-restart`
   - Secret updates: `{"DB_PASSWORD":"new-password"}`

This will:
- Update secrets in tbi-environments
- Re-encrypt with SOPS
- Deploy to Kubernetes
- Restart pods

## 📊 Pipeline Flow

```
Push to develop
    ↓
Lint and Test (Node.js 20.4)
    ├── npm ci
    ├── npm run lint
    ├── npm run test
    └── npm run build
    ↓
Build and Push Images
    ├── Fetch Dockerfile from tbi-devops-environments
    ├── Build Docker image
    └── Push to ECR with tag: {VERSION}-develop-{BUILD_NUM}
    ↓
Deploy to Kubernetes
    ├── Package Helm chart
    ├── Push chart to S3
    ├── Update values file in tbi-environments
    ├── Update requirements.yaml
    ├── Commit changes
    └── Deploy using helm secrets
    ↓
Verify Deployment
    └── kubectl rollout status
```

## 🛠️ Customization

### Adjust Node.js Version
Edit `.circleci/config.yml`:
```yaml
lint-and-test:
  docker:
    - image: cimg/node:20.4  # Change version here
```

### Adjust Dockerfile
Edit `docker/Dockerfile` to match your application structure.

### Adjust Resource Limits
Edit `charts/tbi-acc-hotel-mapping-service/templates/deployment.yaml`:
```yaml
resources:
  requests:
    memory: "256Mi"
    cpu: "100m"
  limits:
    memory: "512Mi"
    cpu: "500m"
```

### Enable Ingress
Edit values file in tbi-environments:
```yaml
ingress:
  enabled: true
  host: tbi-acc-hotel-mapping-service.dev.tbidevelopment.ca
```

### Enable Autoscaling
Edit values file in tbi-environments:
```yaml
autoscaling:
  enabled: true
  minReplicas: 2
  maxReplicas: 5
  targetCPUUtilizationPercentage: 70
```

## 🐛 Troubleshooting

### Build Fails
```bash
# Check CircleCI logs
# Common issues:
# - Missing VERSION file
# - npm run build fails
# - Linting errors
```

### Deployment Fails
```bash
# Check pod status
kubectl get pods -n redtag -l app=tbi-acc-hotel-mapping-service

# Check pod logs
kubectl logs -n redtag deployment/tbi-acc-hotel-mapping-service --tail=100

# Check events
kubectl get events -n redtag --sort-by='.lastTimestamp' | grep tbi-acc-hotel-mapping-service
```

### Secrets Not Loading
```bash
# Verify secret exists
kubectl get secret tbi-acc-hotel-mapping-service-secrets -n redtag

# Check secret content
kubectl get secret tbi-acc-hotel-mapping-service-secrets -n redtag -o jsonpath='{.data.dotenv}' | base64 -d

# Verify format (should have actual newlines, not \n)
```

### "Nothing to commit" Error
This is normal if redeploying the same version. The deployment script handles this gracefully.

## 📚 Documentation

- [Developer Guide](../../docs/DEVELOPER-GUIDE.md)
- [Deployment Script Guide](../../docs/DEPLOYMENT-SCRIPT-GUIDE.md)
- [Secrets Pipeline Setup](../../docs/SECRETS-PIPELINE-SETUP.md)
- [Migration Checklist](../../docs/MIGRATION-CHECKLIST.md)

## 🔗 Related Repositories

- **Application Code**: https://github.com/redlabelvacations/tbi-acc-hotel-mapping-service
- **DevOps Configs**: https://github.com/redlabelvacations/tbi-devops-environments
- **Secrets & Values**: https://github.com/redlabelvacations/tbi-environments

## 📝 Notes

- **Namespace**: redtag
- **Environment**: dev
- **Cluster**: dev
- **AWS Region**: ca-central-1
- **AWS Account**: 594765360358
- **Image Repository**: ECR - tbi-acc-hotel-mapping-service

## ✅ Checklist

- [ ] VERSION file created in service repo
- [ ] GitHub Actions workflow added to service repo
- [ ] GitHub secrets configured
- [ ] Values file created in tbi-environments
- [ ] Secrets file created and encrypted in tbi-environments
- [ ] requirements.yaml updated
- [ ] CircleCI project configured
- [ ] First deployment successful
- [ ] Pods running in redtag namespace
- [ ] Secrets management workflow tested
