Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide the ability to upload patches #78 #104

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public interface ServerInfo
{
/**
* Verify if the path to a specific server is found. If a server path is provided in the XWiki configurations, it
* verifies if the path corresponds to a server. Otherwise, it searches the server location in system properties
* and system environment.
* verifies if the path corresponds to a server. Otherwise, it searches the server location in system properties and
* system environment.
*
* @return {@code true} if the server is used, {@code false} otherwise.
*/
Expand All @@ -62,6 +62,13 @@ public interface ServerInfo
*/
String getXwikiCfgFolderPath();

/**
* Access the path to the XWiki installation folder.
*
* @return the path to the XWiki installation folder.
*/
String getXWikiInstallFolderPath();

/**
* Update the possible paths to the configuration files.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.xwiki.admintools.jobs;

import java.util.List;

import org.xwiki.job.AbstractRequest;
import org.xwiki.stability.Unstable;

/**
* Represents a request to start a package upload job.
*
* @version $Id$
* @since 1.1
*/
@Unstable
public class PackageUploadJobRequest extends AbstractRequest
{
private String fileRef;

/**
* Default constructor.
*/
public PackageUploadJobRequest()
{
setDefaultId();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we should do this. If we do have multiple jobs with the same id, we should be mindful that only one upload job should run at a time. Im not sure what is the behaviour of the job manager/executor if you want to execute two jobs with the same id concurrently. If you can, the job logs will probably be overwritten in the permanent directory. The job progress might also act up.

On a second thought, one might not be interested to save ALL the logs of a potentially really big number of uploads (depends on your use case). If Admin tools is installed as root, it should be ok to have only one id for all the upload jobs. Otherwise, it might be better to have an id per wiki. But i still think we should take precaution and not run these jobs simultaneously.

Copy link
Collaborator Author

@ChiuchiuSorin ChiuchiuSorin Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admin tools is used as root, and for the upload jobs that I'm creating I'm using the attachment reference and startTime of the job to create the job ID, this way the jobs are unique. If you think it would be safer to ensure that only one job at a time is running, I could create a general ID and check if there is any job running for said ID. The issue with this is that the logs will be overwritten. To prevent multiple jobs with the same file reference running at the same time, I'm using JobGroupPath which will prevent two jobs with the same file reference to run at the same time.

}

/**
* Creates a specific request for package upload job.
*
* @param fileRef the attachment reference.
* @param jobId the ID of the request.
*/
public PackageUploadJobRequest(String fileRef, List<String> jobId)
{
this.fileRef = fileRef;
setId(jobId);
}

/**
* Get the attachment reference.
*
* @return a {@link String} representing the attachment reference.
*/
public String getFileRef()
{
return this.fileRef;
}

private void setDefaultId()
{
setId(List.of("adminTools", "import"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.xwiki.admintools.jobs;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

import org.xwiki.job.DefaultJobStatus;
import org.xwiki.logging.LoggerManager;
import org.xwiki.observation.ObservationManager;
import org.xwiki.stability.Unstable;

/**
* The status of a package upload job.
*
* @version $Id$
* @since 1.1
*/
@Unstable
public class PackageUploadJobStatus extends DefaultJobStatus<PackageUploadJobRequest>
{
private List<JobResult> uploadLogs = new LinkedList<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* Create a new import job status.
*
* @param jobType the job type.
* @param request the request provided when the job was started.
* @param observationManager the observation manager.
* @param loggerManager the logger manager.
*/
public PackageUploadJobStatus(String jobType, PackageUploadJobRequest request,
ObservationManager observationManager, LoggerManager loggerManager)
{
super(jobType, request, null, observationManager, loggerManager);
setCancelable(true);
}

/**
* Get the list result list from the job.
*
* @return list with {@link JobResult} containing the results.
*/
public List<JobResult> getJobResults()
{
return uploadLogs;
}

/**
* Add a new log to the job results.
*
* @param statusLog the new log result.
*/
public void addLog(JobResult statusLog)
{
uploadLogs.add(statusLog);
}

/**
* Check if any job result has a specific level of severity.
*
* @param level represents the searched level of severity.
* @return {@code true} if there is any match for the given level, or {@code false} otherwise.
*/
public boolean hasLevel(JobResultLevel level)
{
return this.uploadLogs.stream().anyMatch(checkResult -> Objects.equals(level, checkResult.getLevel()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.xwiki.rest.XWikiRestComponent;
import org.xwiki.rest.XWikiRestException;
import org.xwiki.stability.Unstable;

/**
* Provides the APIs needed by the Admin Tools server in order to download and view configuration files and logs.
Expand Down Expand Up @@ -66,4 +68,19 @@ public interface AdminToolsResource extends XWikiRestComponent
@POST
@Path("/flushCache")
Response flushCache() throws XWikiRestException;

/**
* Start a package upload job based on the attachment reference that was sent.
*
* @param attachReference the reference of the attachment.
* @param startTime the start time of the request.
* @return HTML status code 202 to hint that the upload job has started.
* @throws XWikiRestException if an error occurred while creating the job.
* @since 1.1
*/
@POST
@Path("/import")
@Unstable
Response uploadPackageArchive(@QueryParam("attach") String attachReference,
@QueryParam("startTime") String startTime) throws XWikiRestException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.xwiki.admintools.uploadPackageJob;

import java.io.ByteArrayOutputStream;
import java.io.File;

import org.xwiki.stability.Unstable;

/**
* Store data for package upload job process.
*
* @version $Id$
* @since 1.1
*/
@Unstable
public class UploadPackageJobResource
{
private File targetFile;

private File backupFile;

private ByteArrayOutputStream newFileContent;

private String newFileName;

private boolean uploaded;

/**
* Default constructor.
*/
public UploadPackageJobResource()
{
uploaded = false;
}

/**
* Get the content of the new file.
*
* @return the content of the new file.
*/
public ByteArrayOutputStream getNewFileContent()
{
return newFileContent;
}

/**
* See {@link #getNewFileContent()}.
*
* @param newFileContent the new file content.
*/
public void setNewFileContent(ByteArrayOutputStream newFileContent)
{
this.newFileContent = newFileContent;
}

/**
* Get the backup file.
*
* @return the backup file.
*/
public File getBackupFile()
{
return backupFile;
}

/**
* See {@link #getBackupFile()}.
*
* @param backupFile the backup file.
*/
public void setBackupFile(File backupFile)
{
this.backupFile = backupFile;
}

/**
* Get the file that will be replaced or saved.
*
* @return the update target file.
*/
public File getTargetFile()
{
return targetFile;
}

/**
* See {@link #getTargetFile()}.
*
* @param targetFile the update target file.
*/
public void setTargetFile(File targetFile)
{
this.targetFile = targetFile;
}

/**
* Get the new file name.
*
* @return the name of the new file.
*/
public String getNewFileName()
{
return newFileName;
}

/**
* See {@link #getNewFileName()}.
*
* @param newFileName the name of the new file.
*/
public void setNewFileName(String newFileName)
{
this.newFileName = newFileName;
}

/**
* Set the upload flag to {@code true}.
*/
public void setUploaded()
{
this.uploaded = true;
}

/**
* Get the upload flag.
*
* @return the upload flag.
*/
public boolean isUploaded()
{
return uploaded;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public abstract class AbstractServerInfo implements ServerInfo

protected String[] xwikiCfgPossiblePaths;

protected String[] xwikiInstallPossiblePaths;

@Inject
@Named("default")
protected AdminToolsConfiguration adminToolsConfig;
Expand Down Expand Up @@ -74,4 +76,15 @@ public String getXwikiCfgFolderPath()
}
return null;
}

@Override
public String getXWikiInstallFolderPath()
{
for (String xwikiLibraryFolderPath : this.xwikiInstallPossiblePaths) {
if ((new File(xwikiLibraryFolderPath)).isDirectory()) {
return xwikiLibraryFolderPath;
}
}
return null;
}
}
Loading