-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add automated device form and store opsi/puppet data locally (#365)
* Add more automated flow for creating devices * make department short name optional * Begin storing provided data locally * update to django 3 * Improve data loading * allow device attributes to be archived in description * Add management command to set OS from mac * pad default hostname to 6 digit id * add management command to clean up device names
- Loading branch information
1 parent
059c597
commit 5bb9910
Showing
52 changed files
with
818 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ backup/ | |
node_modules/ | ||
/Pipfile | ||
/Pipfile.lock | ||
/venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from django.utils import timezone | ||
from devicedata.models import ProvidedData | ||
from devicedata.providers.opsi import OpsiProvider | ||
from devicedata.providers.puppet import PuppetProvider | ||
|
||
data_providers = { | ||
"opsi": OpsiProvider, | ||
"puppet": PuppetProvider | ||
} | ||
|
||
|
||
def _get_provider(device): | ||
if device.data_provider is not None and device.data_provider in data_providers.keys(): | ||
return data_providers[device.data_provider]() | ||
else: | ||
for provider in data_providers.values(): | ||
provider_instance = provider() | ||
if provider_instance.has_device(device): | ||
return provider_instance | ||
|
||
|
||
def _update_provided_data(device, data, force=False): | ||
if not force: | ||
old_data = device.provided_data.all() | ||
if len(old_data) > 0: | ||
if (timezone.now() - old_data[0].stored_at).days < 7: | ||
return old_data | ||
device.provided_data.all().delete() | ||
for entry in data.formatted_entries: | ||
pd = ProvidedData() | ||
pd.device = device | ||
pd.name = entry.name | ||
pd.formatted_value = entry.value | ||
pd.save() | ||
return device.provided_data.all() |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from django.core.management import BaseCommand | ||
|
||
from devicedata.generic import _get_provider | ||
from devices.models import Device | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('device_ids', nargs='*', type=int) | ||
|
||
def handle(self, *args, **options): | ||
if "device_ids" in options and len(options["device_ids"]) > 0: | ||
devices = Device.objects.filter(pk__in=options["device_ids"]) | ||
else: | ||
devices = Device.objects.filter(data_provider="", ipaddress__isnull=False) | ||
|
||
if len(devices) is 0: | ||
self.stdout.write("Could not find any devices with data provider.") | ||
return | ||
for device in devices: | ||
provider = _get_provider(device) | ||
if provider is not None: | ||
device.data_provider = provider.name | ||
device.save() | ||
self.stdout.write("Processed: {0} with {1}".format(device, device.data_provider)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.core.management import BaseCommand | ||
|
||
from devicedata.generic import _get_provider, _update_provided_data | ||
from devicedata.models import ProvidedData | ||
from devices.models import Device | ||
from datetime import datetime | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('device_ids', nargs='*', type=int) | ||
parser.add_argument('-f', '--force', action='store_true', dest='force') | ||
|
||
def handle(self, *args, **options): | ||
if "device_ids" in options and len(options["device_ids"]) > 0: | ||
devices = Device.objects.filter(pk__in=options["device_ids"]) | ||
else: | ||
devices = Device.objects.exclude(data_provider__isnull=True) | ||
|
||
if len(devices) is 0: | ||
self.stdout.write("Could not find any devices with data provider.") | ||
return | ||
for device in devices: | ||
self.stdout.write("Processing: {0} from {1}".format(device, device.data_provider)) | ||
provider = _get_provider(device) | ||
if provider is None: | ||
continue | ||
data = provider.get_device_info(device) | ||
_update_provided_data(device, data, options["force"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 2.2.12 on 2020-05-25 16:13 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
('devices', '0013_auto_20200509_1148'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ProvidedData', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('stored_at', models.DateTimeField(auto_created=True)), | ||
('type', models.CharField(max_length=100)), | ||
('name', models.CharField(max_length=200)), | ||
('raw_value', models.CharField(max_length=2000)), | ||
('formatted_value', models.CharField(max_length=500)), | ||
('device', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='provided_data', to='devices.Device')), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
from django.db import models | ||
from devices.models import Device | ||
|
||
# Create your models here. | ||
|
||
class ProvidedData(models.Model): | ||
device = models.ForeignKey(Device, on_delete=models.CASCADE, related_name="provided_data") | ||
|
||
type = models.CharField(max_length=100) | ||
name = models.CharField(max_length=200) | ||
raw_value = models.CharField(max_length=2000) | ||
formatted_value = models.CharField(max_length=500) | ||
stored_at = models.DateTimeField(auto_now_add=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from Lagerregal import settings | ||
from devices.models import Device | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.