Skip to content

Commit

Permalink
[FIX] Change gcp_compute_instance_facts to gcp_compute_instance_info
Browse files Browse the repository at this point in the history
Fixed #1
  • Loading branch information
antoineserrano authored Jan 27, 2020
2 parents 984049a + ac67152 commit 6ccb7bb
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 49 deletions.
22 changes: 11 additions & 11 deletions roles/clusterbuild/cluster_hosts/tasks/gce.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
when: rescuing_instance is defined and cluster_hosts_flat | json_query('[? hostname == `' + rescuing_instance + '`]') | length == 0

- name: Get GCP instance facts
gcp_compute_instance_facts:
gcp_compute_instance_info:
zone: "{{cluster_vars.region}}-{{item}}"
filters:
- "name = {{cluster_name}}*"
Expand All @@ -54,14 +54,14 @@
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
with_items: "{{ cluster_vars[buildenv].hosttype_vars | json_query('*.az|[]') | unique }}"
register: gcp_compute_instance_facts
register: gcp_compute_instance_info
delegate_to: localhost
run_once: true


- set_fact:
running_instance_fact: "{{gcp_compute_instance_facts.results | json_query('[*].items[].{name: name, deploy_status: labels.deploy_status}')}}"
current_cluter_size: "{{gcp_compute_instance_facts.results | json_query('[*].items[]') | length}}"
running_instance_fact: "{{gcp_compute_instance_info.results | json_query('[*].resources[].{name: name, deploy_status: labels.deploy_status}')}}"
current_cluter_size: "{{gcp_compute_instance_info.results | json_query('[*].resources[]') | length}}"
run_once: true

- name: Update cluster_hosts_flat with running_instance_fact data
Expand Down Expand Up @@ -103,7 +103,7 @@
delegate_to: localhost
run_once: true
vars:
deploy_status_tag: "{{gcp_compute_instance_facts.results | json_query('[].items[?name==`' + item.hostname + '`].labels.deploy_status | [] | [0]')}}"
deploy_status_tag: "{{gcp_compute_instance_info.results | json_query('[].resources[?name==`' + item.hostname + '`].labels.deploy_status | [] | [0]')}}"
with_items: "{{cluster_hosts_flat}}"
when: deploy_status_tag != ""

Expand All @@ -120,13 +120,13 @@
delegate_to: localhost
run_once: true
vars:
created_instance_name: "{{gcp_compute_instance_facts.results | json_query('[].items[?name==`' + item.hostname + '`].name | [] | [0]')}}"
deploy_status_tag: "{{gcp_compute_instance_facts.results | json_query('[].items[?name==`' + item.hostname + '`].labels.deploy_status | [] | [0]')}}"
created_instance_name: "{{gcp_compute_instance_info.results | json_query('[].resources[?name==`' + item.hostname + '`].name | [] | [0]')}}"
deploy_status_tag: "{{gcp_compute_instance_info.results | json_query('[].resources[?name==`' + item.hostname + '`].labels.deploy_status | [] | [0]')}}"
with_items: "{{cluster_hosts_flat}}"
when: created_instance_name != ""

- name: Get GCP instance facts
gcp_compute_instance_facts:
gcp_compute_instance_info:
zone: "{{cluster_vars.region}}-{{item.az_name}}"
filters:
- "name = {{item.hostname}}"
Expand All @@ -136,12 +136,12 @@
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
with_items: "{{cluster_hosts_flat}}"
register: gcp_compute_instance_facts_updated
register: gcp_compute_instance_info_updated
delegate_to: localhost

- set_fact:
current_release_per_instance: "{{gcp_compute_instance_facts_updated.results | json_query(\"[].items[].{value: metadata.items[?key==`release`].value | [0], key: name}\") | items2dict}}"
current_deploy_status_per_instance: "{{gcp_compute_instance_facts_updated.results | json_query(\"[].items[].{value: labels.deploy_status, key: name}\") | items2dict}}"
current_release_per_instance: "{{gcp_compute_instance_info_updated.results | json_query(\"[].resources[].{value: metadata.items[?key==`release`].value | [0], key: name}\") | items2dict}}"
current_deploy_status_per_instance: "{{gcp_compute_instance_info_updated.results | json_query(\"[].resources[].{value: labels.deploy_status, key: name}\") | items2dict}}"

- name: update cluster_hosts_flat with current release and deploy_status
set_fact:
Expand Down
2 changes: 0 additions & 2 deletions roles/clusterbuild/config/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,3 @@
run_once: true
with_items: "{{ cluster_hosts_flat }}"
when: cluster_vars.dns_server == "route53" and instance_to_create is undefined and rescuing_instance is undefined
delegate_to: localhost
run_once: true
28 changes: 14 additions & 14 deletions roles/clusterbuild/create/tasks/gce.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@
- name: Create GCP firewalls
block:
- name: GCP network facts
gcp_compute_network_facts:
gcp_compute_network_info:
filters:
- "name = {{cluster_vars[buildenv].vpc_network_name}}"
project: "{{cluster_vars.project_id}}"
auth_kind: "serviceaccount"
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
register: gcp_compute_network_facts
register: gcp_compute_network_info

- name: "Assert that {{cluster_vars[buildenv].vpc_network_name}} network exists"
assert: { that: "gcp_compute_network_facts['items'] | length > 0", msg: "The {{cluster_vars[buildenv].vpc_network_name}} network must exist (create with ' -e create_gce_network=true')" }
assert: { that: "gcp_compute_network_info['resources'] | length > 0", msg: "The {{cluster_vars[buildenv].vpc_network_name}} network must exist (create with ' -e create_gce_network=true')" }

- name: GCP subnetwork facts
gcp_compute_subnetwork_facts:
gcp_compute_subnetwork_info:
filters:
- "name = {{cluster_vars[buildenv].vpc_subnet_name}}"
project: "{{cluster_vars.project_id}}"
auth_kind: "serviceaccount"
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
register: gcp_compute_subnetwork_facts
register: gcp_compute_subnetwork_info
when: (cluster_vars[buildenv].vpc_subnet_name is defined) and (cluster_vars[buildenv].vpc_subnet_name != "")

- name: "Assert that {{cluster_vars[buildenv].vpc_subnet_name}} subnet exists"
assert: { that: "gcp_compute_subnetwork_facts['items'] | length > 0", msg: "The {{cluster_vars[buildenv].vpc_subnet_name}} subnet must exist" }
assert: { that: "gcp_compute_subnetwork_info['resources'] | length > 0", msg: "The {{cluster_vars[buildenv].vpc_subnet_name}} subnet must exist" }
when: (cluster_vars[buildenv].vpc_subnet_name is defined) and (cluster_vars[buildenv].vpc_subnet_name != "")

- name: Create GCP cluster firewalls
Expand All @@ -61,7 +61,7 @@
description: "{{ item.description }}"
source_ranges: "{{ item.source_ranges | default([]) }}"
source_tags: "{{ item.source_tags | default([]) }}"
network: "{{gcp_compute_network_facts['items'][0]}}"
network: "{{gcp_compute_network_info['resources'][0]}}"
auth_kind: "serviceaccount"
service_account_file: "{{gcp_credentials_file}}"
project: "{{cluster_vars.project_id}}"
Expand Down Expand Up @@ -93,8 +93,8 @@
startup-script: "#! /bin/bash\n\n#Whitelist my inbound IPs\n[ -f /etc/sshguard/whitelist ] && echo x.x.x.x/26 >>/etc/sshguard/whitelist && /bin/systemctl restart sshguard"
ssh-keys: "{{ gce_ssh_username }}: {{ gce_ssh_pubkey }} {{ gce_ssh_username }}"
network_interfaces:
- network: "{{ gcp_compute_network_facts['items'][0] | default({}) }}"
subnetwork: "{{ gcp_compute_subnetwork_facts['items'][0] | default({}) }}"
- network: "{{ gcp_compute_network_info['items'][0] | default({}) }}"
subnetwork: "{{ gcp_compute_subnetwork_info['resources'][0] | default({}) }}"
access_configs: "{%- if cluster_vars.assign_public_ip == 'yes' -%}[{\"name\": \"External NAT\", \"type\": \"ONE_TO_ONE_NAT\"}]{%- else -%}[]{%- endif -%}"
zone: "{{cluster_vars.region}}-{{item.az_name}}"
can_ip_forward : "{{cluster_vars.ip_forward}}"
Expand Down Expand Up @@ -184,7 +184,7 @@
# Need this because the gcp_compute_instance module does not return all the facts if the instance is already existing (only if newly created)
# Note: 'scopes' comes from here (https://developers.google.com/identity/protocols/googlescopes#computev1)
- name: Get GCP instance facts
gcp_compute_instance_facts:
gcp_compute_instance_info:
zone: "{{cluster_vars.region}}-{{item.az_name}}"
filters:
- "name = {{item.hostname}}"
Expand All @@ -193,16 +193,16 @@
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
with_items: "{{cluster_hosts_flat}}"
register: gcp_compute_instance_facts
register: gcp_compute_instance_info
run_once: true

# - debug: msg={{gcp_compute_instance_facts}}
# - debug: msg={{gcp_compute_instance_info}}

- set_fact:
dynamic_inventory_flat: |
{%- if cluster_vars.inventory_ip == 'private' -%}
{{ gcp_compute_instance_facts.results | json_query('[*].items[].{hosttype: metadata.items[?key==`hosttype`].value|[0], hostname: name, private_ip: networkInterfaces[0].networkIP, public_ip: networkInterfaces[0].accessConfigs[0].natIP, inventory_ip: networkInterfaces[0].networkIP}') }}
{{ gcp_compute_instance_info.results | json_query('[*].resources[].{hosttype: metadata.items[?key==`hosttype`].value|[0], hostname: name, private_ip: networkInterfaces[0].networkIP, public_ip: networkInterfaces[0].accessConfigs[0].natIP, inventory_ip: networkInterfaces[0].networkIP}') }}
{%- else -%}
{{ gcp_compute_instance_facts.results | json_query('[*].items[].{hosttype: metadata.items[?key==`hosttype`].value|[0], hostname: name, private_ip: networkInterfaces[0].networkIP, public_ip: networkInterfaces[0].accessConfigs[0].natIP, inventory_ip: networkInterfaces[0].accessConfigs[0].natIP}') }}
{{ gcp_compute_instance_info.results | json_query('[*].resources[].{hosttype: metadata.items[?key==`hosttype`].value|[0], hostname: name, private_ip: networkInterfaces[0].networkIP, public_ip: networkInterfaces[0].accessConfigs[0].natIP, inventory_ip: networkInterfaces[0].accessConfigs[0].natIP}') }}
{%- endif -%}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

- block:
- name: Get GCP instance facts
gcp_compute_instance_facts:
gcp_compute_instance_info:
zone: "{{cluster_vars.region}}-{{item.az_name}}"
filters:
- "name = {{item.hostname}}*"
Expand All @@ -49,7 +49,7 @@
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
with_items: "{{cluster_hosts_flat}}"
register: gcp_compute_instance_facts
register: gcp_compute_instance_info
delegate_to: localhost

- name: delete maintenance_mode label
Expand All @@ -65,7 +65,7 @@
delegate_to: localhost
run_once: true
vars:
maintenance_mode_tag: "{{gcp_compute_instance_facts.results | json_query('[].items[?name==`' + item.hostname + '`].labels.maintenance_mode | [] | [0]')}}"
maintenance_mode_tag: "{{gcp_compute_instance_info.results | json_query('[].resources[?name==`' + item.hostname + '`].labels.maintenance_mode | [] | [0]')}}"
with_items: "{{cluster_hosts_flat}}"
when: maintenance_mode_tag != ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- debug: msg="Stop {{host_to_stop.hostname}}, release {{release_to_stop}}"

- name: gcp_gather | Get GCP instance facts
gcp_compute_instance_facts:
gcp_compute_instance_info:
zone: "{{cluster_vars.region}}-{{cluster_hosts_flat | json_query('[? hostname == `' + host_to_stop.hostname + '`].az_name|[0]')}}"
filters:
- "name = {{host_to_stop.hostname}}*"
Expand All @@ -12,10 +12,10 @@
auth_kind: "serviceaccount"
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
register: existing_gcp
register: gcp_compute_instance_info
delegate_to: localhost
run_once: true

- name: "set argument variables for {{mainclusteryml}}"
set_fact:
instance_to_create: "{{ existing_gcp | json_query('items[0].name') | regex_replace('-(?!.*-).*') + '-'+epoch_time}}"
instance_to_create: "{{ gcp_compute_instance_info | json_query('resources[0].name') | regex_replace('-(?!.*-).*') + '-'+epoch_time}}"
14 changes: 7 additions & 7 deletions roles/clusterbuild/redeploy/tasks/gce_del.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- name: Delete GCP instance
block:
- name: Get GCP instance facts
gcp_compute_instance_facts:
gcp_compute_instance_info:
zone: "{{cluster_vars.region}}-{{item.az_name}}"
filters:
- "name = {{item.hostname | regex_replace('-(?!.*-).*')}}*"
Expand All @@ -13,13 +13,13 @@
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
with_items: "{{cluster_hosts_flat}}"
register: gcp_compute_instance_facts
register: gcp_compute_instance_info
delegate_to: localhost
run_once: true

- name: Remove deletion protection
command: "gcloud compute instances update {{item.name}} --no-deletion-protection --zone {{item.zone | regex_replace('.*/(.*)$', '\\1')}}"
with_items: "{{gcp_compute_instance_facts.results | json_query('[].items[]')}}"
with_items: "{{gcp_compute_instance_info.results | json_query('[].resources[]')}}"
delegate_to: localhost
run_once: true
when: "cluster_vars[buildenv].deletion_protection == 'yes'"
Expand All @@ -37,8 +37,8 @@
delegate_to: localhost
run_once: true
vars:
maintenance_mode_tag: "{{gcp_compute_instance_facts.results | json_query('[].items[?name==`' + item.name + '`].labels.maintenance_mode | [] | [0]')}}"
with_items: "{{gcp_compute_instance_facts.results | json_query('[].items[]')}}"
maintenance_mode_tag: "{{gcp_compute_instance_info.results | json_query('[].resources[?name==`' + item.name + '`].labels.maintenance_mode | [] | [0]')}}"
with_items: "{{gcp_compute_instance_info.results | json_query('[].resources[]')}}"
when: maintenance_mode_tag != ""

- name: remove_maintenance_mode
Expand All @@ -53,7 +53,7 @@
state: present
delegate_to: localhost
run_once: true
with_items: "{{gcp_compute_instance_facts.results | json_query('[].items[]')}}"
with_items: "{{gcp_compute_instance_info.results | json_query('[].resources[]')}}"

- name: Delete GCE VMs asynchronously
gcp_compute_instance:
Expand All @@ -64,7 +64,7 @@
service_account_file: "{{gcp_credentials_file}}"
state: "absent"
register: gcp_compute_instance
with_items: "{{gcp_compute_instance_facts.results | json_query('[].items[]')}}"
with_items: "{{gcp_compute_instance_info.results | json_query('[].resources[]')}}"
async: 7200
poll: 0

Expand Down
12 changes: 6 additions & 6 deletions roles/clusterbuild/redeploy/tasks/gce_rescue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- debug: msg="Rescuing"

