Commit 75319b99 authored by ibuler's avatar ibuler

[Update] 更新ap请求

parent 7f4f67aa
# ~*~ coding: utf-8 ~*~
from rest_framework_bulk import BulkModelViewSet
from rest_framework.views import APIView
from rest_framework.views import Response
from rest_framework.views import APIView, Response
from rest_framework.generics import RetrieveAPIView
from django.views.generic.detail import SingleObjectMixin
from common.utils import get_logger
from ..hands import IsSuperUser
from ..hands import IsSuperUser, IsSuperUserOrAppUser
from ..models import Domain, Gateway
from ..utils import test_gateway_connectability
from .. import serializers
......@@ -22,6 +22,16 @@ class DomainViewSet(BulkModelViewSet):
permission_classes = (IsSuperUser,)
serializer_class = serializers.DomainSerializer
def get_serializer_class(self):
if self.request.query_params.get('gateway'):
return serializers.DomainWithGatewaySerializer
return super().get_serializer_class()
def get_permissions(self):
if self.request.query_params.get('gateway'):
self.permission_classes = (IsSuperUserOrAppUser,)
return super().get_permissions()
class GatewayViewSet(BulkModelViewSet):
filter_fields = ("domain",)
......@@ -43,6 +53,3 @@ class GatewayTestConnectionApi(SingleObjectMixin, APIView):
return Response("ok")
else:
return Response({"failed": e}, status=404)
......@@ -2,6 +2,8 @@
#
import uuid
import random
from django.db import models
from django.utils.translation import ugettext_lazy as _
......@@ -20,6 +22,16 @@ class Domain(models.Model):
def __str__(self):
return self.name
def has_gateway(self):
return self.gateway_set.filter(is_active=True).exists()
@property
def gateways(self):
return self.gateway_set.filter(is_active=True)
def random_gateway(self):
return random.choice(self.gateways)
class Gateway(AssetUser):
SSH_PROTOCOL = 'ssh'
......@@ -37,3 +49,4 @@ class Gateway(AssetUser):
def __str__(self):
return self.name
......@@ -38,7 +38,7 @@ class AssetGrantedSerializer(serializers.ModelSerializer):
model = Asset
fields = (
"id", "hostname", "ip", "port", "system_users_granted",
"is_active", "system_users_join", "os",
"is_active", "system_users_join", "os", 'domain',
"platform", "comment"
)
......
......@@ -26,4 +26,25 @@ class GatewaySerializer(serializers.ModelSerializer):
class Meta:
model = Gateway
fields = [
'id', 'name', 'ip', 'port', 'protocol', 'username',
'domain', 'is_active', 'date_created', 'date_updated',
'created_by', 'comment',
]
class GatewayWithAuthSerializer(GatewaySerializer):
def get_field_names(self, declared_fields, info):
fields = super().get_field_names(declared_fields, info)
fields.extend(
['password', 'private_key']
)
return fields
class DomainWithGatewaySerializer(serializers.ModelSerializer):
gateways = GatewayWithAuthSerializer(many=True, read_only=True)
class Meta:
model = Domain
fields = '__all__'
This diff is collapsed.
# -*- coding: utf-8 -*-
#
import random
from .ansible.inventory import BaseInventory
from assets.utils import get_assets_by_hostname_list, get_system_user_by_name
......@@ -11,7 +10,7 @@ __all__ = [
def make_proxy_command(asset):
gateway = random.choice(asset.domain.gateway_set.filter(is_active=True))
gateway = asset.domain.random_gateway()
proxy_command = [
"ssh", "-p", str(gateway.port),
......@@ -49,7 +48,7 @@ class JMSInventory(BaseInventory):
info = asset.to_json()
info["vars"] = vars
if asset.domain and asset.domain.gateway_set.count():
if asset.domain and asset.domain.has_gateway():
vars.update(make_proxy_command(asset))
info.update(vars)
......
......@@ -82,7 +82,10 @@ def get_pid(service):
pid_file = get_pid_file_path(service)
if os.path.isfile(pid_file):
with open(pid_file) as f:
return int(f.read().strip())
try:
return int(f.read().strip())
except ValueError:
return 0
return 0
......@@ -282,9 +285,9 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="""
Jumpserver service control tools;
Example: \r\n
Example: \r\n
%(prog)s start all -d;
"""
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment