Ejemplo n.º 1
0
def install_agent(request, server_id):
    server = Server.objects.get(id=server_id)
    veeam = VeeamManager()
    if request.method == 'GET':
        content = _(
            "Are you sure you want to install Veeam Agent on this server?")
        return {
            'title':
            _("Install Agent?"),
            'content':
            content,
            'use_ajax':
            True,
            'action_url':
            '/veeam/install_agent/{server_id}/'.format(server_id=server_id),
            'submit':
            _("Install"),
        }
    if request.method == 'POST':
        try:
            install_agent_action = CloudBoltHook.objects.get(
                name="Install Veeam Agent")
        except Exception:
            veeam.setup_veeam_install_agent_action()
            install_agent_action = CloudBoltHook.objects.get(
                name="Install Veeam Agent")
        install_job = install_agent_action.run_as_job(server=server)[0]
        messages.success(
            request,
            mark_safe(
                f"<a href='{install_job.get_absolute_url()}'>Job {install_job.id}</a> to install agent "
                f"started"))

    return HttpResponseRedirect(reverse('server_detail', args=[server_id]))
Ejemplo n.º 2
0
def take_backup(request, server_id):
    server = Server.objects.get(id=server_id)
    if request.method == 'GET':
        content = _("Are you sure you want to take a backup for this server?")
        return {
            'title':
            _("Take Backup"),
            'content':
            content,
            'use_ajax':
            True,
            'action_url':
            '/veeam/take_backup/{server_id}/'.format(server_id=server_id),
            'submit':
            _("Take"),
        }
    if request.method == 'POST':
        veeam_manager = VeeamManager()
        try:
            take_backup_action = CloudBoltHook.objects.get(
                name="Take Veeam Backup")
        except Exception:
            veeam_manager.setup_take_backup_action()
            take_backup_action = CloudBoltHook.objects.get(
                name="Take Veeam Backup")

        install_job = take_backup_action.run_as_job(server=server)[0]
        messages.success(
            request,
            mark_safe(
                f"<a href='{install_job.get_absolute_url()}'>Job</a> to take backup started"
            ))

    return HttpResponseRedirect(reverse('server_detail', args=[server_id]))
Ejemplo n.º 3
0
def run(server, *args, **kwargs):
    set_progress("Starting the Veeam take backup process...")
    veeam = VeeamManager()
    ci = veeam.get_connection_info()
    result = veeam.take_backup(CREATE_BACKUP, {
        'server': server,
        'veeam_server': ci
    })
    set_progress("Waiting for the backup process to complete...")
    if result.lower() == 'success':
        set_progress("Backup taken successfully")
        return "SUCCESS", "Backup taken successfully", ""
    else:
        set_progress("Backup not taken successfully")
        return "SUCCESS", "Backup not taken successfully", ""
Ejemplo n.º 4
0
def edit_veeam_endpoint(request, endpoint_id=None):
    """
    Create and edit dialog for a RH's NSX endpoint.
    If `endpoint_id` is None,
    """
    endpoint = VeeamManager().get_connection_info()
    action_url = reverse('create_veeam_endpoint')
    if endpoint or endpoint_id:
        action_url = reverse('edit_veeam_endpoint', args=[endpoint.id])
    if request.method == 'POST':
        form = VeeamEndpointForm(request.POST, instance=endpoint)
        if form.is_valid():
            form.save()
            msg = "The Veeam Server Management Endpoint settings have been saved."
            messages.success(request, msg)
            profile = request.get_user_profile()
            logger.info("Endpoint set to {} by {}.".format(
                endpoint, profile.user.username))
            return HttpResponseRedirect(request.META['HTTP_REFERER'])
    else:
        form = VeeamEndpointForm(instance=endpoint)

    return {
        'title': 'Modify Veeam Server Management Endpoint Settings',
        'form': form,
        'use_ajax': True,
        'action_url': action_url,
        'top_content':
        "Veeam Server Management Endpoint, Used to support advanced backup and restoration actions",
        'submit': 'Save',
    }
Ejemplo n.º 5
0
    def clean(self):
        try:
            from xui.veeam.veeam_admin import VeeamManager
            veeam = VeeamManager()
            veeam.verify_connection(
                self.cleaned_data.get('protocol'),
                self.cleaned_data.get('ip'),
                self.cleaned_data.get('port'),
                self.cleaned_data.get('username'),
                self.cleaned_data.get('password'),
            )
        except Exception as error:
            raise forms.ValidationError(
                f"Unable to connect to Veeam Server Management Endpoint using the parameters provided due to {error}"
            )

        return self.cleaned_data
