Logo

MacStadium Blog

Easily Provision Your Orka VMs with the Orka Python SDK

The Orka Python SDK offers a simple and powerful means of interacting programmatically with all aspects of Orka, including the Orka API, the Kubernetes API, as well as the macOS VMs you create. Check out this post to learn more and see some example code to get you started.

When working with virtualized compute resources in the cloud, you’ll often need to create and recreate a given image from which to spin up your compute resource. While this can be done in an ad hoc manner, best practice dictates that a file that defines the targeted state of the image that can be checked into version control, and thus tracked easily over time, is the way to go.

There are all manner of ways to achieve this, but for those Orka users who have a hankering to work in Python, you’re in for a treat with the new Python SDK for Orka. Today, we’ll walk through basic installation and setup, and we’ll also dig into the syntax of the SDK a bit for good measure.

Installing the Orka SDK

Installation of the SDK is simple! Because it is available on PyPI, you can install by running the following:

pip install orka_sdk

Setup and Authentication

There are a few bells and whistles included in the SDK that you can either take or leave, depending on your needs and preferences – one of which is the ability to programmatically interact with the Kubernetes API associated with your Orka cluster, if you like. In order to do so, you’ll need to download a kubeconfig file from Orka, and update your path to point to it.

You can achieve this by running:

orka kube get

Then, enter your Kubernetes account name, and press enter. You’ll find that your kubeconfig file has been downloaded to your /home directory, and in the terminal dialogue you’ll have instructions for adding it to your system path.

With that out of the way, we can go ahead and import the SDK, instantiate it, and log in, like so:

from orka_sdk.orka_sdk import OrkaSDK

orka = OrkaSDK()
orka.login('user@email.com', 'example-password', 'example-license-key')

We can then confirm that we logged in successfully by running orka.token which should return your newly minted auth token if all has gone well.

SDK Overview

After authenticating with the Orka API, you have access to a variety of resources. You can interact with the Orka API, of course, but you can also interact with the newly minted VM directly via SSH and SFTP connections that are created dynamically. You also optionally have access to the Kubernetes API associated with your Orka cluster.

Example Provisioning Script

from orka_sdk.orka_sdk import OrkaSDK

orka = OrkaSDK()
orka.login('user@email.com', 'example-password', 'example-license-key')
vm_data = {
  'vm_name': 'my-new-vm',
  'orka_base_image': '90GBigSurSSH.img',
  'core_count': 6,
  'vcpu_count': 6
  }
  
r = orka.create_vm(vm_data)
vm = r.data

# We can then upload our provisioning script to the new VM
r = vm.upload('/local/path.sh', '/remote/path.sh')

# And we can call our script remotely, like so:
r = vm.exec('/remote/path.sh')

# We can check the remote output, like so:
if r.success:
  print(r.data['stdout'])

# Suppose we need an environment variable available 
# any time we spin up a new VM from this image, easy peasy

data = {'FOO': 'bar'}
r = vm.write_persistent_env_var(data)

# Maybe we need a process to run at machine startup any time this image is spun up
data = {'name': 'my-launch-daemon', 'path_to_executable': '/um/the/path.sh'}
r = vm.create_launch_daemon(data)

# Or maybe we need our default `admin` user to be logged in at startup
r = vm.enable_auto_login()

# Finally, we can save the state of our provisioned VM as a golden image, like so:
r = orka.save_vm_as_image('new-image-name.img', vm)

# And we can confirm success, like so:
r = orka.images.get('new-image-name.img')
if r.success:
  print('All\'s well!')

TL;DR

Provisioning new virtual machines and saving their state as a golden image in a trackable and repeatable way is simple with the Orka SDK. Above, we have covered installation and authentication with the Orka API, as well as provided a sample provisioning script to get you started.

Posted

May 04, 2022

Written by

Jeff Vincent

Share this article

Logo

Orka, Orka Workspace and Orka Pulse are trademarks of MacStadium, Inc. Apple, Mac, Mac mini, Mac Pro, Mac Studio, and macOS are trademarks of Apple Inc. The names and logos of third-party products and companies shown on the website are the property of their respective owners and may also be trademarked.

©2023 MacStadium, Inc. is a U.S. corporation headquartered at 3525 Piedmont Road, NE, Building 7, Suite 700, Atlanta, GA 30305. MacStadium, Ltd. is registered in Ireland, company no. 562354.