tweak privatebridge to remove exception message when ovs is not installed - prints...
[nodemanager.git] / tools.py
1 """A few things that didn't seem to fit anywhere else."""
2
3 import os, os.path
4 import pwd
5 import tempfile
6 import fcntl
7 import errno
8 import threading
9 import subprocess
10 import shutil
11 import sys
12 import signal
13
14 import logger
15
16 PID_FILE = '/var/run/nodemanager.pid'
17
18 ####################
19 def get_default_if():
20     interface = get_if_from_hwaddr(get_hwaddr_from_plnode())
21     if not interface: interface = "eth0"
22     return interface
23
24 def get_hwaddr_from_plnode():
25     try:
26         for line in open("/usr/boot/plnode.txt", 'r').readlines():
27             if line.startswith("NET_DEVICE"):
28                 return line.split("=")[1].strip().strip('"')
29     except:
30         pass
31     return None
32
33 def get_if_from_hwaddr(hwaddr):
34     import sioc
35     devs = sioc.gifconf()
36     for dev in devs:
37         dev_hwaddr = sioc.gifhwaddr(dev)
38         if dev_hwaddr == hwaddr: return dev
39     return None
40
41 ####################
42 # daemonizing
43 def as_daemon_thread(run):
44     """Call function <run> with no arguments in its own thread."""
45     thr = threading.Thread(target=run)
46     thr.setDaemon(True)
47     thr.start()
48
49 def close_nonstandard_fds():
50     """Close all open file descriptors other than 0, 1, and 2."""
51     _SC_OPEN_MAX = 4
52     for fd in range(3, os.sysconf(_SC_OPEN_MAX)):
53         try: os.close(fd)
54         except OSError: pass  # most likely an fd that isn't open
55
56 # after http://www.erlenstar.demon.co.uk/unix/faq_2.html
57 def daemon():
58     """Daemonize the current process."""
59     if os.fork() != 0: os._exit(0)
60     os.setsid()
61     if os.fork() != 0: os._exit(0)
62     os.chdir('/')
63     os.umask(0022)
64     devnull = os.open(os.devnull, os.O_RDWR)
65     os.dup2(devnull, 0)
66     # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
67     crashlog = os.open('/var/log/nodemanager.daemon', os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
68     os.dup2(crashlog, 1)
69     os.dup2(crashlog, 2)
70
71 def fork_as(su, function, *args):
72     """fork(), cd / to avoid keeping unused directories open, close all nonstandard file descriptors (to avoid capturing open sockets), fork() again (to avoid zombies) and call <function> with arguments <args> in the grandchild process.  If <su> is not None, set our group and user ids appropriately in the child process."""
73     child_pid = os.fork()
74     if child_pid == 0:
75         try:
76             os.chdir('/')
77             close_nonstandard_fds()
78             if su:
79                 pw_ent = pwd.getpwnam(su)
80                 os.setegid(pw_ent[3])
81                 os.seteuid(pw_ent[2])
82             child_pid = os.fork()
83             if child_pid == 0: function(*args)
84         except:
85             os.seteuid(os.getuid())  # undo su so we can write the log file
86             os.setegid(os.getgid())
87             logger.log_exc("tools: fork_as")
88         os._exit(0)
89     else: os.waitpid(child_pid, 0)
90
91 ####################
92 # manage files
93 def pid_file():
94     """We use a pid file to ensure that only one copy of NM is running at a given time.
95 If successful, this function will write a pid file containing the pid of the current process.
96 The return value is the pid of the other running process, or None otherwise."""
97     other_pid = None
98     if os.access(PID_FILE, os.F_OK):  # check for a pid file
99         handle = open(PID_FILE)  # pid file exists, read it
100         other_pid = int(handle.read())
101         handle.close()
102         # check for a process with that pid by sending signal 0
103         try: os.kill(other_pid, 0)
104         except OSError, e:
105             if e.errno == errno.ESRCH: other_pid = None  # doesn't exist
106             else: raise  # who knows
107     if other_pid == None:
108         # write a new pid file
109         write_file(PID_FILE, lambda f: f.write(str(os.getpid())))
110     return other_pid
111
112 def write_file(filename, do_write, **kw_args):
113     """Write file <filename> atomically by opening a temporary file, using <do_write> to write that file, and then renaming the temporary file."""
114     shutil.move(write_temp_file(do_write, **kw_args), filename)
115
116 def write_temp_file(do_write, mode=None, uidgid=None):
117     fd, temporary_filename = tempfile.mkstemp()
118     if mode: os.chmod(temporary_filename, mode)
119     if uidgid: os.chown(temporary_filename, *uidgid)
120     f = os.fdopen(fd, 'w')
121     try: do_write(f)
122     finally: f.close()
123     return temporary_filename
124
125 # replace a target file with a new contents - checks for changes
126 # can handle chmod if requested
127 # can also remove resulting file if contents are void, if requested
128 # performs atomically:
129 #    writes in a tmp file, which is then renamed (from sliverauth originally)
130 # returns True if a change occurred, or the file is deleted
131 def replace_file_with_string (target, new_contents, chmod=None, remove_if_empty=False):
132     try:
133         current=file(target).read()
134     except:
135         current=""
136     if current==new_contents:
137         # if turns out to be an empty string, and remove_if_empty is set,
138         # then make sure to trash the file if it exists
139         if remove_if_empty and not new_contents and os.path.isfile(target):
140             logger.verbose("tools.replace_file_with_string: removing file %s"%target)
141             try: os.unlink(target)
142             finally: return True
143         return False
144     # overwrite target file: create a temp in the same directory
145     path=os.path.dirname(target) or '.'
146     fd, name = tempfile.mkstemp('','repl',path)
147     os.write(fd,new_contents)
148     os.close(fd)
149     if os.path.exists(target):
150         os.unlink(target)
151     shutil.move(name,target)
152     if chmod: os.chmod(target,chmod)
153     return True
154
155
156 ####################
157 # utilities functions to get (cached) information from the node
158
159 # get node_id from /etc/planetlab/node_id and cache it
160 _node_id=None
161 def node_id():
162     global _node_id
163     if _node_id is None:
164         try:
165             _node_id=int(file("/etc/planetlab/node_id").read())
166         except:
167             _node_id=""
168     return _node_id
169
170 _root_context_arch=None
171 def root_context_arch():
172     global _root_context_arch
173     if not _root_context_arch:
174         sp=subprocess.Popen(["uname","-i"],stdout=subprocess.PIPE)
175         (_root_context_arch,_)=sp.communicate()
176         _root_context_arch=_root_context_arch.strip()
177     return _root_context_arch
178
179
180 ####################
181 class NMLock:
182     def __init__(self, file):
183         logger.log("tools: Lock %s initialized." % file, 2)
184         self.fd = os.open(file, os.O_RDWR|os.O_CREAT, 0600)
185         flags = fcntl.fcntl(self.fd, fcntl.F_GETFD)
186         flags |= fcntl.FD_CLOEXEC
187         fcntl.fcntl(self.fd, fcntl.F_SETFD, flags)
188     def __del__(self):
189         os.close(self.fd)
190     def acquire(self):
191         logger.log("tools: Lock acquired.", 2)
192         fcntl.lockf(self.fd, fcntl.LOCK_SH)
193     def release(self):
194         logger.log("tools: Lock released.", 2)
195         fcntl.lockf(self.fd, fcntl.LOCK_UN)
196
197 ####################
198 # Utilities for getting the IP address of a LXC/Openvswitch slice. Do this by
199 # running ifconfig inside of the slice's context.
200
201 def get_sliver_process(slice_name, process_cmdline):
202     """ Utility function to find a process inside of an LXC sliver. Returns
203         (cgroup_fn, pid). cgroup_fn is the filename of the cgroup file for
204         the process, for example /proc/2592/cgroup. Pid is the process id of
205         the process. If the process is not found then (None, None) is returned.
206     """
207     try:
208         cmd = 'grep %s /proc/*/cgroup | grep freezer'%slice_name
209         output = os.popen(cmd).readlines()
210     except:
211         # the slice couldn't be found
212         logger.log("get_sliver_process: couldn't find slice %s" % slice_name)
213         return (None, None)
214
215     cgroup_fn = None
216     pid = None
217     for e in output:
218         try:
219             l = e.rstrip()
220             path = l.split(':')[0]
221             comp = l.rsplit(':')[-1]
222             slice_name_check = comp.rsplit('/')[-1]
223
224             if (slice_name_check == slice_name):
225                 slice_path = path
226                 pid = slice_path.split('/')[2]
227                 cmdline = open('/proc/%s/cmdline'%pid).read().rstrip('\n\x00')
228                 if (cmdline == process_cmdline):
229                     cgroup_fn = slice_path
230                     break
231         except:
232             break
233
234     if (not cgroup_fn) or (not pid):
235         logger.log("get_sliver_process: process %s not running in slice %s" % (process_cmdline, slice_name))
236         return (None, None)
237
238     return (cgroup_fn, pid)
239
240 def get_sliver_ifconfig(slice_name, device="eth0"):
241     """ return the output of "ifconfig" run from inside the sliver.
242
243         side effects: adds "/usr/sbin" to sys.path
244     """
245
246     # See if setns is installed. If it's not then we're probably not running
247     # LXC.
248     if not os.path.exists("/usr/sbin/setns.so"):
249         return None
250
251     # setns is part of lxcsu and is installed to /usr/sbin
252     if not "/usr/sbin" in sys.path:
253         sys.path.append("/usr/sbin")
254     import setns
255
256     (cgroup_fn, pid) = get_sliver_process(slice_name, "/sbin/init")
257     if (not cgroup_fn) or (not pid):
258         return None
259
260     path = '/proc/%s/ns/net'%pid
261
262     result = None
263     try:
264         setns.chcontext(path)
265
266         args = ["/sbin/ifconfig", device]
267         sub = subprocess.Popen(args, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
268         sub.wait()
269
270         if (sub.returncode != 0):
271             logger.log("get_slice_ifconfig: error in ifconfig: %s" % sub.stderr.read())
272
273         result = sub.stdout.read()
274     finally:
275         setns.chcontext("/proc/1/ns/net")
276
277     return result
278
279 def get_sliver_ip(slice_name):
280     ifconfig = get_sliver_ifconfig(slice_name)
281     if not ifconfig:
282         return None
283
284     for line in ifconfig.split("\n"):
285         if "inet addr:" in line:
286             # example: '          inet addr:192.168.122.189  Bcast:192.168.122.255  Mask:255.255.255.0'
287             parts = line.strip().split()
288             if len(parts)>=2 and parts[1].startswith("addr:"):
289                 return parts[1].split(":")[1]
290
291     return None
292
293 ### this returns the kind of virtualization on the node
294 # either 'vs' or 'lxc'
295 # also caches it in /etc/planetlab/virt for next calls
296 # could be promoted to core nm if need be
297 virt_stamp="/etc/planetlab/virt"
298 def get_node_virt ():
299     try:
300         return file(virt_stamp).read().strip()
301     except:
302         pass
303     logger.log("Computing virt..")
304     try: 
305         if subprocess.call ([ 'vserver', '--help' ]) ==0: virt='vs'
306         else:                                             virt='lxc'      
307     except:
308         virt='lxc'
309     with file(virt_stamp,"w") as f:
310         f.write(virt)
311     return virt
312
313 ### this return True or False to indicate that systemctl is present on that box
314 # cache result in memory as _has_systemctl
315 _has_systemctl=None
316 def has_systemctl ():
317     global _has_systemctl
318     if _has_systemctl is None:
319         _has_systemctl = (subprocess.call([ 'systemctl', '--help' ]) == 0)
320     return _has_systemctl
321
322 # how to run a command in a slice
323 # now this is a painful matter
324 # the problem is with capsh that forces a bash command to be injected in its exec'ed command
325 # so because lxcsu uses capsh, you cannot exec anything else than bash
326 # bottom line is, what actually needs to be called is
327 # vs:  vserver exec slicename command and its arguments
328 # lxc: lxcsu slicename "command and its arguments"
329 # which, OK, is no big deal as long as the command is simple enough, 
330 # but do not stretch it with arguments that have spaces or need quoting as that will become a nightmare
331 def command_in_slice (slicename, argv):
332     virt=get_node_virt()
333     if virt=='vs':
334         return [ 'vserver', slicename, 'exec', ] + argv
335     elif virt=='lxc':
336         # wrap up argv in a single string for -c
337         return [ 'lxcsu', slicename, ] + [ " ".join(argv) ]
338     logger.log("command_in_slice: WARNING: could not find a valid virt")
339     return argv
340
341 ####################
342 def init_signals ():
343     def handler (signum, frame):
344         logger.log("Received signal %d - exiting"%signum)
345         os._exit(1)
346     signal.signal(signal.SIGHUP,handler)
347     signal.signal(signal.SIGQUIT,handler)
348     signal.signal(signal.SIGINT,handler)
349     signal.signal(signal.SIGTERM,handler)