Changing ResourceManager naming for platform::ResourceName
[nepi.git] / src / nepi / resources / linux / traceroute.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState 
22 from nepi.resources.linux.application import LinuxApplication
23 from nepi.util.timefuncs import tnow
24
25 import os
26 import socket
27
28 @clsinit_copy
29 class LinuxTraceroute(LinuxApplication):
30     _rtype = "linux::Traceroute"
31
32     @classmethod
33     def _register_attributes(cls):
34         countinuous = Attribute("continuous",
35             "Run traceroute in a while loop",
36             type = Types.Bool,
37             default = False,
38             flags = Flags.Design)
39
40         print_timestamp = Attribute("printTimestamp",
41             "Print timestamp before running traceroute",
42             type = Types.Bool,
43             default = False,
44             flags = Flags.Design)
45
46         use_ip = Attribute("useIP",
47             "Use the IP address instead of the host domain name. "
48             "Useful for environments were dns resolution problems occur "
49             "frequently",
50             type = Types.Bool,
51             default = False,
52             flags = Flags.Design)
53
54         target = Attribute("target",
55             "Traceroute target host (host that will be pinged)",
56             flags = Flags.Design)
57
58         early_start = Attribute("earlyStart",
59             "Start ping as early as deployment. ",
60             type = Types.Bool,
61             default = False,
62             flags = Flags.Design)
63
64         cls._register_attribute(countinuous)
65         cls._register_attribute(print_timestamp)
66         cls._register_attribute(use_ip)
67         cls._register_attribute(target)
68         cls._register_attribute(early_start)
69
70     def __init__(self, ec, guid):
71         super(LinuxTraceroute, self).__init__(ec, guid)
72         self._home = "traceroute-%s" % self.guid
73
74     def upload_start_command(self):
75         super(LinuxTraceroute, self).upload_start_command()
76         
77         if self.get("earlyStart") == True:
78             self._run_in_background()
79
80     def do_deploy(self):
81         if not self.get("command"):
82             self.set("command", self._start_command)
83
84         if not self.get("depends"):
85             self.set("depends", "traceroute")
86
87         super(LinuxTraceroute, self).do_deploy()
88
89     def do_start(self):
90         if self.get("earlyStart") == True:
91             if self.state == ResourceState.READY:
92                 command = self.get("command")
93                 self.info("Starting command '%s'" % command)
94
95                 self.set_started()
96             else:
97                 msg = " Failed to execute command '%s'" % command
98                 self.error(msg, out, err)
99                 raise RuntimeError, msg
100         else:
101            super(LinuxTraceroute, self).do_start()
102
103     @property
104     def _start_command(self):
105         args = []
106         if self.get("continuous") == True:
107             args.append("while true; do ")
108         if self.get("printTimestamp") == True:
109             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
110         args.append("traceroute")
111
112         target = self.get("target")
113         if self.get("useIP") == True:
114             target = socket.gethostbyname(target)
115         args.append(target)
116         
117         if self.get("continuous") == True:
118             args.append("; sleep 2 ; done ")
119
120         command = " ".join(args)
121
122         return command
123
124     def valid_connection(self, guid):
125         # TODO: Validate!
126         return True
127