Last active
June 19, 2018 14:28
-
-
Save austinpapp/da8a46b758f156a6e393fe071afb9794 to your computer and use it in GitHub Desktop.
Simple saltstack sseapi wrapper for testing purposes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sseapi import Sched, Ret, Job | |
__all__ = ('Sched','Ret','Job') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# salt/orch/jackfish.sls | |
xxxx: | |
salt.function: | |
- name: cmd.run | |
- tgt: jackfish | |
- arg: | |
- date |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
from sseapi import Sched, Job | |
name = "jackfish" | |
j_name = '{} job'.format(name) | |
s_name = '{} sched'.format(name) | |
masters = ['peppermint'] | |
print "adding ",j_name | |
job = { | |
"name" : j_name, | |
"cmd" : "runner", | |
"fun" : "state.orchestrate", | |
"arg" : { "arg" : ["orch.jackfish"] }, | |
"masters" : masters | |
} | |
j = Job() | |
j.add(**job) | |
j_uuid=None | |
for x in j.query_all(): | |
if x['name'] == j_name: | |
j_uuid = x['uuid'] | |
break | |
print "created {} with uuid {}".format(j_name, j_uuid) | |
schedule = { | |
'name': s_name, | |
'job_uuid' : j_uuid, | |
'schedule': { | |
'timezone': 'US/Eastern', | |
'maxrunning': 1, | |
'cron': '* * * * *'}} | |
s = Sched() | |
s.add(**schedule) | |
print "Added ",s_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
from sseapi import Sched, Job | |
name = 'tunafish' | |
j_name = '{} job'.format(name) | |
s_name = '{} sched'.format(name) | |
masters = ['peppermint'] | |
print "adding ",j_name | |
job = { | |
"name" : j_name, | |
"cmd" : "runner", | |
"fun" : "state.orchestrate", | |
"arg" : { "arg" : ["orch.tunafish"] }, | |
"masters" : masters | |
} | |
j = Job() | |
j.add(**job) | |
j_uuid=None | |
for x in j.query_all(): | |
if x['name'] == j_name: | |
j_uuid = x['uuid'] | |
break | |
print "created {} with uuid {}".format(j_name, j_uuid) | |
schedule = { | |
'name': s_name, | |
'job_uuid' : j_uuid, | |
'schedule': { | |
'timezone' : 'US/Eastern', | |
'maxrunning': 1, | |
'cron': '* * * * *'}} | |
s = Sched() | |
s.add(**schedule) | |
print "Added ",s_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# salt/orch/tunafish.sls | |
xxxx: | |
salt.function: | |
- name: cmd.run | |
- tgt: tunafish | |
- arg: | |
- date |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
from sseapiclient.tornado import SyncClient | |
class BaseAPI(object): | |
def __init__(self, | |
user='root', | |
pwd='salt'): | |
self.raas_url = 'https://<raas-head-url>' | |
self.user = user | |
self.pwd = pwd | |
self.api = SyncClient.connect( | |
self.raas_url, | |
self.user, | |
self.pwd, | |
ssl_validate_cert=False).api | |
@staticmethod | |
def fmt_dt(fmt, kwargs): | |
import copy | |
clean = copy.deepcopy(kwargs) | |
if 'end' in kwargs: | |
clean['end'] = kwargs['end'].strftime(fmt) | |
if 'start' in kwargs: | |
clean['start'] = kwargs['start'].strftime(fmt) | |
return clean | |
class Job(BaseAPI): | |
klass = 'job' | |
def __init__(self, | |
user='root', | |
pwd='salt'): | |
super(Job, self).__init__(user, pwd) | |
self.client = getattr(self.api, Job.klass) | |
def help(self): | |
print help(self.client) | |
def query(self, *args, **kwargs): | |
r = self.client.get_jobs(*args, **kwargs) | |
return r.ret | |
def query_all(self, *args, **kwargs): | |
r = self.client.get_jobs(*args, **kwargs) | |
return r.ret | |
def add(self, *args, **kwargs): | |
return self.client.save_job(*args, **kwargs) | |
def delete(self, uuid): | |
return self.client.delete_job(uuid) | |
def delete_all(self): | |
r = self.client.get_jobs().ret | |
for x in r: | |
print "deleting job: {}".format(x['uuid']) | |
self.client.delete_job(job_uuid=x['uuid']) | |
class Ret(BaseAPI): | |
klass = 'ret' | |
dt_fmt = '%Y%m%d%H%M%S000000' | |
def __init__(self, | |
user='root', | |
pwd='salt'): | |
super(Ret, self).__init__(user, pwd) | |
self.client = getattr(self.api, Ret.klass) | |
def help(self): | |
print help(self.client) | |
def gety_jid(self, *args, **kwargs): | |
return self.query_jids(*args, **kwargs) | |
def get_jids(self, *args, **kwargs): | |
clean = {} | |
clean = Ret.fmt_dt(Ret.dt_fmt, kwargs) | |
return self.client.get_jids(*args, **clean).ret | |
def get_returns(self, *args, **kwargs): | |
clean = {} | |
clean = Ret.fmt_dt(Ret.dt_fmt, kwargs) | |
return self.client.get_returns(*args, **clean).ret | |
class Sched(BaseAPI): | |
klass = 'schedule' | |
def __init__(self, | |
user='root', | |
pwd='salt'): | |
super(Sched, self).__init__(user, pwd) | |
self.client = getattr(self.api, Sched.klass) | |
def help(self, ep): | |
print help(getattr(self.client, ep)) | |
def query(self, *args, **kwargs): | |
r = self.client.get(*args, **kwargs) | |
return r.ret['count'], r.ret['results'] | |
def add(self, *args, **kwargs): | |
return self.client.save(*args, **kwargs) | |
def update(self, *arg, **kwargs): | |
return self.client.update(*arg, **kwargs) | |
def remove(self, uuid): | |
return self.client.remove(uuid) | |
def remove_all(self): | |
r = self.client.get().ret | |
rm = 0 | |
for s in r['results']: | |
if s: | |
print "removing ",s['uuid'] | |
self.client.remove(s['uuid']) | |
rm += 1 | |
print "removed {} schedules".format(rm) | |
def history(self, **kwargs): | |
return self.client.search_history(**kwargs) | |
def futures(self, *args, **kwargs): | |
return self.client.futures(*args, **kwargs).ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# salt/jackfish.sls | |
rundate: | |
salt.function: | |
- name: cmd.run | |
- tgt: jackfish | |
- args: | |
- date |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# salt/tunafish.sls | |
rundate: | |
salt.function: | |
- name: cmd.run | |
- tgt: tunafish | |
- args: | |
- date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment