build_image.yml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. ---
  2. - name: Verify prerequisites for image build
  3. hosts: localhost
  4. connection: local
  5. gather_facts: no
  6. tasks:
  7. - name: Require openshift_gcp_base_image
  8. fail:
  9. msg: "A base image name or family is required for image building. Please ensure `openshift_gcp_base_image` is defined."
  10. when: openshift_gcp_base_image is undefined
  11. - name: Provision ssh key
  12. hosts: localhost
  13. connection: local
  14. gather_facts: no
  15. tasks:
  16. - name: Set up core host GCP configuration
  17. import_role:
  18. name: openshift_gcp
  19. tasks_from: provision_ssh_keys.yml
  20. - name: Launch image build instance
  21. hosts: localhost
  22. connection: local
  23. gather_facts: no
  24. tasks:
  25. - name: Create the image instance disk
  26. gce_pd:
  27. service_account_email: "{{ (lookup('file', openshift_gcp_iam_service_account_keyfile ) | from_json ).client_email }}"
  28. credentials_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  29. project_id: "{{ openshift_gcp_project }}"
  30. zone: "{{ openshift_gcp_zone }}"
  31. name: "{{ openshift_gcp_prefix }}build-image-instance"
  32. disk_type: pd-ssd
  33. image: "{{ openshift_gcp_base_image }}"
  34. size_gb: 10
  35. state: present
  36. - name: Launch the image build instance
  37. gce:
  38. service_account_email: "{{ (lookup('file', openshift_gcp_iam_service_account_keyfile ) | from_json ).client_email }}"
  39. credentials_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  40. project_id: "{{ openshift_gcp_project }}"
  41. zone: "{{ openshift_gcp_zone }}"
  42. machine_type: n1-standard-1
  43. instance_names: "{{ openshift_gcp_prefix }}build-image-instance"
  44. state: present
  45. tags:
  46. - build-image-instance
  47. disk_auto_delete: false
  48. disks:
  49. - "{{ openshift_gcp_prefix }}build-image-instance"
  50. register: gce
  51. - name: add host to nodes
  52. add_host:
  53. hostname: "{{ item.public_ip }}"
  54. groupname: nodes
  55. with_items: "{{ gce.instance_data }}"
  56. - name: Wait for instance to respond to SSH
  57. wait_for:
  58. delay: 1
  59. host: "{{ item.public_ip }}"
  60. port: 22
  61. state: started
  62. timeout: 120
  63. with_items: "{{ gce.instance_data }}"
  64. - name: Wait for full SSH connection
  65. hosts: nodes
  66. gather_facts: no
  67. tasks:
  68. - wait_for_connection:
  69. - name: Add custom repositories
  70. hosts: nodes
  71. roles:
  72. - role: openshift_repos
  73. - import_playbook: ../../playbooks/init/base_packages.yml
  74. vars:
  75. l_base_packages_hosts: nodes
  76. # Add additional GCP specific behavior
  77. - hosts: nodes
  78. tasks:
  79. - include_role:
  80. name: openshift_gcp
  81. tasks_from: node_cloud_config.yml
  82. - include_role:
  83. name: openshift_gcp
  84. tasks_from: frequent_log_rotation.yml
  85. - name: Install networkmanager-glib to reset MTU
  86. package:
  87. name: NetworkManager-glib
  88. state: present
  89. - name: Set MTU
  90. nmcli:
  91. conn_name: "System eth0"
  92. mtu: "{{ openshift_node_sdn_mtu }}"
  93. type: ethernet
  94. state: present
  95. #Required for storage tests to mount NFS shares
  96. - name: Install packages for tests
  97. package:
  98. name: "nfs-utils"
  99. state: present
  100. - name: Commit image
  101. hosts: localhost
  102. connection: local
  103. tasks:
  104. - name: Terminate the image build instance
  105. gce:
  106. service_account_email: "{{ (lookup('file', openshift_gcp_iam_service_account_keyfile ) | from_json ).client_email }}"
  107. credentials_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  108. project_id: "{{ openshift_gcp_project }}"
  109. zone: "{{ openshift_gcp_zone }}"
  110. instance_names: "{{ openshift_gcp_prefix }}build-image-instance"
  111. state: absent
  112. - name: Save the new image
  113. command: >
  114. gcloud
  115. --project {{ openshift_gcp_project}} compute images create {{ (openshift_gcp_image_name | default(openshift_gcp_image + '-' + lookup('pipe','date +%Y%m%d-%H%M%S'))) | quote }}
  116. --source-disk {{ (openshift_gcp_prefix + 'build-image-instance') | quote }}
  117. --source-disk-zone {{ openshift_gcp_zone | quote }}
  118. --family {{ openshift_gcp_image | quote }}
  119. {% if openshift_gcp_licenses is defined %} --licenses {{ openshift_gcp_licenses | quote }}{% endif %}
  120. - name: Remove the image instance disk
  121. gce_pd:
  122. service_account_email: "{{ (lookup('file', openshift_gcp_iam_service_account_keyfile ) | from_json ).client_email }}"
  123. credentials_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  124. project_id: "{{ openshift_gcp_project }}"
  125. zone: "{{ openshift_gcp_zone }}"
  126. name: "{{ openshift_gcp_prefix }}build-image-instance"
  127. state: absent