Skip to content

Commit d06f415

Browse files
authored
Merge pull request #33 from stackhpc/feat/image-build
Add an OpenHPC image build playbook and role
2 parents 774c104 + d9bbd6f commit d06f415

File tree

17 files changed

+426
-10
lines changed

17 files changed

+426
-10
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
inventory.*
88
__pycache__
99
terraform/backend.tf
10-
inventory
10+
inventory
11+
venv
12+
bin

ansible.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ host_key_checking = False
66
remote_tmp = /tmp
77
# Enable our custom vars plugin that parses variables from the current working directory
88
vars_plugins_enabled = host_group_vars,cwd_host_group_vars
9-
roles_path = roles:vendor/stackhpc/ansible-slurm-appliance/ansible/roles
9+
roles_path = vendor/stackhpc/ansible-slurm-appliance/ansible/roles:roles
10+
callbacks_enabled = ansible.posix.profile_tasks
1011

1112
[ssh_connection]
1213
ssh_args = -o ControlMaster=auto -o ControlPersist=240s -o PreferredAuthentications=publickey -o UserKnownHostsFile=/dev/null

image-build.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
- name: Build image
3+
hosts: openstack
4+
tasks:
5+
- name: Manage image build infra
6+
include_role:
7+
name: image_build_infra
8+
when:
9+
- image_build_manage_infra is defined
10+
- image_build_manage_infra
11+
12+
- block:
13+
- name: Build fat image
14+
include_role:
15+
name: image_build
16+
17+
- name: Set cluster_image fact
18+
set_fact:
19+
cluster_image: "{{ image_build_data.artifact_id }}"
20+
21+
- name: Print cluster_image UUID
22+
debug:
23+
msg: "{{ cluster_image }}"
24+
when: cluster_state is not defined or (cluster_state is defined and cluster_state != "absent")

image-build/hosts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[openstack]
2+
localhost ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"

requirements.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ roles:
33
- src: stackhpc.nfs
44
version: v22.9.1
55
- src: https://github.com/stackhpc/ansible-role-openhpc.git
6-
version: v0.18.0 # requires/uses openhpc v2.6.1
6+
version: v0.20.0 # Allow multiple empty partitions by @sjpb in #156
77
name: stackhpc.openhpc
88
- src: https://github.com/stackhpc/ansible-node-exporter.git
99
version: feature/no-install
@@ -33,11 +33,9 @@ collections:
3333
version: 4.5.0 # https://github.com/ansible-collections/community.general/pull/4281
3434
- name: community.crypto
3535
version: 2.10.0
36-
- name: community.mysql
37-
3836
- name: openstack.cloud
3937
version: 1.10.0
4038
- name: https://github.com/stackhpc/ansible-collection-terraform
4139
type: git
42-
version: 75fb75132bbc77e3e78a05ba674458131da2b1dd
43-
40+
version: dadf2e81b78cfd267821c568f92d3a8da6541a41
41+

roles/cluster_infra/tasks/main.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@
7373
else 'vd'
7474
}}
7575
# Only run when block_device_prefix isn't set as an extravar
76-
when: block_device_prefix is not defined
77-
76+
when:
77+
- block_device_prefix is not defined
78+
- cluster_image is defined
7879

7980
- name: Template Terraform files into project directory
8081
template:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# Attach a floating IP to the Packer build instance
3+
image_build_attach_floating_ip: false
4+
5+
# Use a volume for the root disk of the Packer build instance
6+
image_build_use_blockstorage_volume: false
7+
8+
# Packer image format (only used when image_build_use_blockstorage_volume: true
9+
image_build_image_disk_format: "qcow2"
10+
11+
# Metadata items to set on the Packer image
12+
image_build_metadata: {}
13+
14+
# The directory that contains the openstack.pkr.hcl to build the Slurm image
15+
image_build_packer_root_path: "{{ playbook_dir }}/vendor/stackhpc/ansible-slurm-appliance/packer"
16+
17+
# The appliances_environment_root directory. This may contain a hooks directory
18+
# optionally containing pre.yml, post-bootstrap.yml and post.yml playbooks, to
19+
# run during the image-build process
20+
image_build_appliances_environment_root: "{{ playbook_dir }}/image-build"
21+
22+
# Vars to apply to the builder group
23+
image_build_builder_group_vars:
24+
update_log_path: /tmp/update_log
25+
appliances_repository_root: "{{ playbook_dir }}/vendor/stackhpc/ansible-slurm-appliance"
26+
27+
# ansible_ssh_common_args for Packer build
28+
image_build_ansible_ssh_common_args: >-
29+
{% if image_build_ssh_bastion_host is defined %}
30+
'-o ProxyCommand="ssh -W %h:%p -q
31+
{% if image_build_ssh_bastion_private_key_file is defined %}
32+
-i {{ image_build_ssh_bastion_private_key_file }}
33+
{% endif %}
34+
-l {{ image_build_ssh_bastion_username }}
35+
{{ image_build_ssh_bastion_host }}"'
36+
{% else %}
37+
""
38+
{% endif %}

