38c40a79345db3330c89311e61e46727bc3bf938
[nepi.git] / src / nepi / testbeds / planetlab / application.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from constants import TESTBED_ID
5 import plcapi
6 import operator
7 import os
8
9 from nepi.util.constants import STATUS_NOT_STARTED, STATUS_RUNNING, \
10         STATUS_FINISHED
11
12 class Application(object):
13     def __init__(self, api=None):
14         if not api:
15             api = plcapi.PLCAPI()
16         self._api = api
17         
18         # Attributes
19         self.command = None
20         self.sudo = False
21         
22         self.stdout = None
23         self.stderr = None
24         
25         # Those are filled when an actual node is connected
26         self.node = None
27         
28         # Those are filled when the app is started
29         #   Having both pid and ppid makes it harder
30         #   for pid rollover to induce tracking mistakes
31         self._pid = None
32         self._ppid = None
33         self._stdout_path = None
34         self._stderr_path = None
35     
36     def __str__(self):
37         return "%s<command:%s%s>" % (
38             self.__class__.__name__,
39             "sudo " if self.sudo else "",
40             self.command,
41         )
42     
43     def validate(self):
44         pass
45
46     def start(self):
47         pass
48     
49     def status(self):
50         return STATUS_FINISHED
51     
52     def kill(self):
53         status = self.status()
54         if status == STATUS_RUNNING:
55             # TODO: kill by pid & ppid
56             pass
57     
58     def remote_trace_path(self, whichtrace):
59         if whichtrace == 'stdout':
60             tracefile = self._stdout_path
61         elif whichtrace == 'stderr':
62             tracefile = self._stderr_path
63         else:
64             tracefile = None
65         
66         return tracefile
67     
68     def sync_trace(self, local_dir, whichtrace):
69         tracefile = self.remote_trace_path(whichtrace)
70         if not tracefile:
71             return None
72         
73         local_path = os.join(local_dir, tracefile)
74         
75         # TODO: sync files
76         f = open(local_path, "w")
77         f.write("BLURP!")
78         f.close()
79         
80         return local_path
81     
82