Ejemplo n.º 6
0
def run(server, *args, **kwargs):
    set_progress("Starting install Veeam agent process...")
    veeam = VeeamManager()
    if server.is_windows():
        try:
            output = veeam.install_agent(INSTALL_WINDOWS, {'server': server})
        except Exception as error:
            set_progress(
                f"The following error occurred while trying to install the agent. Error: {error}"
            )
            return "FAILURE", "Agent not installed", f"{error}"

    elif server.os_family.get_base_name() == 'Linux':
        try:
            output = veeam.install_agent(INSTALL_LINUX, {'server': server})
        except Exception as error:
            set_progress(
                f"The following error occurred while trying to install the agent. Error: {error}"
            )
            return "FAILURE", "Agent not installed", f"{error}"
    else:
        set_progress("Veeam Agent can't be installed on this server")
        return "FAILURE", "Agent not installed", "OS is currently not supported"

    output = output.split('\n')

    CustomField.objects.get_or_create(
        name='veeam_agent_id',
        type='STR',
        defaults={
            'label': 'Veeam Agent ID',
            'description': 'Veeam Agent ID that has been installed',
            'show_as_attribute': True
        })

    veeam_agent_id = [
        x.split(':')[-1].strip() for x in output
        if x.split(':')[0].strip() == 'Id'
    ][-1]
    if veeam_agent_id:
        server.veeam_agent_id = veeam_agent_id
        server.save()
        set_progress("Agent installed successfully")
        return "SUCCESS", "Agent Installed", ""
Ejemplo n.º 7
0
def run(server, *args, **kwargs):
    set_progress(f"Starting Veeam Backup restoration... ")
    veeam_manager = VeeamManager()
    context = {}
    context.update(kwargs.get('script_context'))

    connection_info = veeam_manager.get_connection_info()
    context.update({'connection_info': connection_info})

    try:
        set_progress(
            "Starting backup restoration to EC2. This might take a while...")
        result = veeam_manager.restore_backup_to_cloud(template=RESTORE_TO_EC2,
                                                       context=context)
        set_progress(f"Result from the backup restoration {result}")

    except Exception as error:
        set_progress("Error occurred while trying to restore backup to ec2")
        return "FAILURE", "", f"{error}"
Ejemplo n.º 8
0
def refresh_agent(request, server_id):
    """
    Checks if the server specified has a veeam agent
    """
    veeam = VeeamManager()

    server = Server.objects.get(id=server_id)
    # Start a Job to do the refresh

    server = veeam.refresh_server(server, {
        'server': server,
        'veeam_server': veeam.get_connection_info()
    })

    if server.veeam_agent_id:
        messages.success(request, "Veeam Agent Found")
    else:
        messages.warning(request, "No Veeam Agent Found")
    return HttpResponseRedirect(reverse('server_detail', args=[server_id]))
Ejemplo n.º 9
0
def delete_veeam_endpoint(request):
    endpoint = VeeamManager().get_connection_info()
    if request.method == 'POST':
        endpoint.delete()
        msg = "The Veeam Server Endpoint has been deleted."
        messages.success(request, msg)
        return HttpResponseRedirect(request.META['HTTP_REFERER'])

    return {
        'title':
        'Remove Veeam Server Manager Endpoint?',
        'content':
        'Are you sure you want to delete Veeam Server endpoint \'{}\'?'.format(
            endpoint),
        'use_ajax':
        True,
        'action_url':
        reverse('delete_veeam_endpoint'),
        'submit':
        'Remove'
    }
Ejemplo n.º 10
0
def verify_veeam_endpoint(request):
    veeam = VeeamManager()
    endpoint = veeam.get_connection_info()
    if not endpoint:
        messages.warn(request, "No Veeam Endpoint found! Nothing to verify")
        return HttpResponseRedirect(request.META['HTTP_REFERER'])
    try:
        veeam.verify_connection()
    except Exception as err:
        msg = format_html(
            'Could not make a connection to the Veeam Server Manager at'
            '<b>"{}"</b>:<br>{}', endpoint, str(err))
    else:
        msg = format_html(
            'Successfully connected to the Veeam Server Manager at '
            '<b>"{}"</b>.', endpoint)

    return {
        'title': 'Verify connection to Veeam Server Manager Endpoint',
        'content': msg,
        'submit': None,
        'cancel': "OK",
    }
Ejemplo n.º 11
0
def run(server, *args, **kwargs):
    set_progress(f"Starting Veeam Backup restoration... ")
    veeam = VeeamManager()
    server_ci = veeam.get_connection_info()
    url = f'http://{server_ci.ip}:9399/api/vmRestorePoints/' + \
          kwargs.get('restore_point_href') + '?action=restore'
    session_id = veeam.get_veeam_server_session_id()
    header = {"X-RestSvcSessionId": session_id}

    response = requests.post(url=url, headers=header)
    task = minidom.parseString(response.content.decode('utf-8'))
    items = task.getElementsByTagName('Task')[0].attributes.items()
    restoration_url = [item for item in items if item[0] == 'Href'][0][-1]

    def check_state():

        response = requests.get(restoration_url, headers=header)

        dom = minidom.parseString(response.content.decode('utf-8'))
        state = dom.getElementsByTagName('State')[0]
        child = state.firstChild
        return child

    # Wait until the restoration to completed.

    while check_state().data == 'Running':
        # wait
        set_progress("Waiting for restoration to complete...")
        time.sleep(10)

    if check_state().data == 'Finished':
        set_progress("Server restoration completed successfully")
        return "SUCCESS", "Server restoration completed successfully", ""
    else:
        set_progress("Server restoration didn't complete successfully")
        return "FAILURE", "", "Server restoration didn't complete successfully"
Ejemplo n.º 12
0
def admin_page(request):
    veeam = VeeamManager()
    endpoint = veeam.get_connection_info()
    # If no Connection info, show a dialog for adding a connection info.
    if not endpoint:
        veeam_context = {
            'tabs':
            TabGroup(
                template_dir='veeam/templates',
                context={},
                request=request,
                tabs=[(_(""), 'dashboard', dict(context={}))],
            )
        }
        return render(request, 'veeam/templates/admin_page.html',
                      veeam_context)

    jobs = veeam.get_jobs()
    backups = veeam.get_backups()
    summary = veeam.get_summary()

    context = {}
    ip = veeam.get_connection_info().ip
    try:
        server = Server.objects.get(ip=ip)
    except Exception:
        messages.error(request,
                       message="The server running Veeam could not be found.")
        return render(request, 'veeam/templates/admin_page.html', {})

    restore_to_ec2_job = server.jobs.filter(
        job_parameters__hookparameters__hook__name='Restore Veeam Backup To EC2'
    ).last()

    restore_to_ec2_job_running = False
    if restore_to_ec2_job and restore_to_ec2_job.is_active():
        restore_to_ec2_job_running = True
        context.update(
            {'restore_to_ec2_job_url': restore_to_ec2_job.get_absolute_url()})
    context.update({'restore_to_ec2_job_running': restore_to_ec2_job_running})

    restore_to_azure_job = server.jobs.filter(
        job_parameters__hookparameters__hook__name=
        'Restore Veeam Backup To Azure').last()
    restore_to_azure_job_running = False
    if restore_to_azure_job and restore_to_azure_job.is_active():
        restore_to_azure_job_running = True
        context.update({
            'restore_to_azure_job_url':
            restore_to_azure_job.get_absolute_url()
        })
    context.update(
        {'restore_to_azure_job_running': restore_to_azure_job_running})

    context.update({'jobs': jobs, 'backups': backups, 'endpoint': endpoint})

    veeam_context = {
        'tabs':
        TabGroup(
            template_dir='veeam/templates',
            context=context,
            request=request,
            tabs=[
                # First tab uses template 'groups/tabs/tab-main.html'
                # (_("Configuration"), 'configuration', {}),
                # Tab 2 is conditionally-shown in this slot and
                # uses template 'groups/tabs/tab-related-items.html'
                (_("Dashboard"), 'dashboard', dict(context=summary)),
                (_("Jobs"), 'jobs', {}),
                (_("Backups"), 'backups', {})
            ],
        )
    }
    return render(request, 'veeam/templates/admin_page.html', veeam_context)