roles/image_build/tasks/main.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
3+
- name: Run prechecks
4+
include_tasks: prechecks.yml
5+
6+
- name: Create temporary file for pkrvars.hcl
7+
ansible.builtin.tempfile:
8+
state: file
9+
suffix: .pkrvars.hcl
10+
register: pkrvars_hcl_file
11+
12+
- name: Make Packer vars file
13+
template:
14+
src: builder.pkrvars.hcl.j2
15+
dest: "{{ pkrvars_hcl_file.path }}"
16+
17+
- name: Create temporary image-build inventory directory
18+
ansible.builtin.tempfile:
19+
state: directory
20+
prefix: image-build.
21+
register: image_build_inventory
22+
23+
- name: Symlink "everything" layout to image-build inventory
24+
file:
25+
state: link
26+
src: "{{ playbook_dir }}/vendor/stackhpc/ansible-slurm-appliance/environments/common/layouts/everything"
27+
dest: "{{ image_build_inventory.path }}/groups"
28+
29+
- name: Symlink CAAS group_vars to image-build inventory
30+
file:
31+
state: link
32+
src: "{{ playbook_dir }}/group_vars"
33+
dest: "{{ image_build_inventory.path }}/group_vars"
34+
35+
- name: Add builder vars to image-build inventory hosts file
36+
copy:
37+
dest: "{{ image_build_inventory.path }}/hosts"
38+
content: |
39+
{% raw %}
40+
localhost ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"
41+
{% endraw %}
42+
[builder:vars]
43+
{% if image_build_ssh_bastion_host is defined %}
44+
ansible_ssh_common_args={{ image_build_ansible_ssh_common_args }}
45+
{% endif %}
46+
{% for k,v in image_build_builder_group_vars.items() -%}
47+
{{ k }}={{ v }}
48+
{% endfor -%}
49+
50+
- name: Create temporary file for ansible.cfg
51+
ansible.builtin.tempfile:
52+
state: file
53+
suffix: ansible.cfg
54+
register: ansible_cfg_file
55+
56+
- name: Template image-build ansible.cfg
57+
template:
58+
src: ansible.cfg.j2
59+
dest: "{{ ansible_cfg_file.path }}"
60+
61+
- name: Packer init
62+
command:
63+
cmd: |
64+
packer init .
65+
chdir: "{{ image_build_packer_root_path }}"
66+
67+
- name: Build image with packer
68+
command:
69+
cmd: |
70+
packer build -only openstack.openhpc -var-file={{ pkrvars_hcl_file.path }} openstack.pkr.hcl
71+
chdir: "{{ image_build_packer_root_path }}"
72+
environment:
73+
APPLIANCES_ENVIRONMENT_ROOT: "{{ image_build_appliances_environment_root }}"
74+
ANSIBLE_CONFIG: "{{ ansible_cfg_file.path }}"
75+
PACKER_LOG: "1"
76+
PACKER_LOG_PATH: "{{ lookup('ansible.builtin.env', 'PACKER_LOG_PATH', default='/tmp/packer-build.log') }}"
77+
78+
- name: Parse packer-manifest.json
79+
set_fact:
80+
packer_manifest: "{{ lookup('file', '/tmp/builder.manifest.json') | from_json }}"
81+
82+
- name: Extract image-build data
83+
set_fact:
84+
image_build_data: "{{ packer_manifest.builds | selectattr('packer_run_uuid', 'eq', packer_manifest.last_run_uuid) | first }}"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
3+
- name: Check required vars are defined
4+
assert:
5+
that:
6+
- "{{ item }} is defined"
7+
fail_msg: "{{ item }} is not defined"
8+
loop:
9+
- image_build_network_id
10+
- image_build_floating_ip_network
11+
- image_build_source_image_id
12+
- image_build_security_group_id
13+
14+
- name: Ensure builder access mode
15+
fail:
16+
msg: >-
17+
Set either image_build_ssh_bastion_host or
18+
image_build_attach_floating_ip to access the image
19+
build instance via a bastion or directly
20+
when:
21+
- image_build_ssh_bastion_host is defined
22+
- image_build_attach_floating_ip is defined and image_build_attach_floating_ip
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[defaults]
2+
any_errors_fatal = True
3+
gathering = smart
4+
host_key_checking = False
5+
remote_tmp = /tmp
6+
roles_path = {{ playbook_dir }}/vendor/stackhpc/ansible-slurm-appliance/ansible/roles
7+
inventory = {{ playbook_dir }}/vendor/stackhpc/ansible-slurm-appliance/environments/common/inventory,{{ image_build_inventory.path }}
8+
9+
[ssh_connection]
10+
ssh_args = -o ControlMaster=auto -o ControlPersist=240s -o PreferredAuthentications=publickey -o UserKnownHostsFile=/dev/null
11+
pipelining = True
12+
# This is important because we are using one of the hosts in the play as a jump host
13+
# This ensures that if the proxy connection is interrupted, rendering the other hosts
14+
# unreachable, the connection is retried instead of failing the entire play
15+
retries = 10

0 commit comments

Comments
 (0)