Skip to content

Commit

Permalink
Merge pull request #586 from oalbrigt/fence_aws-improve-list-status
Browse files Browse the repository at this point in the history
fence_aws: improve list, list-status and status actions
  • Loading branch information
oalbrigt authored Jun 17, 2024
2 parents 6b577cd + f1ef26c commit 438c91a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
31 changes: 19 additions & 12 deletions agents/aws/fence_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
logger.addHandler(SyslogLibHandler())
logging.getLogger('botocore.vendored').propagate = False

status = {
"running": "on",
"stopped": "off",
"pending": "unknown",
"stopping": "unknown",
"shutting-down": "unknown",
"terminated": "unknown"
}

def get_instance_id(options):
try:
token = requests.put('http://169.254.169.254/latest/api/token', headers={"X-aws-ec2-metadata-token-ttl-seconds" : "21600"}).content.decode("UTF-8")
Expand All @@ -45,11 +54,14 @@ def get_nodes_list(conn, options):
filter_key = options["--filter"].split("=")[0].strip()
filter_value = options["--filter"].split("=")[1].strip()
filter = [{ "Name": filter_key, "Values": [filter_value] }]
for instance in conn.instances.filter(Filters=filter):
result[instance.id] = ("", None)
else:
for instance in conn.instances.all():
result[instance.id] = ("", None)
logging.debug("Filter: {}".format(filter))

for instance in conn.instances.filter(Filters=filter if 'filter' in vars() else []):
instance_name = ""
for tag in instance.tags or []:
if tag.get("Key") == "Name":
instance_name = tag["Value"]
result[instance.id] = (instance_name, status[instance.state["Name"]])
except ClientError:
fail_usage("Failed: Incorrect Access Key or Secret Key.")
except EndpointConnectionError:
Expand All @@ -67,12 +79,7 @@ def get_power_status(conn, options):
instance = conn.instances.filter(Filters=[{"Name": "instance-id", "Values": [options["--plug"]]}])
state = list(instance)[0].state["Name"]
logger.debug("Status operation for EC2 instance %s returned state: %s",options["--plug"],state.upper())
if state == "running":
return "on"
elif state == "stopped":
return "off"
else:
return "unknown"
return status[state]

except ClientError:
fail_usage("Failed: Incorrect Access Key or Secret Key.")
Expand Down Expand Up @@ -146,7 +153,7 @@ def define_new_opts():
all_opt["filter"] = {
"getopt" : ":",
"longopt" : "filter",
"help" : "--filter=[key=value] Filter (e.g. vpc-id=[vpc-XXYYZZAA]",
"help" : "--filter=[key=value] Filter (e.g. vpc-id=[vpc-XXYYZZAA])",
"shortdesc": "Filter for list-action",
"required": "0",
"order": 5
Expand Down
8 changes: 4 additions & 4 deletions lib/fencing.py.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,14 +979,14 @@ def fence_action(connection, options, set_power_fn, get_power_fn, get_outlet_lis

if options["--action"] == "list":
try:
print(outlet_id + options["--separator"] + alias)
print("{}{}{}".format(outlet_id, options["--separator"], alias))
except UnicodeEncodeError as e:
print((outlet_id + options["--separator"] + alias).encode("utf-8"))
print("{}{}{}".format(outlet_id, options["--separator"], alias).encode("utf-8"))
elif options["--action"] == "list-status":
try:
print(outlet_id + options["--separator"] + alias + options["--separator"] + status)
print("{}{}{}{}{}".format(outlet_id, options["--separator"], alias, options["--separator"], status))
except UnicodeEncodeError as e:
print((outlet_id + options["--separator"] + alias).encode("utf-8") + options["--separator"] + status)
print("{}{}{}{}{}".format(outlet_id, options["--separator"], alias, options["--separator"], status).encode("utf-8"))

return result

Expand Down

0 comments on commit 438c91a

Please sign in to comment.