1. Frank Becker
  2. MyHost

Commits

Frank Becker  committed 61cb44b

added first Tapper model, wrote tapper scraper

  • Participants
  • Parent commits ceb7757
  • Branches default

Comments (0)

Files changed (2)

File src/hosts/models.py

View file
 class Tapper(models.Model):
     """Tapper data."""
 
+    HOST_STATI = (
+        ('A', u'activated'),
+        ('D', u'deactivated'),
+    )
+
     tapper_id = models.PositiveIntegerField(
         verbose_name=_(u"Tapper ID"),
         max_length=5,
+        unique=True,
+        )
+
+    tags = TaggableManager(
+        blank=True,
+    )
+
+    host_status = models.CharField(
+        verbose_name=_(u"Host status"),
+        max_length=1,
+        choices=HOST_STATI,
+        blank=True,
+        null=True,
+    )
+
+    tapper_status = models.CharField(
+        verbose_name=_(u"Testrun ID"),
+        help_text=_(u"Tapper testrun id or free"),
+        max_length=12,
+        blank=True,
+        null=True,
+    )
+
+    comment = models.CharField(
+        verbose_name=_(u"Comment"),
+        help_text=_(u"max. 100 char comment"),
+        max_length=100,
+        blank=True,
+        null=True,
+        )
+
+    queues = models.CharField(
+        verbose_name=_(u"Queues"),
+        help_text=_(u"comma separated Tapper queues"),
+        max_length=100,
+        blank=True,
+        null=True,
         )
 
 

File src/tapper_scraper.py

View file
+# -*- coding: utf-8 -*-
+
+"""
+Python source code - replace this with a description of the code and write
+the code below this text.
+"""
+from StringIO import StringIO
+import os
+import paramiko
+import pickle
+from hosts.models import Host, Tapper
+
+__author__ = "Frank Becker <fb@alien8.de>"
+__version__ = ": 0.0 $"
+__date__ = ": YDATE $"
+__copyright__ = "Copyright (c) 2012 Frank Becker"
+__license__ = "Python"
+
+
+
+def get_tapper_hosts():
+    """
+
+    Args:
+
+
+    Returns:
+
+    """
+    cmd = '/bin/bash -l -c "tapper-testrun listhost -v"'
+    host = 'tapper'
+    user = 'tapper'
+    passwd = 'tapper'
+    ssh = paramiko.SSHClient()
+
+    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+    ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
+    ssh.connect(host, username=user, password=passwd)
+    stdin, stdout, stderr = ssh.exec_command(cmd)
+    stdin.flush()
+    error = stderr.read()
+    #Reading the error stream of the executed command
+    if len(error):
+        print "Error: {0}".format(error,)
+    ssh.close()
+    return stdout
+
+def update_host(hostname, data):
+    """
+
+    Args:
+        hostname
+
+    Returns:
+
+    """
+    host = Host.objects.get(name=hostname)
+    try:
+        tapper = Tapper.objects.get(tapper_id=data[0])
+    except Tapper.DoesNotExist:
+        tapper = Tapper(tapper_id=data[0])
+    tapper.tags.clear()
+    [tapper.tags.add(t) for t in data[1].split(',')]
+    tapper.host_status = data[2]
+    tapper.tapper_status = data[3]
+    tapper.comment = data[4]
+    tapper.queues = data[5]
+    tapper.save()
+
+    host.tapper = tapper
+    host.save()
+
+def main():
+    """
+    main()
+    """
+    #tapper_listhosts = get_tapper_hosts()
+    #tapper_listhosts = tapper_listhosts.readlines()
+    #pickle.dump(tapper_listhosts, open( "tapper_listhosts.pickle", "wb"))
+    tapper_listhosts = StringIO()
+    tapper_listhosts.writelines(pickle.load(open( "tapper_listhosts.pickle", "rb")))
+    tapper_listhosts.seek(0)
+    # skip first 2 lines
+    print tapper_listhosts.readline()
+    tapper_listhosts.readline()
+
+    tapper_hosts = {}
+    for line in tapper_listhosts.readlines():
+        (host_id,
+        hostname,
+        tags,
+        host_status,
+        tapper_status,
+        comment,
+        tapper_queues) = [e.strip() for e in line.split('|')]
+
+        tapper_host_data = (host_id, tags, host_status, tapper_status, comment, tapper_queues)
+        tapper_hosts[hostname] = tapper_host_data
+
+    for host, data in tapper_hosts.items():
+        #print host, data
+        update_host(host, data)
+
+if __name__ == "__main__":
+    main()