65af3f1705d688713bad3082aaf1147ebaf04d13
[bootmanager.git] / source / steps / StartDebug.py
1 #!/usr/bin/python
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 from __future__ import print_function
10
11 import os
12
13 from Exceptions import *
14 import utils
15
16
17 warning_message = \
18 """
19 ---------------------------------------------------------
20 This machine has entered a temporary debug state, so
21 Planetlab Support can login and fix any problems that
22 might have occurred.
23
24 Please do not reboot this machine at this point, unless
25 specifically asked to.
26
27 Thank you.
28 ---------------------------------------------------------
29 """
30
31 # this can be invoked 
32 # either at the end of the bm logic, because something failed (last_resort = True)
33 # and/or it can be invoked as a fallback very early in the bootmanager logic,
34 # so we can reach the node regardless of what happens (e.g. bm sometimes hangs)
35
36 def Run(vars, log, last_resort = True):
37
38     """
39     Bring up sshd inside the boot cd environment for debug purposes.
40
41     Once its running, touch the file /tmp/SSHD_RUNNING so future
42     calls to this function don't do anything.
43
44     Expect the following variables in vars to be set:
45     BM_SOURCE_DIR     The source dir for the boot manager sources that
46                         we are currently running from
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     except KeyError, var:
64         raise BootManagerException, "Missing variable in vars: %s\n" % var
65     except ValueError, var:
66         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
67
68     # constants
69     ssh_source_files = "%s/debug_files/" % BM_SOURCE_DIR    
70     ssh_dir = "/etc/ssh/"
71     ssh_home = "/root/.ssh"
72     cancel_boot_flag = "/tmp/CANCEL_BOOT"
73     sshd_started_flag = "/tmp/SSHD_RUNNING"
74
75     # pre-sshd
76     pre_sshd_script = os.path.join(ssh_source_files, "pre-sshd")
77     if os.path.exists(pre_sshd_script):
78         utils.sysexec_noerr(pre_sshd_script, log)
79     
80     # create host keys if needed
81     if not os.path.isdir (ssh_dir):
82         utils.makedirs (ssh_dir)
83     key=ssh_dir+"/ssh_host_key"
84     if not os.path.isfile (key):
85         log.write("Creating host rsa1 key %s\n"%key)
86         utils.sysexec( "ssh-keygen -t rsa1 -b 1024 -f %s -N ''" % key, log )
87     key=ssh_dir+"/ssh_host_rsa_key"
88     if not os.path.isfile (key):
89         log.write("Creating host rsa key %s\n"%key)
90         utils.sysexec( "ssh-keygen -t rsa -f %s -N ''" % key, log )
91     key=ssh_dir+"/ssh_host_dsa_key"
92     if not os.path.isfile (key):
93         log.write("Creating host dsa key %s\n"%key)
94         # very old versions did 'ssh-keygen -d' instead of 'ssh-keygen -t dsa' 
95         utils.sysexec( "ssh-keygen -t dsa -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 {}/debug_root_ssh_key {}/authorized_keys".format(ssh_source_files, ssh_home), log)
107     utils.sysexec("chmod 700 {}".format(ssh_home), log)
108     utils.sysexec("chmod 600 {}/authorized_keys".format(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 {}".format(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 {}".format(cancel_boot_flag), log)
124
125     if last_resort:
126         print(warning_message)
127     
128     return