pydigdrop

This Python script uses Digital Ocean api services. Importing the features from the Python module digitalocean. Data is pulled and edited.

Here is a list of features and ideas.

  • Returns user server list.
  • Returns info about servers - name/ip/location/status/distro id/distro.
  • shutdown, make snapshot, startup all servers (or just one!)
  • save info off as json/html
  • Plans start as little as $5.00 a month. Servers are charged per the hour. Digital Ocean

    TODO:

    • fix login
    
    
    In [28]:
    import os
    import json
    import digitalocean
    
    
    
    In [29]:
    opcli = open('passwd', 'r')
    opapi = open('passap', 'r')
    
    
    
    In [30]:
    digclip =  'a24a5636402aaf75d07b774d98591ea3'
    digcli = 'dd418a3ea26e7a7ff9e7cd791c0f1b4d'
    
    
    
    In [30]:
    
    
    
    
    In [31]:
    manager = digitalocean.Manager(client_id = digclip, api_key = digcli)
    
    
    
    In [32]:
    mydrop = manager.get_all_droplets()
    
    
    
    In [33]:
    print mydrop
    
    
    
    
    [<digitalocean.Droplet.Droplet object at 0x7fc9d00afb10>, <digitalocean.Droplet.Droplet object at 0x7fc9d00aff50>, <digitalocean.Droplet.Droplet object at 0x7fc9d00af450>, <digitalocean.Droplet.Droplet object at 0x7fc9d00afd10>]
    
    
    
    In [34]:
    dropo = mydrop[0]
    
    
    
    In [35]:
    print dropo
    
    
    
    
    <digitalocean.Droplet.Droplet object at 0x7fc9d00afb10>
    
    
    
    In [36]:
    events = dropo.get_events()
    
    
    
    In [37]:
    for event in events:
        event.load()
        print event.percentage
    
    
    
    In [38]:
    servlis = []
    
    
    
    In [39]:
    meip.keys
    
    
    
    
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-39-018bfb339428> in <module>()
    ----> 1 meip.keys
    
    AttributeError: 'Droplet' object has no attribute 'keys'
    
    
    In [40]:
    for meip in mydrop:
        print meip.name
        servlis.append(meip.name)
        print meip.ip_address
        servlis.append(meip.ip_address)
        print meip.status
        servlis.append(meip.status)
        print meip.image_id
        servlis.append(meip.image_id)
        servlis.append('wcmckee')
        servlis.append(meip.region_id)
        servlis.append(meip.)
    
    
    
    
      File "<ipython-input-40-b463e12da011>", line 12
        servlis.append(meip.)
                            ^
    SyntaxError: invalid syntax
    
    
    
    In [41]:
    servlis.sort()
    
    
    
    In [42]:
    fservlz = list(set(servlis))
    
    
    
    In [43]:
    fservlz.sort()
    
    
    
    In [44]:
    fservlz
    
    
    
    
    Out[44]:
    []
    
    
    In [45]:
    clisz = []
    
    
    
    In [46]:
    thrlis = []
    
    
    
    In [27]:
    dictac = zip(olis, servlis)
    
    
    
    
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-27-aaca29a901bd> in <module>()
    ----> 1 dictac = zip(olis, servlis)
    
    NameError: name 'olis' is not defined
    
    
    In [26]:
    dictac
    
    
    
    
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-26-24838ac656f2> in <module>()
    ----> 1 dictac
    
    NameError: name 'dictac' is not defined

    when converting to a dict why does it only take the first part of dictac.

    
    
    In [73]:
    dicza = dict(dictac)
    
    
    
    In [74]:
    dicza
    
    
    
    
    Out[74]:
    {'id': 3445812,
     'ip': u'107.170.136.184',
     'name': u'compohub',
     'status': u'active',
     'user': 'wcmckee'}
    
    
    In [64]:
    jsndigoc = json.dumps(dicza)
    
    
    
    In [66]:
    jsndigoc
    
    
    
    
    Out[66]:
    '{"status": "active", "ip": "107.170.136.184", "user": "wcmckee", "name": "compohub", "id": 3445812}'
    
    
    In [67]:
    digocz = open('/home/will/Desktop/brobeur-static/feeds/digocserv.json', 'w')
    
    
    
    In [68]:
    digocz.write(jsndigoc)
    
    
    
    In [69]:
    digocz.close()
    
    
    
    In [42]:
    servlis
    
    
    
    
    Out[42]:
    [u'wcmckee',
     u'128.199.199.74',
     u'active',
     308287,
     'wcmckee',
     u'catgang',
     u'188.226.209.162',
     u'active',
     3135725,
     'wcmckee',
     u'compohub',
     u'107.170.136.184',
     u'active',
     3445812,
     'wcmckee']
    
    
    In [43]:
    dropo.ip_address
    
    
    
    
    Out[43]:
    u'128.199.199.74'
    
    
    In [44]:
    dropo.name
    
    
    
    
    Out[44]:
    u'wcmckee'
    
    
    In [45]:
    dropo
    
    
    
    
    Out[45]:
    <digitalocean.Droplet.Droplet at 0x31259d0>
    
    
    In [46]:
    droplis = []
    
    
    
    In [47]:
    iplis = []
    
    
    
    In [48]:
    dropdict = dict{}
    
    
    
    
      File "<ipython-input-48-07dc5ea10dbf>", line 1
        dropdict = dict{}
                       ^
    SyntaxError: invalid syntax
    
    
    
    In [49]:
    drstr = []
    
    
    
    In [50]:
    for dr in mydrop:
        print dr.ip_address
        droplis.append(dr.name)
        droplis.append(dr.ip_address)
        iplis.append(dr.ip_address)
        droplis.append(dr.status)
        print dr.status
        droplis.append(dr.region_id)
        print dr.region_id
        droplis.append(dr.ssh_key_ids)
        droplis.append(dr.id)
        droplis.append(dr)
        print dr.name
        print dr.ssh_key_ids
        print dr.id
        print dr.image_id
        print dr
        drstr.append(dr)
    
    
    
    
    128.199.199.74
    active
    6
    wcmckee
    None
    1331748
    308287
    <digitalocean.Droplet.Droplet object at 0x31259d0>
    188.226.209.162
    active
    5
    catgang
    None
    1496053
    3135725
    <digitalocean.Droplet.Droplet object at 0x3125a10>
    107.170.136.184
    active
    4
    compohub
    None
    1585671
    3445812
    <digitalocean.Droplet.Droplet object at 0x3125a90>
    
    
    
    In [51]:
    print droplis
    
    
    
    
    [u'wcmckee', u'128.199.199.74', u'active', 6, None, 1331748, <digitalocean.Droplet.Droplet object at 0x31259d0>, u'catgang', u'188.226.209.162', u'active', 5, None, 1496053, <digitalocean.Droplet.Droplet object at 0x3125a10>, u'compohub', u'107.170.136.184', u'active', 4, None, 1585671, <digitalocean.Droplet.Droplet object at 0x3125a90>]
    
    
    
    In [52]:
    print drstr
    
    
    
    
    [<digitalocean.Droplet.Droplet object at 0x31259d0>, <digitalocean.Droplet.Droplet object at 0x3125a10>, <digitalocean.Droplet.Droplet object at 0x3125a90>]
    
    
    
    In [53]:
    print iplis
    
    
    
    
    [u'128.199.199.74', u'188.226.209.162', u'107.170.136.184']
    
    
    
    In [54]:
    #!/usr/bin/python
     
    # All SSH libraries for Python are junk (2011-10-13).
    # Too low-level (libssh2), too buggy (paramiko), too complicated
    # (both), too poor in features (no use of the agent, for instance)
     
    # Here is the right solution today:
     
    import subprocess
    import sys
     
    HOST= '74.50.51.32'
    # Ports are handled in ~/.ssh/config since we use OpenSSH
    COMMAND="uname -a"
     
    ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
    shell=False,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE)
    result = ssh.stdout.readlines()
    if result == []:
        error = ssh.stderr.readlines()
        print >>sys.stderr, "ERROR: %s" % error
    else:
        print result
    
    
    
    
    ERROR: ['Connection closed by 74.50.51.32\r\n']
    
    
    
    In [55]:
    pwd
    
    
    
    
    Out[55]:
    u'/home/will/Desktop/artcontrol-api'
    
    
    In [40]:
    from crontab import CronTab
    
    system_cron   = CronTab()
    system_cron.new
    
    
    
    
    Out[40]:
    <bound method CronTab.new of <crontab.CronTab object at 0x2e44450>>
    
    
    In [50]:
    dropo.api_key
    
    
    
    
    Out[50]:
    '9898216d345df5b70ab943c5005c05df'
    
    
    In [51]:
    dropo.call_reponse
    
    
    
    In [52]:
    dropo.image_id
    
    
    
    
    Out[52]:
    308287
    
    
    In [53]:
    dropo.size_id
    
    
    
    
    Out[53]:
    66
    
    
    In [54]:
    dropo.events
    
    
    
    
    Out[54]:
    []
    
    
    In [55]:
    dropo.size_id
    
    
    
    
    Out[55]:
    66
    
    
    In [56]:
    domain = manager.get_all_domains
    
    
    
    In [57]:
    print domain
    
    
    
    
    <bound method Manager.get_all_domains of <digitalocean.Manager.Manager object at 0x2fdc410>>
    
    
    
    In [58]:
    domain.im_self
    
    
    
    
    Out[58]:
    <digitalocean.Manager.Manager at 0x2fdc410>
    
    
    In [59]:
    gimg = manager.get_global_images
    
    
    
    In [60]:
    gimg
    
    
    
    
    Out[60]:
    <bound method Manager.get_global_images of <digitalocean.Manager.Manager object at 0x2fdc410>>
    
    
    In [61]:
    list(gimg)
    
    
    
    
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-61-580ff6d51e82> in <module>()
    ----> 1 list(gimg)
    
    TypeError: 'instancemethod' object is not iterable
    
    
    In [96]:
    mysnap = manager.get_my_images()
    
    
    
    In [97]:
    my_droplets = manager.get_all_droplets()
    
    
    
    In [98]:
    for snap in mysnap:
        print snap.name
        print snap.id
    
    
    
    
    deb
    3266196
    deb
    3353451
    
    
    
    In [73]:
    my_droplets = manager.get_all_droplets()
    for droplet in my_droplets:
        print droplet
        droplet.power_off()
    
    
    
    
    <digitalocean.Droplet.Droplet object at 0x2fdcd90>
    <digitalocean.Droplet.Droplet object at 0x2fdcd50>
    
    
    
    In [46]:
    for droplet in my_droplets:
        print droplet
        droplet.power_on()
    
    
    
    
    <digitalocean.Droplet.Droplet object at 0x2e25990>
    <digitalocean.Droplet.Droplet object at 0x2e44ad0>
    <digitalocean.Droplet.Droplet object at 0x2e44fd0>
    <digitalocean.Droplet.Droplet object at 0x2e44d10>
    <digitalocean.Droplet.Droplet object at 0x2e44810>
    
    
    
    In [64]:
    for droplet in my_droplets:
        print droplet
        print droplet.name
    
    
    
    
    <digitalocean.Droplet.Droplet object at 0x2d45b10>
    wcmckee
    <digitalocean.Droplet.Droplet object at 0x2fdcbd0>
    catgang
    
    
    
    In [54]:
    dropo.power_off()
    
    
    
    In [74]:
    dropo.take_snapshot('deb')
    
    
    
    In [82]:
    dropo.status
    
    
    
    
    Out[82]:
    u'active'
    
    
    In [75]:
    dropo.rebuild()
    
    
    
    
    ---------------------------------------------------------------------------
    Exception                                 Traceback (most recent call last)
    <ipython-input-75-735d4c41b1e7> in <module>()
    ----> 1 dropo.rebuild()
    
    /usr/lib/python2.7/site-packages/digitalocean/Droplet.pyc in rebuild(self, image_id)
        112         if self.image_id and not image_id:
        113             image_id = self.image_id
    --> 114         self.__call_api("/rebuild/", {"image_id": image_id})
        115 
        116     def enable_backups(self):
    
    /usr/lib/python2.7/site-packages/digitalocean/Droplet.pyc in __call_api(self, path, params)
         31         if data['status'] != "OK":
         32             msg = [data[m] for m in ("message", "error_message", "status") if m in data][0]
    ---> 33             raise Exception(msg)
         34         #add the event to the object's event list.
         35         event_id = data.get(u'event_id',None)
    
    Exception: An invalid size was specified
    
    
    In [55]:
    droplet.client_id
    
    
    
    
    Out[55]:
    'a24a5636402aaf75d07b774d98591ea3'
    
    
    In [67]:
    myderp = manager.get_all_regions()
    
    
    
    In [68]:
    print myderp
    
    
    
    
    [<digitalocean.Region.Region object at 0x2fcf450>, <digitalocean.Region.Region object at 0x2fcf110>, <digitalocean.Region.Region object at 0x2fcf310>, <digitalocean.Region.Region object at 0x2fdced0>]
    
    
    
    In [69]:
    myderp
    
    
    
    
    Out[69]:
    [<digitalocean.Region.Region at 0x2fcf450>,
     <digitalocean.Region.Region at 0x2fcf110>,
     <digitalocean.Region.Region at 0x2fcf310>,
     <digitalocean.Region.Region at 0x2fdced0>]
    
    
    In [70]:
    for derp in myderp:
        print derp
        droplis.append(derp.name)
        droplis.append(derp.id)
        print derp.name
        print derp.id
    
    
    
    
    <digitalocean.Region.Region object at 0x2fcf450>
    San Francisco 1
    3
    <digitalocean.Region.Region object at 0x2fcf110>
    New York 2
    4
    <digitalocean.Region.Region object at 0x2fcf310>
    Amsterdam 2
    5
    <digitalocean.Region.Region object at 0x2fdced0>
    Singapore 1
    6
    
    
    
    In [72]:
    print droplis
    
    
    
    
    [u'wcmckee', u'128.199.199.74', u'active', 6, None, 1331748, <digitalocean.Droplet.Droplet object at 0x2fdca10>, u'catgang', u'188.226.209.162', u'active', 5, None, 1496053, <digitalocean.Droplet.Droplet object at 0x2fdc9d0>, u'San Francisco 1', 3, u'New York 2', 4, u'Amsterdam 2', 5, u'Singapore 1', 6]
    
    
    
    In [102]:
    dropserv = digitalocean.Droplet(client_id=digcli, api_key=apikey, name = 'uburub', region_id=5, image_id=3137635, size_id=66, backup_active=False)
    #dropserv.create()
    dropserv.Droplet
    
    
    
    In [ ]: