change order so old boot cds don't keep pulling the boot manager from plc.
[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 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
31 def Run( vars, log ):
32     """
33     Bring up sshd inside the boot cd environment for debug purposes.
34
35     Once its running, touch the file /tmp/SSHD_RUNNING so future
36     calls to this function don't do anything.
37
38     Expect the following variables in vars to be set:
39     BM_SOURCE_DIR     The source dir for the boot manager sources that
40                       we are currently running from
41     BOOT_CD_VERSION          A tuple of the current bootcd version
42     """
43
44     log.write( "\n\nStep: Starting debug mode.\n" )
45     
46     # make sure we have the variables we need
47     try:
48         BM_SOURCE_DIR= vars["BM_SOURCE_DIR"]
49         if BM_SOURCE_DIR == "":
50             raise ValueError, "BM_SOURCE_DIR"
51
52         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
53         if BOOT_CD_VERSION == "":
54             raise ValueError, "BOOT_CD_VERSION"
55
56     except KeyError, var:
57         raise BootManagerException, "Missing variable in vars: %s\n" % var
58     except ValueError, var:
59         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
60
61
62     log.write( "Starting debug environment\n" )
63
64     ssh_source_files= "%s/debug_files/" % BM_SOURCE_DIR    
65     ssh_dir= "/etc/ssh/"
66     ssh_home= "/root/.ssh"
67     cancel_boot_flag= "/tmp/CANCEL_BOOT"
68     sshd_started_flag= "/tmp/SSHD_RUNNING"
69     
70     sshd_started= 0
71     try:
72         os.stat(sshd_started_flag)
73         sshd_started= 1
74     except OSError, e:
75         pass
76
77     if not sshd_started:
78         # NOTE: these commands hang if ssh_host_*_key files exist, b/c 
79         #     ssh-keygen asks for user input to confirm the overwrite.  
80                 #     could fix this with "echo 'y' | "
81         log.write( "Creating ssh host keys\n" )
82         
83         utils.makedirs( ssh_dir )
84         utils.sysexec( "ssh-keygen -t rsa1 -b 1024 -f %s/ssh_host_key -N ''" %
85                        ssh_dir, log )
86         utils.sysexec( "ssh-keygen -t rsa -f %s/ssh_host_rsa_key -N ''" %
87                        ssh_dir, log )
88         utils.sysexec( "ssh-keygen -d -f %s/ssh_host_dsa_key -N ''" %
89                        ssh_dir, log )
90
91         if BOOT_CD_VERSION[0] >= 3:
92             utils.sysexec( "cp -f %s/sshd_config_v3 %s/sshd_config" %
93                            (ssh_source_files,ssh_dir), log )
94         else:
95             utils.sysexec( "cp -f %s/sshd_config_v2 %s/sshd_config" %
96                            (ssh_source_files,ssh_dir), log )
97     else:
98         log.write( "ssh host keys already created\n" )
99
100
101     # always update the key, may have change in this instance of the bootmanager
102     log.write( "Installing debug ssh key for root user\n" )
103     
104     utils.makedirs( ssh_home )
105     utils.sysexec( "cp -f %s/debug_root_ssh_key %s/authorized_keys" %
106                    (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     if not sshd_started:
111         log.write( "Starting sshd\n" )
112         
113         if BOOT_CD_VERSION[0] == 2:
114             utils.sysexec( "/usr/sbin/sshd", log )
115         else:
116             utils.sysexec( "service sshd start", log )
117         
118         # flag that ssh is running
119         utils.sysexec( "touch %s" % sshd_started_flag, log )
120     else:
121         log.write( "sshd already running\n" )
122
123     
124     # this will make the initial script stop requesting scripts from PLC
125     utils.sysexec( "touch %s" % cancel_boot_flag, log )
126
127     # for ease of use, setup lvm on 2.x cds
128     if BOOT_CD_VERSION[0] == 2:
129         compatibility.setup_lvm_2x_cd(vars,log)
130
131     print message
132     
133     return
134