Commit cfc325cc authored by Noe Nieto's avatar Noe Nieto 💬

Command status prints the IPAddress if available

parent 5b298e33
......@@ -7,6 +7,8 @@ import subprocess
import json
import shutil
from pathlib import Path
import ipaddress
vagrant_template = """
Vagrant.configure("2") do |config|
......@@ -39,6 +41,14 @@ def only_if_box_exist(func):
return wrapper
def is_ipaddress(ip_addr):
try:
ipaddress.ip_address(ip_addr)
except ValueError:
return False
return True
def print_status():
"""
Prints the status of all the virtual machines managed by shipyard
......@@ -50,11 +60,25 @@ def print_status():
)
count = 0
if vgt_db.exists():
print('hostname \t State')
print('hostname \t State\t IP Address')
db = json.loads(vgt_db.open().read())
for k, v in db['machines'].items():
if v['vagrantfile_path'] in shpyrd_paths:
print('{name} \t {state}'.format(name=v['name'], state=v['state']))
vagrant_wd = v['vagrantfile_path']
if vagrant_wd in shpyrd_paths:
# get the ip address
cp = subprocess.run(
['vagrant','ssh-config'], cwd=vagrant_wd,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True
)
if cp.returncode != 0:
ip_addr = "<N/A>"
else:
for fld in cp.stdout.split():
if is_ipaddress(str(fld)):
ip_addr = str(fld)
break
print(f"{v['name']} \t {v['state']}\t {ip_addr}")
count+=1
print('Total {} machine(s)'.format(count))
......@@ -107,7 +131,7 @@ def run_playbook(hostname, playbook):
@only_if_box_exist
def invoke_shell(hostname):
"""
run a playbook against one of the boxes
Open a SSH session to the hostname
"""
dot_shipyard = Path(os.environ['HOME'], '.shipyard')
vagrant_wd = dot_shipyard.joinpath(hostname)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment