github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/azure/vmextension.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package azure 5 6 import ( 7 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute" 8 "github.com/Azure/go-autorest/autorest/to" 9 "github.com/juju/errors" 10 jujuos "github.com/juju/os" 11 ) 12 13 const extensionName = "JujuCustomScriptExtension" 14 15 const ( 16 windowsExecuteCustomScriptCommand = `` + 17 `move C:\AzureData\CustomData.bin C:\AzureData\CustomData.ps1 && ` + 18 `powershell.exe -ExecutionPolicy Unrestricted -File C:\AzureData\CustomData.ps1 && ` + 19 `del /q C:\AzureData\CustomData.ps1` 20 windowsCustomScriptPublisher = "Microsoft.Compute" 21 windowsCustomScriptType = "CustomScriptExtension" 22 windowsCustomScriptVersion = "1.4" 23 ) 24 25 const ( 26 // The string will be split and executed by Python's 27 // subprocess.call, not interpreted as a shell command. 28 linuxExecuteCustomScriptCommand = `bash -c 'base64 -d /var/lib/waagent/CustomData | bash'` 29 linuxCustomScriptPublisher = "Microsoft.OSTCExtensions" 30 linuxCustomScriptType = "CustomScriptForLinux" 31 linuxCustomScriptVersion = "1.4" 32 ) 33 34 // vmExtension creates a CustomScript VM extension for the given VM 35 // which will execute the CustomData on the machine as a script. 36 func vmExtensionProperties(os jujuos.OSType) (*compute.VirtualMachineExtensionProperties, error) { 37 var commandToExecute, extensionPublisher, extensionType, extensionVersion string 38 39 switch os { 40 case jujuos.Windows: 41 commandToExecute = windowsExecuteCustomScriptCommand 42 extensionPublisher = windowsCustomScriptPublisher 43 extensionType = windowsCustomScriptType 44 extensionVersion = windowsCustomScriptVersion 45 case jujuos.CentOS: 46 commandToExecute = linuxExecuteCustomScriptCommand 47 extensionPublisher = linuxCustomScriptPublisher 48 extensionType = linuxCustomScriptType 49 extensionVersion = linuxCustomScriptVersion 50 default: 51 // Ubuntu renders CustomData as cloud-config, and interprets 52 // it with cloud-init. Windows and CentOS do not use cloud-init 53 // on Azure. 54 return nil, errors.NotSupportedf("CustomScript extension for OS %q", os) 55 } 56 57 extensionSettings := map[string]interface{}{ 58 "commandToExecute": commandToExecute, 59 } 60 return &compute.VirtualMachineExtensionProperties{ 61 Publisher: to.StringPtr(extensionPublisher), 62 Type: to.StringPtr(extensionType), 63 TypeHandlerVersion: to.StringPtr(extensionVersion), 64 AutoUpgradeMinorVersion: to.BoolPtr(true), 65 Settings: &extensionSettings, 66 }, nil 67 }