Skip to content

Commit

Permalink
Add lifecycle functions to Equinix (#4184)
Browse files Browse the repository at this point in the history
* Add lifecycle functions to Equinix

* fix static test
  • Loading branch information
fridim authored Oct 21, 2021
1 parent 1edf0d5 commit ee5157e
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ instances:
value: "bastions,hypervisors"
- key: "ostype"
value: "linux"

ocp4_aio_ssh_key_path: >-
{{ ssh_key
| default(infra_ssh_key)
| default(ansible_ssh_private_key_file)
| default(default_key_name) }}
remote_user: root
3 changes: 3 additions & 0 deletions ansible/lifecycle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@

- when: cloud_provider == 'osp'
include_tasks: lifecycle_osp.yml

- when: cloud_provider == 'equinix_metal'
include_tasks: lifecycle_equinix_metal.yml
7 changes: 7 additions & 0 deletions ansible/lifecycle_equinix_metal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- when: >-
ACTION == 'stop'
or ACTION == 'start'
or ACTION == 'status'
include_role:
name: infra-equinix-metal-resources
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@
block:
- include_tasks: delete_instances.yaml
- include_tasks: delete_project.yaml

- when: ACTION in ['status', 'start', 'stop']
block:
- include_tasks: lookup_project.yaml
- when: equinix_metal_project_id is defined
block:
- include_tasks: "{{ ACTION }}_instances.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
- name: Get all devices
changed_when: false
uri:
url: >-
{{- equinix_metal_api_uri -}}
/projects/
{{- equinix_metal_project_ids[0] -}}
/devices
headers:
X-Auth-Token: "{{ equinix_metal_api_token }}"
register: r_equinix_metal_devices
retries: "{{ equinix_metal_api_retries }}"
delay: "{{ equinix_metal_api_delay }}"
until: r_equinix_metal_devices is succeeded

- name: Start devices
loop: "{{ r_equinix_metal_devices.json.devices | default([]) }}"
loop_control:
loop_var: _device
uri:
url: >-
{{- equinix_metal_api_uri -}}
/devices/
{{- _device.id -}}
/actions
headers:
X-Auth-Token: "{{ equinix_metal_api_token }}"
method: POST
body_format: json
body:
type: power_on
status_code: [200, 202]

register: r_start
failed_when: >-
not r_start is succeeded
and not 'Device must be powered off' in r_start.json.errors | default([])
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
- name: Get all devices
changed_when: false
uri:
url: >-
{{- equinix_metal_api_uri -}}
/projects/
{{- equinix_metal_project_ids[0] -}}
/devices
headers:
X-Auth-Token: "{{ equinix_metal_api_token }}"
register: r_equinix_metal_devices
retries: "{{ equinix_metal_api_retries }}"
delay: "{{ equinix_metal_api_delay }}"
until: r_equinix_metal_devices is succeeded

- name: Report status in user info
agnosticd_user_info:
msg: |-
{{ "%-20s %-10s %-14s %s" | format("Instance", "State", "Type", "Location") }}
----------------------------------------------------------------
{% for device in r_equinix_metal_devices.json.devices | default([]) %}
{{ "%-20s %-10s %-14s %s" | format(device.hostname, device.state, device.plan.slug, device.facility.name) }}
{% endfor %}
- name: Print status information to a file
copy:
dest: "{{ output_dir }}/status.txt"
content: |-
{{ "%-20s %-10s %-14s %s" | format("Instance", "State", "Type", "Location") }}
----------------------------------------------------------------
{% for device in r_equinix_metal_devices.json.devices | default([]) %}
{{ "%-20s %-10s %-14s %s" | format(device.hostname, device.state, device.plan.slug, device.facility.name) }}
{% endfor %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
- name: Get all devices
changed_when: false
uri:
url: >-
{{- equinix_metal_api_uri -}}
/projects/
{{- equinix_metal_project_ids[0] -}}
/devices
headers:
X-Auth-Token: "{{ equinix_metal_api_token }}"
register: r_equinix_metal_devices
retries: "{{ equinix_metal_api_retries }}"
delay: "{{ equinix_metal_api_delay }}"
until: r_equinix_metal_devices is succeeded

- name: Stop devices
loop: "{{ r_equinix_metal_devices.json.devices | default([]) }}"
loop_control:
loop_var: _device
uri:
url: >-
{{- equinix_metal_api_uri -}}
/devices/
{{- _device.id -}}
/actions
headers:
X-Auth-Token: "{{ equinix_metal_api_token }}"
method: POST
body_format: json
body:
type: power_off
status_code: [200, 202]

register: r_stop
failed_when: >-
not r_stop is succeeded
and not 'Device must be powered on' in r_stop.json.errors | default([])

0 comments on commit ee5157e

Please sign in to comment.