github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/provider/azure/vmextension.go (about)

     1  package azure
     2  
     3  import (
     4  	"github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest/to"
     5  	"github.com/Azure/azure-sdk-for-go/arm/compute"
     6  	"github.com/juju/errors"
     7  	jujuos "github.com/juju/utils/os"
     8  )
     9  
    10  const extensionName = "JujuCustomScriptExtension"
    11  
    12  const (
    13  	windowsExecuteCustomScriptCommand = `` +
    14  		`move C:\AzureData\CustomData.bin C:\AzureData\CustomData.ps1 && ` +
    15  		`powershell.exe -ExecutionPolicy Unrestricted -File C:\AzureData\CustomData.ps1 && ` +
    16  		`del /q C:\AzureData\CustomData.ps1`
    17  	windowsCustomScriptPublisher = "Microsoft.Compute"
    18  	windowsCustomScriptType      = "CustomScriptExtension"
    19  	windowsCustomScriptVersion   = "1.4"
    20  )
    21  
    22  const (
    23  	// The string will be split and executed by Python's
    24  	// subprocess.call, not interpreted as a shell command.
    25  	linuxExecuteCustomScriptCommand = `bash -c 'base64 -d /var/lib/waagent/CustomData | bash'`
    26  	linuxCustomScriptPublisher      = "Microsoft.OSTCExtensions"
    27  	linuxCustomScriptType           = "CustomScriptForLinux"
    28  	linuxCustomScriptVersion        = "1.4"
    29  )
    30  
    31  // createVMExtension creates a CustomScript VM extension for the given VM
    32  // which will execute the CustomData on the machine as a script.
    33  func createVMExtension(
    34  	vmExtensionClient compute.VirtualMachineExtensionsClient,
    35  	os jujuos.OSType, resourceGroup, vmName, location string, vmTags map[string]string,
    36  ) 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 errors.NotSupportedf("CustomScript extension for OS %q", os)
    55  	}
    56  
    57  	extensionSettings := map[string]*string{
    58  		"commandToExecute": to.StringPtr(commandToExecute),
    59  	}
    60  	extension := compute.VirtualMachineExtension{
    61  		Location: to.StringPtr(location),
    62  		Tags:     toTagsPtr(vmTags),
    63  		Properties: &compute.VirtualMachineExtensionProperties{
    64  			Publisher:               to.StringPtr(extensionPublisher),
    65  			Type:                    to.StringPtr(extensionType),
    66  			TypeHandlerVersion:      to.StringPtr(extensionVersion),
    67  			AutoUpgradeMinorVersion: to.BoolPtr(true),
    68  			Settings:                &extensionSettings,
    69  		},
    70  	}
    71  	_, err := vmExtensionClient.CreateOrUpdate(
    72  		resourceGroup, vmName, extensionName, extension,
    73  	)
    74  	return err
    75  }