# Makefile for VM Deployment

.PHONY: vm-deploy vm-destroy vm-status vm-ssh vm-logs vm-backup vm-restore

# Deploy VM network
vm-deploy:
	@echo "Deploying Besu network on VMs..."
	cd terraform && terraform apply -var-file=terraform.tfvars.vm -target=module.vm_validators -target=module.vm_sentries -target=module.vm_rpc

# Destroy VM network
vm-destroy:
	@echo "Destroying VM network..."
	cd terraform && terraform destroy -var-file=terraform.tfvars.vm -target=module.vm_validators -target=module.vm_sentries -target=module.vm_rpc

# Check VM status
vm-status:
	@echo "Checking VM status..."
	az vm list --resource-group $(RESOURCE_GROUP) --show-details --query "[].{Name:name, Status:powerState, IP:publicIps}" -o table

# SSH into VM
vm-ssh:
	@echo "SSH into VM..."
	ssh besuadmin@$(VM_IP)

# View VM logs
vm-logs:
	@echo "Viewing VM logs..."
	ssh besuadmin@$(VM_IP) "docker logs -f besu-$(NODE_TYPE)-$(NODE_INDEX)"

# Backup VM data
vm-backup:
	@echo "Backing up VM data..."
	./scripts/vm-deployment/backup-vm.sh $(VM_IP)

# Restore VM data
vm-restore:
	@echo "Restoring VM data..."
	./scripts/vm-deployment/restore-vm.sh $(VM_IP) $(BACKUP_FILE)

# Setup VM
vm-setup:
	@echo "Setting up VM..."
	./scripts/vm-deployment/setup-vm.sh $(NODE_TYPE) $(NODE_INDEX)

# Update VM configuration
vm-update-config:
	@echo "Updating VM configuration..."
	./scripts/vm-deployment/update-vm-config.sh $(VM_IP) $(NODE_TYPE) $(CONFIG_FILE)

# Monitor VMs
vm-monitor:
	@echo "Monitoring VMs..."
	./scripts/vm-deployment/monitor-vm.sh

# Scale VMSS
vm-scale:
	@echo "Scaling VMSS..."
	az vmss scale --resource-group $(RESOURCE_GROUP) --name $(VMSS_NAME) --new-capacity $(CAPACITY)

# Help
vm-help:
	@echo "VM Deployment Commands:"
	@echo "  make vm-deploy          - Deploy VM network"
	@echo "  make vm-destroy         - Destroy VM network"
	@echo "  make vm-status          - Check VM status"
	@echo "  make vm-ssh             - SSH into VM (VM_IP=ip)"
	@echo "  make vm-logs            - View VM logs (VM_IP=ip, NODE_TYPE=type, NODE_INDEX=index)"
	@echo "  make vm-backup          - Backup VM data (VM_IP=ip)"
	@echo "  make vm-restore         - Restore VM data (VM_IP=ip, BACKUP_FILE=file)"
	@echo "  make vm-setup           - Setup VM (NODE_TYPE=type, NODE_INDEX=index)"
	@echo "  make vm-update-config   - Update VM config (VM_IP=ip, NODE_TYPE=type, CONFIG_FILE=file)"
	@echo "  make vm-monitor         - Monitor VMs"
	@echo "  make vm-scale           - Scale VMSS (VMSS_NAME=name, CAPACITY=count)"

