merged 5.0 (traditional vserver-based) and 5.1 (aka lxc_devel)
[bootmanager.git] / source / steps / StartDebug.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
7 # All rights reserved.
8
9 import os
10
11 from Exceptions import *
12 import utils
13
14
15 warning_message= \
16 """
17 ---------------------------------------------------------
18 This machine has entered a temporary debug state, so
19 Planetlab Support can login and fix any problems that
20 might have occurred.
21
22 Please do not reboot this machine at this point, unless
23 specifically asked to.
24
25 Thank you.
26 ---------------------------------------------------------
27 """
28
29 # this can be invoked 
30 # either at the end of the bm logic, because something failed (last_resort = True)
31 # and/or it can be invoked as a fallback very early in the bootmanager logic,
32 # so we can reach the node regardless of what happens (e.g. bm sometimes hangs)
33
34 def Run( vars, log, last_resort = True):
35
36     """
37     Bring up sshd inside the boot cd environment for debug purposes.
38
39     Once its running, touch the file /tmp/SSHD_RUNNING so future
40     calls to this function don't do anything.
41
42     Expect the following variables in vars to be set:
43     BM_SOURCE_DIR     The source dir for the boot manager sources that
44                         we are currently running from
45     """
46
47     if last_resort:
48         message="Starting debug mode"
49     else:
50         message="Starting fallback sshd"
51
52
53     log.write( "\n\nStep: %s.\n"%message )
54     
55     # make sure we have the variables we need
56     try:
57         BM_SOURCE_DIR= vars["BM_SOURCE_DIR"]
58         if BM_SOURCE_DIR == "":
59             raise ValueError, "BM_SOURCE_DIR"
60
61     except KeyError, var:
62         raise BootManagerException, "Missing variable in vars: %s\n" % var
63     except ValueError, var:
64         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
65
66     # constants
67     ssh_source_files= "%s/debug_files/" % BM_SOURCE_DIR    
68     ssh_dir= "/etc/ssh/"
69     ssh_home= "/root/.ssh"
70     cancel_boot_flag= "/tmp/CANCEL_BOOT"
71     sshd_started_flag= "/tmp/SSHD_RUNNING"
72
73     # pre-sshd
74     pre_sshd_script= os.path.join(ssh_source_files, "pre-sshd")
75     if os.path.exists(pre_sshd_script):
76         utils.sysexec_noerr( pre_sshd_script, log )
77     
78     # create host keys if needed
79     if not os.path.isdir (ssh_dir):
80         utils.makedirs (ssh_dir)
81     key=ssh_dir+"/ssh_host_key"
82     if not os.path.isfile (key):
83         log.write("Creating host rsa1 key %s\n"%key)
84         utils.sysexec( "ssh-keygen -t rsa1 -b 1024 -f %s -N ''" % key, log )
85     key=ssh_dir+"/ssh_host_rsa_key"
86     if not os.path.isfile (key):
87         log.write("Creating host rsa key %s\n"%key)
88         utils.sysexec( "ssh-keygen -t rsa -f %s -N ''" % key, log )
89     key=ssh_dir+"/ssh_host_dsa_key"
90     if not os.path.isfile (key):
91         log.write("Creating host dsa key %s\n"%key)
92         # very old versions did 'ssh-keygen -d' instead of 'ssh-keygen -t dsa' 
93         utils.sysexec( "ssh-keygen -t dsa -f %s -N ''" % key, log )
94
95     # (over)write sshd config
96     utils.sysexec( "cp -f %s/sshd_config %s/sshd_config" % (ssh_source_files,ssh_dir), log )
97     
98     ### xxx ### xxx ### xxx ### xxx ### xxx 
99
100     # always update the key, may have changed in this instance of the bootmanager
101     log.write( "Installing debug ssh key for root user\n" )
102     if not os.path.isdir ( ssh_home):
103         utils.makedirs( ssh_home )
104     utils.sysexec( "cp -f %s/debug_root_ssh_key %s/authorized_keys" % (ssh_source_files,ssh_home), log )
105     utils.sysexec( "chmod 700 %s" % ssh_home, log )
106     utils.sysexec( "chmod 600 %s/authorized_keys" % ssh_home, log )
107
108     # start sshd
109     if not os.path.isfile(sshd_started_flag):
110         log.write( "Starting sshd\n" )
111         utils.sysexec( "service sshd start", log )
112         # flag that ssh is running
113         utils.sysexec( "touch %s" % sshd_started_flag, log )
114     else:
115         # it is expected that sshd is already running when last_resort==True
116         if not last_resort:
117             log.write( "sshd is already running\n" )
118
119     if last_resort:
120         # this will make the initial script stop requesting scripts from PLC
121         utils.sysexec( "touch %s" % cancel_boot_flag, log )
122
123     if last_resort:
124         print warning_message
125     
126     return