|
| 1 | +#!/usr/bin/python |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +import sys, os |
| 20 | +from subprocess import PIPE, Popen |
| 21 | +import logging |
| 22 | +import traceback |
| 23 | +from os.path import exists, join |
| 24 | +from signal import alarm, signal, SIGALRM, SIGKILL |
| 25 | + |
| 26 | +class CloudRuntimeException(Exception): |
| 27 | + def __init__(self, errMsg): |
| 28 | + self.errMsg = errMsg |
| 29 | + def __str__(self): |
| 30 | + return self.errMsg |
| 31 | +def formatExceptionInfo(maxTBlevel=5): |
| 32 | + cla, exc, trbk = sys.exc_info() |
| 33 | + excTb = traceback.format_tb(trbk, maxTBlevel) |
| 34 | + msg = str(exc) + "\n" |
| 35 | + for tb in excTb: |
| 36 | + msg += tb |
| 37 | + return msg |
| 38 | + |
| 39 | +class bash: |
| 40 | + def __init__(self, args, timeout=600): |
| 41 | + self.args = args |
| 42 | + logging.debug("execute:%s"%args) |
| 43 | + self.timeout = timeout |
| 44 | + self.process = None |
| 45 | + self.success = False |
| 46 | + self.run() |
| 47 | + |
| 48 | + def run(self): |
| 49 | + class Alarm(Exception): |
| 50 | + pass |
| 51 | + def alarm_handler(signum, frame): |
| 52 | + raise Alarm |
| 53 | + |
| 54 | + try: |
| 55 | + self.process = Popen(self.args, shell=True, stdout=PIPE, stderr=PIPE) |
| 56 | + if self.timeout != -1: |
| 57 | + signal(SIGALRM, alarm_handler) |
| 58 | + alarm(self.timeout) |
| 59 | + |
| 60 | + try: |
| 61 | + self.stdout, self.stderr = self.process.communicate() |
| 62 | + if self.timeout != -1: |
| 63 | + alarm(0) |
| 64 | + except Alarm: |
| 65 | + os.kill(self.process.pid, SIGKILL) |
| 66 | + raise CloudRuntimeException("Timeout during command execution") |
| 67 | + |
| 68 | + self.success = self.process.returncode == 0 |
| 69 | + except: |
| 70 | + raise CloudRuntimeException(formatExceptionInfo()) |
| 71 | + |
| 72 | +# if not self.success: |
| 73 | +# raise CloudRuntimeException(self.getStderr()) |
| 74 | + |
| 75 | + def isSuccess(self): |
| 76 | + return self.success |
| 77 | + |
| 78 | + def getStdout(self): |
| 79 | + return self.stdout.strip("\n") |
| 80 | + |
| 81 | + def getLines(self): |
| 82 | + return self.stdout.split("\n") |
| 83 | + |
| 84 | + def getStderr(self): |
| 85 | + return self.stderr.strip("\n") |
| 86 | + |
| 87 | + |
| 88 | +def initLoging(logFile=None): |
| 89 | + try: |
| 90 | + if logFile is None: |
| 91 | + logging.basicConfig(level=logging.DEBUG) |
| 92 | + else: |
| 93 | + logging.basicConfig(filename=logFile, level=logging.DEBUG) |
| 94 | + except: |
| 95 | + logging.basicConfig(level=logging.DEBUG) |
| 96 | + |
| 97 | +def writeProgressBar(msg, result=None): |
| 98 | + if msg is not None: |
| 99 | + output = "%-80s"%msg |
| 100 | + elif result is True: |
| 101 | + output = "[ \033[92m%-2s\033[0m ]\n"%"OK" |
| 102 | + elif result is False: |
| 103 | + output = "[ \033[91m%-6s\033[0m ]\n"%"FAILED" |
| 104 | + sys.stdout.write(output) |
| 105 | + sys.stdout.flush() |
| 106 | + |
| 107 | +def printError(msg): |
| 108 | + sys.stderr.write(msg) |
| 109 | + sys.stderr.write("\n") |
| 110 | + sys.stderr.flush() |
| 111 | + |
| 112 | +def printMsg(msg): |
| 113 | + sys.stdout.write(msg+"\n") |
| 114 | + sys.stdout.flush() |
| 115 | + |
| 116 | +def checkRpm(pkgName): |
| 117 | + chkPkg = bash("rpm -q %s"%pkgName) |
| 118 | + writeProgressBar("Checking %s"%pkgName, None) |
| 119 | + if not chkPkg.isSuccess(): |
| 120 | + writeProgressBar(None, False) |
| 121 | + printError("%s is not found, please make sure it is installed. You may try 'yum install %s'\n"%(pkgName, pkgName)) |
| 122 | + return False |
| 123 | + else: |
| 124 | + writeProgressBar(None, True) |
| 125 | + return True |
| 126 | + |
| 127 | +def checkEnv(): |
| 128 | + writeProgressBar("Checking is root") |
| 129 | + ret = bash("whoami") |
| 130 | + if ret.getStdout() != "root": |
| 131 | + writeProgressBar(None, False) |
| 132 | + printError("This script must run as root") |
| 133 | + return False |
| 134 | + else: |
| 135 | + writeProgressBar(None, True) |
| 136 | + |
| 137 | + pkgList = ['tftp-server', 'syslinux', 'xinetd', 'chkconfig', 'dhcp'] |
| 138 | + for pkg in pkgList: |
| 139 | + if not checkRpm(pkg): |
| 140 | + return False |
| 141 | + return True |
| 142 | + |
| 143 | +def exitIfFail(ret): |
| 144 | + if not ret: sys.exit(1) |
| 145 | + |
| 146 | +def bashWithResult(cmd): |
| 147 | + writeProgressBar("Executing '%s'"%cmd) |
| 148 | + ret = bash(cmd) |
| 149 | + if not ret.isSuccess(): |
| 150 | + writeProgressBar(None, False) |
| 151 | + writeProgressBar(ret.getStderr() + '\n') |
| 152 | + return False |
| 153 | + else: |
| 154 | + writeProgressBar(None, True) |
| 155 | + return True |
| 156 | + |
| 157 | +def configurePxeStuff(): |
| 158 | + stuff = ['tftp', 'xinetd', 'dhcpd'] |
| 159 | + cmds = ['chkconfig --level 345 %s on' % i for i in stuff] |
| 160 | + cmds.append('/etc/init.d/xinetd restart') |
| 161 | + |
| 162 | + for cmd in cmds: |
| 163 | + if not bashWithResult(cmd): return False |
| 164 | + |
| 165 | + chkIptable = bash('chkconfig --list iptables') |
| 166 | + if 'on' in chkIptable.getStdout(): |
| 167 | + printMsg("Detected iptables is running, need to open tftp port 69") |
| 168 | + if not bashWithResult('iptables -I INPUT 1 -p udp --dport 69 -j ACCEPT'): return False |
| 169 | + if not bashWithResult('/etc/init.d/iptables save'): return False |
| 170 | + |
| 171 | + return True |
| 172 | + |
| 173 | +def getTftpRootDir(tftpRootDirList): |
| 174 | + tftpRoot = bash("cat /etc/xinetd.d/tftp | grep server_args") |
| 175 | + if not tftpRoot.isSuccess(): |
| 176 | + printError("Cannot get tftp root directory from /etc/xinetd.d/tftp, here may be something wrong with your tftp-server, try reinstall it\n") |
| 177 | + return False |
| 178 | + tftpRootDir = tftpRoot.getStdout() |
| 179 | + index = tftpRootDir.find("/") |
| 180 | + if index == -1: |
| 181 | + printError("Wrong server_arg in /etc/xinetd.d/tftp (%s)"%tftpRootDir) |
| 182 | + return False |
| 183 | + tftpRootDir = tftpRootDir[index:] |
| 184 | + tftpRootDirList.append(tftpRootDir) |
| 185 | + return True |
| 186 | + |
| 187 | +def preparePING(tftpRootDir): |
| 188 | + pingFiles = ['boot.msg', 'initrd.gz', 'kernel', 'pxelinux.0'] |
| 189 | + pingDir = "/usr/share/PING" |
| 190 | + |
| 191 | + for f in pingFiles: |
| 192 | + path = join(pingDir, f) |
| 193 | + if not exists(path): |
| 194 | + printError("Cannot find %s, please make sure PING-3.01 is installed"%path) |
| 195 | + return False |
| 196 | + if not bashWithResult("cp -f %s %s"%(path, tftpRootDir)): return False |
| 197 | + |
| 198 | + if not bashWithResult("mkdir -p %s/pxelinux.cfg"%tftpRootDir): return False |
| 199 | + |
| 200 | + return True |
| 201 | + |
| 202 | +def prepareSyslinux(tftpRootDir): |
| 203 | + pkg = bash('rpm -ql syslinux | grep "/pxelinux.0$"') |
| 204 | + if not pkg.isSuccess(): |
| 205 | + printError('cannot find pxelinux.0 on system. is syslinux installed?') |
| 206 | + return False |
| 207 | + |
| 208 | + pkg = pkg.getStdout() |
| 209 | + cp = "cp -f %s %s" % (pkg, tftpRootDir) |
| 210 | + if not bashWithResult(cp): |
| 211 | + return False |
| 212 | + |
| 213 | + return True |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == "__main__": |
| 218 | + initLoging("/tmp/cloud-setup-baremetal.log") |
| 219 | + tftpRootDirList = [] |
| 220 | + |
| 221 | + exitIfFail(checkEnv()) |
| 222 | + exitIfFail(configurePxeStuff()) |
| 223 | + exitIfFail(getTftpRootDir(tftpRootDirList)) |
| 224 | + |
| 225 | + tftpRootDir = tftpRootDirList[0].strip() |
| 226 | + #exitIfFail(preparePING(tftpRootDir)) |
| 227 | + exitIfFail(prepareSyslinux(tftpRootDir)) |
| 228 | + printMsg("") |
| 229 | + printMsg("Setup BareMetal PXE server successfully") |
| 230 | + printMsg("TFTP root directory is: %s\n"%tftpRootDir) |
| 231 | + sys.exit(0) |
| 232 | + |
0 commit comments