applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / linux / ping.py
index 3447658..f585b2e 100644 (file)
@@ -3,9 +3,8 @@
 #    Copyright (C) 2013 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,7 +17,7 @@
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
 from nepi.execution.attribute import Attribute, Flags, Types
-from nepi.execution.resource import clsinit_copy 
+from nepi.execution.resource import clsinit_copy, ResourceState 
 from nepi.resources.linux.application import LinuxApplication
 from nepi.util.timefuncs import tnow
 
@@ -26,7 +25,7 @@ import os
 
 @clsinit_copy
 class LinuxPing(LinuxApplication):
-    _rtype = "LinuxPing"
+    _rtype = "linux::Ping"
 
     @classmethod
     def _register_attributes(cls):
@@ -34,56 +33,56 @@ class LinuxPing(LinuxApplication):
             "Sets ping -c option. Determines the number of ECHO_REQUEST "
             "packates to send before stopping.",
             type = Types.Integer,
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         mark = Attribute("mark",
             "Sets ping -m option. Uses 'mark' to tag outgoing packets. ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         interval = Attribute("interval",
             "Sets ping -i option. Leaves interval seconds between "
             "successive ECHO_REUQEST packets. ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         address = Attribute("address",
             "Sets ping -I option. Sets ECHO_REQUEST packets souce address "
             "to the specified interface address ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         preload = Attribute("preload",
             "Sets ping -l option. Sends preload amount of packets "
             "without waiting for a reply ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         numeric = Attribute("numeric",
             "Sets ping -n option. Disables resolution of host addresses into "
             "symbolic names. ",
             type = Types.Bool,
             default = False,
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         pattern = Attribute("pattern",
             "Sets ping -p option. Species a up to 16 ''pad'' bytes to fill "
             "out sent packets. ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         printtmp = Attribute("printTimestamp",
             "Sets ping -D option. Prints timestamp befor each line as: "
             "unix time + microseconds as in gettimeofday ",
             type = Types.Bool,
             default = False,
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         tos = Attribute("tos",
             "Sets ping -Q option. Sets Quality of Service related bits in ICMP "
             "datagrams. tos can be either a decimal or hexadecime number ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         quiet = Attribute("quiet",
             "Sets ping -q option. Disables ping standard output ",
             type = Types.Bool,
             default = False,
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         rec_route = Attribute("recordRoute",
             "Sets ping -R option. Includes the RECORD_ROUTE option in the "
@@ -91,37 +90,37 @@ class LinuxPing(LinuxApplication):
             "ping standard output.",
             type = Types.Bool,
             default = False,
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         route_bypass = Attribute("routeBypass",
             "Sets ping -r option. Bypasses normal routing tables and sends "
             "ECHO REQUEST packets directly yo a host on an attached interface. ",
             type = Types.Bool,
             default = False,
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         packetsize = Attribute("packetSize",
             "Sets ping -s option. Specifies the number of data bytes to be "
             "sent. Defaults to 56. ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         sendbuff = Attribute("sendBuff",
             "Sets ping -S option. Specifies the number of packets to buffer. "
             "Defaults to one. ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         ttl = Attribute("ttl",
             "Sets ping -t option. Specifies the IP Time to Live for the "
             "packets. ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         timestamp = Attribute("timestamp",
             "Sets ping -T option. Sets special IP timestamp options. ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         hint = Attribute("hint",
             "Sets ping -M option. Selects Path MTU Discovery strategy. ",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         full_latency = Attribute("fullLatency",
             "Sets ping -U option. Calculates round trip time taking into "
@@ -129,32 +128,38 @@ class LinuxPing(LinuxApplication):
             "network round trip time. ",
             type = Types.Bool,
             default = False,
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         verbose = Attribute("verbose",
             "Sets ping -v option. Verbose output. ",
             type = Types.Bool,
             default = False,
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         flood = Attribute("flood",
             "Sets ping -f option. Flood ping. ",
             type = Types.Bool,
             default = False,
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         deadline = Attribute("deadline",
             "Sets ping -w option. Specify a timeout, in seconds, before ping "
             "exits regardless of how many packets have been sent or received.",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         timeout = Attribute("timeout",
             "Sets ping -W option. Time to wait for a respone in seconds .",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
 
         target = Attribute("target",
             "The host to ping .",
-            flags = Flags.ExecReadOnly)
+            flags = Flags.Design)
+
+        early_start = Attribute("earlyStart",
+            "Start ping as early as deployment. ",
+            type = Types.Bool,
+            default = False,
+            flags = Flags.Design)
 
         cls._register_attribute(count)
         cls._register_attribute(mark)
@@ -179,16 +184,37 @@ class LinuxPing(LinuxApplication):
         cls._register_attribute(deadline)
         cls._register_attribute(timeout)
         cls._register_attribute(target)
+        cls._register_attribute(early_start)
 
     def __init__(self, ec, guid):
         super(LinuxPing, self).__init__(ec, guid)
         self._home = "ping-%s" % self.guid
 
-    def deploy(self):
+    def upload_start_command(self):
+        super(LinuxPing, self).upload_start_command()
+        
+        if self.get("earlyStart") == True:
+            self._run_in_background()
+
+    def do_deploy(self):
         if not self.get("command"):
             self.set("command", self._start_command)
 
-        super(LinuxPing, self).deploy()
+        super(LinuxPing, self).do_deploy()
+
+    def do_start(self):
+        if self.get("earlyStart") == True:
+            if self.state == ResourceState.READY:
+                command = self.get("command")
+                self.info("Starting command '%s'" % command)
+
+                self.set_started()
+            else:
+                msg = " Failed to execute command '%s'" % command
+                self.error(msg, out, err)
+                raise RuntimeError(msg)
+        else:
+           super(LinuxPing, self).do_start()
 
     @property
     def _start_command(self):