Source

cannula / cannula / api / base / projects.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

from logging import getLogger

from django import http
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.utils.decorators import method_decorator
from django import forms

from cannula.api import DuplicateObject
from cannula.api import UnitDoesNotExist
from cannula.api import PermissionError
from cannula.api import ApiError
from cannula.api import BaseAPI
from cannula.conf import api, PACKAGE_CHOICES, VCS_CHOICES
from cannula import tasks
from cannula.utils import format_exception, validate_python_package_name, add_blank_choice

log = getLogger('api')

class CreateProject(forms.Form):
    group = forms.ChoiceField()
    name = forms.CharField(max_length=100,
        help_text="A free-form name for your project, e.g. 'Staff Time Sheets'.")
    abbr = forms.CharField(max_length=50,
        help_text="Python package name for your project, e.g. 'timesheets'.")
    repository = forms.CharField(max_length=512,
        help_text="URL of the repository for this project. This is the URL"
        " that you would 'clone' or 'checkout'")
    vcs = forms.ChoiceField(choices=VCS_CHOICES,
        help_text="Repository type.")
    user = forms.CharField(max_length=100, required=False,
        help_text="(Optional) Repository username.")
    password = forms.CharField(max_length=100, required=False,
        help_text="(Optional) Repository password.")
    package = forms.ChoiceField(choices=PACKAGE_CHOICES, required=False,
        help_text="(Optional) Select the type of project you wish to create.")
    desc = forms.CharField(max_length=1024, required=False,
        widget=forms.Textarea(attrs={'rows': 3}),
        help_text="(Optional) Description of this project.")

    def __init__(self, *args, **kwargs):
        """
        Dynamically populate the env and stage choices.
        """
        self.username = kwargs.pop('username')
        super(CreateProject, self).__init__(*args, **kwargs)

        groups = api.groups.list(self.username)
        group_choices = [(g.abbr, '%s (%s)' % (g.name, g.abbr)) for g in groups]
        group_choices = add_blank_choice(group_choices)
        self.fields['group'].choices = group_choices

    def clean_name(self):
        name = self.cleaned_data['name']
        if name:
            name = name.strip()
        return name

    def clean_abbr(self):
        abbr = self.cleaned_data['abbr']
        validate_python_package_name(abbr)
        try:
            api.projects.get(self.cleaned_data['group'], abbr)
            raise forms.ValidationError(u'A project with this name already exists in this group.')
        except UnitDoesNotExist:
            pass
        return abbr

class DeleteProject(forms.Form):
    """
    Form displayed when deleting project to make sure the user confirms project
    deletion.  This form will delete actually delete the project when is_valid
    is called if all the fields are error-free.
    """
    required_text = ("You must confirm this statement in order to delete this"
                     " project.")

    confirm_delete = forms.BooleanField(label='Delete this project',
        help_text="By clicking this box, you confirm that this project will be"
                  " deleted from the interface and that this project's"
                  " associated repository will also be deleted.",
        error_messages={'required': required_text})
    confirm = forms.BooleanField(label='Delete data',
        help_text="By clicking this box you confirm that this deletion is an"
                  " irreversible action, and that your code and data will be"
                  " permanently deleted.  You confirm that you have made any"
                  " necessary backups of your data from the project's"
                  " repository.",
        error_messages={'required': required_text})


class DeployProject(forms.Form):
    cluster = forms.ChoiceField(widget=forms.RadioSelect,
        help_text="Select a stage to deploy this application to.")
    repo_path = forms.CharField(label="Repo Path", initial='',
        help_text="Enter the path within your repository you would like to"
                  " deploy (including your project directory)."
                  " SVN Examples: /trunk/myproj, /tags/1.0/myproj,"
                  " GIT/HG Examples: master, mybranch, tip, default")
    revision = forms.CharField(max_length=25, required=False,
        help_text="Enter the revision number to deploy, or leave blank to"
                  " deploy the latest revision.")
    package = forms.ChoiceField()

    def __init__(self, *args, **kwargs):
        """
        Dynamically populate the env and stage choices.
        """
        self.username = kwargs.pop('username')
        super(DeployProject, self).__init__(*args, **kwargs)

        packages = api.packages.list(self.username)
        choices = [(env.pk, env) for env in packages]
        choices = add_blank_choice(choices)
        self.fields['package'].choices = choices

        clusters = api.clusters.list(self.username)
        choices = [(stage.abbr, stage) for stage in clusters]
        self.fields['cluster'].choices = choices
        

