Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def claim_workspaces(self, request, queryset):
cli = docker_cli()
for container in cli.containers(filters={'ancestor': 'kdelfour/cloud9-docker'}):
if container['Names'][0].startswith('/falkor__user_'):
if Project.objects.filter(container_id=container['Id']).count() == 0:
workspace = Project()
workspace.user = User.objects.get(username='sueastside')
workspace.name = container['Names'][0].split('__workspace_', 1)[1]
while Project.objects.filter(name__iexact=workspace.name, user=workspace.user).count():
workspace.name = workspace.name + '_'
workspace.container_id = container['Id']
workspace.save()
claim_workspaces.short_description = "Claim workspaces"
def fix_networks(self, request, queryset):
cli = docker_cli()
for workspace in queryset.all():
from guardian.shortcuts import assign_perm
assign_perm('can_open_ide', workspace.user, workspace)
assign_perm('can_start_stop', workspace.user, workspace)
assign_perm('can_edit_shares', workspace.user, workspace)
assign_perm('delete_project', workspace.user, workspace)
assign_perm('change_project', workspace.user, workspace)
user_network = get_or_create_user_network(cli, workspace.user)
print user_network
container = cli.inspect_container(workspace.container_id)
if container['State']['Status'] == 'running':
for name, network in container["NetworkSettings"]["Networks"].items():
if user_network['Name'] != name and name != 'bridge':
cli.disconnect_container_from_network(container['Id'], name)
if user_network['Name'] not in container["NetworkSettings"]["Networks"]:
def docker_events(message):
workspace = get_object_or_404(Project, pk=message['workspace__pk'])
cli = docker_cli()
get_workspace_state(cli, workspace)
Group("user-%s" % workspace.user.pk).send(create_event('workspaces.update', model_to_dict(workspace)))
@channel_session_user_from_http
def ws_add(message):
Group("user-%s" % message.user.pk).add(message.reply_channel)
cli = docker_cli()
workspaces = get_workspaces_for_user(cli, message.user)
Group("user-%s" % message.user.pk).send(create_event('workspaces', {"workspaces": [model_to_dict(x) for x in workspaces],}))
@channel_session_user
def select_workspace(message):
data = json.loads(message['data'])
workspace = get_object_or_404(Project, pk=data['workspace_id'])
if not message.user.has_perm('falkor.can_open_ide', workspace):
return
cli = docker_cli()
get_workspace_full(cli, message.user, workspace)
Group("user-%s" % message.user.pk).send(create_event('workspaces.select', model_to_dict(workspace)))
@receiver(post_save, sender=Project)
def send_update(sender, instance, created, **kwargs):
cli = docker_cli()
get_workspace_state(cli, instance)
#TODO: send for each user
Group("user-%s" % instance.user.pk).send(create_event('workspaces.created' if created else 'workspaces.update', model_to_dict(instance)))
@receiver(post_delete, sender=Project)
def send_delete(sender, instance, **kwargs):
cli = docker_cli()
#TODO: send for each user
Group("user-%s" % instance.user.pk).send(create_event('workspaces.deleted', model_to_dict(instance)))
@channel_session_user
def control_workspace(message):
data = json.loads(message['data'])
workspace = get_object_or_404(Project, pk=data['workspace_id'])
if not message.user.has_perm('falkor.can_start_stop', workspace):
Group("user-%s" % message.user.pk).send(create_event('notifications', {'level': 'info', 'message': 'you cannot'}))
return
command = data['command']
cli = docker_cli()
if workspace.container_id:
if command == 'start':
cli.start(workspace.container_id)
Group("user-%s" % message.user.pk).send(create_event('notifications', {'level': 'info', 'message': 'started container'}))
elif command == 'stop':
cli.stop(workspace.container_id)
Group("user-%s" % message.user.pk).send(create_event('notifications', {'level': 'info', 'message': 'stopped container'}))
else:
Group("user-%s" % message.user.pk).send(create_event('notifications', {'level': 'error', 'message': 'No such command '+command}))
get_workspace_full(cli, message.user, workspace)
Group("user-%s" % message.user.pk).send(create_event('workspaces.select', model_to_dict(workspace)))
return
workspace = Project()
workspace.editor_type = editor_type
workspace.user = message.user
workspace.name = workspace_name
workspace.save()
from guardian.shortcuts import assign_perm
assign_perm('can_open_ide', message.user, workspace)
assign_perm('can_start_stop', message.user, workspace)
assign_perm('can_edit_shares', message.user, workspace)
assign_perm('delete_project', workspace.user, workspace)
assign_perm('change_project', workspace.user, workspace)
cli = docker_cli()
network = get_or_create_user_network(cli, message.user)
name = 'falkor__user_{0}__workspace_{1}'.format(slugify(message.user.username), workspace.slug)
host_config = cli.create_host_config(network_mode=network['Id'])
container = cli.create_container(image=workspace.editor_type.image, detach=True, stdin_open=True, tty=True, name=name, host_config=host_config)
for proxy in cli.containers(filters={'status':'running', 'label': 'com.docker.compose.service=proxy'}):
if network['Name'] not in proxy["NetworkSettings"]["Networks"]:
cli.connect_container_to_network(proxy['Id'], network['Name'])
workspace.container_id = container['Id']
workspace.save()
cli.start(container=container.get('Id'))
def delete_and_remove(self, request, queryset):
cli = docker_cli()
for workspace in queryset.all():
try:
cli.remove_container(workspace.container_id, force=True)
except:
pass
workspace.delete()