1. John Kane
  2. bespin-erlang

Commits

Kevin Dangoor  committed 9ab9fa9

adding server side bits for deployment

  • Participants
  • Parent commits c9583ff
  • Branches default

Comments (0)

Files changed (3)

File backend/python/bespin/controllers.py

View file
 
 from bespin.config import c
 from bespin.framework import expose, BadRequest
-from bespin import vcs
+from bespin import vcs, deploy
 from bespin.database import User, get_project
 from bespin.filesystem import NotAuthorized, OverQuota, File
 from bespin.utils import send_email_template
     response.body = simplejson.dumps(result)
     return response()
     
+@expose('^/project/deploy/(?P<project_name>[^/]+)/setup', 'PUT')
+def deploy_setup(request, response):
+    user = request.user
+    project_name = request.kwargs['project_name']
+    project = get_project(user, user, project_name)
+    
+    data = request.POST
+    deploy_options = dict(remote_host = data.get("remoteHost"),
+        remote_directory = data.get("remoteDirectory"),
+        type = data.get("connType"))
+    pdo = deploy.ProjectDeploymentOptions(project, **deploy_options)
+    pdo.save()
+    
+    response.content_type="text/plain"
+    response.body=""
+    return response()
 
 def db_middleware(app):
     def wrapped(environ, start_response):

File backend/python/bespin/deploy.py

View file
 # ***** END LICENSE BLOCK *****
 # 
 
+from simplejson import dumps, loads
+
 from bespin.vcs import KeyChain
 
 # Ideally, we would have a better "remote server" abstraction than this!
         except KeyError:
             pass
         
-        self._save()
+        self._save()
+        
+class ProjectDeploymentOptions(object):
+    """Manages the deployment options for a project."""
+    
+    @classmethod
+    def get(cls, project):
+        """Retrieve the deployment options for this project.
+        Returns None if the options aren't set."""
+        md = project.metadata
+        info_json = md.get("deployment")
+        if info_json is None:
+            return None
+        info_json = loads(info_json)
+        # keyword argument names must be str objects, not unicode
+        kw = dict((key.encode("ascii"), value) 
+            for key, value in info_json.items())
+        return cls(project, **kw)
+    
+    def __init__(self, project, remote_host, remote_directory, type):
+        self.project = project
+        self.remote_host = remote_host
+        self.remote_directory = remote_directory
+        self.type = type
+        
+    def save(self):
+        """Save the options in the project metadata."""
+        md = self.project.metadata
+        info_dict = dict(remote_host = self.remote_host,
+            remote_directory = self.remote_directory,
+            type = self.type)
+        md["deployment"] = dumps(info_dict)
+        
+    

File backend/python/bespin/tests/test_deploy.py

View file
 
 from bespin.tests import BespinTestApp
 
+app = None
+
 def setup_module(module):
     global app
     config.set_profile('test')
     kc = deploy.DeploymentKeyChain(macgyver, "foobar")
     credentials = kc.get_credentials_for_project(bigmac)
     assert credentials is None
+
+def test_set_project_deployment_metadata():
+    _init_data()
+    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
+    pdo = deploy.ProjectDeploymentOptions(bigmac,
+        remote_host="macgyver.com",
+        remote_directory="/home/macgyver/knownunknowns",
+        type="sftp")
+    pdo.save()
+    
+    md = bigmac.metadata
+    options_json = md['deployment']
+    assert "remote_host" in options_json
+    assert "sftp" in options_json
+    
+    pdo = deploy.ProjectDeploymentOptions.get(bigmac)
+    assert pdo.remote_host == "macgyver.com"
+    assert pdo.remote_directory == "/home/macgyver/knownunknowns"
+    
+# Web tests
+
+def test_deployment_setup():
+    _init_data()
+    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
+    resp = app.put("/project/deploy/bigmac/setup", dict(
+        remoteHost="macgyver.com",
+        remoteDirectory="/home/macgyver/knownunknowns",
+        connType="sftp",
+        kcpass="sekretkeychain",
+        authType="ssh",
+        username="macman"))
+    
+    bigmac = get_project(macgyver, macgyver, "bigmac")
+    pdo = deploy.ProjectDeploymentOptions.get(bigmac)
+    assert pdo.remote_host == "macgyver.com"
+    assert pdo.remote_directory == "/home/macgyver/knownunknowns"