build_image.yml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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: Launch image build instance
  12. hosts: localhost
  13. connection: local
  14. gather_facts: no
  15. tasks:
  16. - name: Set facts
  17. set_fact:
  18. openshift_node_bootstrap: True
  19. openshift_master_unsupported_embedded_etcd: True
  20. - name: Create the image instance disk
  21. gce_pd:
  22. service_account_email: "{{ (lookup('file', openshift_gcp_iam_service_account_keyfile ) | from_json ).client_email }}"
  23. credentials_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  24. project_id: "{{ openshift_gcp_project }}"
  25. zone: "{{ openshift_gcp_zone }}"
  26. name: "{{ openshift_gcp_prefix }}build-image-instance"
  27. disk_type: pd-ssd
  28. image: "{{ openshift_gcp_base_image }}"
  29. size_gb: 10
  30. state: present
  31. - name: Launch the image build instance
  32. gce:
  33. service_account_email: "{{ (lookup('file', openshift_gcp_iam_service_account_keyfile ) | from_json ).client_email }}"
  34. credentials_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  35. project_id: "{{ openshift_gcp_project }}"
  36. zone: "{{ openshift_gcp_zone }}"
  37. machine_type: n1-standard-1
  38. instance_names: "{{ openshift_gcp_prefix }}build-image-instance"
  39. state: present
  40. tags:
  41. - build-image-instance
  42. disk_auto_delete: false
  43. disks:
  44. - "{{ openshift_gcp_prefix }}build-image-instance"
  45. register: gce
  46. - name: add host to nodes
  47. add_host:
  48. hostname: "{{ item.public_ip }}"
  49. groupname: nodes
  50. with_items: "{{ gce.instance_data }}"
  51. - name: Wait for instance to respond to SSH
  52. wait_for:
  53. delay: 1
  54. host: "{{ item.public_ip }}"
  55. port: 22
  56. state: started
  57. timeout: 120
  58. with_items: "{{ gce.instance_data }}"
  59. - name: Wait for full SSH connection
  60. hosts: nodes
  61. gather_facts: no
  62. tasks:
  63. - wait_for_connection:
  64. - hosts: nodes
  65. tasks:
  66. - name: Set facts
  67. set_fact:
  68. openshift_node_bootstrap: True
  69. # This is the part that installs all of the software and configs for the instance
  70. # to become a node.
  71. - import_playbook: ../../openshift-node/private/image_prep.yml
  72. # Add additional GCP specific behavior
  73. - hosts: nodes
  74. tasks:
  75. - include_role:
  76. name: openshift_gcp
  77. tasks_from: node_cloud_config.yml
  78. - include_role:
  79. name: openshift_gcp
  80. tasks_from: frequent_log_rotation.yml
  81. - name: Commit image
  82. hosts: localhost
  83. connection: local
  84. tasks:
  85. - name: Terminate the image build instance
  86. gce:
  87. service_account_email: "{{ (lookup('file', openshift_gcp_iam_service_account_keyfile ) | from_json ).client_email }}"
  88. credentials_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  89. project_id: "{{ openshift_gcp_project }}"
  90. zone: "{{ openshift_gcp_zone }}"
  91. instance_names: "{{ openshift_gcp_prefix }}build-image-instance"
  92. state: absent
  93. - name: Save the new image
  94. command: gcloud --project "{{ openshift_gcp_project}}" compute images create "{{ openshift_gcp_image_name | default(openshift_gcp_image + '-' + lookup('pipe','date +%Y%m%d-%H%M%S')) }}" --source-disk "{{ openshift_gcp_prefix }}build-image-instance" --source-disk-zone "{{ openshift_gcp_zone }}" --family "{{ openshift_gcp_image }}"
  95. - name: Remove the image instance disk
  96. gce_pd:
  97. service_account_email: "{{ (lookup('file', openshift_gcp_iam_service_account_keyfile ) | from_json ).client_email }}"
  98. credentials_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  99. project_id: "{{ openshift_gcp_project }}"
  100. zone: "{{ openshift_gcp_zone }}"
  101. name: "{{ openshift_gcp_prefix }}build-image-instance"
  102. state: absent