Skip to content

Instantly share code, notes, and snippets.

@rosscdh
Last active November 10, 2016 15:48
Show Gist options
  • Select an option

  • Save rosscdh/3efba86ce4e098b83704202708d5f589 to your computer and use it in GitHub Desktop.

Select an option

Save rosscdh/3efba86ce4e098b83704202708d5f589 to your computer and use it in GitHub Desktop.
Salt Wrapper
#import requests
from universalclient import Client
class SaltyBiscuit(object):
url = 'http://192.168.99.100:8000/'
salt = None
token = None
def __init__(self, *args, **kwargs):
self.url = kwargs.get('url', self.url)
self.salt = Client(self.url)
def _headers(self):
if not self.token:
raise Exception('No token, you need to login')
return {
'X-Auth-Token': self.token
}
def _send(self, *args, **kwargs):
resp = self.salt.post(headers=self._headers(),
data=kwargs)
if resp.ok is True:
return resp
else:
raise Exception(resp.content)
def login(self, username, password, eauth='pam'):
data = {
'username': username,
'password': password,
'eauth': eauth
}
resp = self.salt.login.post(data=data)
if resp.ok:
self.token = resp.json().get('return', [])[0].get('token')
else:
raise Exception(resp.content)
def command(self, tgt, fun, arg=None, client='local'):
return self._send(client=client,
tgt=tgt,
fun=fun,
arg=arg)
service = SaltyBiscuit()
service.login(username='saltdev', password='saltpass')
print service.command(tgt='*',
fun='cmd.run',
arg='echo "hi salt-api!"')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment