applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / linux / ccn / ccncontent.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     ResourceAction
22 from nepi.resources.linux.application import LinuxApplication
23 from nepi.resources.linux.ccn.ccnr import LinuxCCNR
24 from nepi.util.timefuncs import tnow
25
26 import os
27
28 @clsinit_copy
29 class LinuxCCNContent(LinuxApplication):
30     _rtype = "linux::CCNContent"
31
32     @classmethod
33     def _register_attributes(cls):
34         content_name = Attribute("contentName",
35                 "The name of the content to publish (e.g. ccn:/VIDEO) ",
36                 flags = Flags.Design)
37
38         content = Attribute("content",
39                 "The content to publish. It can be a path to a file or plain text ",
40                 flags = Flags.Design)
41
42         scope = Attribute("scope",
43                 "Use the given scope on the start-write request (if -r specified). "
44                 "scope can be 1 (local), 2 (neighborhood), or 3 (unlimited). "
45                 "Note that a scope of 3 is encoded as the absence of any scope in the interest. ",
46                 type = Types.Integer,
47                 default = 1,
48                 flags = Flags.Design)
49
50         cls._register_attribute(content_name)
51         cls._register_attribute(content)
52         cls._register_attribute(scope)
53
54     def __init__(self, ec, guid):
55         super(LinuxCCNContent, self).__init__(ec, guid)
56         self._home = "content-%s" % self.guid
57         
58     @property
59     def ccnr(self):
60         ccnr = self.get_connected(LinuxCCNR.get_rtype())
61         if ccnr: return ccnr[0]
62         return None
63
64     @property
65     def ccnd(self):
66         if self.ccnr: return self.ccnr.ccnd
67         return None
68
69     @property
70     def node(self):
71         if self.ccnr: return self.ccnr.node
72         return None
73
74     def do_deploy(self):
75         if not self.ccnr or self.ccnr.state < ResourceState.READY:
76             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
77             
78             # ccnr needs to wait until ccnd is deployed and running
79             self.ec.schedule(self.reschedule_delay, self.deploy)
80         else:
81             if not self.get("command"):
82                 self.set("command", self._start_command)
83
84             if not self.get("env"):
85                 self.set("env", self._environment)
86
87             # set content to stdin, so the content will be
88             # uploaded during provision
89             self.set("stdin", self.get("content"))
90
91             command = self.get("command")
92
93             self.info("Deploying command '%s' " % command)
94
95             self.do_discover()
96             self.do_provision()
97
98             self.set_ready()
99
100     def upload_start_command(self):
101         command = self.get("command")
102         env = self.get("env")
103
104         self.info("Uploading command '%s'" % command)
105
106         # We want to make sure the content is published
107         # before the experiment starts.
108         # Run the command as a bash script in the background, 
109         # in the host ( but wait until the command has
110         # finished to continue )
111         env = self.replace_paths(env)
112         command = self.replace_paths(command)
113
114         (out, err), proc = self.execute_command(command, 
115                 env, blocking = True)
116
117         if proc.poll():
118             msg = "Failed to execute command"
119             self.error(msg, out, err)
120             raise RuntimeError(msg)
121
122     def do_start(self):
123         if self.state == ResourceState.READY:
124             command = self.get("command")
125             self.info("Starting command '%s'" % command)
126
127             self.set_started()
128         else:
129             msg = " Failed to execute command '%s'" % command
130             self.error(msg, out, err)
131             raise RuntimeError(msg)
132
133     @property
134     def _start_command(self):
135         command = ["ccnseqwriter"]
136         command.append("-r %s" % self.get("contentName"))
137         command.append("-s %d" % self.get("scope"))
138         command.append("< %s" % os.path.join(self.app_home, 'stdin'))
139
140         command = " ".join(command)
141         return command
142
143     @property
144     def _environment(self):
145         return self.ccnd.path
146        
147     def valid_connection(self, guid):
148         # TODO: Validate!
149         return True
150