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