Modified FailureManager to abort only when critical resources fail
[nepi.git] / src / nepi / resources / linux / ccn / ccnpingserver.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 ResourceManager, clsinit_copy, \
22         ResourceState, reschedule_delay, failtrap
23 from nepi.resources.linux.ccn.ccnapplication import LinuxCCNApplication
24 from nepi.util.timefuncs import tnow, tdiffsec
25
26 import os
27
28 @clsinit_copy
29 class LinuxCCNPingServer(LinuxCCNApplication):
30     _rtype = "LinuxCCNPingServer"
31
32     @classmethod
33     def _register_attributes(cls):
34         daemon = Attribute("d",
35             "Run ccnping server as a daemon in background",
36             type = Types.Bool,
37             default = False,
38             flags = Flags.ExecReadOnly)
39
40         freshness = Attribute("x",
41             "Set FreshnessSeconds",
42             type = Types.Integer,
43             flags = Flags.ExecReadOnly)
44
45         prefix = Attribute("prefix",
46             "Prefix to serve content (e.g. ccnx:/name/prefix)",
47             flags = Flags.ExecReadOnly)
48
49         cls._register_attribute(daemon)
50         cls._register_attribute(freshness)
51         cls._register_attribute(prefix)
52
53     def __init__(self, ec, guid):
54         super(LinuxCCNPingServer, self).__init__(ec, guid)
55         self._home = "ccnping-serv-%s" % self.guid
56
57     @failtrap
58     def deploy(self):
59         if not self.get("command"):
60             self.set("command", self._start_command)
61
62         if not self.get("env"):
63             self.set("env", self._environment)
64
65         if not self.get("depends"):
66             self.set("depends", self._dependencies)
67
68         if not self.get("build"):
69             self.set("build", self._build)
70
71         if not self.get("install"):
72             self.set("install", self._install)
73
74         super(LinuxCCNPingServer, self).deploy()
75
76     @property
77     def _start_command(self):
78         args = []
79         args.append("ccnpingserver")
80         args.append(self.get("prefix"))
81         if self.get("d") == True:
82             args.append("-d")
83         if self.get("x"):
84             args.append("-x %d" % self.get("x"))
85
86         command = " ".join(args)
87
88         return command
89
90     @property
91     def _dependencies(self):
92         return "git"
93
94     @property
95     def _build(self):
96         return (
97             # Evaluate if ccnx binaries are already installed
98             " ( "
99                 " test -f ${BIN}/ccnping && "
100                 " echo 'binaries found, nothing to do' "
101             " ) || ( "
102             # If not, untar and build
103                 " ( "
104                     " git clone git://github.com/NDN-Routing/ccnping ${SRC}/ccnping "
105                  " ) && "
106                     # build
107                     "cd ${SRC}/ccnping && "
108                     " ( "
109                     " ./configure LDFLAGS=-L${SRC}/ccnx-0.7.2/lib CFLAGS=-I${SRC}/ccnx-0.7.2/include "
110                     " --prefix=${BIN}/ccnping && make "
111                     " ) "
112              " )") 
113
114     @property
115     def _install(self):
116         return (
117             # Evaluate if ccnx binaries are already installed
118             " ( "
119                 " test -f ${BIN}/ccnping && "
120                 " echo 'binaries found, nothing to do' "
121             " ) || ( "
122             # If not, install
123                 "  mkdir -p ${BIN}/ccnping && "
124                 "  mv ${SRC}/ccnping/ccnping ${BIN}/ccnping/ && "
125                 "  mv ${SRC}/ccnping/ccnpingserver ${BIN}/ccnping/ "
126             " )"
127             )
128
129     @property
130     def _environment(self):
131         return "%s:%s" % (self.ccnd.path, "${BIN}/ccnping")
132        
133     def valid_connection(self, guid):
134         # TODO: Validate!
135         return True
136