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