コード例 #1
0
 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
コード例 #2
0
ファイル: upcloud.py プロジェクト: LightcurveHQ/ansible
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)
コード例 #3
0
def test_infra_ops():
    global CREATED_SERVERS
    global CREATED_TAGS

    CREATED_TAGS = TAGS

    manager = CloudManager(USERNAME, PASSWORD, timeout=120)

    try:
        auth = manager.authenticate()
        assert True
    except Exception:
        assert False

    all_servers = create_cluster(manager, CLUSTER)

    # collect & populate servers from CLUSTER
    cluster_servers = []
    for name in CLUSTER:
        server = CLUSTER[name]
        server.populate()
        cluster_servers.append(server)

    CREATED_SERVERS = cluster_servers

    # assert all_servers contain cluster_servers
    for cs in cluster_servers:
        assert cs.state == 'started'

        found = False
        for server in all_servers:
            if server.uuid == cs.uuid:
                found = True

        if not found:
            raise Exception('server {0} not found in all_servers'.format(
                cs.uuid))

    # assert servers' states
    # TODO(elnygren): add more assertions here

    # web2 non default IP configuration
    web2 = CLUSTER['web2']
    assert len(web2.ip_addresses) == 1
    assert web2.ip_addresses[0].family == 'IPv6'

    test_server = CLUSTER['web1']

    test_server.populate()
    test_server._wait_for_state_change(['started'])
    test_server.stop()
    test_server._wait_for_state_change(['stopped'])

    # contain appropriate asserts
    firewall_test(manager, FIREWALL_RULES, test_server)
    server_test(manager, test_server)
    tag_servers_test(manager, TAGS, CLUSTER)
コード例 #4
0
def test_infra_ops():
    global CREATED_SERVERS
    global CREATED_TAGS

    CREATED_TAGS = TAGS

    manager = CloudManager(USERNAME, PASSWORD, timeout=120)

    try:
        auth = manager.authenticate()
        assert True
    except Exception:
        assert False

    all_servers = create_cluster(manager, CLUSTER)

    # collect & populate servers from CLUSTER
    cluster_servers = []
    for name in CLUSTER:
        server = CLUSTER[name]
        server.populate()
        cluster_servers.append(server)


    CREATED_SERVERS = cluster_servers

    # assert all_servers contain cluster_servers
    for cs in cluster_servers:
        assert cs.state == 'started'

        found = False
        for server in all_servers:
            if server.uuid == cs.uuid:
                found = True

        if not found:
            raise Exception('server {} not found in all_servers'.format(cs.uuid))

    # assert servers' states
    # TODO(elnygren): add more assertions here

    # web2 non default IP configuration
    web2 = CLUSTER['web2']
    assert len(web2.ip_addresses) == 1
    assert web2.ip_addresses[0].family == 'IPv6'



    test_server = CLUSTER['web1']
    test_server.stop()
    test_server._wait_for_state_change(['stopped'])

    # contain appropriate asserts
    firewall_test(manager, FIREWALL_RULES, test_server)
    server_test(manager, test_server)
    tag_servers_test(manager, TAGS, CLUSTER)
コード例 #5
0
def teardown_module(module):
    manager = CloudManager(USERNAME, PASSWORD, timeout=120)

    # if we are at CIRCLECI, clean up everything
    if os.environ.get('CIRCLECI', False):
        pool = multiprocessing.Pool()
        pool.map(destroy_server, manager.get_servers())
        pool.map(delete_tag, manager.get_tags())
    else:
        print('removing {}'.format(CREATED_SERVERS))
        for server in CREATED_SERVERS:
            server.stop_and_destroy()

        print('removing {}'.format(CREATED_TAGS))
        for tag in CREATED_TAGS:
            manager.delete_tag(tag)
コード例 #6
0
def test_infra_ops():
    global CREATED_SERVERS
    global CREATED_TAGS

    CREATED_TAGS = TAGS

    manager = CloudManager(USERNAME, PASSWORD, timeout=120)

    try:
        auth = manager.authenticate()
        assert True
    except Exception:
        assert False

    all_servers = create_cluster(manager, CLUSTER)

    # collect & populate servers from CLUSTER
    cluster_servers = []
    for name in CLUSTER:
        server = CLUSTER[name]
        server.populate()
        cluster_servers.append(server)

    CREATED_SERVERS = cluster_servers

    # assert all_servers contain cluster_servers
    for cs in cluster_servers:
        assert cs.state == 'started'

        found = False
        for server in all_servers:
            if server.uuid == cs.uuid:
                found = True

        if not found:
            raise Exception('server {} not found in all_servers'.format(
                cs.uuid))

    test_server = CLUSTER['web1']
    test_server.stop()
    test_server._wait_for_state_change(['stopped'])

    # contain appropriate asserts
    firewall_test(manager, FIREWALL_RULES, test_server)
    server_test(manager, test_server)
    tag_servers_test(manager, TAGS, CLUSTER)
