s3_registry.yml 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ---
  2. # This playbook creates an S3 bucket named after your cluster and configures the docker-registry service to use the bucket as its backend storage.
  3. # Usage:
  4. # ansible-playbook s3_registry.yml -e clusterid="mycluster" -e aws_bucket="clusterid-docker" -e aws_region="us-east-1"
  5. #
  6. # The AWS access/secret keys should be the keys of a separate user (not your main user), containing only the necessary S3 access role.
  7. # The 'clusterid' is the short name of your cluster.
  8. - hosts: tag_clusterid_{{ clusterid }}:&tag_host-type_openshift-master
  9. remote_user: root
  10. gather_facts: False
  11. vars:
  12. aws_access_key: "{{ lookup('env', 'S3_ACCESS_KEY_ID') }}"
  13. aws_secret_key: "{{ lookup('env', 'S3_SECRET_ACCESS_KEY') }}"
  14. aws_bucket_name: "{{ aws_bucket | default(clusterid ~ '-docker') }}"
  15. aws_bucket_region: "{{ aws_region | default(lookup('env', 'S3_REGION') | default('us-east-1', true)) }}"
  16. aws_create_bucket: "{{ aws_create | default(True) }}"
  17. aws_tmp_path: "{{ aws_tmp_pathfile | default('/root/config.yml')}}"
  18. aws_delete_tmp_file: "{{ aws_delete_tmp | default(True) }}"
  19. tasks:
  20. - name: Check for AWS creds
  21. fail:
  22. msg: "Couldn't find {{ item }} creds in ENV"
  23. when: "{{ item }} == ''"
  24. with_items:
  25. - aws_access_key
  26. - aws_secret_key
  27. - name: Scale down registry
  28. command: oc scale --replicas=0 dc/docker-registry
  29. - name: Create S3 bucket
  30. when: aws_create_bucket | bool
  31. local_action:
  32. module: s3 bucket="{{ aws_bucket_name }}" mode=create
  33. - name: Set up registry environment variable
  34. command: oc env dc/docker-registry REGISTRY_CONFIGURATION_PATH=/etc/registryconfig/config.yml
  35. - name: Generate docker registry config
  36. template: src="s3_registry.j2" dest="/root/config.yml" owner=root mode=0600
  37. - name: Determine if new secrets are needed
  38. command: oc get secrets
  39. register: secrets
  40. - name: Create registry secrets
  41. command: oc secrets new dockerregistry /root/config.yml
  42. when: "'dockerregistry' not in secrets.stdout"
  43. - name: Determine if service account contains secrets
  44. command: oc describe serviceaccount/registry
  45. register: serviceaccount
  46. - name: Add secrets to registry service account
  47. command: oc secrets add serviceaccount/registry secrets/dockerregistry
  48. when: "'dockerregistry' not in serviceaccount.stdout"
  49. - name: Determine if deployment config contains secrets
  50. command: oc volume dc/docker-registry --list
  51. register: dc
  52. - name: Add secrets to registry deployment config
  53. command: oc volume dc/docker-registry --add --name=dockersecrets -m /etc/registryconfig --type=secret --secret-name=dockerregistry
  54. when: "'dockersecrets' not in dc.stdout"
  55. - name: Wait for deployment config to take effect before scaling up
  56. pause: seconds=30
  57. - name: Scale up registry
  58. command: oc scale --replicas=1 dc/docker-registry
  59. - name: Delete temporary config file
  60. file: path={{ aws_tmp_path }} state=absent
  61. when: aws_delete_tmp_file | bool