Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the CLI structure to support docker swarm #126

Open
xmnlab opened this issue Sep 29, 2024 · 1 comment
Open

Change the CLI structure to support docker swarm #126

xmnlab opened this issue Sep 29, 2024 · 1 comment

Comments

@xmnlab
Copy link
Member

xmnlab commented Sep 29, 2024

In order to support docker swarm, we need to rewrite the CLI commands to something more generic and create a map for docker compose and swarm.

Here's a proposal for a new Sugar CLI API that abstracts common container orchestration commands, mapping them to both Docker Compose and Docker Swarm. This unified API aims to provide a consistent user experience regardless of the underlying orchestration tool.

this should be done before #124


Proposed Sugar Commands and Their Mappings

  1. sugar deploy

    • Description: Deploy services as defined in the configuration file.
    • Docker Compose Mapping: docker-compose up -d
    • Docker Swarm Mapping: docker stack deploy -c [compose-file] [stack-name]
  2. sugar remove

    • Description: Remove all services and associated resources.
    • Docker Compose Mapping: docker-compose down
    • Docker Swarm Mapping: docker stack rm [stack-name]
  3. sugar build

    • Description: Build or rebuild services.
    • Docker Compose Mapping: docker-compose build
    • Docker Swarm Consideration: Docker Swarm doesn't build images; you need to build images beforehand or use pre-built images.
  4. sugar start

    • Description: Start existing stopped services.
    • Docker Compose Mapping: docker-compose start
    • Docker Swarm Mapping: Scale services up if they are scaled down.
      • Command: docker service scale [service]=[replicas]
  5. sugar stop

    • Description: Stop running services without removing them.
    • Docker Compose Mapping: docker-compose stop
    • Docker Swarm Mapping: Scale services down to zero replicas.
      • Command: docker service scale [service]=0
  6. sugar restart

    • Description: Restart services.
    • Docker Compose Mapping: docker-compose restart
    • Docker Swarm Mapping: Update services to force a restart.
      • Command: docker service update --force [service]
  7. sugar scale [service]=[replicas]

    • Description: Set the number of replicas for a service.
    • Docker Compose Mapping: docker-compose up -d --scale [service]=[replicas]
    • Docker Swarm Mapping: docker service scale [service]=[replicas]
  8. sugar logs [service]

    • Description: View output from services.
    • Docker Compose Mapping: docker-compose logs [service]
    • Docker Swarm Mapping: docker service logs [service]
  9. sugar exec [service] [command]

    • Description: Execute a command in a running service container.
    • Docker Compose Mapping: docker-compose exec [service] [command]
    • Docker Swarm Consideration: Since Swarm may have multiple replicas, you might need to exec into a specific task or use docker exec on a container ID.
      • Command: docker exec [container_id] [command]
  10. sugar status

    • Description: Display the status of deployed services.
    • Docker Compose Mapping: docker-compose ps
    • Docker Swarm Mapping: docker stack services [stack-name]
  11. sugar ps

    • Description: List containers or tasks.
    • Docker Compose Mapping: docker-compose ps
    • Docker Swarm Mapping: docker service ps [service]
  12. sugar update

    • Description: Update services with new configurations or images.
    • Docker Compose Mapping: docker-compose up -d (after changing the compose file)
    • Docker Swarm Mapping: docker service update [options] [service]
  13. sugar config

    • Description: Validate and view the configuration.
    • Docker Compose Mapping: docker-compose config
    • Docker Swarm Consideration: No direct equivalent, but validation happens during deployment.
  14. sugar inspect [service]

    • Description: Display detailed information on services.
    • Docker Compose Mapping: docker-compose exec [service] inspect
    • Docker Swarm Mapping: docker service inspect [service]
  15. sugar pause / sugar unpause

    • Description: Pause or unpause services.
    • Docker Compose Mapping: docker-compose pause / docker-compose unpause
    • Docker Swarm Consideration: Not directly supported; can simulate by scaling down to zero and then scaling up.
  16. sugar secret

    • Description: Manage secrets.
    • Docker Compose Mapping: Secrets are defined in the compose file.
    • Docker Swarm Mapping: Use docker secret commands.
      • Commands: docker secret create, docker secret ls, etc.
  17. sugar network

    • Description: Manage networks.
    • Docker Compose Mapping: Networks are managed via the compose file.
    • Docker Swarm Mapping: Use docker network commands.
      • Commands: docker network create, docker network ls, etc.

Additional Considerations

  • Configuration File Detection: Sugar can detect whether to use Docker Compose or Docker Swarm based on a configuration flag or the presence of Swarm-specific settings.

  • Context Flag: Introduce a global --context flag to specify the orchestration tool (e.g., --context compose or --context swarm).

  • Error Handling: Provide informative errors when a command isn't supported in the current context.

  • Extended Commands: For Swarm-specific features like rolling updates, health checks, and secret management, extend Sugar with additional commands as needed.

  • Aliases and Shortcuts: Allow for command aliases to make the CLI more intuitive.


Example Usage

  • Deploy Services

    sugar deploy
  • Scale a Service

    sugar scale web=5
  • View Logs

    sugar logs web
  • Execute Command in a Service

    sugar exec web bash
  • Update a Service

    sugar update
  • Stop Services

    sugar stop

By defining these commands, Sugar provides a unified interface that abstracts away the differences between Docker Compose and Docker Swarm. This allows users to manage their containerized applications seamlessly, regardless of the underlying orchestration platform.

@xmnlab
Copy link
Member Author

xmnlab commented Sep 29, 2024

we can also use kubernetes as a reference for naming the commands.

Based on Kubernetes commands, here's a proposed set of Sugar commands that map to both Docker Swarm and Docker Compose commands. This unified command set aims to provide a consistent interface inspired by Kubernetes, allowing users to manage services across different orchestration platforms seamlessly.


