Skip to content

Instantly share code, notes, and snippets.

@vi4m
Last active December 11, 2015 05:48
Show Gist options
  • Save vi4m/4554591 to your computer and use it in GitHub Desktop.
Save vi4m/4554591 to your computer and use it in GitHub Desktop.

Ralph shell

UF options

  • dir()
  • autocompletion - po ewaluacji
  • root

Jak wyszukiwac obiekty wg atrybutow?

  • Po Primary Key
    Device.objects.get(pk=1)
  • Po typach
    from ralph.discovery.models import DeviceType
    Device.objects.filter(model__type=DeviceType.rack.id)
  • Wszystkie
    Device.objects.all()
  • Po adresie ip
    import ipaddr
    Device.objects.filter(ipaddress=int(ipaddr.IPAddress('127.0.0.1')))
  • warunek "AND"

    Po prostu kolejne argumenty do filter po przecinkach lub &

  • warunek "OR"

    from django.db.models import Q
    Device.objects.filter(Q(model__name__contains='VMware') | Q(pk__gt=1))
  • Zbiory (sets)
    d = Device.objects.all()[0]
    
    d.ethernet
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    AttributeError: 'Device' object has no attribute 'ethernet'
    
    d.ethernet_set.all()
    
    [<Ethernet: eth0: Broadcom Corporation NetXtreme II BCM5709 Gigabit Ethernet (18A9053BBBCC)>, <Ethernet: .... ]

Pętle for - wyświetl status verified dla venture cloud

for d in Device.objects.filter(venture__name='Cloud'):
    print d.verified

Jak na nich zmieniac jakies atrybuty?

  • Prosta zmiana 1 obiektu
    d = Device.objects.get(pk=1)
    d.name = '...'
    d.save()
  • Zmiana wiele obiektów naraz
    Network.objects.filter(pk__gt=1000).update(max_ip=0)
    Network.objects.get(pk=625).racks.all()
    [<Device: DC4::Rack 312 (1948487)>]
    
    moj_rack = ...
    n = Network.objects.all()[0]
    n.racks.add(moj_rack)

Skad brac informacje o modelu danych?

https://github.com/allegro/ralph

Przyklady

  • Deploymenty w trakcie
    from ralph.deployment.models import DeploymentStatus
    Deployment.objects.filter(status=DeploymentStatus.in_progress).all()
  • Lista deployment id / mac
    [(x.id, x.mac) for x in Deployment.objects.all()]

Przykladowe "jednolinijkowce" najczesciej przydatne w pracy z ralph shell

ls - /home/ralph/project-allegro/scripts

from ralph.discovery.models_device import Device
import csv

csvfile=open('/tmp/output.csv', 'wb')
writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)

x = [ (d.venture, d.role,d.name, d.id, d.cached_price, d.processor_set.count(), d.storage_set.count(), d.memory_set.count() ) \
        for d in Device.objects.filter(venture__department__name='ZUIS2') \
        if not(d.cached_price) or (not d.processor_set.count() or not d.memory_set.count() or not d.storage_set.count()) ]

for row in x:
    writer.writerow(row)

csvfile.close()

Gdzie szukac informacji o metodach/funkcjach dostepnych do operowania na obiektach ralpha z shella? Czego absolutnie nie można robic i na co szczególnie uwazac żeby nie popsuc ralpha?

Czego absolutnie nie można robic i na co szczególnie uwazac żeby nie popsuc ralpha?

    # Nie używaj delete()
    
    device.deleted = True
    device.save()
    
    # Uważaj na update() - lepiej iterować 
    

B-grafia:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment