github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/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"
     5  	"github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest/to"
     6  	"github.com/Azure/azure-sdk-for-go/arm/compute"
     7  	"github.com/juju/errors"
     8  	jujuos "github.com/juju/utils/os"
     9  )
    10  
    11  const extensionName = "JujuCustomScriptExtension"
    12  
    13  const (
    14  	windowsExecuteCustomScriptCommand = `` +
    15  		`move C:\AzureData\CustomData.bin C:\AzureData\CustomData.ps1 && ` +
    16  		`powershell.exe -ExecutionPolicy Unrestricted -File C:\AzureData\CustomData.ps1 && ` +
    17  		`del /q C:\AzureData\CustomData.ps1`
    18  	windowsCustomScriptPublisher = "Microsoft.Compute"
    19  	windowsCustomScriptType      = "CustomScriptExtension"
    20  	windowsCustomScriptVersion   = "1.4"
    21  )
    22  
    23  const (
    24  	// The string will be split and executed by Python's
    25  	// subprocess.call, not interpreted as a shell command.
    26  	linuxExecuteCustomScriptCommand = `bash -c 'base64 -d /var/lib/waagent/CustomData | bash'`
    27  	linuxCustomScriptPublisher      = "Microsoft.OSTCExtensions"
    28  	linuxCustomScriptType           = "CustomScriptForLinux"
    29  	linuxCustomScriptVersion        = "1.4"
    30  )
    31  
    32  // createVMExtension creates a CustomScript VM extension for the given VM
    33  // which will execute the CustomData on the machine as a script.
    34  func createVMExtension(
    35  	callAPI callAPIFunc,
    36  	vmExtensionClient compute.VirtualMachineExtensionsClient,
    37  	os jujuos.OSType, resourceGroup, vmName, location string, vmTags map[string]string,
    38  ) error {
    39  	var commandToExecute, extensionPublisher, extensionType, extensionVersion string
    40  
    41  	switch os {
    42  	case jujuos.Windows:
    43  		commandToExecute = windowsExecuteCustomScriptCommand
    44  		extensionPublisher = windowsCustomScriptPublisher
    45  		extensionType = windowsCustomScriptType
    46  		extensionVersion = windowsCustomScriptVersion
    47  	case jujuos.CentOS:
    48  		commandToExecute = linuxExecuteCustomScriptCommand
    49  		extensionPublisher = linuxCustomScriptPublisher
    50  		extensionType = linuxCustomScriptType
    51  		extensionVersion = linuxCustomScriptVersion
    52  	default:
    53  		// Ubuntu renders CustomData as cloud-config, and interprets
    54  		// it with cloud-init. Windows and CentOS do not use cloud-init
    55  		// on Azure.
    56  		return errors.NotSupportedf("CustomScript extension for OS %q", os)
    57  	}
    58  
    59  	extensionSettings := map[string]interface{}{
    60  		"commandToExecute": commandToExecute,
    61  	}
    62  	extension := compute.VirtualMachineExtension{
    63  		Location: to.StringPtr(location),
    64  		Tags:     toTagsPtr(vmTags),
    65  		Properties: &compute.VirtualMachineExtensionProperties{
    66  			Publisher:               to.StringPtr(extensionPublisher),
    67  			Type:                    to.StringPtr(extensionType),
    68  			TypeHandlerVersion:      to.StringPtr(extensionVersion),
    69  			AutoUpgradeMinorVersion: to.BoolPtr(true),
    70  			Settings:                &extensionSettings,
    71  		},
    72  	}
    73  	err := callAPI(func() (autorest.Response, error) {
    74  		result, err := vmExtensionClient.CreateOrUpdate(
    75  			resourceGroup, vmName, extensionName, extension,
    76  		)
    77  		return result.Response, err
    78  	})
    79  	return err
    80  }