koalalorenzo/python-digitalocean

SSH_Key format error in droplet creation

Closed this issue ยท 12 comments

Tried making a droplet via the create method of the Droplet object.
The parameter ssh_keys containing the key_id as a string ("xxxxxx") or as a list (["xxxxxx"])
returns this error.
Key invalid, key should be of the format type key [comment]

Find the attached image to see the problem in action
any workaround ?

ssh_keys issue

Are you using the latest version (master of the git repository) or the pypi version?

Yup.
ssh_keys issue

What if you use the latest master version on this repository?

Nope still doesn't work
ssh_keys issue

Is it possible to see the source code you are running?

Sure

ssh_keys issue

...as text! ๐Ÿ‘

import digitalocean
from Crypto.PublicKey import RSA


token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
manager = digitalocean.Manager(token=token)


def Keyobject(name=None,public_key=None,id=None):
    if public_key is not None and name is not None:
        auth_object = digitalocean.SSHKey(token=token, name=name, public_key=public_key)
    elif name is not None and public_key is None:
        auth_object = digitalocean.SSHKey(token=token, name=name)
    elif id is not None:
        auth_object = digitalocean.SSHKey(token=token, id=id)
    else:
        auth_object = digitalocean.SSHKey(token=token)
    return auth_object


class DigitalOcean:
    def __init__(self):
        self.name = "DigitalOceanIntegration"

    def fetch_images(self):
        images = manager.get_distro_images()
        all_images = {}
        for image in images:
            all_images[image.id] = image.distribution+"  "+image.name
        return all_images

    def fetch_all_droplets(self):
        droplets = manager.get_all_droplets()
        my_droplets = {}
        for droplet in droplets:
            my_droplets[droplet.id] = droplet.name
        return my_droplets

    def fetch_regions(self):
        regions = manager.get_all_regions()
        all_regions = {}
        for region in regions:
            all_regions[region.slug] = region.name
        return all_regions

    def fetch_sizes(self):
        sizes = manager.get_all_sizes()
        return sizes

    def create_key(self,name):
        key = RSA.generate(2048)
        private_key = key.exportKey('PEM')
        with open("private.key", 'w') as content_file:
            content_file.write(private_key)
        public_key = key.exportKey('OpenSSH')
        PubKeyObject = Keyobject(public_key=public_key, name=name)
        return PubKeyObject.create()

    def delete_key(self, id):
        Key = Keyobject(id=id)
        return Key.destroy()

    def fetch_keys(self):
        keys = manager.get_all_sshkeys()
        all_keys = {}
        for key in keys:
            all_keys[key.id] = key.name
        return all_keys

class DigitalOceanDroplet:
    def __init__(self):
        self.name = "Droplet"

    def fetch_droplet(self, droplet_id):
        droplet = manager.get_droplet(droplet_id)
        return droplet

    def snapshot(self, droplet, name):
        if droplet.take_snapshot(snapshot_name=name):
            return True
        else:
            return False

    def create_vm(self, name, region, image_id, size, ssh_key, user_data):
        droplet = digitalocean.Droplet(name=name, region=region, image=image_id, size=size, ssh_keys=ssh_key, user_data=user_data, token=token)
        droplet.create()

DigitalOcean = DigitalOcean()
DigitalOceanDroplet = DigitalOceanDroplet()

name = raw_input("Enter a name : ")
print DigitalOcean.fetch_regions()
region = raw_input("Choose a region: ")
print '''
{ 23047968 : Ubuntu 16.10,
22236156 : CentOS 6.8 }
'''
snapshot = raw_input("Choose an image id : ")
print DigitalOcean.fetch_sizes()
size = raw_input("Choose a size : ")
print DigitalOcean.fetch_keys()
key_in = raw_input("Choose a key_id : ")
key = [key_in]
print DigitalOceanDroplet.create_vm(name, region, snapshot, size, key, user_data=None)

I have updated your comment to make it readable ๐Ÿ‘

Thanks. i was about to do it as well. ๐Ÿ‘

your key value should be integer, if you want to use the string, you need to pass the fingerprint.
Quick fix:key = [int(key_in)]
is it working now?

Thanks worked like a charm.