applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / linux / mtr.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 LinuxMtr(LinuxApplication):
28     _rtype = "linux::Mtr"
29
30     @classmethod
31     def _register_attributes(cls):
32         report_cycles = Attribute("reportCycles",
33             "sets mtr --report-cycles (-c) option. Determines the number of "
34             "pings sent to determine both machines in the networks. Each "
35             "cycle lasts one sencond.",
36             flags = Flags.Design)
37
38         no_dns = Attribute("noDns",
39             "sets mtr --no-dns (-n) option. Forces mtr to display IPs intead of "
40             "trying to resolve to host names ",
41             type = Types.Bool,
42             default = True,
43             flags = Flags.Design)
44
45         address = Attribute("address",
46             "sets mtr --address (-a) option. Binds the socket to send outgoing "
47             "packets to the interface of the specified address, so that any "
48             "any packets are sent through this interface. ",
49             flags = Flags.Design)
50
51         interval = Attribute("interval",
52             "sets mtr --interval (-i) option. Specifies the number of seconds "
53             "between ICMP ECHO requests. Default value is one second ",
54             flags = Flags.Design)
55
56         countinuous = Attribute("continuous",
57             "Run mtr in a while loop",
58             type = Types.Bool,
59             default = False,
60             flags = Flags.Design)
61
62         print_timestamp = Attribute("printTimestamp",
63             "Print timestamp before running mtr",
64             type = Types.Bool,
65             default = False,
66             flags = Flags.Design)
67
68         target = Attribute("target",
69             "mtr target host (host that will be pinged)",
70             flags = Flags.Design)
71
72         early_start = Attribute("earlyStart",
73             "Start ping as early as deployment. ",
74             type = Types.Bool,
75             default = False,
76             flags = Flags.Design)
77
78         cls._register_attribute(report_cycles)
79         cls._register_attribute(no_dns)
80         cls._register_attribute(address)
81         cls._register_attribute(interval)
82         cls._register_attribute(countinuous)
83         cls._register_attribute(print_timestamp)
84         cls._register_attribute(target)
85         cls._register_attribute(early_start)
86
87     def __init__(self, ec, guid):
88         super(LinuxMtr, self).__init__(ec, guid)
89         self._home = "mtr-%s" % self.guid
90         self._sudo_kill = True
91
92     def upload_start_command(self):
93         super(LinuxMtr, self).upload_start_command()
94         
95         if self.get("earlyStart") == True:
96             self._run_in_background()
97
98     def do_deploy(self):
99         if not self.get("command"):
100             self.set("command", self._start_command)
101
102         if not self.get("env"):
103             self.set("env", "PATH=$PATH:/usr/sbin/")
104
105         if not self.get("depends"):
106             self.set("depends", "mtr")
107
108         super(LinuxMtr, self).do_deploy()
109
110     def do_start(self):
111         if self.get("earlyStart") == True:
112             if self.state == ResourceState.READY:
113                 command = self.get("command")
114                 self.info("Starting command '%s'" % command)
115
116                 self.set_started()
117             else:
118                 msg = " Failed to execute command '%s'" % command
119                 self.error(msg, out, err)
120                 raise RuntimeError(msg)
121         else:
122            super(LinuxMtr, self).do_start()
123
124     @property
125     def _start_command(self):
126         args = []
127         if self.get("continuous") == True:
128             args.append("while true; do ")
129         if self.get("printTimestamp") == True:
130             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
131         args.append("sudo -S mtr --report")
132         if self.get("reportCycles"):
133             args.append("-c %s" % self.get("reportCycles"))
134         if self.get("noDns") == True:
135             args.append("-n")
136         if self.get("address"):
137             args.append("-a %s" % self.get("address"))
138         args.append(self.get("target"))
139         if self.get("continuous") == True:
140             args.append("; done ")
141
142         command = " ".join(args)
143
144         return command
145
146     def valid_connection(self, guid):
147         # TODO: Validate!
148         return True
149