Porting changes in LinuxApplication directory structure to CCNx RMs
[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 as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState, \
22     ResourceAction, reschedule_delay
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.resources.linux.ccn.ccnr import LinuxCCNR
25 from nepi.util.timefuncs import tnow
26
27 import os
28
29 @clsinit_copy
30 class LinuxCCNContent(LinuxApplication):
31     _rtype = "LinuxCCNContent"
32
33     @classmethod
34     def _register_attributes(cls):
35         content_name = Attribute("contentName",
36                 "The name of the content to publish (e.g. ccn:/VIDEO) ",
37                 flags = Flags.ExecReadOnly)
38
39         content = Attribute("content",
40                 "The content to publish. It can be a path to a file or plain text ",
41                 flags = Flags.ExecReadOnly)
42
43         cls._register_attribute(content_name)
44         cls._register_attribute(content)
45
46     def __init__(self, ec, guid):
47         super(LinuxCCNContent, self).__init__(ec, guid)
48         self._home = "content-%s" % self.guid
49         
50     @property
51     def ccnr(self):
52         ccnr = self.get_connected(LinuxCCNR.rtype())
53         if ccnr: return ccnr[0]
54         return None
55
56     @property
57     def ccnd(self):
58         if self.ccnr: return self.ccnr.ccnd
59         return None
60
61     @property
62     def node(self):
63         if self.ccnr: return self.ccnr.node
64         return None
65
66
67     def deploy(self):
68         if not self.ccnr or self.ccnr.state < ResourceState.READY:
69             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
70             
71             # ccnr needs to wait until ccnd is deployed and running
72             self.ec.schedule(reschedule_delay, self.deploy)
73         else:
74             command = self._start_command
75             env = self._environment
76
77             self.set("command", command)
78             self.set("env", env)
79
80             # set content to stdin, so the content will be
81             # uploaded during provision
82             self.set("stdin", self.get("content"))
83
84             self.info("Deploying command '%s' " % command)
85
86             # create run dir for application
87             self.node.mkdir(self.run_home)
88
89             # upload content 
90             self.upload_stdin()
91
92             # We want to make sure the content is published
93             # before the experiment starts.
94             # Run the command as a bash script in the background, 
95             # in the host ( but wait until the command has
96             # finished to continue )
97             (out, err), proc = self.execute_command(command, env)
98
99             if proc.poll():
100                 self._state = ResourceState.FAILED
101                 msg = "Failed to execute command"
102                 self.error(msg, out, err)
103                 raise RuntimeError, msg
104
105             self.debug("----- READY ---- ")
106             self._ready_time = tnow()
107             self._state = ResourceState.READY
108
109     def start(self):
110         if self._state == ResourceState.READY:
111             command = self.get("command")
112             self.info("Starting command '%s'" % command)
113
114             self._start_time = tnow()
115             self._state = ResourceState.STARTED
116         else:
117             msg = " Failed to execute command '%s'" % command
118             self.error(msg, out, err)
119             self._state = ResourceState.FAILED
120             raise RuntimeError, msg
121
122     @property
123     def state(self):
124         return self._state
125
126     @property
127     def _start_command(self):
128         return "ccnseqwriter -r %s < %s" % (self.get("contentName"),
129                 os.path.join(self.app_home, 'stdin'))
130
131     @property
132     def _environment(self):
133         return self.ccnd.path
134        
135     def execute_command(self, command, env):
136         environ = self.node.format_environment(env, inline = True)
137         command = environ + command
138         command = self.replace_paths(command)
139
140         return self.node.execute(command)
141
142     def valid_connection(self, guid):
143         # TODO: Validate!
144         return True
145