score-spec / score-compose

Reference implementation for docker-compose target platform support
https://score.dev/
Apache License 2.0
445 stars 40 forks source link

feat(provisioner): add mysql resource provisioner #132

Closed giesan closed 4 months ago

giesan commented 4 months ago

Description

This pull request is a proposal for solving the issue #128.

What does this PR do?

This pull request implements a provisioner that prepares a service with MySQL (image mysql:8) with the corresponding parameters.

This provisions a single mysql container with persistent data and healthcheck. An exec command is provided to support working with the data.

score.yaml:

apiVersion: score.dev/v1b1

metadata:
  name: hello-world

containers:
  first:
    image: nginx
    variables:
      CONNECTION_STRING: "mysql://${resources.db.username}:${resources.db.password}@${resources.db.host}:${resources.db.port}/${resources.db.name}"

resources:
  db:
    type: mysql

generate:

$ score-compose generate score.yaml
INFO: Loaded state directory with docker compose project '12-mysql-database'
INFO: Validated workload 'hello-world'
INFO: Successfully loaded 10 resource provisioners
INFO: mysql.default#hello-world.db: mysql.default#hello-world.db: To connect to mysql, enter password 'vOB0YIS5ToOihZXr' at: "docker run -it --network 12-mysql-database_default --rm mysql:8 mysql -h mysql-oeD62h -u user-iPVvhkqZ -p"
INFO: Provisioned 1 resources
INFO: Converting workload 'hello-world' to Docker compose

And docker compose up returns:

[+] Running 4/2
 ✔ Network 12-mysql-database_default                 Created
 ✔ Container 12-mysql-database-mysql-oeD62h-1        Created
 ✔ Container 12-mysql-database-wait-for-resources-1  Created
 ✔ Container 12-mysql-database-hello-world-first-1   Created

The hello world container contains the connection string

$ docker exec -it 4502cbe09933 sh -c 'echo $CONNECTION_STRING'
mysql://user-iPVvhkqZ:vOB0YIS5ToOihZXr@mysql-oeD62h:3306/dbpLOcppIB

Types of changes

Checklist: