Exemplo n.º 1
0
 def __init__(self, instance):
     super(Foreman, self).__init__(instance)
     self.credentials = get_or_none_credentials_for(
         self.environment, CredentialType.FOREMAN
     )
     self._provider = None
     self.host_prov_client = HostProviderClient(self.environment)
Exemplo n.º 2
0
    def do(self):
        pod_metadata = self.client.read_namespaced_pod(self.pod_name,
                                                       'default')
        self.instance.address = pod_metadata.status.pod_ip
        self.instance.port = self.driver.default_port

        host = self.host
        host.address = self.instance.address
        host.save()
        hp_client = HostProviderClient(self.environment)
        hp_client.edit_host(host.identifier, payload={'address': host.address})
class VipProviderStep(BaseInstanceStep):

    def __init__(self, instance=None):
        super(VipProviderStep, self).__init__(instance)
        self.credentials = None
        self._provider = None
        self.host_prov_client = HostProviderClient(self.environment)

    @property
    def provider(self):
        if not self._provider:
            self._provider = Provider(self.instance, self.environment)
        return self._provider

    @property
    def vm_properties(self):
        if not (hasattr(self, '_vm_properties') and self._vm_properties):
            self._vm_properties = self.host_prov_client.get_vm_by_host(
                self.host)
        return self._vm_properties

    @property
    def equipments(self):
        equipments = []
        for instance in self.infra.instances.all():
            host = instance.hostname
            if host.future_host:
                host = host.future_host
            vm_info = self.host_prov_client.get_vm_by_host(host)
            equipment = {
                'host_address': host.address,
                'port': instance.port,
                'identifier': vm_info.identifier
            }
            equipments.append(equipment)
        return equipments

    @property
    def team(self):
        ## TODO
        return "dbaas"
        if self.has_database:
            return self.database.team.name
        return self.create.team.name

    def do(self):
        raise NotImplementedError

    def undo(self):
        pass
Exemplo n.º 4
0
class VipProviderStep(BaseInstanceStep):
    def __init__(self, instance=None):
        super(VipProviderStep, self).__init__(instance)
        self.credentials = None
        self._provider = None
        self.host_prov_client = HostProviderClient(self.environment)

    @property
    def provider(self):
        if not self._provider:
            self._provider = Provider(self.instance, self.environment)
        return self._provider

    @property
    def vm_properties(self):
        if not (hasattr(self, '_vm_properties') and self._vm_properties):
            self._vm_properties = self.host_prov_client.get_vm_by_host(
                self.host)
        return self._vm_properties

    @property
    def equipments(self):
        equipments = []
        for instance in self.infra.instances.all():
            host = instance.hostname
            if host.future_host:
                host = host.future_host
            vm_info = self.host_prov_client.get_vm_by_host(host)
            equipment = {
                'host_address': host.address,
                'port': instance.port,
                'identifier': vm_info.identifier
            }
            equipments.append(equipment)
        return equipments

    @property
    def team(self):
        ## TODO
        return "dbaas"
        if self.has_database:
            return self.database.team.name
        return self.create.team.name

    def do(self):
        raise NotImplementedError

    def undo(self):
        pass
Exemplo n.º 5
0
    def do(self):
        host_provider_cli = HostProviderClient(self.infra.environment)
        vm = host_provider_cli.get_vm_by_host(self.host)
        cloudstack_offering_id = self.cs_provider.get_vm_offering_id(
            vm_id=vm.identifier,
            project_id=self.cs_credentials.project
        )

        if cloudstack_offering_id == self.target_offering_id:
            return

        success = self.cs_provider.change_service_for_vm(
            vm.identifier, self.target_offering_id
        )
        if not success:
            raise Exception("Could not change offering")