Ejemplo n.º 13
0
def veeam_tab(request, obj_id):
    server = Server.objects.get(id=obj_id)
    veeam = VeeamManager()

    restore_points = veeam.get_restore_points(server.hostname)
    if restore_points:
        restore_points.sort(key=lambda r: datetime.strptime(
            r.get('time'), '%Y-%m-%d %H:%M:%S'),
                            reverse=True)
    is_agent_installed = veeam.should_install_agent(server)

    take_backup_job = server.jobs.filter(
        job_parameters__hookparameters__hook__name='Take Veeam Backup').last()

    context = {}

    backup_job_running = False
    if take_backup_job:
        if take_backup_job.is_active():
            backup_job_running = True
            context.update(
                {'take_backup_job_url': take_backup_job.get_absolute_url()})
    context.update({'backup_job_running': backup_job_running})

    if not context:
        # Check if server is powered on.
        if server.power_status == 'POWERON':
            # This server has no agent installed
            context.update({'install_agent': False})
            context.update({'power_status': True})
        else:
            context.update({'power_status': False})
            status = 'warning'
            msg = "Veeam might be installed in this server but the server is powered off"

            server_not_powered_on = helper_tags.alert(status, msg)
            context.update({'server_not_powered_on': server_not_powered_on})
    else:
        context.update({'install_agent': True})

    context.update({'server_id': obj_id})
    server_settings_ok = check_server_settings_status(server)

    if not server_settings_ok:
        status = 'warning'
        msg = "Veeam agent is not installed and the server username and password are not correctly setup. This might make it imposible to install the agent on this server from cloudbolt. You can configure them on the Configuration page on the server details tab. "

        server_credentials_not_set = helper_tags.alert(status, msg)
        context.update(
            {'server_credentials_not_set': server_credentials_not_set})

    install_job = server.jobs.filter(
        job_parameters__hookparameters__hook__name='Install Veeam Agent').last(
        )

    install_job_running = False
    if install_job:
        if install_job.is_active():
            install_job_running = True
            context.update({'job_url': install_job.get_absolute_url()})
    context.update({'job_running': install_job_running})

    restore_job = server.jobs.filter(
        job_parameters__hookparameters__hook__name='Restore Veeam Backup'
    ).last()
    restore_job_running = False
    if restore_job:
        if restore_job.is_active():
            restore_job_running = True
            context.update({'restore_job_url': restore_job.get_absolute_url()})
    context.update({'restore_job_running': restore_job_running})

    context.update({
        'server': server,
        'restore_points': restore_points,
        'install_agent': is_agent_installed
    })
    return render(request, 'veeam/templates/server_tab.html', context)
Ejemplo n.º 14
0
def restore_backup_to_ec2_cloud(request, backup_name):
    if request.method == 'GET':
        content = _("Provide the information below to restore to EC2")
        form = EC2RestoreForm()
        return {
            'title':
            _("Restore Backup"),
            'content':
            content,
            'form':
            form,
            'use_ajax':
            True,
            'action_url':
            reverse('restore_to_ec2_cloud',
                    kwargs={"backup_name": backup_name}),
            'submit':
            _("Restore"),
            'extra_onready_js':
            mark_safe(
                """
                                        $('#div_id_environment').on('change',(function () {
                                        var env_id = $('#div_id_environment option:selected').val();
                                        $.getJSON('%s?env_id='+env_id, function (vpc_ids) {
                                            var $selectElement = $("#id_vpc_id");
                                            $selectElement.empty();
                                            $.each(vpc_ids, function (key, value) {
                                                $selectElement.append($("<option></option>").attr("value", value).text(value));
                                            });
                                        });
                                        $.getJSON('%s?env_id='+env_id, function (sg_ids) {
                                            var $securityGroupElement = $("#id_sgroup_name");
                                            $securityGroupElement.empty();
                                            $.each(sg_ids, function (key, value) {
                                                $securityGroupElement.append($("<option></option>").attr("value", value).text(value));
                                            });
                                        });
                                        $.getJSON('%s?env_id='+env_id, function (availability_zones) {
                                            var $availabilityZoneElement = $("#id_availability_zone");
                                            $availabilityZoneElement.empty();
                                            $.each(availability_zones, function (key, value) {
                                                $availabilityZoneElement.append($("<option></option>").attr("value", value).text(value));
                                            });
                                        });
                                    })).change();
                            """ %
                (reverse('get_aws_vpc'), reverse('get_aws_security_groups'),
                 reverse('get_aws_availability_zones')))
        }
    if request.method == 'POST':
        veeam_manager = VeeamManager()

        form = EC2RestoreForm(request.POST)
        # Since the choices fields have not been declared during form creation, we need to dynamically declare them.
        vpc_id = request.POST.get('vpc_id')
        sgroup_name = request.POST.get('sgroup_name')
        availability_zone = request.POST.get('availability_zone')

        form.fields['vpc_id'].choices = [(vpc_id, vpc_id)]
        form.fields['sgroup_name'].choices = [(sgroup_name, sgroup_name)]
        form.fields['availability_zone'].choices = [(availability_zone,
                                                     availability_zone)]

        if form.is_valid():
            environment = form.cleaned_data['environment']
            env = Environment.objects.get(id=environment)
            resource_handler = env.resource_handler

            context = {
                'vm_name': form.cleaned_data['vm_name'],
                'environment': environment,
                'backup_name': backup_name,
                'amazon_access_key': resource_handler.serviceaccount,
                'region_name': env.aws_region,
                'region_type': form.cleaned_data['region_type'],
                'disk_type': form.cleaned_data['disk_type'],
                'instance_type': form.cleaned_data['instance_type'],
                'license_type': form.cleaned_data['license_type'],
                'vpc_id': form.cleaned_data['vpc_id'],
                'sgroup_name': form.cleaned_data['sgroup_name'],
                'reason': form.cleaned_data['reason'],
                'availability_zone': form.cleaned_data['availability_zone'],
            }
            try:
                restore_backup_action = CloudBoltHook.objects.get(
                    name="Restore Veeam Backup To EC2")
            except Exception:
                veeam_manager.setup_restore_backup_to_ec2__action()
                restore_backup_action = CloudBoltHook.objects.get(
                    name="Restore Veeam Backup To EC2")

            # Server running this job will be the veeam server. We can find it using the IP address in the connection Info
            ip = veeam_manager.get_connection_info().ip
            try:
                server = Server.objects.get(ip=ip)
            except Exception as error:
                # No server associated with the connection info IP address exists.
                messages.error(request, "The Veeam server could not be found.")
                return HttpResponseRedirect(
                    request.META.get('HTTP_REFERER', '/'))

            restore_backup_job = restore_backup_action.run_as_job(
                server=server, script_context=context)[0]
            messages.success(
                request,
                mark_safe(
                    f"<a href='{restore_backup_job.get_absolute_url()}'>Job {restore_backup_job.id}</a> to restore backup started"
                ))
        else:
            raise Exception(form.errors)

        return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
