In this blog post we are going to have a look on how we can interact with Exoscale open cloud using Windows PowerShell.

Today Exoscale open cloud can be managed / automated using many different clients or libraries flavors like:

However none of them are Windows based. So, for all our Windows fans & friends, we are providing you a way to manage your instances using your favorite shell: PowerShell

Getting started

Environment setup

First we need to get the PowerShell cloudstack module and associated sample scripts hosted on github

If you already have a git client installed:

git clone https://github.com/exoscale/CloudStack-PowerShell.git

If you don’t have git setup, you may:

  1. download & setup git http://git-scm.com/downloads

  2. or download the PowerShell module and sample scripts manually:

Once everything has been downloaded, we’re going to install the module. Run the following commands in PowerShell:

$PSModulePath = $Env:PSModulePath -split ";" | Select -Index ([int][bool]$Global)
mkdir $PSModulePath\CloudStackClient
Copy-Item .\CloudStackClient.psm1 $PSModulePath\CloudStackClient\CloudStackClient.psm1

Next we need to create the configuration file in which we store the Exoscale open cloud API URL and credentials.

Your API credentials can be obtained in the Exoscale control panel

Paste the following commands in a cmd prompt. Ensure to replace the ‘#Your API Key here#’ and ‘#Your API secret here#’ with yours:

echo [general] >> %USERPROFILE%\cloud-settings.txt
echo Address=https://api.exoscale.ch/compute >> %USERPROFILE%\cloud-settings.txt
echo ApiKey=#####Your API Key here##### >> %USERPROFILE%\cloud-settings.txt
echo SecretKey=#####Your API secret here#####  >> %USERPROFILE%\cloud-settings.txt

Then we need to set the PowerShell execution-policy as we’re going to run scripts. More info on execution-policy. The minimum level must be set to “RemoteSigned” using the below PS command:

Set-ExecutionPolicy RemoteSigned

If you got an error, it is likely that you didn’t start the PowerShell using admin elevated privilege.

We’re now ready to start!

Create an instance

The CloudStackClient module will be used in our sample scripts using the Import-Module CloudStackClient cmdlet.

We’re going to use a sample script for creating our first instance. Before doing so, we need to get some required IDs parameters for API command deployVirtualMachine used by our script. By the way, all the cloudstack API commands can be found here

To deploy a new instance, we need to provide to the API the following mandatory parameters:

  1. The zone ID
  2. The desired security group ID
  3. The desired template ID
  4. The desired Service offering ID

We’re going to use the script CloudStackListIDs.ps1 to list all the required IDs for us:

./CloudStackListIDs.ps1

The script output the templates available, the zone, the security groups and the service offering as below:

list IDs

Please take note of the desired IDs.

We have now all the required parameters to spawn our first Windows 2012 R2 50GB small instance using the script CloudStackDeployVM.ps1 and all the IDs parameters:

./CloudStackDeployVM.ps1 -templateid  a0644aba-de69-4fc3-9723-6b59ff276b6e -zoneid 1128bd56-b4d9-4ac6-a7b9-c715b187ce11 -serviceoffering 21624abb-764e-4def-81d7-9fc54b5957fb -securitygroupids 04162c08-cb0f-4ab8-a4e8-be03ad68e8eb -displayname ExoscaleRocks

N.B.: if you copy the command above, please ensure to adapt at least the security group ID with yours otherwise it won’t work.

A couple of seconds after you run this example, you should see a new instance pop up in the control panel:

new instance pop up

Our instance is now running and IP address and admin password have been returned by our sample deploy script :

new instance result

We can also use the script CloudStackListVirtualMachines.ps1 to list our running instances:

./CloudStackListVirtualMachines.ps1

List VMs

Before being able to remote desktop the server, we must wait a few seconds for the sysprep to complete. We must also add an inbound security group rule for port 3389, if this has not been done previously.

Minimum required PowerShell code

Once the CloudstackClient module has been installed, we’re able to issue commands to the Exoscale open cloud API using the below code:

Import-Module CloudStackClient
$parameters = Import-CloudStackConfig

$cloud = New-CloudStack -apiEndpoint $parameters[0] -apiPublicKey $parameters[1] -apiSecretKey $parameters[2]
$job = Get-CloudStack -cloudStack $cloud -command listZones

for commands requiring mandatory parameters, use the -options parameter :

$job = Get-CloudStack -cloudStack $cloud -command destroyVirtualMachine -options id=$instanceid

For async commands like deployVirtualMachine, you may get the job status using the below sample code:

if($job){
$jobid = $job.deployvirtualmachineresponse.jobid
do {
  Write-Host -NoNewline "."
  $jobStatus = Get-CloudStack -cloudStack $cloud -command queryAsyncJobResult -options jobid=$jobid
  Start-Sleep -Seconds 2
}
while ($jobStatus.queryasyncjobresultresponse.jobstatus -eq 0)
$statusCode = $jobStatus.queryasyncjobresultresponse.jobresultcode
if ($statusCode -ne 0) {
  Write-Error $jobStatus.queryasyncjobresultresponse.errortext
}
else {
  $vm = $jobStatus.queryasyncjobresultresponse.jobresult.virtualmachine
  $vmid = $vm.id
  $ip = $vm.nic.ipaddress
  $password = $vm.password
  Write-Host "`nVM $vmid deployed. VM IP Address is $ip and Administrator password is $password"
}

Conclusion

It’s pretty easy to manage / automate and interact with Exoscale open cloud in a programmatic way from a Windows system. Once the CloudstackClient module is imported and configured, all the power of the CloudStack API can be leveraged easily using a minimum of shell code.