VMware DevOps Meetup #2 - connpass 用のネタ。
詳しくは当日の LT にて…(資料も上がると思います)
Photon OS 初期設定コード
#!/bin/python3 import xml.etree.ElementTree as ET import subprocess import textwrap import os VMTOOLSD = "/usr/bin/vmtoolsd --cmd 'info-get guestinfo.ovfenv'" HOSTNAMECTL = "hostnamectl set-hostname {}" NS_OE = {'oe': 'http://schemas.dmtf.org/ovf/environment/1'} VALKEY = "{http://schemas.dmtf.org/ovf/environment/1}value" VCENTERID = "{http://www.vmware.com/schema/ovfenv}vCenterId" ENV_XPATH = "oe:PropertySection/oe:Property[@oe:key='{}']" OVFKEY = ["ip", "prefix", "gateway", "hostname", "dns"] LOCKFILE = "/var/lib/misc/photoninit.lck" NETFILE = "/etc/systemd/network/10-static-en.network" DHCPFILE = "/etc/systemd/network/99-dhcp-en.network" def getOvfEnv(): try: res = subprocess.check_output(VMTOOLSD, shell=True).decode('utf-8') except SubprocessError: raise return res def chkLock(vmid): if os.path.exists(LOCKFILE): with open(LOCKFILE, "r") as f: s = f.read() if s == vmid: return True return False def setLock(vmid): with open(LOCKFILE, "w") as f: f.write(vmid) def deployHostname(hostname): try: subprocess.run(HOSTNAMECTL.format(hostname), shell=True) except SubprocessError: raise def deployNetwork(ip, prefix, gateway, dns): conf = textwrap.dedent(''' [Match] Name=eth0 [Network] Address={t_ip}/{t_prefix} Gateway={t_gateway} DNS={t_dns} ''').format(t_ip=ip, t_prefix=prefix, t_gateway=gateway, t_dns=dns).strip() with open(NETFILE, "w") as f: f.write(conf) os.chmod(NETFILE, 0o644) if os.path.exists(DHCPFILE): disableDHCP() def disableDHCP(): conf = textwrap.dedent(''' [Match] Name=e* [Network] DHCP=no ''').strip() with open(DHCPFILE, "w") as f: f.write(conf) os.chmod(DHCPFILE, 0o644) def main(): try: xml = getOvfEnv() except Exception as e: print("vmtoolsd error:{}".format(e)) return 1 tree = ET.fromstring(xml) vmid = tree.attrib[VCENTERID] if chkLock(vmid): print("Nothing to do.") return 0 dic = {} for key in OVFKEY: attr = ENV_XPATH.format(key) dic[key] = tree.find(attr, NS_OE).attrib[VALKEY] try: deployHostname(dic["hostname"]) except Exception as e: print("Hostname setting error:{}".format(e)) return 1 deployNetwork(dic["ip"], dic["prefix"], dic["gateway"], dic["dns"]) setLock(vmid) return 0 if __name__ == '__main__': exit(main())
service
[Unit] Description=photoninit Before=getty@tty1.service systemd-networkd.service systemd-resolved.service After=local-fs.target [Service] Type=oneshot ExecStart=/usr/local/bin/photoninit RemainAfterExit=yes [Install] WantedBy=multi-user.target
git でないのはアカウント無かったから…そのうち作るかも?