Ejemplo n.º 15
0
 def should_display(self):
     veeam = VeeamManager()
     if not veeam.get_connection_info():
         return False
     return True
Ejemplo n.º 16
0
def restore_backup_to_cloud(request, backup_name):
    if request.method == 'GET':
        content = _(
            "Provide the information below to restore to Microsoft Azure.")
        form = AzureRestoreForm()
        return {
            'title':
            _("Restore Backup"),
            'content':
            content,
            'form':
            form,
            'use_ajax':
            True,
            'action_url':
            '/veeam/restore_backup_to_cloud/{backup_name}/'.format(
                backup_name=backup_name),
            'submit':
            _("Restore"),
        }
    if request.method == 'POST':
        veeam_manager = VeeamManager()
        form = AzureRestoreForm(request.POST)
        if form.is_valid():
            context = {
                'vmname': form.cleaned_data['vm_name'],
                'backup_name': backup_name,
                'network_name': form.cleaned_data['network_name'],
                'vm_size': form.cleaned_data['vm_size'],
                'location': form.cleaned_data['location'],
                'storage_account': form.cleaned_data['storage_account'],
                'resource_group': form.cleaned_data['resource_group'],
            }
            try:
                restore_backup_action = CloudBoltHook.objects.get(
                    name="Restore Veeam Backup To Azure")
            except Exception:
                veeam_manager.setup_restore_backup_to_azure_action()
                restore_backup_action = CloudBoltHook.objects.get(
                    name="Restore Veeam Backup To Azure")

                # Server running this job will be the veeam server. We can find it using the IP address in the
                # connection Info
            ip = veeam_manager.get_connection_info().ip
            try:
                server = Server.objects.get(ip=ip)
            except Exception:
                # No server associated with the connection info IP address exists.
                messages.error(request, "The Veeam server could not be found.")
                return HttpResponseRedirect(
                    request.META.get('HTTP_REFERER', '/'))

            restore_backup_to_azure_job = restore_backup_action.run_as_job(
                server=server, script_context=context)[0]
            messages.success(
                request,
                mark_safe(
                    f"<a href='{restore_backup_to_azure_job.get_absolute_url()}'>Job {restore_backup_to_azure_job.id}</a> to restore backup started"
                ))
        else:
            raise Exception(form.errors)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))