undo this one for building rc26
[bootmanager.git] / source / steps / StartDebug.py
1 #!/usr/bin/python2
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 import compatibility
14
15
16 warning_message= \
17 """
18 ---------------------------------------------------------
19 This machine has entered a temporary debug state, so
20 Planetlab Support can login and fix any problems that
21 might have occurred.
22
23 Please do not reboot this machine at this point, unless
24 specifically asked to.
25
26 Thank you.
27 ---------------------------------------------------------
28 """
29
30 # this can be invoked 
31 # either at the end of the bm logic, because something failed (last_resort = True)
32 # and/or it can be invoked as a fallback very early in the bootmanager logic,
33 # so we can reach the node regardless of what happens (e.g. bm sometimes hangs)
34
35 def Run( vars, log, last_resort = True):
36
37     """
38     Bring up sshd inside the boot cd environment for debug purposes.
39
40     Once its running, touch the file /tmp/SSHD_RUNNING so future
41     calls to this function don't do anything.
42
43     Expect the following variables in vars to be set:
44     BM_SOURCE_DIR     The source dir for the boot manager sources that
45                       we are currently running from
46     BOOT_CD_VERSION          A tuple of the current bootcd version
47     """
48
49     if last_resort:
50         message="Starting debug mode"
51     else:
52         message="Starting fallback sshd"
53
54
55     log.write( "\n\nStep: %s.\n"%message )
56     
57     # make sure we have the variables we need
58     try:
59         BM_SOURCE_DIR= vars["BM_SOURCE_DIR"]
60         if BM_SOURCE_DIR == "":
61             raise ValueError, "BM_SOURCE_DIR"
62
63         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
64         if BOOT_CD_VERSION == "":
65             raise ValueError, "BOOT_CD_VERSION"
66
67     except KeyError, var:
68         raise BootManagerException, "Missing variable in vars: %s\n" % var
69     except ValueError, var:
70         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
71
72     # constants
73     ssh_source_files= "%s/debug_files/" % BM_SOURCE_DIR    
74     ssh_dir= "/etc/ssh/"
75     ssh_home= "/root/.ssh"
76     cancel_boot_flag= "/tmp/CANCEL_BOOT"
77     sshd_started_flag= "/tmp/SSHD_RUNNING"
78     
79     # create host keys if needed
80     if not os.path.isdir (ssh_dir):
81         utils.makedirs (ssh_dir)
82     key=ssh_dir+"/ssh_host_key"
83     if not os.path.isfile (key):
84         log.write("Creating host rsa1 key %s\n"%key)
85         utils.sysexec( "ssh-keygen -t rsa1 -b 1024 -f %s -N ''" % key, log )
86     key=ssh_dir+"/ssh_host_rsa_key"
87     if not os.path.isfile (key):
88         log.write("Creating host rsa key %s\n"%key)
89         utils.sysexec( "ssh-keygen -t rsa -f %s -N ''" % key, log )
90     key=ssh_dir+"/ssh_host_dsa_key"
91     if not os.path.isfile (key):
92         log.write("Creating host dsa key %s\n"%key)
93         utils.sysexec( "ssh-keygen -d -f %s -N ''" % key, log )
94
95     if BOOT_CD_VERSION[0] >= 3:
96         utils.sysexec( "cp -f %s/sshd_config_v3 %s/sshd_config" % (ssh_source_files,ssh_dir), log )
97     else:
98         utils.sysexec( "cp -f %s/sshd_config_v2 %s/sshd_config" % (ssh_source_files,ssh_dir), log )
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         if BOOT_CD_VERSION[0] == 2: utils.sysexec( "/usr/sbin/sshd", log )
112         else:                       utils.sysexec( "service sshd start", log )
113         # flag that ssh is running
114         utils.sysexec( "touch %s" % sshd_started_flag, log )
115     else:
116         # it is expected that sshd is already running when last_resort==True
117         if not last_resort:
118             log.write( "sshd is already running\n" )
119
120     if last_resort:
121         # this will make the initial script stop requesting scripts from PLC
122         utils.sysexec( "touch %s" % cancel_boot_flag, log )
123
124     # for ease of use, setup lvm on 2.x cds
125     if BOOT_CD_VERSION[0] == 2:
126         compatibility.setup_lvm_2x_cd(vars,log)
127
128     if last_resort:
129         print warning_message
130     
131     return