D94 #100DaysOfCode

D94 #100DaysOfCode. Today I added some more features to my Digital Ocean Manager.

Read Time1 Minute, 44 Second

Today I’ve been working on my Digital Ocean Manager App.

I’ve made a bit of a menu now so it’s a bit easier to manage what you want to do.

I’ve now added two features.

The first feature I’ve added today is the ability to upload your local public key to Digital Ocean so you can then use it when creating servers.

The second feature I added is the ability to create Droplets with your local SSH key, it makes it a bit easier to get the ball rolling rather than generating a new one and then uploading that to the console. I should note this is set up to only work with Linux at the moment.

Here’s the code below.

#Import Start
import digitalocean
from digitalocean import SSHKey
import os
from alive_progress import alive_bar
#Import End

#Gloabl Variables Start
USER = os.environ.get("USER")
API_SECRET = os.environ.get("TOKEN")
manager = digitalocean.Manager(token=API_SECRET)
my_droplets = manager.get_all_droplets()
keys = manager.get_all_sshkeys()
#Global Varibles End

#Functions Start
def dropletsList():
    for droplet in my_droplets:
        print(droplet)

def dropletsLog():
    with alive_bar() as bar:
        for droplet in my_droplets:
            print(f"Pulling logs for {droplet}")
            print(droplet.get_events(), file=open("droplet_events.txt", "a"))
            bar()

def main():
    print("""
----------------------
Digital Ocean Manager
----------------------   

[1] List All Droplets
[2] Export Droplet Logs
[3] Create Droplet
[4] Upload local SSH key to Digital Ocean
    """)
    choice = input("Select from the menu: ")
    if choice == '1':
        dropletsList()
    elif choice == '2':
        dropletsLog()
    elif choice == '3':
        createDroplet()
    elif choice == '4':
        dropletSSHKey()
    else:
        print(":(")

def dropletSSHKey():
    user_ssh_key = open(f'/home/{USER}/.ssh/id_rsa.pub').read()
    key = SSHKey(token=API_SECRET,
             name='archyDesktop',
             public_key=user_ssh_key)
    key.create()
    print("Key Created")

def createDroplet():
    newDroplet = digitalocean.Droplet(token=API_SECRET,
    name='testserver',
    region='SGP1',
    image='ubuntu-14-04-x64',
    size_slug='512mb',
    ssh_keys=keys,
    backups=False)

    newDroplet.create()

#Functions End


if __name__ == "__main__":
    main()

Oh, I nearly forgot! I also added a neat little progress bar when downloading the Droplet Logs.

0 0
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleppy
Sleppy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Leave a Reply

Your email address will not be published. Required fields are marked *

Close