class ProjectAPIBase(BaseAPI):
    
    details_template = ['cannula/project_details.html']
    deploy_template = ['cannula/form.html']
    delete_template = ['cannula/form.html']
    create_template = ['cannula/form.html']
    list_template = ['cannula/project.html']
    
    create_form = CreateProject
    delete_form = DeleteProject
    deploy_form = DeployProject
    
    def _get(self, group, projectname):
        "Subclasses must define this"
        raise NotImplementedError()
    
    
    def get(self, groupname, projectname):
        group = api.groups.get(groupname)
        try:
            return self._get(group, projectname)
        except:
            raise UnitDoesNotExist("Project does not exist")
    
    
    def _list(self, user=None, group=None):
        "Subclasses must define this"
        raise NotImplementedError()
    
    
    def list(self, user=None, group=None):
        """
        Return a list of Project units for the ProjectGroup with the given
        groupname.

        Required Arguments:

         * user: (String) EID of the the user doing the query.
         * group: (String) Name of project group.
        """
        if user:
            user = api.users.get(user)
            # TODO: return list of projects user has access to?
        if group:
            group = api.groups.get(group)
            
        return self._list(user, group)
    
    
    def _create(self, group, abbr, name, pkg):
        "Subclasses must define this"
        raise NotImplementedError()
    
    
    def create(self, user, group, abbr, pkg, name=None, 
               desc='', perm='create', **kwargs):
        """
        Create a project and return its project object.

        This requires permission to create project in the group and  a unique
        string identifier for the project.  If the project is not unique, a
        DuplicateObject Exception is raised.

        Required Arguments:

         * user: (String) User attempting the create function.
         * group: (String) Name of project group to put project under.
         * abbr: (String) Name of project to create.
         * pkg: The name of the package to use.

        Optional Arguments:

         * name: A long name for the project.
         * desc: Text description of the project.

        Returns:

         On successful create:
           The newly created project object

         On failure:
           Exception
        """
        user = api.users.get(user)
        group = api.groups.get(group)
        if not api.permissions.has_perm(user, perm, group=group):
            raise PermissionError("User: %s does not have perm: %s" % (user, perm))

        # Check group members and remove inactive ones so we don't get errors
        # from UTForge when granting members permission after the project gets
        # created.
        api.groups.check_members(group, user)

        # Ensure the Python package name (abbr) doesn't already exist.
        try:
            self.get(group, abbr)
            raise DuplicateObject("This project already exists in this group.")
        except UnitDoesNotExist:
            pass

        if name is None:
            name = abbr

        # Create the Project object.
        project = self._create(group=group,
            name=name,
            abbr=abbr,
            desc=desc,
        )

        log.info("Project %s created in %s" % (project, group))
        return project
    
    
    def deploy(self, req_user, group, project, cluster, url,
               package, repo_path, revision):
        """
        Deploy this project to a given cluster using the deployment method
        chosen. If this is the first deployment of this project first sync the
        cluster.
        """
        req_user = api.users.get(req_user)
        group = api.groups.get(group)
        project = self.get(group, project)
        cluster = api.clusters.get(cluster)
        package = api.packages.get(package)

        try:
            # check if we are already deployed.
            current = api.deployments.get(group, project, cluster)
            message = "Updated deployment: %s @ %s to %s @ %s" % (
                    current.repo_path, current.revision,
                    repo_path, revision)
            # mark it as in-active so we don't get errors
            current.active = False
            current.save()
        except UnitDoesNotExist:
            # First deployment of this project on this cluster.
            current = None
            message = "Initial deployment: %s @ %s" % (repo_path, revision)

        deployment = api.deployments.create(group, project, cluster, url,
                        package, repo_path, revision, current)

        # Deploy the project
        try:
            tasks.deploy_project.delay(group.abbr, project.abbr, cluster.name)
        except:
            message = "Failed to deploy project, " \
                      "group: %s, project: %s" % (group, project)
            log.error(message)
            log.error(format_exception())
            tasks.delete_deployment(deployment.id)
            deployment.delete()
            raise

        log.info("%s by %s" % (message, req_user))
        api.log.create(message=message, user=req_user, group=group, 
                       project=project, cluster=cluster)
        return deployment
    
    
    def delete(self, group, project, user):
        group = api.groups.get(group)
        project = api.projects.get(group, project)
        user = api.users.get(user)

        deployments = api.deployments.list(group, project)
        for deploy in deployments:
            deploy.delete_deployment()
            deploy.delete()

        try:
            connection = project.repo.server.connect()
            with connection:
                connection.delete_project(project.repo.url, user.username)
        except:
            log.error("Problem deleting project: %s" % project)
            raise

        unix_ids = api.unix_ids.list(group, project)
        for uid in unix_ids:
            log.info("Unsyncing Unix ID: %s" % uid)
            uid.unsync()

        project.delete()
        message = "%s deleted project %s from group %s" % (user, project, group)
        log.info(message)
        api.log.create(message, user=user, group=group)

    
    @method_decorator(login_required)
    def details_view(self, request, group, project):
        """
        List the deployments and their statuses for a single project.
        """
        group = api.groups.get_or_404(group)
        project = self.get_or_404(group, project)
        if not request.user.has_perm('read', group):
            return http.HttpResponseForbidden("<h1>Forbidden</h1>")
    
        page = request.GET.get('page', '1')
        apps = api.deployments.list(group, project, active_only=True)
        is_admin = request.user.is_superuser
        context = {
            'title': "%s Deployments" % project,
            'applications': apps,
            'project': project,
            'group': group,
            'has_deploy_permission': True,
            'is_admin': is_admin,
            'logs': api.log.list(group, project, page=page),
        }
        return self.respond(request, self.details_template, context)
    
    @method_decorator(login_required)
    @transaction.commit_on_success
    def deploy_view(self, request, group, project):
        """
        Display/process form for deploying a project to a cluster.
        """
        group = api.groups.get_or_404(group)
        project = self.get_or_404(group, project)
        if not request.user.has_perm('deploy', project):
            log.debug('403: %s attempted to deploy %s.%s' % (request.user, group.abbr, project.abbr))
            return http.HttpResponseForbidden('<h1>Forbidden</h1>')
    
        errors = False
        add = True
        app_info = {}
        title = "Add Deployment"
    
        if request.method == "POST":
            form = self.deploy_form(request.POST, username=request.user.username)
            if form.is_valid():
                #TODO: hook url up!
                self.deploy(request.user, group, project, 
                            form.cleaned_data['cluster'],
                            url=None, repo_path=form.cleaned_data['repo_path'],
                            package=form.cleaned_data['package'],
                            revision=form.cleaned_data['revision'])
                message = "%s successfully deployed." % project
                log.debug(message)
                request.session['message'] = message
                return http.HttpResponseRedirect('../')
            errors = True
        else:
            cluster = request.GET.get('cluster')
            # If a cluster was specified, then we are updating an existing
            # deployment and want to pre-fill the form with the existing data.
            if cluster:
                # Check that a valid cluster was specified.
                try:
                    cluster = api.clusters.get(cluster)
                except UnitDoesNotExist:
                    return http.HttpResponseBadRequest('Invalid cluster name.')
                if cluster not in api.clusters.list(request.user):
                    return http.HttpResponseBadRequest('Invalid cluster name.')
                add = False
                deployment = api.deployments.get(group, project, cluster)
                app_info = {
                    'cluster_name': cluster.abbr,
                    'repo_path': deployment.repo_path,
                    'revision': deployment.revision,
                    'env': deployment.package.pk,
                    'processes': deployment.num_processes,
                }
                title = "Update %s Deployment" % cluster
            form = self.deploy_form(initial=app_info, username=request.user.username)

        context = {
            'form': form,
            'title': title,
            'errors': errors,
            'project': project,
            'group': group,
            'add': add,
        }
        return self.respond(request, self.deploy_template, context)
    
    @method_decorator(login_required)
    @transaction.commit_on_success
    def delete_view(self, request, group, project):
        """
        Display/process delete confirmation form.
        """
        group = api.groups.get_or_404(group)
        project = api.projects.get_or_404(group, project)
        if not request.user.has_perm('admin', project):
            log.debug('403: %s attempted to delete %s.%s' % (
                request.user, group.abbr, project.abbr)
            )
            return http.HttpResponseForbidden('<h1>Forbidden</h1>')
    
        errors = False
        if request.method == 'POST':
            form = self.delete_form(data=request.POST)
            if form.is_valid():
                # Delete the project
                self.delete(group, project, request.user)
                request.session['message'] = "Project %s deleted." % project
                return http.HttpResponseRedirect(reverse('cannula-index'))
            errors = True
        else:
            form = self.delete_form()
        context = {
            'title': "Delete Project %s" % project,
            'form': form,
            'add': False,
            'errors': errors,
            }
        return self.respond(request, self.delete_template, context)

    @method_decorator(login_required)
    @transaction.commit_manually
    def create_view(self, request):
        errors = False
        if request.method == "POST":
            form = self.create_form(data=request.POST, username=request.user)
            if form.is_valid():
                group = form.cleaned_data['group']
                abbr = form.cleaned_data['abbr']
                package = form.cleaned_data['package']
                name = form.cleaned_data['name'] or None
                desc = form.cleaned_data['desc'] or None
                try:
                    self.create(request.user, group, abbr, package, name, desc)
                    request.session['message'] = "Project successfully created."
                    transaction.commit()
                    return http.HttpResponseRedirect(reverse('cannula-index'))
                except Exception as create_exception:
                    log.error("Error creating project, group: %s project: %s"
                              % (group, abbr))
                    log.error(format_exception())
                    transaction.rollback()
                    # Explicitly specify the original exception instance here in
                    # case the repo deletion also raises an exception.
                    raise create_exception
            errors = True
        else:
            # Allow group to be passed as GET parameter, used for groups creating
            # their first project from the home page.
            initial = {'group': request.GET.get('group', '')}
            form = self.create_form(initial=initial, username=request.user)
        context = {
            'title': "Add Project",
            'form': form,
            'cancel_url': reverse('cannula-index'),
            'errors': errors,
            'add': True,
        }
        return self.respond(request, self.create_template, context)

    @method_decorator(login_required)
    def list_view(self, request):
        """
        Display a list of projects.
        """
        projects = self.list()
        context = {
            'title': "Projects",
            'projects': projects,
            'count': len(projects),
        }
        return self.respond(request, self.list_template, context)