Skip to content

Commit

Permalink
Update discovery modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ezh committed Feb 23, 2020
1 parent c738fee commit bbaff04
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
22 changes: 16 additions & 6 deletions cloudselect/discovery/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ def __init__(self):
def run(self):
"""Collect AWS instances."""
self.log.debug("Discover AWS instances")
return list(self.instances())
instances = list(self.instances())
representation = instances[-1]
del instances[-1]
return (representation, instances)

def instances(self):
"""Collect AWS instances."""
# Array with maximum field length for each element in representation
fields_length = []
for i in self.find():
instance_id = i["InstanceId"]
metadata = self.get_metadata(i)
Expand All @@ -42,15 +47,20 @@ def instances(self):
user = self.get_user(i, config)

representation = [instance_id, ip]
for field in self.config().get("fzf_extra", []):
if field in i:
representation.append(i[field])
elif field.startswith("tag:"):
representation.append(self.tag(i, field.replace("tag:", "")))
self.enrich_representation(representation, metadata)

# Update maximum field length
for idx, value in enumerate(representation):
if idx >= len(fields_length):
fields_length.append(len(value))
else:
fields_length[idx] = max(fields_length[idx], len(value))

instance = CloudInstance(
instance_id, ip, None, metadata, representation, key, user, 22,
)
yield instance
yield fields_length

@staticmethod
def find():
Expand Down
15 changes: 14 additions & 1 deletion cloudselect/discovery/hetzner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ def __init__(self):
def run(self):
"""Collect Hetzner instances."""
self.log.debug("Discover Hetzner instances")
return list(self.instances())
instances = list(self.instances())
representation = instances[-1]
del instances[-1]
return (representation, instances)

def instances(self):
"""Collect Hetzner instances."""
# Array with maximum field length for each element in representation
fields_length = []
for i in self.find():
instance_id = i["id"]
metadata = i
Expand All @@ -47,10 +52,18 @@ def instances(self):
representation = [instance_id, ip]
self.enrich_representation(representation, metadata)

# Update maximum field length
for idx, value in enumerate(representation):
if idx >= len(fields_length):
fields_length.append(len(value))
else:
fields_length[idx] = max(fields_length[idx], len(value))

instance = CloudInstance(
instance_id, ip, None, metadata, representation, key, user, port,
)
yield instance
yield fields_length

@staticmethod
def find():
Expand Down
18 changes: 17 additions & 1 deletion cloudselect/discovery/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ def __init__(self):
def run(self):
"""Collect instances from shell output."""
self.logger.debug("Discover local instances")
return list(self.instances())
instances = list(self.instances())
representation = instances[-1]
del instances[-1]
return (representation, instances)

def instances(self):
"""Collect instances from shell output."""
config = Container.config.discovery

# Array with maximum field length for each element in representation
fields_length = []
stdout = subprocess.check_output(config.cmd(), shell=True) # noqa: DUO116
output = sorted(
filter(lambda item: item.strip() != "", stdout.decode().split("\n")),
Expand All @@ -42,12 +47,23 @@ def instances(self):
ip = host
key = self.get_key(host)
metadata = {"host": host}

representation = [host_id, host]
self.enrich_representation(representation, metadata)

# Update maximum field length
for idx, value in enumerate(representation):
if idx >= len(fields_length):
fields_length.append(len(value))
else:
fields_length[idx] = max(fields_length[idx], len(value))

user = self.get_user(host)
instance = CloudInstance(
host_id, ip, None, metadata, representation, key, user, None,
)
yield instance
yield fields_length

def get_key(self, host):
"""Get key for ssh host."""
Expand Down

0 comments on commit bbaff04

Please sign in to comment.