diff --git a/README.md b/README.md index 4e0f10a..fa3ccab 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # Geco-cloudbase-init + This is a Proxmox patch that allows us to use Cloudbase-init with windows hosts. What can you do with this patch? @@ -18,3 +19,23 @@ There is two files that we need to modify Qemu.pm and Cloudinit.pm. * Cloudinit.pm to generate a metadata json file with variables that are compatible with Cloudbase-Init. If you want to apply the patch manually you can follow these steps: [Manual Patching](https://git.geco-it.net/c.soylu/Geco-cloudbase-init/src/branch/master/MANUALPATCH.md) + +## Scripts +We have two scripts that do some fonctionality that we needed; enabling administrator user and enabling dhcp +You need to move those scripts into Cloudbase Solutions\Cloudbase-Init\LocalScripts\ in your program files. + +## Powershell Script +This powershell script has a few uses. +* Deletes the "cloudbase-init" user, delegates "cloudbase-init" service to local Systeme user and modifies execution path of the script also to use local system user. +* Installs OpenSSH-Server from optional features of Windows. +* Removes a store language package that causes an error when generelazing for sysprep. + +You need to run this script after installing Cloudbase-Init Continous Build. We need continous build to use Username fonctionality of openstack service. + +When everythin is installed simply run below in powershell to launch sysprep: +``` +cd ‘C:\Program Files\Cloudbase Solutions\Cloudbase-Init\conf’ +C:\Windows\System32\sysprep\sysprep.exe /generalize /oobe /unattend:Unattend.xml + +``` + diff --git a/localscripts/ActivateAdministrator.py b/localscripts/ActivateAdministrator.py new file mode 100644 index 0000000..7260489 --- /dev/null +++ b/localscripts/ActivateAdministrator.py @@ -0,0 +1,44 @@ +import os,json,sys,subprocess + +def find_drive(file_path): + for number in range(65,91): + drive_letter = chr(number) + if os.path.exists(drive_letter+file_path): + return drive_letter+file_path + print("\n Searched file could not be found on any drive with path:" + file_path) + return False + +def load_json_file(file_path,variable): + file = open(file_path) + data = json.load(file) + file.close() + return data.get(variable) + + +def get_administrator_status(): + command = "(Get-LocalUser | Where-Object{$_.SID -like \"S-1-5-*-500\"}).Enabled" + run = subprocess.run(["powershell", "-Command", command], stdout=subprocess.PIPE, universal_newlines=True) + print("Is admin account enabled already: " + run.stdout) + return run.stdout + + +def enable_administrator_account(): + command = "(Get-LocalUser | Where-Object{$_.SID -like \"S-1-5-*-500\"}).Name | Enable-LocalUser" + run = subprocess.run(["powershell", "-Command", command], stdout=subprocess.PIPE, universal_newlines=True) + print("\n Administrator account is activated by localscript") + return run.stdout + +#execute + +meta_data_path = find_drive(":\OPENSTACK\LATEST\META_DATA.json") +if meta_data_path != "False": + meta_data = load_json_file(meta_data_path,"meta") +else: + sys.exit(0) + +if meta_data["admin_username"] in ["Administrateur","Administrator"] and "False" in get_administrator_status(): + run = enable_administrator_account() + sys.exit(1001) +else: + print("Cloud-init user is not Administrateur/Administrator or Admin account is already enabled, script aborted.") + sys.exit(0) \ No newline at end of file diff --git a/localscripts/ActivateDHCP.py b/localscripts/ActivateDHCP.py new file mode 100644 index 0000000..42558da --- /dev/null +++ b/localscripts/ActivateDHCP.py @@ -0,0 +1,47 @@ +import os,json,sys,wmi +from cloudbaseinit.osutils import factory as osutils_factory +from cloudbaseinit.utils import network + + + +def load_json_variable(file_path,variable): + file = open(file_path) + data = json.load(file) + file.close() + return data.get(variable) + +def find_drive(file_path): + for number in range(65,91): + drive_letter = chr(number) + if os.path.exists(drive_letter+file_path): + return drive_letter+file_path + print("\n Searched file could not be found on any drive with path:" + file_path) + return False + +def get_name_by_mac(mac): + osutils = osutils_factory.get_os_utils() + name = osutils.get_network_adapter_name_by_mac_address(mac) + return name + +def activate_dhcp(name, family): + osutils = osutils_factory.get_os_utils() + osutils._fix_network_adapter_dhcp(name, True, family) + + +# variables +meta_data_path = find_drive(":\OPENSTACK\LATEST\META_DATA.json") +# 2 for ipv4 and 6 for ipv6 +family = 2 +# execute +if meta_data_path != "False": + macs = load_json_variable(meta_data_path,"dhcp") + + for mac in macs: + name = get_name_by_mac(mac) + activate_dhcp(name, family) + + + +else: + sys.exit(0) + diff --git a/powershell/FixUserService.ps1 b/powershell/FixUserService.ps1 new file mode 100644 index 0000000..c0686a4 --- /dev/null +++ b/powershell/FixUserService.ps1 @@ -0,0 +1,7 @@ +Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Serv*' | Select-Object -ExpandProperty Name | %{Add-WindowsCapability -Online -Name $_} +Set-Service -Name sshd -StartupType 'Automatic' +net user cloudbase-init /delete +sc.exe config cloudbase-init obj= .\LocalSystem +$newtext = Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\cloudbase-init' -Name 'ImagePath' | Select-Object -ExpandProperty ImagePath | %{$_.replace(" cloudbase-init ", " NT-AUTHORITY\SYSTEM ")} +Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\cloudbase-init' -Name 'ImagePath' -Value $newtext +Get-AppxPackage | Where-Object {$_.name -Like "*Language*"} | Remove-AppxPackage \ No newline at end of file