コード例 #7
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, ip_address):
        """
        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, e:
                pass  # no server found

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

            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]

        # try with ip-address, if given and nothing was found with uuid or hostname
        if ip_address:
            try:
                machine = self.manager.get_server_by_ip(ip_address)
                return machine
            except UpCloudAPIError as e:
                if e.error_code == 'IP_ADDRESS_NOT_FOUND':
                    self.module.fail_json(
                        msg='No server was found with IP-address: ' +
                        ip_address)
                else:
                    raise

        return None
コード例 #8
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, ip_address):
        """
        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, e:
                pass # no server found

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

            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]

        # try with ip-address, if given and nothing was found with uuid or hostname
        if ip_address:
            try:
                machine = self.manager.get_server_by_ip(ip_address)
                return machine
            except UpCloudAPIError as e:
                if e.error_code == 'IP_ADDRESS_NOT_FOUND':
                    self.module.fail_json(msg='No server was found with IP-address: ' + ip_address)
                else:
                    raise

        return None
コード例 #9
0
def teardown_module(module):
    manager = CloudManager(USERNAME, PASSWORD, timeout=160)

    # if we are at CIRCLECI, clean up everything
    if os.environ.get('CIRCLECI', False):
        pool = multiprocessing.Pool()
        pool.map(destroy_server, manager.get_servers())
        pool.map(delete_tag, manager.get_tags())
    else:
        print('removing {0}'.format(CREATED_SERVERS))
        for server in CREATED_SERVERS:
            server.stop_and_destroy()

        print('removing {0}'.format(CREATED_TAGS))
        for tag in CREATED_TAGS:
            manager.delete_tag(tag)
コード例 #10
0
ファイル: upcloud.py プロジェクト: elnygren/upcloud-ansible
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, 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
コード例 #11
0
ファイル: upcloud.py プロジェクト: jarkkom/upcloud-ansible
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, 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
コード例 #12
0
def teardown_module(module):
    manager = CloudManager(USERNAME, PASSWORD, timeout=120)

    # if we are at CIRCLECI, clean up everything
    if os.environ.get('CIRCLECI', False):
        for server in manager.get_servers():
            server.stop_and_destroy()

        for tag in manager.get_tags():
            tag.destroy()
    else:
        print('removing {}'.format(CREATED_SERVERS))
        for server in CREATED_SERVERS:
            server.stop_and_destroy()

        print('removing {}'.format(CREATED_TAGS))
        for tag in CREATED_TAGS:
            manager.delete_tag(tag)
コード例 #13
0
 def __init__(self, api_user, api_passwd):
     self.manager = CloudManager(api_user, api_passwd)
コード例 #14
0
#!/usr/bin/env python3
#
# Destroy a populated instance at UpCloud by IP address.
#
# pip3 install --user upcloud-api
# chmod 0700 ./upcloud_destroy_server.py
# ./upcloud_destroy_server.py IP-ADDRESS

import sys
from upcloud_api import CloudManager


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

manager.authenticate()

populated_server = manager.get_server_by_ip(sys.argv[1])
populated_server.stop_and_destroy()
コード例 #15
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)
コード例 #16
0
 def __init__(self, api_user, api_passwd, default_timeout):
     self.manager = CloudManager(api_user, api_passwd, default_timeout)
コード例 #17
0
#!/usr/bin/env python3
#
# Create a new instance at UpCloud.
#
# pip3 install --user upcloud-api
# 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/upcloud-init.sh'
)
コード例 #18
0
ファイル: upcloud.py プロジェクト: elnygren/upcloud-ansible
 def __init__(self, api_user, api_passwd):
     self.manager = CloudManager(api_user, api_passwd)
コード例 #19
0
#!/usr/bin/env python3
#
# Create a new instance at UpCloud.
#
# pip3 install --user upcloud-api
# 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()