コード例 #1
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)
コード例 #2
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
コード例 #3
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
コード例 #4
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
コード例 #5
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