Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Add a couple more grains - vpc and nvme #205

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions grains/ec2_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
import socket
import json
import os
try:
from http.client import HTTPConnection, BadStatusLine
except ImportError:
Expand Down Expand Up @@ -67,6 +68,7 @@ def _get_ec2_hostinfo(path=""):
return line
else:
d[_dash_to_snake_case(line[:-1])] = _get_ec2_hostinfo(path + line)

return d


Expand Down Expand Up @@ -142,6 +144,31 @@ def _get_instance_identity():

return result

def _get_ec2_is_vpc():
"""
Determine if this instance is in a VPC or not
"""

resp = _call_aws('/latest/meta-data/mac')
mac = resp.read().decode('utf-8')

resp = _call_aws('/latest/meta-data/network/interfaces/macs/' + mac + '/vpc-id')

if resp.status == 200:
return True
elif resp.status == 404:
return False
else:
raise BadStatusLine("Could not determine if instance is in VPC")

def _get_ec2_ebs_is_nvme():
"""
Determine if this instance mounts disks as nvme
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html
"""
cmd = "lsblk | awk '{print $1}' | grep -q nvme"

return os.system(cmd) == 0

def ec2_info():
"""
Expand All @@ -152,6 +179,8 @@ def ec2_info():
grains.update({'user-data': _get_ec2_user_data()})
grains.update(_get_ec2_hostinfo())
grains['instance_identity'].update(_get_instance_identity())
grains['is_vpc'] = _get_ec2_is_vpc()
grains['is_nvme_disk'] = _get_ec2_ebs_is_nvme()
return {'ec2': grains}

except BadStatusLine as error:
Expand Down