Proposed Sugar Commands and Their Mappings

  1. sugar apply

    • Description: Apply configurations and deploy services from the configuration file.
    • Kubernetes Command: kubectl apply -f [file]
    • Docker Compose Mapping: docker-compose up -d
    • Docker Swarm Mapping: docker stack deploy -c [compose-file] [stack-name]
  2. sugar delete

    • Description: Delete services defined in the configuration.
    • Kubernetes Command: kubectl delete -f [file]
    • Docker Compose Mapping: docker-compose down
    • Docker Swarm Mapping: docker stack rm [stack-name]
  3. sugar get services

    • Description: List services and their status.
    • Kubernetes Command: kubectl get services
    • Docker Compose Mapping: docker-compose ps
    • Docker Swarm Mapping: docker stack services [stack-name]
  4. sugar describe service [service]

    • Description: Show detailed information about a specific service.
    • Kubernetes Command: kubectl describe service [service]
    • Docker Compose Mapping: Use docker-compose ps and docker inspect on the container ID.
    • Docker Swarm Mapping: docker service inspect [service]
  5. sugar logs [service]

    • Description: Display logs from a service's containers.
    • Kubernetes Command: kubectl logs [pod]
    • Docker Compose Mapping: docker-compose logs [service]
    • Docker Swarm Mapping: docker service logs [service]
  6. sugar exec [service] [command]

    • Description: Execute a command inside a running service container.
    • Kubernetes Command: kubectl exec [pod] -- [command]
    • Docker Compose Mapping: docker-compose exec [service] [command]
    • Docker Swarm Mapping: Requires identifying a specific container ID:
      • List tasks: docker service ps [service]
      • Exec into container: docker exec -it [container_id] [command]
  7. sugar scale [service]=[replicas]

    • Description: Scale a service to the specified number of replicas.
    • Kubernetes Command: kubectl scale deployment/[deployment] --replicas=[replicas]
    • Docker Compose Mapping: docker-compose up -d --scale [service]=[replicas]
    • Docker Swarm Mapping: docker service scale [service]=[replicas]
  8. sugar rollout restart [service]

    • Description: Restart a service gracefully.
    • Kubernetes Command: kubectl rollout restart deployment/[deployment]
    • Docker Compose Mapping: docker-compose restart [service]
    • Docker Swarm Mapping: docker service update --force [service]
  9. sugar top

    • Description: Display resource usage statistics.
    • Kubernetes Command: kubectl top nodes or kubectl top pods
    • Docker Compose Mapping: docker-compose top
    • Docker Swarm Mapping: docker stats or docker service ps [service]
  10. sugar config

    • Description: Validate and view the current configuration.
    • Kubernetes Command: kubectl config view
    • Docker Compose Mapping: docker-compose config
    • Docker Swarm Mapping: Not directly applicable; configurations are part of the stack file.
  11. sugar restart [service]

    • Description: Restart a service.
    • Kubernetes Command: Not a direct command; use kubectl rollout restart.
    • Docker Compose Mapping: docker-compose restart [service]
    • Docker Swarm Mapping: docker service update --force [service]
  12. sugar stop [service]

    • Description: Stop running services without removing them.
    • Kubernetes Command: kubectl scale deployment/[deployment] --replicas=0
    • Docker Compose Mapping: docker-compose stop [service]
    • Docker Swarm Mapping: docker service scale [service]=0
  13. sugar start [service]

    • Description: Start stopped services.
    • Kubernetes Command: Scale replicas up: kubectl scale deployment/[deployment] --replicas=[replicas]
    • Docker Compose Mapping: docker-compose start [service]
    • Docker Swarm Mapping: docker service scale [service]=[replicas]
  14. sugar update

    • Description: Update services with new configurations or images.
    • Kubernetes Command: kubectl apply -f [file] with updated configuration.
    • Docker Compose Mapping: docker-compose up -d after modifying the compose file.
    • Docker Swarm Mapping: docker service update [options] [service]
  15. sugar inspect [resource]

    • Description: Display detailed information on a resource (service, container).
    • Kubernetes Command: kubectl describe [resource]
    • Docker Compose Mapping: docker inspect [container_id]
    • Docker Swarm Mapping: docker service inspect [service] or docker inspect [resource]
  16. sugar events

    • Description: Stream real-time events from the services.
    • Kubernetes Command: kubectl get events --watch
    • Docker Compose Mapping: docker-compose events
    • Docker Swarm Mapping: docker events
  17. sugar logs --follow [service]

    • Description: Follow logs from a service in real-time.
    • Kubernetes Command: kubectl logs [pod] --follow
    • Docker Compose Mapping: docker-compose logs -f [service]
    • Docker Swarm Mapping: docker service logs -f [service]
  18. sugar pause [service]

    • Description: Pause a running service.
    • Kubernetes Command: kubectl scale deployment/[deployment] --replicas=0 (as an approximation)
    • Docker Compose Mapping: docker-compose pause [service]
    • Docker Swarm Mapping: Not directly supported; scaling down is an alternative.
  19. sugar unpause [service]

    • Description: Unpause a paused service.
    • Kubernetes Command: Scale replicas up.
    • Docker Compose Mapping: docker-compose unpause [service]
    • Docker Swarm Mapping: Scale service back to desired replicas.
  20. sugar rm [service]

    • Description: Remove stopped service containers.
    • Kubernetes Command: kubectl delete pod [pod]
    • Docker Compose Mapping: docker-compose rm [service]
    • Docker Swarm Mapping: Not directly applicable; services are removed with docker service rm [service]
  21. sugar build

    • Description: Build or rebuild services from Dockerfiles.
    • Kubernetes Command: No direct equivalent; images are built separately.
    • Docker Compose Mapping: docker-compose build
    • Docker Swarm Consideration: Docker Swarm expects pre-built images; build beforehand.
  22. sugar apply -f [file]

    • Description: Apply a specific configuration file.
    • Kubernetes Command: kubectl apply -f [file]
    • Docker Compose Mapping: docker-compose -f [file] up -d
    • Docker Swarm Mapping: docker stack deploy -c [file] [stack-name]

Example Usage

  • Deploy Services

    sugar apply
  • Scale a Service

    sugar scale web=5
  • View Logs

    sugar logs web
  • Execute a Command in a Service Container

    sugar exec web bash
  • Restart a Service

    sugar restart web
  • Update Services with New Configuration

    sugar update
  • Get Detailed Information About a Service

    sugar describe service web
  • Display Resource Usage

    sugar top
  • Remove Stopped Containers

    sugar rm web

Additional Notes

  • Configuration Files: Sugar can use Docker Compose YAML files as configuration, similar to Kubernetes manifest files.
  • Context Awareness: Sugar can determine whether to use Docker Compose or Docker Swarm based on the context or a specified flag (e.g., --orchestrator [compose|swarm]).
  • Error Handling: Commands not supported by an orchestrator should provide informative messages.
  • Extensibility: Additional Kubernetes-inspired commands can be added as needed, with careful mapping to Docker functionalities.
  • Consistency: Using Kubernetes command patterns helps users familiar with Kubernetes to adapt quickly to Sugar.

By aligning Sugar's command set with Kubernetes commands, you provide users with a familiar interface that abstracts the underlying differences between Docker Swarm and Docker Compose. This approach simplifies container orchestration and improves developer productivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant