예제 #1
0
class ServerManager():
    """Helpers for managing upcloud.Server instance"""

    def __init__(self, api_user, api_passwd):
        self.manager = CloudManager(api_user, api_passwd)


    def find_server(self, uuid, hostname):
        """
        Finds a server first by uuid (if given) and then by hostname.

        Exits if the given hostname has duplicates as this could potentially
        lead to destroying the wrong server.
        """
        # try with uuid first, if given
        if uuid:
            try:
                server = self.manager.get_server(uuid)
                return server
            except Exception as e:
                pass # no server found

        # try with hostname, if given and nothing was found with uuid
        if hostname:
            servers = self.manager.get_servers()

            found_servers = []
            for server in servers:
                if server.hostname == hostname:
                    found_servers.append(server)

            if len(found_servers) > 1:
                module.fail_json(msg='More than one server matched the given hostname. Please use unique hostnames.')

            if len(found_servers) == 1:
                return found_servers[0]

        return None


    def create_server(self, module):
        """Create a server from module.params. Filters out unwanted attributes."""

        # filter out 'filter_keys' and those who equal None from items to get server's attributes for POST request
        items = list(module.params.items())
        filter_keys = set(['state', 'api_user', 'api_passwd', 'user', 'ssh_keys'])
        server_dict = dict((key,value) for key, value in items if key not in filter_keys and value is not None)

        if module.params.get('ssh_keys'):
            login_user = upcloud_api.login_user_block(
                username=module.params.get('user'),
                ssh_keys=module.params['ssh_keys'],
                create_password=False
            )
            server_dict['login_user'] = login_user

        return self.manager.create_server(server_dict)
예제 #2
0
class UpCloudAPI(AbstractCloudAPI):

    name = 'upcloud'

    def __init__(self, config):
        super().__init__(max_nodes=config.getint(
            'clouds', 'upcloud_max_nodes', fallback=None))
        self.client = CloudManager(config.get('clouds', 'upcloud_login'),
                                   config.get('clouds', 'upcloud_pass'))
        self.client.authenticate()
        self.config = config

    def init_key(self):
        super().init_key()
        self.login_user = login_user_block(username=self.config.get(
            'remote', 'user'),
                                           ssh_keys=[self.public_key],
                                           create_password=False)

    def create_node(self):
        assert self.ssh_custom_key

        server = self.client.create_server(
            Server(core_number=8,
                   memory_amount=4096,
                   hostname=self.get_rnd_name('node'),
                   zone=ZONE.London,
                   storage_devices=[Storage(os='Debian 10.0', size=40)],
                   login_user=self.login_user))
        ip = server.get_public_ip()
        logging.info('CREATED %s' % ip)
        logging.info('WAITING FOR START...')
        time.sleep(30)

        # warm up
        for _ in range(10):
            ssh_conn = SSH_Connection(host=ip,
                                      user=self.config.get('remote', 'user'),
                                      connect_kwargs=self.ssh_custom_key)
            try:
                ssh_conn.run('whoami', hide=True)
            except:
                time.sleep(5)
            else:
                break

        return ip

    def delete_node(self, ip):
        for server in self.client.get_servers():
            if server.get_public_ip() == ip:
                server.stop()
                logging.info('WAITING FOR STOP...')
                time.sleep(20)
                while True:
                    try:
                        server.destroy()
                    except:
                        time.sleep(5)
                    else:
                        break
                for storage in server.storage_devices:
                    storage.destroy()
                logging.info('DELETED %s' % ip)
                break
        else:
            logging.info('NODE %s NOT DELETED AS UNKNOWN' % ip)
# chmod 0700 ./upcloud_create_server.py

from upcloud_api import CloudManager, Server, Storage, ZONE, login_user_block

# EDIT here
manager = CloudManager('USERNAME', 'PASSWORD')

user_viktor = login_user_block(
    username='******',
    ssh_keys=[
        'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJnaM2JLvO4DWkmmSXys+jn0KhTRVkCfAAhv/1Pszs0DJTheQgOR9e3ThNCgR7CxIqZ5kXrZ+BIDtDs5IGrg9IA= szv-ecdsa'
    ],
    create_password=False)

new_server_config = Server(
    hostname='upcloud.keszul.tk',
    zone=ZONE.Frankfurt,
    plan='2xCPU-4GB',
    storage_devices=[Storage(os='Debian 9.0', size=80)],
    login_user=user_viktor,
    # Docker + pip
    user_data=
    'https://github.com/szepeviktor/debian-server-tools/raw/master/debian-setup/upcloud-init.sh'
)

manager.authenticate()
new_server = manager.create_server(new_server_config)

# Print IP
print(new_server.get_public_ip() + '\n')
from upcloud_api import CloudManager, Server, Storage, ZONE, login_user_block


# EDIT here
manager = CloudManager('USERNAME', 'PASSWORD')

user_viktor = login_user_block(
    username='******',
    ssh_keys=['ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJnaM2JLvO4DWkmmSXys+jn0KhTRVkCfAAhv/1Pszs0DJTheQgOR9e3ThNCgR7CxIqZ5kXrZ+BIDtDs5IGrg9IA= szv-ecdsa'],
    create_password=False
)

new_server_config = Server(
    hostname='upcloud.keszul.tk',
    zone=ZONE.Frankfurt,
    plan='2xCPU-4GB',
    storage_devices=[
        Storage(os='Debian 9.0', size=80)
    ],
    login_user=user_viktor,
    # Docker + pip
    user_data='https://github.com/szepeviktor/debian-server-tools/raw/master/upcloud-init.sh'
)

manager.authenticate()
new_server = manager.create_server(new_server_config)

# Print IP
print(new_server.get_public_ip() + '\n')