github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/sdk/azcore/to"
     8  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v2"
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/core/os/ostype"
    12  )
    13  
    14  const extensionName = "JujuCustomScriptExtension"
    15  
    16  const (
    17  	// The string will be split and executed by Python's
    18  	// subprocess.call, not interpreted as a shell command.
    19  	linuxExecuteCustomScriptCommand = `bash -c 'base64 -d /var/lib/waagent/CustomData | bash'`
    20  	linuxCustomScriptPublisher      = "Microsoft.OSTCExtensions"
    21  	linuxCustomScriptType           = "CustomScriptForLinux"
    22  	linuxCustomScriptVersion        = "1.4"
    23  )
    24  
    25  // vmExtension creates a CustomScript VM extension for the given VM
    26  // which will execute the CustomData on the machine as a script.
    27  func vmExtensionProperties(os ostype.OSType) (*armcompute.VirtualMachineExtensionProperties, error) {
    28  	var commandToExecute, extensionPublisher, extensionType, extensionVersion string
    29  
    30  	switch os {
    31  	case ostype.CentOS:
    32  		commandToExecute = linuxExecuteCustomScriptCommand
    33  		extensionPublisher = linuxCustomScriptPublisher
    34  		extensionType = linuxCustomScriptType
    35  		extensionVersion = linuxCustomScriptVersion
    36  	default:
    37  		// Ubuntu renders CustomData as cloud-config, and interprets
    38  		// it with cloud-init. CentOS does not use cloud-init on Azure.
    39  		return nil, errors.NotSupportedf("CustomScript extension for OS %q", os)
    40  	}
    41  
    42  	extensionSettings := map[string]interface{}{
    43  		"commandToExecute": commandToExecute,
    44  	}
    45  	return &armcompute.VirtualMachineExtensionProperties{
    46  		Publisher:               to.Ptr(extensionPublisher),
    47  		Type:                    to.Ptr(extensionType),
    48  		TypeHandlerVersion:      to.Ptr(extensionVersion),
    49  		AutoUpgradeMinorVersion: to.Ptr(true),
    50  		Settings:                &extensionSettings,
    51  	}, nil
    52  }