Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Ability to run RightScripts and bugfix #1

Merged
merged 4 commits into from

2 participants

Aleksey Sivokon Jordan Sissel
Aleksey Sivokon

Hello!

First of all, thanks for open-sourcing your package! It's good written and may be extended with ease.

In my company we needed to automate launching of RightScripts. I've added Server.run_script() method that returns Status class. This Status object may be used for tracking script execution status.

Also, XMLModel.refresh() didn't work, so I've fixed it.

We've been using this modified package for a while, everything seems to be fine.

I'd love to commit back those changes.

Jordan Sissel jordansissel merged commit d25eecb into from
Jordan Sissel

Sweet :)

I'll push a new package to pypi sometime today. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 1, 2011
  1. Server.run_script() and Status support.

    silver authored
  2. Added docstring for Status class.

    silver authored
  3. XMLModel.refresh() now works ok.

    silver authored
  4. Dependency on netifaces module are now only needed if RightScale.whoa…

    silver authored
    …mi() is called.
This page is out of date. Refresh to see the latest.
2  src/rightscale/RightScale.py
View
@@ -16,7 +16,6 @@
# third party
import httplib2
-import netifaces
#httplib2.debuglevel=1
@@ -154,6 +153,7 @@ def whoami(self):
# This isn't likely the most optimal way to find the server, but
# it's not bad either. We could query by aws_id which would only require 1
# query, not 2n where n is the number of addresses on the system.
+ import netifaces
self.ensure_authenticated()
for interface in netifaces.interfaces():
addresses = netifaces.ifaddresses(interface)
9 src/rightscale/Server.py
View
@@ -4,6 +4,7 @@
from Tags import Tags
from ServerSettings import ServerSettings
from ServerTemplate import ServerTemplate
+from Status import Status
# TODO(sissel): Refactor
import xml.etree.ElementTree as ElementTree
@@ -135,6 +136,14 @@ def tags(self, value):
self._tags = value
# def tags
+ def run_script(self, script_id):
+ """ Runs script on this Server. """
+ href = self.href + "/run_script"
+ params = {'right_script': script_id}
+ response, content = self.rsapi.request(href, params, method="POST")
+ return Status(response.get('location'), self.rsapi)
+
+
def save(self):
""" Save any modifications made to this Server.
53 src/rightscale/Status.py
View
@@ -0,0 +1,53 @@
+from XMLModel import XMLModel
+from rightscale.util import ElementTreeValueOK
+
+class Status(XMLModel):
+ """ Provides a view of the status of any running jobs that you may have in
+ your account. The most common use of this resource is to check the
+ status of the execution of a RightScript.
+
+ See http://support.rightscale.com/15-References/RightScale_API_Reference_Guide/02-Management/03-Statuses
+ for details.
+ """
+ _state = None
+ _description = None
+ _href = None
+
+ @property
+ def state(self):
+ """ State of script execution: "queued", "in progress", "aborted",
+ "completed", "failed"
+ """
+ return self._state
+
+ @state.setter
+ @ElementTreeValueOK
+ def state(self, value):
+ self._state = value
+
+ @property
+ def description(self):
+ return self._description
+
+ @description.setter
+ @ElementTreeValueOK
+ def description(self, value):
+ self._description = value
+
+ @property
+ def href(self):
+ return self._href
+
+ @href.setter
+ @ElementTreeValueOK
+ def href(self, value):
+ self._href = value
+
+
+ ELEMENTS = {
+ 'state': state,
+ 'description': description,
+ 'href': href
+ }
+
+# class Status
10 src/rightscale/XMLModel.py
View
@@ -29,7 +29,7 @@ class XMLModel(object):
cache = dict()
- def __init__(self, data=None, rsapi=None):
+ def __init__(self, data=None, rsapi=None, use_cache=True):
self.rsapi = rsapi
self.tainted = dict()
@@ -37,11 +37,12 @@ def __init__(self, data=None, rsapi=None):
self._init_data = {
"data": data,
"rsapi": rsapi,
+ "use_cache": False,
}
if isinstance(data, str):
if data.startswith("https://"):
cachekey = data
- if cachekey in self.cache:
+ if use_cache and cachekey in self.cache:
response, content = self.cache[cachekey]
else:
response, content = self.rsapi.request(data)
@@ -49,7 +50,8 @@ def __init__(self, data=None, rsapi=None):
# Cache results. It'd be cool to cache objects, but
# we can't 'return' from __init__ so we'd need to move to the
# factory pattern to get this done :(
- self.cache[cachekey] = (response, content)
+ if use_cache:
+ self.cache[cachekey] = (response, content)
try:
self.from_xml_string(content)
except ExpatError, e:
@@ -77,7 +79,7 @@ def __iter__(self):
then it will be fetched again and this object will be updated with the
result. """
def refresh(self):
- super(Servers, self).__init__(**self._init_data)
+ self.__init__(**self._init_data)
# def refresh
def from_xml_string(self, string):
Something went wrong with that request. Please try again.