Exemplo n.º 6
0
    def equipments(self):
        equipments = []
        host_provider_cli = HostProviderClient(self.infra.environment)
        for instance in self.infra.instances.all():
            host = instance.hostname
            vm = host_provider_cli.get_vm_by_host(host)
            if vm is None:
                raise Exception(
                    "Cannot get information for host {} for identifier {}".
                    format(host, host.identifier))
            equipment = Equipment(
                '{}-{}'.format(self.api.vm_name, vm.identifier), host.address,
                instance.port)
            equipments.append(equipment)

        return equipments
Exemplo n.º 7
0
class Foreman(BaseInstanceStep):

    host_status = HostStatus

    def __init__(self, instance):
        super(Foreman, self).__init__(instance)
        self.credentials = get_or_none_credentials_for(
            self.environment, CredentialType.FOREMAN
        )
        self._provider = None
        self.host_prov_client = HostProviderClient(self.environment)

    @property
    def provider(self):
        if self._provider is None:
            self._provider = get_foreman_provider(self.infra, self.credentials)
        return self._provider

    @property
    def fqdn(self):
        if self.host_status.is_up(self.host):
            script = 'hostname -f'
            output = self.host.ssh.run_script(script)
            return output['stdout'][0].strip()
        vm_properties = self.host_prov_client.get_vm_by_host(self.host)
        if vm_properties and vm_properties.fqdn:
            return vm_properties.fqdn
        raise FqdnNotFoundExepition("Fqdn is not found")

    @property
    def reverse_ip(self):
        if self.host_status.is_up(self.host):
            reverse_ip = subprocess.check_output(
                ("nslookup {} | grep 'name' | "
                 "awk '/name = / {{print $4}}'".format(
                    self.host.address)),
                shell=True)
            if reverse_ip.endswith('.'):
                return reverse_ip[:-1]
            return reverse_ip
        return self.fqdn

    @property
    def is_valid(self):
        return self.credentials is not None

    def do(self):
        raise NotImplementedError

    def undo(self):
        pass
 def __init__(self, instance):
     super(VolumeProviderBase, self).__init__(instance)
     self._credential = None
     self.host_prov_client = HostProviderClient(self.environment)
class VolumeProviderBase(BaseInstanceStep):
    def __init__(self, instance):
        super(VolumeProviderBase, self).__init__(instance)
        self._credential = None
        self.host_prov_client = HostProviderClient(self.environment)

    @property
    def driver(self):
        return self.infra.get_driver()

    @property
    def credential(self):
        if not self._credential:
            self._credential = get_credentials_for(
                self.environment, CredentialType.VOLUME_PROVIDER)
        return self._credential

    @property
    def volume(self):
        return self.host.volumes.get(is_active=True)

    @property
    def inactive_volume(self):
        return self.host.volumes.filter(is_active=False).last() or None

    @property
    def volume_migrate(self):
        return self.host_migrate.host.volumes.get(is_active=True)

    @property
    def provider(self):
        return self.credential.project

    @property
    def base_url(self):
        return "{}/{}/{}/".format(self.credential.endpoint, self.provider,
                                  self.environment)

    @property
    def headers(self):
        header = {}
        if self.pool:
            header = self.pool.as_headers
        header["K8S-Namespace"] = self.infra.name
        return header

    @property
    def host_vm(self):
        return self.host_prov_client.get_vm_by_host(self.host)

    def create_volume(self,
                      group,
                      size_kb,
                      to_address='',
                      snapshot_id=None,
                      is_active=True,
                      zone=None,
                      vm_name=None):
        url = self.base_url + "volume/new"
        data = {
            "group": group,
            "size_kb": size_kb,
            "to_address": to_address,
            "snapshot_id": snapshot_id,
            "zone": zone,
            "vm_name": vm_name
        }

        response = post(url, json=data, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)

        volume = Volume()
        volume.host = self.host
        volume.identifier = response.json()['identifier']
        volume.total_size_kb = self.infra.disk_offering.size_kb
        volume.is_active = is_active
        volume.save()
        return volume

    def destroy_volume(self, volume):
        url = "{}volume/{}".format(self.base_url, volume.identifier)
        response = delete(url, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        volume.delete()

    def destroy_old_volume(self, volume):
        url = "{}remove-old-volume/{}".format(self.base_url, volume.identifier)
        response = delete(url, headers=self.headers)

        if not response.ok:
            raise IndexError(response.content, response)

        volume.delete()
        return response.json()

    def detach_disk(self, volume):
        url = "{}detach-disk/{}".format(self.base_url, volume.identifier)
        response = post(url, headers=self.headers)

        if not response.ok:
            raise IndexError(response.content, response)

        return response.json()

    def get_volume(self, volume):
        url = "{}volume/{}".format(self.base_url, volume.identifier)
        response = get(url, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        return response.json()

    def get_path(self, volume):
        vol = self.get_volume(volume)
        return vol['path']

    def run_script(self, script, host=None):
        output = {}
        return_code = exec_remote_command_host(host or self.host, script,
                                               output)
        if return_code != 0:
            raise EnvironmentError('Could not execute script {}: {}'.format(
                return_code, output))
        return output

    def take_snapshot(self):
        url = "{}snapshot/{}".format(self.base_url, self.volume.identifier)
        data = {
            "engine": self.engine.name,
            "db_name": self.database.name,
            "team_name": self.database.team.name
        }
        response = post(url, json=data, headers=self.headers)

        if not response.ok:
            raise IndexError(response.content, response)
        return response.json()

    def delete_snapshot(self, snapshot, force):
        url = "{}snapshot/{}?force={}".format(self.base_url,
                                              snapshot.snapshopt_id, force)
        response = delete(url, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        return response.json()['removed']

    def restore_snapshot(self, snapshot):
        url = "{}snapshot/{}/restore".format(self.base_url,
                                             snapshot.snapshopt_id)
        response = post(url, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        return response.json()

    def add_access(self, volume, host, access_type=None):
        url = "{}access/{}".format(self.base_url, volume.identifier)
        data = {"to_address": host.address}
        if access_type:
            data['access_type'] = access_type
        response = post(url, json=data, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        return response.json()

    def get_snapshot_state(self, snapshot):
        url = "{}snapshot/{}/state".format(self.base_url,
                                           snapshot.snapshopt_id)
        response = get(url, headers=self.headers)
        if not response.ok:
            raise VolumeProviderGetSnapshotState(response.content, response)
        return response.json()['state']

    def _get_command(self, url, payload, exception_class):
        response = get(url, json=payload, headers=self.headers)
        if not response.ok:
            raise exception_class(response.content, response)
        return response.json()['command']

    def get_create_pub_key_command(self, host_ip):
        url = "{}commands/create_pub_key".format(self.base_url)
        return self._get_command(url, {'host_ip': host_ip},
                                 VolumeProviderCreatePubKeyCommand)

    def get_remove_pub_key_command(self, host_ip):
        url = "{}commands/remove_pub_key".format(self.base_url)
        return self._get_command(url, {'host_ip': host_ip},
                                 VolumeProviderRemovePubKeyCommand)

    def get_add_hosts_allow_command(self, host_ip):
        url = "{}commands/add_hosts_allow".format(self.base_url)
        return self._get_command(url, {'host_ip': host_ip},
                                 VolumeProviderAddHostAllowCommand)

    def get_remove_hosts_allow_command(self, host_ip):
        url = "{}commands/remove_hosts_allow".format(self.base_url)
        return self._get_command(url, {'host_ip': host_ip},
                                 VolumeProviderRemoveHostAllowCommand)

    def remove_access(self, volume, host):
        url = "{}access/{}/{}".format(self.base_url, volume.identifier,
                                      host.address)
        response = delete(url, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        return response.json()

    def get_mount_command(self, volume, data_directory="/data", fstab=True):
        url = "{}commands/{}/mount".format(self.base_url, volume.identifier)
        data = {
            'with_fstab': fstab,
            'data_directory': data_directory,
            'host_vm': self.host_vm.name,
            'host_zone': self.host_vm.zone
        }
        response = post(url, json=data, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        return response.json()['command']

    def get_copy_files_command(self,
                               snapshot,
                               source_dir,
                               dest_dir,
                               snap_dir=''):
        # snap = volume.backups.order_by('created_at').first()
        url = "{}commands/copy_files".format(self.base_url)
        data = {
            'snap_identifier': snapshot.snapshopt_id,
            'source_dir': source_dir,
            'dest_dir': dest_dir,
            'snap_dir': snap_dir
        }
        response = post(url, json=data, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        return response.json()['command']

    def get_scp_from_snapshot_command(self, snapshot, source_dir, dest_ip,
                                      dest_dir):
        url = "{}snapshots/{}/commands/scp".format(self.base_url,
                                                   snapshot.snapshopt_id)
        data = {
            'source_dir': source_dir,
            'target_ip': dest_ip,
            'target_dir': dest_dir
        }
        response = get(url, json=data, headers=self.headers)
        if not response.ok:
            raise VolumeProviderScpFromSnapshotCommand(response.content,
                                                       response)
        return response.json()['command']

    def get_umount_command(self, volume, data_directory="/data"):
        url = "{}commands/{}/umount".format(self.base_url, volume.identifier)
        data = {'data_directory': data_directory}
        response = post(url, json=data, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        return response.json()['command']

    def clean_up(self, volume):
        url = "{}commands/{}/cleanup".format(self.base_url, volume.identifier)
        response = get(url, headers=self.headers)
        if not response.ok:
            raise IndexError(response.content, response)
        command = response.json()['command']
        if command:
            self.run_script(command)

    def do(self):
        raise NotImplementedError

    def undo(self):
        pass
Exemplo n.º 10
0
 def __init__(self, instance=None):
     super(VipProviderStep, self).__init__(instance)
     self.credentials = None
     self._provider = None
     self.host_prov_client = HostProviderClient(self.environment)
Exemplo n.º 11
0
 def __init__(self, instance=None):
     super(VipProviderStep, self).__init__(instance)
     self.credentials = None
     self._provider = None
     self.host_prov_client = HostProviderClient(self.environment)
Exemplo n.º 12
0
class VipProviderStep(BaseInstanceStep):
    def __init__(self, instance=None):
        super(VipProviderStep, self).__init__(instance)
        self.credentials = None
        self._provider = None
        self.host_prov_client = HostProviderClient(self.environment)

    @property
    def provider(self):
        if not self._provider:
            self._provider = Provider(self.instance, self.environment)
        return self._provider

    @property
    def vm_properties(self):
        if not (hasattr(self, '_vm_properties') and self._vm_properties):
            self._vm_properties = self.host_prov_client.get_vm_by_host(
                self.host)
        return self._vm_properties

    @property
    def equipments(self):
        equipments = []
        ## TODO CHECK
        for instance in self.infra.instances.filter(future_instance=None):
            host = instance.hostname
            if host.future_host:
                host = host.future_host
            vm_info = self.host_prov_client.get_vm_by_host(host)
            equipment = {
                'host_address': host.address,
                'port': instance.port,
                'identifier': vm_info.identifier,
                'zone': vm_info.zone,
                'group': vm_info.group,
                'name': vm_info.name
            }
            equipments.append(equipment)
        return equipments

    @property
    def team(self):
        # @TODO
        return "dbaas"
        if self.has_database:
            return self.database.team.name
        return self.create.team.name

    @property
    def current_vip(self):
        vip = Vip.objects.filter(infra=self.infra)
        if vip.exists():
            return vip.last()

        return None

    @property
    def current_instance_group(self):
        if not self.current_vip:
            return None

        return VipInstanceGroup.objects.filter(vip=self.current_vip)

    def do(self):
        raise NotImplementedError

    def undo(self):
        pass