spotinst / spotinst-sdk-python

Spotinst SDK for the Python programming language.
Apache License 2.0
17 stars 17 forks source link
core-infra netapp-public owned-by-anurag-sharma

Build Status Coverage Status Python 2.7 Python 3.6

Spotinst SDK Python

The official Spotinst SDK for the Python programming language.

Version 2

The new Spotinst Python SDK comes with a few breaking changes but do not fear, here we will explain all you need to know to make sure you can go right back in buisness in no time.

Here is a basic example of how to create an Ocean cluster using the Ocean Client and the Ocean Models

from spotinst_sdk2 import SpotinstSession
from spotinst_sdk2.models.ocean.aws import *

session = SpotinstSession()
client = session.client("ocean_aws")

################ Compute ################
launch_specification = LaunchSpecifications(security_group_ids=["sg-12345"],
 image_id="ami-12345", key_pair="Noam-key")

instance_types = InstanceTypes(whitelist=["c4.8xlarge"])

compute = Compute(instance_types=instance_types, 
  subnet_ids=["subnet-1234"], launch_specification=launch_specification)

################ Strategy ################

strategy = Strategy(utilize_reserved_instances=False, fallback_to_od=True, spot_percentage=100)

################ Capacity ################

capacity = Capacity(minimum=0, maximum=0, target=0)

################# Ocean #################

ocean = Ocean(name="Ocean SDK Test", controller_cluster_id="ocean.k8s", 
  region="us-west-2", capacity=capacity, strategy=strategy, compute=compute)

client.create_ocean_cluster(ocean=ocean)

In the SDK Client documentation you can view what methods are supported by each client.
For information on what models are supported checkout the SDK Model documentation.
More examples can be found in the SDK Examples Documentation.

Table of Contents

Installation

pip install --upgrade spotinst-sdk2

Authentication

The mechanism in which the SDK looks for credentials is to search through a list of possible locations and stop as soon as it finds credentials. The order in which the SDK searches for credentials is:

  1. Passing credentials as parameters when creating a SpotinstSession object:
session = SpotinstSession(auth_token='foo', account_id='bar')
  1. Environment variables:
export SPOTINST_TOKEN=foo
export SPOTINST_ACCOUNT=bar
  1. Shared credentials file:
[default]
token   = foo
account = bar
[default]
token   = foo
account = bar

[dev]
token   = foo2
account = bar2

[prod]
token   = foo3
account = bar3
default:
  token: foo
  account: bar

dev:
  token: foo2
  account: bar2

prod:
  token: foo3
  account: bar3
export SPOTINST_SHARED_CREDENTIALS_FILE=/path/to/credentials_file
export SPOTINST_PROFILE=dev
session = SpotinstSession(credentials_file='/path/to/credentials_file', profile='dev')

Setup Clients

After a session is created you can use the session object to create clients. Clients are used to make request to the different services that Spotinsts has to offer. To create a client simply use the method client from the session object and pass in the string of the client you wish to create. Here is an example:

session = SpotinstSession()

eg_client = session.client("elastigroup_aws")
ocean_client = session.client("ocean")

Take note you can create more than one client with the session. The currently supported clients are

Client Options

A full list of endpoints and clients can be found in the documentation here.

Documentation

For a comprehensive documentation, check out the API documentation.

Getting Help

We use GitHub issues for tracking bugs and feature requests. Please use these community resources for getting help:

Community

License

Code is licensed under the Apache License 2.0. See NOTICE.md for complete details, including software and third-party licenses and permissions.