- name: gce_restore | Get GCP instance facts
gcp_compute_instance_facts:
gcp_compute_instance_info:
zone: "{{cluster_vars.region}}-{{item.az_name}}"
filters:
- "name = {{item.hostname | regex_replace('-(?!.*-).*')}}*"
Expand All @@ -13,32 +13,32 @@
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
with_items: "{{cluster_hosts_flat}}"
register: rollback_instances
register: gcp_compute_instance_info
delegate_to: localhost
run_once: true

- name: gce_restore | set hosts_to_rollback_stopped
set_fact: hosts_to_rollback_stopped={{rollback_instances | json_query('results[].items[?labels.deploy_status == `old` && status==`TERMINATED`].name | []') | list | sort(reverse=True)}}
set_fact: hosts_to_rollback_stopped={{gcp_compute_instance_info | json_query('results[].resources[?labels.deploy_status == `old` && status==`TERMINATED`].name | []') | list | sort(reverse=True)}}

- name: gce_restore | set expected nodes
set_fact:
remove_first: "{{hosts_running|int - current_cluter_size|int}}"
vars:
hosts_running: "{{rollback_instances | json_query('results[].items[?status==`RUNNING`].name | []') | length}}"
hosts_running: "{{gcp_compute_instance_info | json_query('results[].resources[?status==`RUNNING`].name | []') | length}}"

- name: gce_restore | remove spare instance (if exixsts)
block:

- set_fact:
host_to_redeploy: "{'hostname': \"{{host_to_redeploy_hostname}}\"}"
vars:
host_to_redeploy_hostname: "{{rollback_instances | json_query('results[].items[?labels.deploy_status == `new` && status==`RUNNING`].name | []') | list | sort(reverse=True) | first}}"
host_to_redeploy_hostname: "{{gcp_compute_instance_info | json_query('results[].resources[?labels.deploy_status == `new` && status==`RUNNING`].name | []') | list | sort(reverse=True) | first}}"

- set_fact:
j_recover_arg: "[?starts_with(@, '{{host_to_redeploy.hostname | regex_replace('-(?!.*-).*')}}')] | [0]"

- set_fact:
host_to_recover: "{{rollback_instances | json_query('results[*].items[].name') | difference([host_to_redeploy.hostname])| to_json | from_json | json_query(j_recover_arg)}}"
host_to_recover: "{{gcp_compute_instance_info | json_query('results[*].resources[].name') | difference([host_to_redeploy.hostname])| to_json | from_json | json_query(j_recover_arg)}}"

- name: Replace CNAME
nsupdate:
Expand Down
6 changes: 3 additions & 3 deletions roles/clusterbuild/redeploy/tasks/gce_stop_instance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
block:

- name: Get GCP instance facts
gcp_compute_instance_facts:
gcp_compute_instance_info:
zone: "{{cluster_vars.region}}-{{host_to_stop.hostname | regex_replace('.*-([a-z])[0-9]-.*', '\\1')}}"
filters:
- "name = {{host_to_stop.hostname}}*"
Expand All @@ -16,7 +16,7 @@
auth_kind: "serviceaccount"
service_account_file: "{{gcp_credentials_file}}"
scopes: ["https://www.googleapis.com/auth/compute.readonly"]
register: gcp_compute_instance_facts
register: gcp_compute_instance_info
delegate_to: localhost

- name: Delete maintenance_mode label
Expand All @@ -32,7 +32,7 @@
delegate_to: localhost
run_once: true
vars:
maintenance_mode_tag: "{{gcp_compute_instance_facts | json_query('items[?name==`' + host_to_stop.hostname + '`].labels.maintenance_mode | [] | [0]')}}"
maintenance_mode_tag: "{{gcp_compute_instance_info | json_query('resources[?name==`' + host_to_stop.hostname + '`].labels.maintenance_mode | [] | [0]')}}"
when: maintenance_mode_tag != ""

- name: Set_maintenance_mode
Expand Down

0 comments on commit 6ccb7bb

Please sign in to comment.