Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #139 from scaleoutsystems/feature/STACKN-120
Browse files Browse the repository at this point in the history
Feature/stackn 120
  • Loading branch information
stefanhellander authored Aug 25, 2020
2 parents c78926e + 4eb1e00 commit e17fdfc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 30 deletions.
17 changes: 11 additions & 6 deletions components/studio/projects/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import requests as r
import yaml

from .tasks import create_keycloak_client_task

import re

import modules.keycloak_lib as keylib
Expand All @@ -29,15 +31,18 @@ def create_settings_file(project, username, token):

return yaml.dump(proj_settings)




def create_project_resources(project, username, repository=None):
create_helm_resources(project, username, repository)


# Create Keycloak client for project with default project role.
HOST = settings.DOMAIN
RELEASE_NAME = str(project.slug)
URL = 'https://{}/{}/{}'.format(HOST, username.username, RELEASE_NAME)
keylib.keycloak_setup_base_client(URL, RELEASE_NAME, username.username)
# The creator of the project assumes all roles by default.
create_keycloak_client_task.delay(project.slug, username.username, [])

create_helm_resources(project, username, repository)



def create_helm_resources(project, user, repository=None):
from rest_framework.authtoken.models import Token
Expand Down
4 changes: 1 addition & 3 deletions components/studio/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def generate_passkey(self, length=20):

def create_project(self, name, owner, description, repository):
letters = string.ascii_lowercase
slug = name.replace(" ","-").replace("_","-")
from .helpers import urlify
slug = urlify(slug)
slug = slugify(name)
slug_extension = ''.join(random.choice(letters) for i in range(3))

slug = '{}-{}'.format(slugify(slug), slug_extension)
Expand Down
22 changes: 22 additions & 0 deletions components/studio/projects/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from celery import shared_task
# from .helpers import create_environment_image, create_helm_resources
import modules.keycloak_lib as keylib
from django.conf import settings

@shared_task
def create_keycloak_client_task(project_slug, username, repository):
# create_environment_image(project, repository)
# create_helm_resources(project, username, repository)
# Create Keycloak client for project with default project role.
# The creator of the project assumes all roles by default.
print('Creating Keycloak resources.')
HOST = settings.DOMAIN
print('host: '+HOST)
RELEASE_NAME = str(project_slug)
print('release: '+RELEASE_NAME)
URL = 'https://{}/{}/{}'.format(HOST, username, RELEASE_NAME)
print(URL)

keylib.keycloak_setup_base_client(URL, RELEASE_NAME, username, settings.PROJECT_ROLES, settings.PROJECT_ROLES)

print('Done Keycloak.')
6 changes: 6 additions & 0 deletions components/studio/projects/templates/project.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

{% extends 'baseproject.html' %}

{% load staticfiles %}

{% block content %}
{% if is_authorized %}
<div class="col-md-9">
<div class="d-flex flex-column pt-3 pb-3 mb-5 border-bottom">
<h3>{{ project.name }}</h3>
Expand Down Expand Up @@ -115,4 +117,8 @@ <h4>
</div>
</div>
</div>
{% endif %}
{% endblock %}



8 changes: 0 additions & 8 deletions components/studio/projects/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,6 @@ <h5>Grant access</h5>
</div>
{% endif %}

<div class="container-fluid" style="padding:15px;">
<h5>Project Settings</h5>
<h6 style="padding-bottom: 15px;">
You need a project settings file to use the CLI.
</h6>
<a href="{% url 'projects:download_settings' request.user project.slug %}" class="btn btn-sm btn-primary">Download project.yaml</a>
</div>

<div class="container-fluid" style="padding:15px;">
<h5>Publish project on GitHub</h5>

Expand Down
45 changes: 32 additions & 13 deletions components/studio/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
from django.conf import settings as sett
import logging
import markdown
from .forms import TransferProjectOwnershipForm, PublishProjectToGitHub #, GrantAccessForm
import time
from .forms import TransferProjectOwnershipForm, PublishProjectToGitHub, GrantAccessForm
from django.db.models import Q
from models.models import Model
import requests as r
import base64
from projects.helpers import get_minio_keys
import modules.keycloak_lib as kc
from multiprocessing import Process
from .tasks import create_keycloak_client_task

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -105,17 +109,25 @@ def grant_access_to_project(request, user, project_slug):
project = Project.objects.filter(slug=project_slug).first()

if request.method == 'POST':
print('temp test')
form = GrantAccessForm(request.POST)
if form.is_valid():
selected_users = form.cleaned_data.get('selected_users')
project.authorized.set(selected_users)
project.save()
# form = GrantAccessForm(request.POST)
print(request.POST)
# if form.is_valid():
selected_users = request.POST['selected_users'] #form.cleaned_data.get('selected_users')
project.authorized.set(selected_users)
project.save()

if len(selected_users) == 1:
selected_users = list(selected_users)

for selected_user in selected_users:
user_tmp = User.objects.get(pk=selected_user)
username_tmp = user_tmp.username
logger.info('Trying to add user {} to project.'.format(username_tmp))
kc.keycloak_add_role_to_user(project.slug, username_tmp, 'member')

return HttpResponseRedirect(
reverse('projects:settings', kwargs={'user': user, 'project_slug': project.slug}))


@login_required(login_url='/accounts/login')
def create(request):
template = 'index_projects.html'
Expand All @@ -125,18 +137,22 @@ def create(request):
access = request.POST.get('access', 'org')
description = request.POST.get('description', '')
repository = request.POST.get('repository', '')
project = Project.objects.create_project(name=name, owner=request.user, description=description,
project = Project.objects.create_project(name=name,
owner=request.user,
description=description,
repository=repository)

success = True
try:
create_project_resources(project, request.user, repository=repository)
request.session['oidc_id_token_expiration'] = time.time()-100
request.session.save()
except ProjectCreationException as e:
print("ERROR: could not create project resources")
success = False

if success:
project.save()
if not success:
project.delete()

next_page = request.POST.get('next', '/{}/{}'.format(request.user, project.slug))

Expand All @@ -147,15 +163,18 @@ def create(request):

@login_required(login_url='/accounts/login')
def details(request, user, project_slug):

is_authorized = kc.keycloak_verify_user_role(request, project_slug, ['member'])

template = 'project.html'

url_domain = sett.DOMAIN

project = None
message = None

username = request.user.username
try:
owner = User.objects.filter(username=user).first()
owner = User.objects.filter(username=username).first()
project = Project.objects.filter(Q(owner=owner) | Q(authorized=owner), Q(slug=project_slug)).first()
except Exception as e:
message = 'No project found'
Expand Down

0 comments on commit e17fdfc

Please sign in to comment.