summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorkelvin <xiubuzhe@sina.com>2023-12-19 22:28:55 +0800
committerkelvin <xiubuzhe@sina.com>2023-12-19 22:28:55 +0800
commit8a52a10fb6d6f6802872cc7ceee6f38ccd631c9b (patch)
tree57fe5f6a989d12eba721735dfa443301b72f2731 /lib
parente17ec5a485ec6452b03e29e1215de2281699b7d6 (diff)
downloadsunhpc-8a52a10fb6d6f6802872cc7ceee6f38ccd631c9b.tar.gz
sunhpc-8a52a10fb6d6f6802872cc7ceee6f38ccd631c9b.tar.bz2
sunhpc-8a52a10fb6d6f6802872cc7ceee6f38ccd631c9b.zip
add cdpxe command
Diffstat (limited to 'lib')
-rw-r--r--lib/sunhpc/commands/pxelinux/build/__init__.py78
-rw-r--r--lib/sunhpc/commands/pxelinux/build/cdpxe/__init__.py90
2 files changed, 159 insertions, 9 deletions
diff --git a/lib/sunhpc/commands/pxelinux/build/__init__.py b/lib/sunhpc/commands/pxelinux/build/__init__.py
index 39ab37e..370f846 100644
--- a/lib/sunhpc/commands/pxelinux/build/__init__.py
+++ b/lib/sunhpc/commands/pxelinux/build/__init__.py
@@ -12,7 +12,10 @@ class RollHandler(object):
self.cmd = cmd
self.clean = clean
self.mntdir = mnt
+ self.repodirs = ''
self.rinfo = None
+ self.isoname = 'unknown'
+ self.foreign = False
def is_mounted(self):
cmd = 'mount |grep %s' % self.mntdir
@@ -22,6 +25,7 @@ class RollHandler(object):
def mount_iso(self, iso):
subprocess.run('mount -r %s %s' % (iso, self.mntdir), shell=True, check=True)
+ self.isoname = iso
def umount_iso(self):
subprocess.run('umount %s' % self.mntdir, shell=True)
@@ -36,7 +40,25 @@ class RollHandler(object):
pass
def foreign_roll(self):
- self.cmd.msg('This ISO file cannot be recognized.This ISO was not produced by sunhpc created.', 'w')
+ self.cmd.msg('The "%s" was not produced by sunhpc created.' % self.isoname, 'w')
+ # check iso .discinfo file
+ discinfo = os.path.join(self.mntdir, '.discinfo')
+ if not os.path.exists(discinfo):
+ self.cmd.msg('The %s is not exists.' % discinfo, 'a')
+
+ info = []
+ with open(discinfo, 'r') as fn:
+ info = fn.readlines()
+
+ try:
+ self.foreign = True
+ self.rinfo = sunhpc.core.utils.Struct()
+ self.rinfo.name = info[1].split()[0].strip()
+ self.rinfo.version = info[1].split()[-1].strip()
+ self.rinfo.arch = info[2].strip()
+ except IndexError:
+ self.foreign = False
+
def copy_iso(self):
self.read_iso()
@@ -47,11 +69,20 @@ class RollHandler(object):
def copy_roll(self):
# self.rinfo是经过file.RollInfoFile处理过的,返回相应的类对象.
- roll_name = self.rinfo.getRollName()
- roll_vers = self.rinfo.getRollVersion()
- roll_arch = self.rinfo.getRollArch()
- roll_os = self.rinfo.getRollOS()
- roll_boot = self.rinfo.isBootable()
+ if self.foreign:
+ roll_name = self.rinfo.name
+ roll_vers = self.rinfo.version
+ roll_arch = self.rinfo.arch
+ roll_os = self.cmd.os
+ roll_boot = None
+ roll_path = self.mntdir
+ else:
+ roll_name = self.rinfo.getRollName()
+ roll_vers = self.rinfo.getRollVersion()
+ roll_arch = self.rinfo.getRollArch()
+ roll_os = self.rinfo.getRollOS()
+ roll_boot = self.rinfo.isBootable()
+ roll_path = os.path.join(self.mntdir, roll_name)
# 获取rolls存放位置
cmd = '/opt/sunhpc/bin/sunhpc report distro'
@@ -59,10 +90,16 @@ class RollHandler(object):
distro = line[:-1]
# /export/sunhpc/install/rolls
- rolls_dir = '%s/rolls' % (distro)
+ if self.foreign:
+ rolls_dir = '%s/repos' % (distro)
+ else:
+ rolls_dir = '%s/rolls' % (distro)
# /export/sunhpc/install/rolls/kernel......
- roll_dir = os.path.join(rolls_dir, roll_name)
+ if self.foreign:
+ roll_dir = os.path.join(rolls_dir, roll_name, roll_vers, roll_arch)
+ else:
+ roll_dir = os.path.join(rolls_dir, roll_name)
if self.clean:
# /export/sunhpc/install/rolls/kernel/version/x86_64
@@ -75,7 +112,7 @@ class RollHandler(object):
os.makedirs(specific_roll_dir)
cwd = os.getcwd()
- os.chdir(os.path.join(self.mntdir, roll_name))
+ os.chdir(roll_path)
# 计算一下大小
cmd = 'du -sh %s' % self.mntdir
@@ -122,6 +159,7 @@ class RollHandler(object):
# 如果是引导roll,需要使用软件Kylins create distro重新部署.
# 重新创建images,stage2,和相关连接相关任务.
self.cmd.msg('Copy the %s finished.' % roll_name)
+ self.repodirs = roll_dir
def clean_dirs(self, dirs):
for root, dirs, files in os.walk(dirs, topdown=False):
@@ -202,6 +240,28 @@ class Command(command):
roll_handler.copy_iso()
roll_handler.umount_iso()
+ if roll_handler.foreign:
+ local_repos = '/etc/yum.repos.d/Rocky-Local.repo'
+ with open(local_repos, 'w') as fn:
+ fn.write('#\n# Rocky-Local.repo\n# Generate by sunhpc\n#\n\n')
+ fn.write('[local-baseos]\n')
+ fn.write('name=Rocky Linux $releasever - Local - BaseOS\n')
+ fn.write('baseurl=file:///%s/BaseOS\n' % roll_handler.repodirs)
+ fn.write('gpgcheck=1\n')
+ fn.write('enabled=1\n')
+ fn.write('gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial\n\n')
+
+ fn.write('[local-appstream]\n')
+ fn.write('name=Rocky Linux $releasever - Local - AppStream\n')
+ fn.write('baseurl=file:///%s/AppStream\n' % roll_handler.repodirs)
+ fn.write('gpgcheck=1\n')
+ fn.write('enabled=1\n')
+ fn.write('gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial\n\n')
+
+ self.msg('Add %s to %s' % (roll_handler.repodirs, local_repos))
+ self.msg(' Use the command: yum/dnf repolist all')
+ sys.exit(0)
+
if pxesrv:
self.configSRV(q)
else:
diff --git a/lib/sunhpc/commands/pxelinux/build/cdpxe/__init__.py b/lib/sunhpc/commands/pxelinux/build/cdpxe/__init__.py
new file mode 100644
index 0000000..a245cae
--- /dev/null
+++ b/lib/sunhpc/commands/pxelinux/build/cdpxe/__init__.py
@@ -0,0 +1,90 @@
+#coding:utf-8
+
+import os
+import sys
+import sunhpc
+import shutil
+import textwrap
+import subprocess
+class Command(sunhpc.commands.pxelinux.build.command):
+ """
+ Build the iso pxe (dhcpd, tftpd, httpd service for the sunhpc cluster.
+
+ <param type='Bool' name='Quiet'>
+ Whether to output detailed information, default: no
+ </param>
+
+ <param type='name' name='dev'>
+ supply an network device name,e.g,, eth0,eth1...
+ </param>
+
+ <example cmd='pxelinux build cdpxe'>
+ In local build the iso pxe dhcpd, tftpd, httpd services.
+ </example>
+
+ <example cmd='pxelinux build cdpxe'>
+ Build the iso pxe dhcpd, tftpd, httpd service.
+ </example>
+ """
+ def run(self, params, args):
+
+ (quiet, devices) = self.fillParams([('quiet', 'no'), ('dev', None)])
+
+ q = self.str2bool(quiet)
+ self.msg('Starting build the dhcpd service...', q=q)
+
+ if devices is None:
+ self.msg('Must supply an network dev name,e.g,, eth0/eth1...', 'a')
+
+ # install dhcp server
+ self.installDhcpd(q, devices)
+
+ def installDhcpd(self, q, devices):
+ config = '/etc/dhcp/dhcpd.conf'
+
+ # The dhcpd is not installed.
+ if not os.path.exists(config):
+ self.shcmd('yum install -y dhcp-server')
+
+ with open(config, 'w') as f:
+ f.write(textwrap.dedent("""\
+ #
+ # dhcpd.conf
+ #
+ subnet 172.16.2.0 netmask 255.255.255.0 {
+ range 172.16.2.10 172.16.2.200;
+ option domain-name "hpc.com";
+ option domain-name-servers 8.8.8.8;
+ option routers 172.16.2.1;
+ default-lease-time 600;
+ max-lease-time 7200;
+ }
+ """))
+
+ dhcpd_srv_conf = '/usr/lib/systemd/system/dhcpd.service'
+ with open(dhcpd_srv_conf, 'w') as f:
+ f.write(textwrap.dedent("""\
+ [Unit]
+ Description=DHCPv4 Server Daemon
+ Documentation=man:dhcpd(8) man:dhcpd.conf(5)
+ Wants=network-online.target
+ After=network-online.target
+ After=time-sync.target
+
+ [Service]
+ Type=notify
+ ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid %s
+
+ [Install]
+ WantedBy=multi-user.target
+ """ % devices))
+
+ self.fw.addports(['67'], ['udp'])
+ self.fw.addports(['68'], ['udp'])
+ self.shcmd('systemctl daemon-reload')
+ self.shcmd('systemctl stop dhcpd')
+ self.shcmd('systemctl start dhcpd')
+ self.shcmd('systemctl enable dhcpd')
+ os.system('systemctl status dhcpd')
+
+RollName = "base"