Gogs 7 лет назад
Родитель
Сommit
5752d0df95
8 измененных файлов с 157 добавлено и 19 удалено
  1. 2 1
      .env
  2. 29 0
      api/resources/playbook_resource.py
  3. 3 1
      api/urls.py
  4. 2 0
      api/utils/ansible.py
  5. 29 0
      api/utils/command.py
  6. 90 0
      api/utils/docker_api.py
  7. 0 17
      api/views.py
  8. 2 0
      odoo_control/settings.py

+ 2 - 1
.env

@@ -4,4 +4,5 @@ DEBUG = True
 JWT_ACCEPT_HEADER = 'HTTP_AUTHORIZATION'
 JWT_PREFIX_HEADER = 'JWT'
 JWT_SECRET_KEY = '123456789'
-PLAYBOOKS_PATH = '/home/robert/workspace/playbooks'
+PLAYBOOKS_PATH = '/home/robert/workspace/playbooks/'
+DOCKER_SOCK_DIR = '/var/run/docker.sock'

+ 29 - 0
api/resources/playbook_resource.py

@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+from django.conf.urls import url
+from django.conf import settings
+from tastypie.resources import Resource
+from tastypie.utils import trailing_slash
+from api.utils.command import list_files_and_folders
+from api.utils.docker_api import get_all_containers, get_all_images
+
+'''
+'''
+class PlaybookResource(Resource):
+    class Meta:
+        allowed_methods = ['get']
+
+    '''
+    '''
+    def prepend_urls(self):
+        return [
+            url(r'^(?P<resource_name>%s)/discovery%s$' % (self._meta.resource_name, trailing_slash), self.wrap_view('discovery_playbooks'), name='api_discovery_playbooks'),
+        ]
+
+    '''
+    '''
+    def discovery_playbooks(self, request, **kwargs):
+        get_all_images()
+
+        bundle = self.build_bundle(obj={}, request=request)
+        return self.create_response(request, bundle.obj)

+ 3 - 1
api/urls.py

@@ -9,6 +9,7 @@ from api.resources.group_resource import GroupResource
 from api.resources.jwt_resource import JWTResource
 from api.resources.request_resource import RequestResource
 from api.resources.task_resource import TaskResource
+from api.resources.playbook_resource import PlaybookResource
 
 v1_api = Api(api_name='v1')
 v1_api.register(UserResource())
@@ -16,4 +17,5 @@ v1_api.register(PermissionResource())
 v1_api.register(GroupResource())
 v1_api.register(JWTResource())
 v1_api.register(RequestResource())
-v1_api.register(TaskResource())
+v1_api.register(TaskResource())
+v1_api.register(PlaybookResource())

+ 2 - 0
api/utils/ansible.py

@@ -0,0 +1,2 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals

+ 29 - 0
api/utils/command.py

@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+from subprocess import check_output, STDOUT
+import subprocess
+
+'''
+'''
+def list_files_and_folders(path, hiddens=True):
+    output = check_output(['ls', '-a', path], stderr=STDOUT)
+
+    items = []
+    for item in output.split('\n'):
+        if item == '' or item == '.' or item == '..':
+            continue
+        
+        if not hiddens and item.startswith('.'):
+            continue
+
+        items.append(item)
+
+    return {
+        'items': items
+    }
+
+'''
+'''
+def execute(command, *args):
+    print(command)
+    print(args)

+ 90 - 0
api/utils/docker_api.py

@@ -0,0 +1,90 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+from django.conf import settings
+from docker import DockerClient
+from docker.errors import DockerException, NotFound, APIError
+
+'''
+'''
+def get_docker_client():
+    try:
+        return DockerClient(base_url='unix:/%s' % settings.DOCKER_SOCK_DIR)
+    except DockerException:
+        return None
+
+'''
+'''
+def get_all_containers():
+    client = get_docker_client()
+
+    containers = []
+
+    if not client:
+        return containers
+
+    for container in client.containers.list():
+        containers.append({
+            'id': container.id,
+            'short_id': container.short_id,
+            'name': container.name,
+            'image': container.image,
+            'labels': container.labels,
+            'status': container.status
+        })
+
+    return containers
+
+'''
+'''
+def get_all_images():
+    client = get_docker_client()
+
+    images = []
+
+    if not client:
+        return images
+
+    for image in client.images.list():
+        images.append({
+            'id': image.id,
+            'short_id': image.short_id,
+            'tags': image.tags
+        })
+
+'''
+'''
+def start_container(id=None):
+    if not id:
+        return False
+
+    client = get_docker_client()
+
+    if not client:
+        return False
+
+    try:
+        container = client.containers.get(id)
+        container.start()
+    except NotFound:
+        return False
+    except APIError:
+        return 
+        
+'''
+'''
+def stop_container(id=None):
+    if not id:
+        return False
+
+    client = get_docker_client()
+
+    if not client:
+        return False
+
+    try:
+        container = client.containers.get(id)
+        container.stop()
+    except NotFound:
+        return False
+    except APIError:
+        return 

+ 0 - 17
api/views.py

@@ -1,17 +0,0 @@
-# -*- coding: utf-8 -*-
-from __future__ import unicode_literals
-from django.http import JsonResponse
-
-'''
-'''
-def get_token(request):
-    return JsonResponse({
-        'error': 'Under construction'
-    })
-
-'''
-'''
-def verify_token(request):
-    return JsonResponse({
-        'error': 'Under construction'
-    })

+ 2 - 0
odoo_control/settings.py

@@ -100,3 +100,5 @@ JWT_PREFIX_HEADER = config('JWT_PREFIX_HEADER')
 JWT_SECRET_KEY = config('JWT_SECRET_KEY')
 
 PLAYBOOKS_PATH = config('PLAYBOOKS_PATH')
+
+DOCKER_SOCK_DIR = config('DOCKER_SOCK_DIR')