2016-08-09 09:27:37 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
#
|
2018-01-26 06:47:10 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
import paramiko
|
2018-01-26 06:47:10 +00:00
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
from common.utils import get_object_or_none
|
2018-01-26 06:47:10 +00:00
|
|
|
from .models import Asset, SystemUser, Label
|
2016-09-04 09:43:03 +00:00
|
|
|
|
2017-12-06 10:31:51 +00:00
|
|
|
|
|
|
|
def get_assets_by_id_list(id_list):
|
|
|
|
return Asset.objects.filter(id__in=id_list)
|
2017-12-07 05:01:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_assets_by_hostname_list(hostname_list):
|
|
|
|
return Asset.objects.filter(hostname__in=hostname_list)
|
|
|
|
|
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
def get_system_user_by_name(name):
|
|
|
|
system_user = get_object_or_none(SystemUser, name=name)
|
|
|
|
return system_user
|
2017-12-12 04:19:45 +00:00
|
|
|
|
|
|
|
|
2018-01-26 06:47:10 +00:00
|
|
|
class LabelFilter:
|
|
|
|
def filter_queryset(self, queryset):
|
2018-01-26 07:53:43 +00:00
|
|
|
queryset = super().filter_queryset(queryset)
|
2018-01-26 06:47:10 +00:00
|
|
|
query_keys = self.request.query_params.keys()
|
|
|
|
all_label_keys = Label.objects.values_list('name', flat=True)
|
|
|
|
valid_keys = set(all_label_keys) & set(query_keys)
|
|
|
|
labels_query = {}
|
|
|
|
for key in valid_keys:
|
|
|
|
labels_query[key] = self.request.query_params.get(key)
|
|
|
|
|
|
|
|
conditions = []
|
|
|
|
for k, v in labels_query.items():
|
|
|
|
query = {'labels__name': k, 'labels__value': v}
|
|
|
|
conditions.append(query)
|
|
|
|
|
|
|
|
if conditions:
|
|
|
|
for kwargs in conditions:
|
|
|
|
queryset = queryset.filter(**kwargs)
|
|
|
|
return queryset
|
2018-02-09 03:12:40 +00:00
|
|
|
|
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
def test_gateway_connectability(gateway):
|
|
|
|
"""
|
|
|
|
Test system cant connect his assets or not.
|
|
|
|
:param gateway:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
client = paramiko.SSHClient()
|
|
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
|
|
|
|
proxy_command = [
|
|
|
|
"ssh", "{}@{}".format(gateway.username, gateway.ip),
|
|
|
|
"-p", str(gateway.port), "-W", "127.0.0.1:{}".format(gateway.port),
|
|
|
|
]
|
|
|
|
|
|
|
|
if gateway.password:
|
|
|
|
proxy_command.insert(0, "sshpass -p '{}'".format(gateway.password))
|
|
|
|
if gateway.private_key:
|
|
|
|
proxy_command.append("-i {}".format(gateway.private_key_file))
|
|
|
|
|
|
|
|
try:
|
|
|
|
sock = paramiko.ProxyCommand(" ".join(proxy_command))
|
|
|
|
except paramiko.ProxyCommandFailure as e:
|
|
|
|
return False, str(e)
|
|
|
|
|
|
|
|
try:
|
|
|
|
client.connect("127.0.0.1", port=gateway.port,
|
|
|
|
username=gateway.username,
|
|
|
|
password=gateway.password,
|
|
|
|
key_filename=gateway.private_key_file,
|
|
|
|
sock=sock,
|
|
|
|
timeout=5
|
|
|
|
)
|
|
|
|
except (paramiko.SSHException, paramiko.ssh_exception.SSHException,
|
|
|
|
paramiko.AuthenticationException, TimeoutError) as e:
|
|
|
|
return False, str(e)
|
|
|
|
finally:
|
|
|
|
client.close()
|
|
|
|
return True, None
|