Beispiel #1
0
def cattle_wake(lassod, *hostnames):
    """Wake up specified cattle. Return cattle state as a list."""
    output = []
    for host in hostnames:
        client = CattleClient(host)
        output.append(client.wake())
    return output
Beispiel #2
0
def cattle_state(lassod, *hostnames):
    """Get the state of specified cattle. Return cattle state as a list."""
    output = []
    for host in hostnames:
        client = CattleClient(host)
        output.append(client.state())
    return output
Beispiel #3
0
def cattle_sleep(lassod, *hostnames):
    """Put the specified cattle to sleep. Return cattle state as a list."""
    output = []
    for host in hostnames:
        client = CattleClient(host)
        output.append(client.sleep())
    return output
Beispiel #4
0
 def kill_hook(task):
     self.info("Stopped task %d." % task.id)
     self.queue.remove(task.id)
     if task.status == task.RUNNING:
         cattle_id = task.running
         cattle = db.query(Cattle).filter(Cattle.id==cattle_id).first()
         client = CattleClient(cattle.hostname)
         if client.kill_task(task.id):
             output.append(task.id)
             print task.id, 'has been killed.'
Beispiel #5
0
def task_kill(lassod, *ids):
    """Kill task by id number."""
    db = Session()
    output = list()
    tasks = db.query(Task).filter(Task.id.in_(ids)).all()
    for task in tasks:
        if task.running:
            cattle = db.query(Cattle).filter(Cattle.id==task.running).first()
            client = CattleClient(cattle.hostname)
            client.kill_task(task.id)
            output.append(True)
        else:
            output.append(False)
    db.close()
    return output
Beispiel #6
0
 def __init__(self, task_id):
     self.task_id = task_id
     self.client = CattleClient()
     self.connect()
     self.monitor()
     self.disconnect()
     self.return_code = 2
     self.run_time = -1
Beispiel #7
0
class ProcessMonitor(object):
    def __init__(self, task_id):
        self.task_id = task_id
        self.client = CattleClient()
        self.connect()
        self.monitor()
        self.disconnect()
        self.return_code = 2
        self.run_time = -1

    def connect(self):
        task_data = self.client.monitor_connect(self.task_id)
        self.command = task_data['command']
        self.uid = task_data['uid']
        self.gid = task_data['gid']
        self.stdout_file_path = task_data['stdout_file_path']
        self.stderr_file_path = task_data['stderr_file_path']

    def disconnect(self):
        self.client.monitor_disconnect(self.task_id, self.run_time, self.return_code)

    def monitor(self):
        #Set uid
        try:
            os.setgid(self.gid)
        except OSError:
            print "ERROR: Could not set group id [%d] for task %d." % (self.gid, self.task_id)
            sys.exit(1)

        #Set gid
        try:
            os.setuid(self.uid)
        except OSError:
            print "ERROR: Could not set user id [%d] for task %d." % (self.uid, self.task_id)
            sys.exit(1)

        #Setup user environment
        try:
            os.environ['USER'] = pwd.getpwuid(self.uid)[0]
        except KeyError:
            print "ERROR: Could not find %d in /etc/passwd" % uid
            sys.exit(1)

        stdout_fd = open(self.stdout_file_path, 'w')
        stderr_fd = open(self.stderr_file_path, 'w')
        start_time = time.time()
        proc = Popen(self.command, shell=True, stdout=stdout_fd, stderr=stderr_fd)
        self.client.monitor_start(self.task_id, proc.pid)
        while proc.poll() is None:
            if timeout():
                probes = {}
                probes['memory'] = memory(proc.pid)
                probes['pcpu'] = cpu_usage(proc.pid)
                probes['pid'] = proc.pid
                self.client.monitor_probe(self.task_id, probes)
            time.sleep(0.3333)
        self.run_time = time.time() - start_time
        self.return_code = proc.returncode
        stdout_fd.close()
        stderr_fd.close()