Эх сурвалжийг харах

using database to config instances

Roberto Berto 5 жил өмнө
parent
commit
2be5fd89aa

+ 3 - 0
.gitignore

@@ -130,3 +130,6 @@ dmypy.json
 
 # Macos
 .DS_Store
+
+# Database 
+db.sqlite3

BIN
vycenter/.vyos.py.swp


+ 22 - 0
vycenter/config/migrations/0004_auto_20200427_1159.py

@@ -0,0 +1,22 @@
+# Generated by Django 2.0.3 on 2020-04-27 11:59
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('config', '0003_auto_20200426_1259'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='instance',
+            name='id',
+        ),
+        migrations.AlterField(
+            model_name='instance',
+            name='hostname',
+            field=models.CharField(max_length=120, primary_key=True, serialize=False),
+        ),
+    ]

+ 1 - 1
vycenter/config/models.py

@@ -2,8 +2,8 @@ from django.db import models
 
 
 class Instance(models.Model):
+    hostname = models.CharField(max_length=120, primary_key=True)
     alias = models.CharField(max_length=30)
-    hostname = models.CharField(max_length=120)
     port = models.IntegerField()
     key = models.CharField(max_length=100)
     https = models.BooleanField()

+ 33 - 0
vycenter/config/templates/config/instance_conntry.html

@@ -0,0 +1,33 @@
+{% extends "base.html" %}
+
+{% block header_title %}List instances{% endblock %}
+{% block section_title %}List instances{% endblock %}
+
+{% block content %}
+
+{% if instance %}
+    {% if connected == True %}
+        <div style="background-color: rgb(87, 87, 182); margin-bottom: 20px; padding: 2px 5px;">Instance connected</div>
+    {% else %}
+        <div style  ="background-color: rgb(184, 78, 78); margin-bottom: 20px; padding: 2px 5px;">Instance not connected</div>
+    {% endif %}
+
+    <table border="1" width="100%">
+    <tr><th>alias</th><th>hostname</th><th>port</th><th>key</th><th>https</th></tr>
+
+    <tr>
+        <td>{{ instance.alias }}</td>
+        <td>{{ instance.hostname }}</td>
+        <td>{{ instance.port }}</td>
+        <td>{{ instance.key }}</td>
+        <td>{{ instance.https }}</td>
+    </tr>
+
+
+    </table>   
+{% else %}
+    <p>No instances.</p>
+{% endif %}
+
+
+{% endblock %}

+ 2 - 1
vycenter/config/templates/config/instances.html

@@ -8,7 +8,7 @@
 {% if instances %}
 
     <table border="1" width="100%">
-    <tr><th>alias</th><th>hostname</th><th>port</th><th>key</th><th>https</th></tr>
+    <tr><th>alias</th><th>hostname</th><th>port</th><th>key</th><th>https</th><th>test connection</th></tr>
 
     {% for instance in instances %}
     <tr>
@@ -17,6 +17,7 @@
         <td>{{ instance.port }}</td>
         <td>{{ instance.key }}</td>
         <td>{{ instance.https }}</td>
+        <td><a href="{% url 'config:instance-conntry' instance.hostname %}">test</a></td>
     </tr>
 
     {% endfor %}

+ 1 - 0
vycenter/config/urls.py

@@ -8,6 +8,7 @@ app_name = 'config'
 urlpatterns = [
     path('', views.index, name='index'),
     path('instance-add', views.instance_add, name='instance-add'),
+    path('instance-conntry/<str:hostname>', views.instance_conntry, name='instance-conntry'),
     path('instances', views.instances, name='instances'),
 
 ]

+ 21 - 3
vycenter/config/views.py

@@ -9,17 +9,20 @@ from .models import Instance
 
 def index(request):
     #interfaces = vyos.get_interfaces()
-    
+    all_instances = vyos.instance_getall()
+
     template = loader.get_template('config/instance.html')
     context = { 
         #'interfaces': interfaces,
+        'instances': all_instances,
     }   
     return HttpResponse(template.render(context, request))
 
 
 
 def instances(request):
-    all_instances = Instance.objects.all()
+    all_instances = vyos.instance_getall()
+    
 
     template = loader.get_template('config/instances.html')
     context = { 
@@ -29,7 +32,8 @@ def instances(request):
 
 def instance_add(request):
     #interfaces = vyos.get_interfaces()
-    
+    all_instances = vyos.instance_getall()
+
     if len(request.POST) > 0:
         instance = Instance()
         instance.alias = request.POST['alias']
@@ -47,10 +51,24 @@ def instance_add(request):
     template = loader.get_template('config/instance_add.html')
     context = { 
         'instance_id': instance_id,
+        'instances': all_instances,
     }   
     return HttpResponse(template.render(context, request))
 
+def instance_conntry(request, hostname):
+    all_instances = vyos.instance_getall()
+
+    # permcheck
+    instance = Instance.objects.get(hostname=hostname)
+    connected = vyos.conntry(hostname)
 
+    template = loader.get_template('config/instance_conntry.html')
+    context = { 
+        'instance': instance,
+        "connected": connected,
+        'instances': all_instances,
+    }   
+    return HttpResponse(template.render(context, request))
 
 
 

+ 8 - 1
vycenter/vycenter/templates/base.html

@@ -40,7 +40,14 @@
     <div class="col-3 menu">
 
     <h2>Manage VyOS</h2>
-    <p><select name="vyos-id"><option>xxxx - 192.168.4.4</option></select></p>
+    <p><select name="vyos-id">
+      {% for instance in instances %}
+      <option value="{{instance.hostname}}">{{instance.alias}}</option>
+      {% endfor %}
+
+      
+    
+    </select></p>
     <ol>
     <li><a href="/instance/">Dashboard</a></li>
     <li>Static Routing</li>

+ 88 - 13
vycenter/vyos.py

@@ -8,30 +8,97 @@ import sys
 sys.path.append('/var/secrets')
 import local
 
+from config.models import Instance
 
+def get_url(hostname):
+    # permcheck
+    instance = Instance.objects.get(hostname=hostname)
+    if instance.https == True:
+        protocol = "https"
+    else:
+        protocol = "http"
 
+    if (instance.port == None):
+        instance.port = 443
+
+    url = protocol + "://" + instance.hostname + ":" + str(instance.port)
+
+    return url
+
+def get_url_manage(hostname):
+    url = get_url(hostname) + '/config-file'
+    return url
+
+def get_url_configure(hostname):
+    url = get_url(hostname) + '/configure'
+    return url
+
+def get_url_retrieve(hostname):
+    url = get_url(hostname) + '/retrieve'        
+    return url
+
+def get_key(hostname):
+    # permcheck
+    instance = Instance.objects.get(hostname=hostname)
+    return instance.key
 
- 
-SERVER_URL_MANAGE = local.SERVER_URL + 'config-file'
-SERVER_URL_CONFIG = local.SERVER_URL + 'configure'
-SERVER_URL_RETRIE = local.SERVER_URL + 'retrieve'
 
 #data='{"op": "showConfig", "path": ["interfaces", "dummy"]}
+def instance_getall():
+    instances = Instance.objects.all()
+    return instances
 
 
-def getall():
+def conntry(hostname): 
+    cmd = {"op": "showConfig", "path": ["interfaces"]}
+
+    print(json.dumps(cmd))
+    post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
+    print(post)
+
+
+    # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
+    # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
+
+    
+    print(get_url_retrieve(hostname))
+
+    try:
+        resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
+    except requests.exceptions.ConnectionError:
+        return False
+    
+    print(resp.status_code)
+
+    if (resp.status_code == 200):
+        return True
+    
+    pprint.pprint(resp)
+    pprint.pprint(resp.json())
+
+    return False
+
+
+
+
+def getall(hostname="179.127.12.142"):
     #cmd = {"op": "save", "file": "/config/config.boot"}
     cmd = {"op": "showConfig", "path": ["interfaces", "dummy"]}
 
     print(json.dumps(cmd))
-    post = {'key': local.SERVER_KEY, 'data': json.dumps(cmd)}
+    post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     print(post)
 
 
     # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
     # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
 
-    resp = requests.post(SERVER_URL_RETRIE, verify=False, data=post)
+    try:
+        resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
+    except requests.exceptions.ConnectionError:
+        return False
+
+
     print(resp.status_code)
     pprint.pprint(resp)
 
@@ -48,18 +115,22 @@ def getall():
     return resp
 
 
-def get_interfaces():
+def get_interfaces(hostname="179.127.12.142"):
     cmd = {"op": "showConfig", "path": ["interfaces"]}
 
     print(json.dumps(cmd))
-    post = {'key': local.SERVER_KEY, 'data': json.dumps(cmd)}
+    post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     print(post)
 
 
     # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
     # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
 
-    resp = requests.post(SERVER_URL_RETRIE, verify=False, data=post)
+    try:
+        resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
+    except requests.exceptions.ConnectionError:
+        return False
+
     print(resp.status_code)
     pprint.pprint(resp)
 
@@ -80,18 +151,22 @@ def get_interfaces():
 
     return result1['data']
 
-def get_interface(interface_type, interface_name):
+def get_interface(interface_type, interface_name, hostname="179.127.12.142"):
     cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
 
     print(json.dumps(cmd))
-    post = {'key': local.SERVER_KEY, 'data': json.dumps(cmd)}
+    post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     print(post)
 
 
     # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
     # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
 
-    resp = requests.post(SERVER_URL_RETRIE, verify=False, data=post)
+    try:
+        resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
+    except requests.exceptions.ConnectionError:
+        return False
+
     print(resp.status_code)
     pprint.pprint(resp)