# Setup Summary for tbi-acc-hotel-mapping-service

## ✅ What Has Been Created in tbi-devops-environments

All CI/CD configuration files have been created in `dev/tbi-acc-hotel-mapping-service/`:

### 1. CircleCI Pipeline (`.circleci/config.yml`)
- Lint and test job (Node.js 20.4)
- Build and push Docker images to ECR
- Deploy to Kubernetes using Helm
- Configured for `develop` branch → dev environment

### 2. Docker Configuration (`docker/Dockerfile`)
- Multi-stage build (build + production)
- Node.js 20 base image
- Optimized for production with pruned dependencies

### 3. Deployment Scripts (`scripts/`)
- `setup-kubectl.sh` - Sets up kubectl, helm-secrets, SOPS
- `deploy-services.sh` - Main deployment script with:
  - Git commit check (prevents "nothing to commit" errors)
  - requirements.yaml updates
  - Proper error handling
  - All critical fixes from documentation

### 4. Helm Chart (`charts/tbi-acc-hotel-mapping-service/`)
- `Chart.yaml` - Chart metadata
- `values.yaml` - Default configuration
- `templates/` - Kubernetes manifests:
  - `deployment.yaml` - Pod deployment with resource limits
  - `service.yaml` - ClusterIP service
  - `secrets.yaml` - Secrets management
  - `ingress.yaml` - Optional ingress (disabled by default)
  - `hpa.yaml` - Optional horizontal pod autoscaler
  - `pdb.yaml` - Optional pod disruption budget

### 5. GitHub Actions Workflow (`.github/workflows/manage-secrets.yml`)
- View secrets
- Update secrets
- Update and restart pods
- SNS notifications
- Email support

### 6. Documentation
- `README.md` - Complete setup and usage guide
- `SETUP-SUMMARY.md` - This file

## 📋 Next Steps

### Step 1: Commit to tbi-devops-environments (This Repo)
```bash
git add dev/tbi-acc-hotel-mapping-service/
git commit -m "Add CI/CD configuration for tbi-acc-hotel-mapping-service

- CircleCI pipeline for dev environment
- Docker multi-stage build configuration
- Deployment scripts with critical fixes
- Helm chart with all templates
- GitHub Actions secrets management workflow
- Complete documentation

Service details:
- Namespace: redtag
- Technology: Node.js 20
- Builds own Docker image
"
git push origin master
```

### Step 2: Service Repository Setup (tbi-acc-hotel-mapping-service)

Location: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-acc-hotel-mapping-service`

#### a. Create VERSION file
```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-acc-hotel-mapping-service
echo "1.0.0" > VERSION
git add VERSION
git commit -m "Add VERSION file for deployment"
```

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

git add .github/workflows/manage-secrets.yml
git commit -m "Add GitHub Actions secrets management workflow"
```

#### c. Add GitHub Secret
1. Go to: https://github.com/redlabelvacations/tbi-acc-hotel-mapping-service/settings/secrets/actions
2. Add secret: `TBI_GITHUB_TOKEN` (get from DevOps team or use existing org-level token)

#### d. Push to master
```bash
git push origin master
```

### Step 3: tbi-environments Repository Setup

#### 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"
```

#### 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
```

**IMPORTANT**: Replace with actual values for your service!

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

#### d. Update 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 and push
```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 in dev environment"
git push origin master
```

### Step 4: CircleCI Configuration

1. Go to: https://app.circleci.com/projects/project-dashboard/github/redlabelvacations/
2. Click "Set Up Project" for `tbi-acc-hotel-mapping-service`
3. Select "Use existing config"
4. Verify the `devops-mgt` context is available with required variables

### Step 5: First Deployment

```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-acc-hotel-mapping-service
git checkout develop
# Make a small change or empty commit
git commit --allow-empty -m "Trigger first deployment"
git push origin develop
```

Monitor at: https://app.circleci.com/pipelines/github/redlabelvacations/tbi-acc-hotel-mapping-service

### Step 6: Verify Deployment

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

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

# Check service
kubectl get svc -n redtag tbi-acc-hotel-mapping-service
```

## 🎯 Key Configuration Details

- **Service Name**: tbi-acc-hotel-mapping-service
- **Namespace**: redtag
- **Environment**: dev
- **Cluster**: dev
- **AWS Region**: ca-central-1
- **AWS Account**: 594765360358
- **Node.js Version**: 20.4
- **Port**: 3000 (container) → 80 (service)
- **Replicas**: 1 (default)

## 🔐 Secrets Management

After deployment, you can manage secrets via GitHub Actions:

1. Go to service repo → Actions → "Manage Secrets"
2. Select environment: `dev`
3. Choose action:
   - `view-secrets` - View current secrets
   - `update-secrets` - Update without restart
   - `update-and-restart` - Update and restart pods

## 📊 Pipeline Stages

1. **Lint and Test** (Node.js 20.4)
   - Install dependencies
   - Run linting
   - Run tests
   - Build validation

2. **Build and Push Images**
   - Fetch Dockerfile from tbi-devops-environments
   - Build Docker image
   - Push to ECR with immutable tag

3. **Deploy to Kubernetes**
   - Package Helm chart
   - Push to S3 Helm repository
   - Update values in tbi-environments
   - Update requirements.yaml
   - Deploy using helm secrets
   - Verify rollout

## ⚠️ Important Notes

1. **Dockerfile Customization**: The provided Dockerfile assumes:
   - `npm run build` creates a `dist/` directory
   - Main entry point is `dist/index.js`
   - Adjust if your structure is different

2. **Environment Variables**: Update the secrets file with your actual database credentials, API keys, etc.

3. **Resource Limits**: Default limits are conservative. Adjust based on your application's needs.

4. **Ingress**: Disabled by default. Enable in values file if you need external access.

5. **Autoscaling**: Disabled by default. Enable in values file for production workloads.

## 🐛 Common Issues

### Issue: Build fails with "npm run build not found"
**Solution**: Ensure your package.json has a build script, or adjust the CircleCI config.

### Issue: Pods crash with "Cannot find module"
**Solution**: Check that all dependencies are in package.json and Dockerfile copies all necessary files.

### Issue: Secrets not loading
**Solution**: Verify secrets file format uses `|-` with actual newlines, not `\n` literals.

### Issue: "Nothing to commit" error
**Solution**: This is handled automatically by the deployment script. It's not an error.

## 📚 Reference Documentation

- [Developer Guide](../../docs/DEVELOPER-GUIDE.md) - Complete developer workflows
- [Deployment Script Guide](../../docs/DEPLOYMENT-SCRIPT-GUIDE.md) - Detailed script documentation
- [Secrets Pipeline Setup](../../docs/SECRETS-PIPELINE-SETUP.md) - Secrets management details
- [Migration Checklist](../../docs/MIGRATION-CHECKLIST.md) - Step-by-step migration guide

## ✅ Completion Checklist

- [ ] Files committed to tbi-devops-environments
- [ ] VERSION file created in service repo
- [ ] GitHub Actions workflow copied to service repo
- [ ] GitHub secret `TBI_GITHUB_TOKEN` configured
- [ ] Values file created in tbi-environments
- [ ] Secrets file created and encrypted in tbi-environments
- [ ] requirements.yaml updated in tbi-environments
- [ ] CircleCI project configured
- [ ] First deployment triggered
- [ ] Pods running successfully
- [ ] Logs show no errors
- [ ] Secrets management workflow tested

## 🎉 Success Criteria

Your setup is complete when:
1. CircleCI pipeline runs successfully (all green)
2. Pods are running in redtag namespace
3. Pod logs show application started without errors
4. GitHub Actions secrets workflow can view/update secrets
5. Service is accessible within the cluster

---

**Need Help?**
- Check the troubleshooting section in README.md
- Review the documentation in docs/
- Contact DevOps team
