62a94a17c5bca85fc43c9e328d9737c0c0327e46
[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     # create host keys if needed
74     if not os.path.isdir (ssh_dir):
75         utils.makedirs (ssh_dir)
76     key=ssh_dir+"/ssh_host_key"
77     if not os.path.isfile (key):
78         log.write("Creating host rsa1 key %s\n"%key)
79         utils.sysexec( "ssh-keygen -t rsa1 -b 1024 -f %s -N ''" % key, log )
80     key=ssh_dir+"/ssh_host_rsa_key"
81     if not os.path.isfile (key):
82         log.write("Creating host rsa key %s\n"%key)
83         utils.sysexec( "ssh-keygen -t rsa -f %s -N ''" % key, log )
84     key=ssh_dir+"/ssh_host_dsa_key"
85     if not os.path.isfile (key):
86         log.write("Creating host dsa key %s\n"%key)
87         utils.sysexec( "ssh-keygen -d -f %s -N ''" % key, log )
88
89     # (over)write sshd config
90     utils.sysexec( "cp -f %s/sshd_config %s/sshd_config" % (ssh_source_files,ssh_dir), log )
91     
92     ### xxx ### xxx ### xxx ### xxx ### xxx 
93
94     # always update the key, may have changed in this instance of the bootmanager
95     log.write( "Installing debug ssh key for root user\n" )
96     if not os.path.isdir ( ssh_home):
97         utils.makedirs( ssh_home )
98     utils.sysexec( "cp -f %s/debug_root_ssh_key %s/authorized_keys" % (ssh_source_files,ssh_home), log )
99     utils.sysexec( "chmod 700 %s" % ssh_home, log )
100     utils.sysexec( "chmod 600 %s/authorized_keys" % ssh_home, log )
101
102     # start sshd
103     if not os.path.isfile(sshd_started_flag):
104         log.write( "Starting sshd\n" )
105         utils.sysexec( "service sshd start", log )
106         # flag that ssh is running
107         utils.sysexec( "touch %s" % sshd_started_flag, log )
108     else:
109         # it is expected that sshd is already running when last_resort==True
110         if not last_resort:
111             log.write( "sshd is already running\n" )
112
113     if last_resort:
114         # this will make the initial script stop requesting scripts from PLC
115         utils.sysexec( "touch %s" % cancel_boot_flag, log )
116
117     if last_resort:
118         print warning_message
119     
120     return