applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / linux / ping.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
26 @clsinit_copy
27 class LinuxPing(LinuxApplication):
28     _rtype = "linux::Ping"
29
30     @classmethod
31     def _register_attributes(cls):
32         count = Attribute("count",
33             "Sets ping -c option. Determines the number of ECHO_REQUEST "
34             "packates to send before stopping.",
35             type = Types.Integer,
36             flags = Flags.Design)
37
38         mark = Attribute("mark",
39             "Sets ping -m option. Uses 'mark' to tag outgoing packets. ",
40             flags = Flags.Design)
41
42         interval = Attribute("interval",
43             "Sets ping -i option. Leaves interval seconds between "
44             "successive ECHO_REUQEST packets. ",
45             flags = Flags.Design)
46
47         address = Attribute("address",
48             "Sets ping -I option. Sets ECHO_REQUEST packets souce address "
49             "to the specified interface address ",
50             flags = Flags.Design)
51
52         preload = Attribute("preload",
53             "Sets ping -l option. Sends preload amount of packets "
54             "without waiting for a reply ",
55             flags = Flags.Design)
56
57         numeric = Attribute("numeric",
58             "Sets ping -n option. Disables resolution of host addresses into "
59             "symbolic names. ",
60             type = Types.Bool,
61             default = False,
62             flags = Flags.Design)
63
64         pattern = Attribute("pattern",
65             "Sets ping -p option. Species a up to 16 ''pad'' bytes to fill "
66             "out sent packets. ",
67             flags = Flags.Design)
68
69         printtmp = Attribute("printTimestamp",
70             "Sets ping -D option. Prints timestamp befor each line as: "
71             "unix time + microseconds as in gettimeofday ",
72             type = Types.Bool,
73             default = False,
74             flags = Flags.Design)
75
76         tos = Attribute("tos",
77             "Sets ping -Q option. Sets Quality of Service related bits in ICMP "
78             "datagrams. tos can be either a decimal or hexadecime number ",
79             flags = Flags.Design)
80
81         quiet = Attribute("quiet",
82             "Sets ping -q option. Disables ping standard output ",
83             type = Types.Bool,
84             default = False,
85             flags = Flags.Design)
86
87         rec_route = Attribute("recordRoute",
88             "Sets ping -R option. Includes the RECORD_ROUTE option in the "
89             "ECHO REQUEST packet and displays route buffer on the Disables "
90             "ping standard output.",
91             type = Types.Bool,
92             default = False,
93             flags = Flags.Design)
94
95         route_bypass = Attribute("routeBypass",
96             "Sets ping -r option. Bypasses normal routing tables and sends "
97             "ECHO REQUEST packets directly yo a host on an attached interface. ",
98             type = Types.Bool,
99             default = False,
100             flags = Flags.Design)
101
102         packetsize = Attribute("packetSize",
103             "Sets ping -s option. Specifies the number of data bytes to be "
104             "sent. Defaults to 56. ",
105             flags = Flags.Design)
106
107         sendbuff = Attribute("sendBuff",
108             "Sets ping -S option. Specifies the number of packets to buffer. "
109             "Defaults to one. ",
110             flags = Flags.Design)
111
112         ttl = Attribute("ttl",
113             "Sets ping -t option. Specifies the IP Time to Live for the "
114             "packets. ",
115             flags = Flags.Design)
116
117         timestamp = Attribute("timestamp",
118             "Sets ping -T option. Sets special IP timestamp options. ",
119             flags = Flags.Design)
120
121         hint = Attribute("hint",
122             "Sets ping -M option. Selects Path MTU Discovery strategy. ",
123             flags = Flags.Design)
124
125         full_latency = Attribute("fullLatency",
126             "Sets ping -U option. Calculates round trip time taking into "
127             "account the full user-to-user latency instead of only the "
128             "network round trip time. ",
129             type = Types.Bool,
130             default = False,
131             flags = Flags.Design)
132
133         verbose = Attribute("verbose",
134             "Sets ping -v option. Verbose output. ",
135             type = Types.Bool,
136             default = False,
137             flags = Flags.Design)
138
139         flood = Attribute("flood",
140             "Sets ping -f option. Flood ping. ",
141             type = Types.Bool,
142             default = False,
143             flags = Flags.Design)
144
145         deadline = Attribute("deadline",
146             "Sets ping -w option. Specify a timeout, in seconds, before ping "
147             "exits regardless of how many packets have been sent or received.",
148             flags = Flags.Design)
149
150         timeout = Attribute("timeout",
151             "Sets ping -W option. Time to wait for a respone in seconds .",
152             flags = Flags.Design)
153
154         target = Attribute("target",
155             "The host to ping .",
156             flags = Flags.Design)
157
158         early_start = Attribute("earlyStart",
159             "Start ping as early as deployment. ",
160             type = Types.Bool,
161             default = False,
162             flags = Flags.Design)
163
164         cls._register_attribute(count)
165         cls._register_attribute(mark)
166         cls._register_attribute(interval)
167         cls._register_attribute(address)
168         cls._register_attribute(preload)
169         cls._register_attribute(numeric)
170         cls._register_attribute(pattern)
171         cls._register_attribute(printtmp)
172         cls._register_attribute(tos)
173         cls._register_attribute(quiet)
174         cls._register_attribute(rec_route)
175         cls._register_attribute(route_bypass)
176         cls._register_attribute(packetsize)
177         cls._register_attribute(sendbuff)
178         cls._register_attribute(ttl)
179         cls._register_attribute(timestamp)
180         cls._register_attribute(hint)
181         cls._register_attribute(full_latency)
182         cls._register_attribute(verbose)
183         cls._register_attribute(flood)
184         cls._register_attribute(deadline)
185         cls._register_attribute(timeout)
186         cls._register_attribute(target)
187         cls._register_attribute(early_start)
188
189     def __init__(self, ec, guid):
190         super(LinuxPing, self).__init__(ec, guid)
191         self._home = "ping-%s" % self.guid
192
193     def upload_start_command(self):
194         super(LinuxPing, self).upload_start_command()
195         
196         if self.get("earlyStart") == True:
197             self._run_in_background()
198
199     def do_deploy(self):
200         if not self.get("command"):
201             self.set("command", self._start_command)
202
203         super(LinuxPing, self).do_deploy()
204
205     def do_start(self):
206         if self.get("earlyStart") == True:
207             if self.state == ResourceState.READY:
208                 command = self.get("command")
209                 self.info("Starting command '%s'" % command)
210
211                 self.set_started()
212             else:
213                 msg = " Failed to execute command '%s'" % command
214                 self.error(msg, out, err)
215                 raise RuntimeError(msg)
216         else:
217            super(LinuxPing, self).do_start()
218
219     @property
220     def _start_command(self):
221         args = []
222
223         args.append("echo 'Starting PING to %s' ;" % self.get("target"))
224
225         if self.get("printTimestamp") == True:
226             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
227
228         args.append("ping ")
229         
230         if self.get("count"):
231             args.append("-c %s" % self.get("count"))
232         if self.get("mark"):
233             args.append("-m %s" % self.get("mark"))
234         if self.get("interval"):
235             args.append("-i %s" % self.get("interval"))
236         if self.get("address"):
237             args.append("-I %s" % self.get("address"))
238         if self.get("preload"):
239             args.append("-l %s" % self.get("preload"))
240         if self.get("numeric") == True:
241             args.append("-n")
242         if self.get("pattern"):
243             args.append("-p %s" % self.get("pattern"))
244         if self.get("tos"):
245             args.append("-Q %s" % self.get("tos"))
246         if self.get("quiet"):
247             args.append("-q %s" % self.get("quiet"))
248         if self.get("recordRoute") == True:
249             args.append("-R")
250         if self.get("routeBypass") == True:
251             args.append("-r")
252         if self.get("packetSize"):
253             args.append("-s %s" % self.get("packetSize"))
254         if self.get("sendBuff"):
255             args.append("-S %s" % self.get("sendBuff"))
256         if self.get("ttl"):
257             args.append("-t %s" % self.get("ttl"))
258         if self.get("timestamp"):
259             args.append("-T %s" % self.get("timestamp"))
260         if self.get("hint"):
261             args.append("-M %s" % self.get("hint"))
262         if self.get("fullLatency") == True:
263             args.append("-U")
264         if self.get("verbose") == True:
265             args.append("-v")
266         if self.get("flood") == True:
267             args.append("-f")
268         if self.get("deadline"):
269             args.append("-w %s" % self.get("deadline"))
270         if self.get("timeout"):
271             args.append("-W %s" % self.get("timeout"))
272         args.append(self.get("target"))
273
274         command = " ".join(args)
275
276         return command
277
278     def valid_connection(self, guid):
279         # TODO: Validate!
280         return True
281