Detangled steps. No step makes calls into another step.
[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         log.write( "Creating ssh host keys\n" )
79         
80         utils.makedirs( ssh_dir )
81         utils.sysexec( "ssh-keygen -t rsa1 -b 1024 -f %s/ssh_host_key -N ''" %
82                        ssh_dir, log )
83         utils.sysexec( "ssh-keygen -t rsa -f %s/ssh_host_rsa_key -N ''" %
84                        ssh_dir, log )
85         utils.sysexec( "ssh-keygen -d -f %s/ssh_host_dsa_key -N ''" %
86                        ssh_dir, log )
87
88         if BOOT_CD_VERSION[0] == 3:
89             utils.sysexec( "cp -f %s/sshd_config_v3 %s/sshd_config" %
90                            (ssh_source_files,ssh_dir), log )
91         else:
92             utils.sysexec( "cp -f %s/sshd_config_v2 %s/sshd_config" %
93                            (ssh_source_files,ssh_dir), log )
94     else:
95         log.write( "ssh host keys already created\n" )
96
97
98     # always update the key, may have change in this instance of the bootmanager
99     log.write( "Installing debug ssh key for root user\n" )
100     
101     utils.makedirs( ssh_home )
102     utils.sysexec( "cp -f %s/debug_root_ssh_key %s/authorized_keys" %
103                    (ssh_source_files,ssh_home), log )
104     utils.sysexec( "chmod 700 %s" % ssh_home, log )
105     utils.sysexec( "chmod 600 %s/authorized_keys" % ssh_home, log )
106
107     if not sshd_started:
108         log.write( "Starting sshd\n" )
109         
110         if BOOT_CD_VERSION[0] == 2:
111             utils.sysexec( "/usr/sbin/sshd", log )
112         else:
113             utils.sysexec( "service sshd start", log )
114         
115         # flag that ssh is running
116         utils.sysexec( "touch %s" % sshd_started_flag, log )
117     else:
118         log.write( "sshd already running\n" )
119
120
121     # for ease of use, setup lvm on 2.x cds
122     if BOOT_CD_VERSION[0] == 2:
123         compatibility.setup_lvm_2x_cd(vars,log)
124
125     
126     # this will make the initial script stop requesting scripts from PLC
127     utils.sysexec( "touch %s" % cancel_boot_flag, log )
128
129     print message
130     
131     return
132