ebb78582e6eb44db3e800c8aa921c912cc76f254
[bootmanager.git] / source / steps / StartDebug.py
1 import os
2
3 from Exceptions import *
4 import utils
5 import compatibility
6
7
8 message= \
9 """
10 ---------------------------------------------------------
11 This machine has entered a temporary debug state, so
12 Planetlab Support can login and fix any problems that
13 might have occurred.
14
15 Please do not reboot this machine at this point, unless
16 specifically asked to.
17
18 Thank you.
19 ---------------------------------------------------------
20 """
21
22
23 def Run( vars, log ):
24     """
25     Bring up sshd inside the boot cd environment for debug purposes.
26
27     Once its running, touch the file /tmp/SSHD_RUNNING so future
28     calls to this function don't do anything.
29
30     Expect the following variables in vars to be set:
31     BM_SOURCE_DIR     The source dir for the boot manager sources that
32                       we are currently running from
33     BOOT_CD_VERSION          A tuple of the current bootcd version
34     """
35
36     log.write( "\n\nStep: Starting debug mode.\n" )
37     
38     # make sure we have the variables we need
39     try:
40         BM_SOURCE_DIR= vars["BM_SOURCE_DIR"]
41         if BM_SOURCE_DIR == "":
42             raise ValueError, "BM_SOURCE_DIR"
43
44         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
45         if BOOT_CD_VERSION == "":
46             raise ValueError, "BOOT_CD_VERSION"
47
48     except KeyError, var:
49         raise BootManagerException, "Missing variable in vars: %s\n" % var
50     except ValueError, var:
51         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
52
53
54     log.write( "Starting debug environment\n" )
55
56     ssh_source_files= "%s/debug_files/" % BM_SOURCE_DIR    
57     ssh_dir= "/etc/ssh/"
58     ssh_home= "/root/.ssh"
59     cancel_boot_flag= "/tmp/CANCEL_BOOT"
60     sshd_started_flag= "/tmp/SSHD_RUNNING"
61     
62     sshd_started= 0
63     try:
64         os.stat(sshd_started_flag)
65         sshd_started= 1
66     except OSError, e:
67         pass
68
69     if not sshd_started:
70         log.write( "Creating ssh host keys\n" )
71         
72         utils.makedirs( ssh_dir )
73         utils.sysexec( "ssh-keygen -t rsa1 -b 1024 -f %s/ssh_host_key -N ''" %
74                        ssh_dir, log )
75         utils.sysexec( "ssh-keygen -t rsa -f %s/ssh_host_rsa_key -N ''" %
76                        ssh_dir, log )
77         utils.sysexec( "ssh-keygen -d -f %s/ssh_host_dsa_key -N ''" %
78                        ssh_dir, log )
79
80         if BOOT_CD_VERSION[0] == 3:
81             utils.sysexec( "cp -f %s/sshd_config_v3 %s/sshd_config" %
82                            (ssh_source_files,ssh_dir), log )
83         else:
84             utils.sysexec( "cp -f %s/sshd_config_v2 %s/sshd_config" %
85                            (ssh_source_files,ssh_dir), log )
86     else:
87         log.write( "ssh host keys already created\n" )
88
89
90     # always update the key, may have change in this instance of the bootmanager
91     log.write( "Installing debug ssh key for root user\n" )
92     
93     utils.makedirs( ssh_home )
94     utils.sysexec( "cp -f %s/debug_root_ssh_key %s/authorized_keys" %
95                    (ssh_source_files,ssh_home), log )
96     utils.sysexec( "chmod 700 %s" % ssh_home, log )
97     utils.sysexec( "chmod 600 %s/authorized_keys" % ssh_home, log )
98
99     if not sshd_started:
100         log.write( "Starting sshd\n" )
101         
102         if BOOT_CD_VERSION[0] == 2:
103             utils.sysexec( "/usr/sbin/sshd", log )
104         else:
105             utils.sysexec( "service sshd start", log )
106         
107         # flag that ssh is running
108         utils.sysexec( "touch %s" % sshd_started_flag, log )
109     else:
110         log.write( "sshd already running\n" )
111
112
113     # for ease of use, setup lvm on 2.x cds
114     if BOOT_CD_VERSION[0] == 2:
115         compatibility.setup_lvm_2x_cd(vars,log)
116
117     
118     # this will make the initial script stop requesting scripts from PLC
119     utils.sysexec( "touch %s" % cancel_boot_flag, log )
120
121     print message
122     
123     return
124