Skip to content

Commit 0765f91

Browse files
committed
Gpt4all Custom Updater
This PR establishes the initial infrastructure required to migrate gpt4all away from reliance on the IFW framwork by implementing a custom updater. This custom updater (currently headless only) can perform the same operations as the existing updater with the option for more functionality to be added and most importantly, is installer agnostic, meaning gpt4all can start to leverage platform specific installers. Implements both offline and online installation and update mechanisms. Initial implementation of: #2878 Signed-off-by: John Parent <[email protected]>
1 parent 9cafd38 commit 0765f91

34 files changed

+1252
-0
lines changed

gpt4all-updater/CMakeLists.txt

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
set(CMAKE_CXX_STANDARD 20)
4+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5+
6+
set(APP_VERSION_MAJOR 0)
7+
set(APP_VERSION_MINOR 0)
8+
set(APP_VERSION_PATCH 0)
9+
10+
set(APP_VERSION ${APP_VERSION_MAJOR}.${APP_VERSION_MINOR}.${APP_VERSION_PATCH})
11+
12+
project(Gpt4AllAutoUpdater VERSION ${APP_VERSION} LANGUAGES CXX)
13+
14+
option(BUILD_OFFLINE_UPDATER "Build an offline updater" OFF)
15+
16+
if(BUILD_OFFLINE_UPDATER AND NOT GPT4ALL_INSTALLER_PATH)
17+
message(FATAL_ERROR "The path to GPT4ALL's installer is required to construct this updater.
18+
Please provide it on the command line using the argument -DGPT4ALL_INSTALLER_PATH=<pth>")
19+
endif()
20+
21+
if(NOT BUILD_OFFLINE_UPDATER AND NOT GPT4ALL_MANIFEST_ENDPOINT)
22+
message(FATAL_ERROR "The manifest endpoint was not provided, the online installer will be unable to detect updates")
23+
endif()
24+
25+
if(APPLE)
26+
option(BUILD_UNIVERSAL "Build universal binary on MacOS" OFF)
27+
if(BUILD_UNIVERSAL)
28+
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
29+
else()
30+
set(CMAKE_OSX_ARCHITECTURES "${CMAKE_HOST_SYSTEM_PROCESSOR}" CACHE STRING "" FORCE)
31+
endif()
32+
endif()
33+
34+
if(APPLE AND BUILD_OFFLINE_UPDATER)
35+
enable_language(ASM)
36+
configure_file(src/asm/bake_installer.S.in ${CMAKE_BINARY_DIR}/bake_installer.S @ONLY)
37+
set(ASSEMBLER_SOURCES ${CMAKE_BINARY_DIR}/bake_installer.S src/Embedded.cxx)
38+
elseif(WIN32 AND BUILD_OFFLINE_UPDATER)
39+
configure_file(src/resources/installer.rc.in ${CMAKE_BINARY_DIR}/resource.rc @ONLY)
40+
set(RC_FILES ${CMAKE_BINARY_DIR}/resource.rc src/Resource.cxx)
41+
endif()
42+
43+
if(NOT BUILD_OFFLINE_UPDATER)
44+
configure_file(src/Download.cxx.in ${CMAKE_BINARY_DIR}/Download.cxx @ONLY)
45+
endif()
46+
47+
find_package(Qt6 REQUIRED COMPONENTS Core Network)
48+
49+
set(auto_updater_sources
50+
src/Command.cxx
51+
src/CommandFactory.cxx
52+
src/CommandLine.cxx
53+
src/Downgrade.cxx
54+
${CMAKE_BINARY_DIR}/Download.cxx
55+
src/Manifest.cxx
56+
src/Modify.cxx
57+
src/Package.cxx
58+
src/State.cxx
59+
src/Uninstall.cxx
60+
src/Update.cxx
61+
src/utils.cxx
62+
src/main.cxx
63+
${ASSEMBLER_SOURCES}
64+
${RC_FILES}
65+
)
66+
67+
add_executable(autoupdater ${auto_updater_sources})
68+
target_link_libraries(autoupdater PRIVATE Qt6::Core Qt6::Network)
69+
target_include_directories(autoupdater PRIVATE include)
70+
71+
if(BUILD_OFFLINE_UPDATER)
72+
target_compile_definitions(autoupdater PRIVATE OFFLINE)
73+
endif()

gpt4all-updater/README

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Gpt4All Updater
2+
3+
## Testing
4+
5+
Testing this updater requires a bit of manual setup and walking through the
6+
integration with gpt4all's installer, as this process has yet to be fully automated.
7+
8+
There are two modes, offline and online, each is tested differently:
9+
10+
### Online
11+
12+
The online updater workflow takes a url endpoint (for early testing, this will be a file url) that points to a manifest file, a sample of which has been provided under the `tmp` directory relative to this README. The manifest file includes, amount other things specified in the updater RFC, another file URL to a gpt4all installer.
13+
14+
The first thing testing this will require is a manual update of that file url in the manifest xml file to point to the DMG on MacOS or exe on Windows, of an existing online Gpt4All installer, as well as a corresponding sha256 sum of the given installer. The manifest is filled with other stub info for testing, you're welcome to leave it as is or fill it out correctly for each testing iteration.
15+
16+
That is all that is required for configuration. Now simply build this project via CMake, using standard CMake build practices. CMake will build the online installer by default, so no extra arguments are required.
17+
18+
One argument is required for the online installer, the url where the updater should expect the manifest file to be. This can be any url accessible to your system, but for simplicity and testing reasons, its usually best to use a file url.
19+
This is provided via the cmake command line argument `-DGPT4ALL_MANIFEST_ENDPOINT=<url>`.
20+
21+
Now configure and build the updater with CMake.
22+
23+
To test the installer, query the command line interface using the `--help` argument to determine which actions are available. Then select a given action, and provide the required arguments (there shouldn't be any at the moment), and let the updater drive. The updater will determine the operation requested, fetch the appropriate installer, and drive said installer with the appropriate arguments to effect the requested operation. If the operation is a modification or uninstall, the updater will not fetch a new installer, and instead will execute the older installer, as a new installer is not required.
24+
25+
### Offline
26+
27+
The offline updater is somewhat simpler. To instruct CMake to build the offline updater, specify the `-DBUILD_OFFLINE_UPDATER=ON` argument to CMake on the command line. One additional argument is required to properly configure CMake for the offline updater project, `-DGPT4ALL_INSTALLER_PATH` which should be set to the path to the DMG file containing an offline version of the GPT4All installer.
28+
29+
Now, after building, simply run the updater. It should remove your current installation of Gpt4All (but not the config or models), and then run the offline installer in install mode. Once that process is complete, you should have an upgraded Gpt4All available on your system.
30+

gpt4all-updater/include/Command.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include <QCoreApplication>
3+
#include <QProcess>
4+
#include <QStringList>
5+
#include <QFile>
6+
7+
namespace gpt4all {
8+
namespace command{
9+
10+
class Command
11+
{
12+
public:
13+
virtual bool execute();
14+
QStringList arguments;
15+
QFile* installer;
16+
};
17+
}
18+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include "Command.h"
4+
#include "CommandLine.h"
5+
6+
7+
8+
class CommandFactory : public QObject
9+
{
10+
public:
11+
std::shared_ptr<gpt4all::command::Command> GetCommand(gpt4all::command::CommandType type);
12+
};

gpt4all-updater/include/CommandLine.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include <QCommandLineParser>
4+
5+
6+
namespace gpt4all {
7+
namespace command {
8+
9+
enum CommandType {
10+
UPDATE,
11+
MODIFY,
12+
DOWNGRADE,
13+
UNINSTALL
14+
};
15+
16+
class CommandLine : public QObject
17+
{
18+
public:
19+
CommandLine();
20+
~CommandLine();
21+
void parse(QCoreApplication &app);
22+
CommandType command();
23+
private:
24+
CommandType type;
25+
QCommandLineParser * parser;
26+
};
27+
28+
}
29+
}
30+

gpt4all-updater/include/Downgrade.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "Command.h"
4+
5+
6+
namespace gpt4all {
7+
namespace downgrade {
8+
9+
class Downgrade : public gpt4all::command::Command
10+
{
11+
public:
12+
Downgrade(QFile &installer);
13+
};
14+
15+
16+
}
17+
}

gpt4all-updater/include/Download.h

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#pragma once
2+
3+
#include "Manifest.h"
4+
#include "State.h"
5+
6+
#include <QCryptographicHash>
7+
#include <QDateTime>
8+
#include <QFile>
9+
#include <QHash>
10+
#include <QList>
11+
#include <QMap>
12+
#include <QObject>
13+
#include <QtGlobal>
14+
#include <QVersionNumber>
15+
#include <QSslError>
16+
#include <QThread>
17+
#include <QString>
18+
#include <QNetworkAccessManager>
19+
#include <QNetworkReply>
20+
#include <QCoreApplication>
21+
22+
namespace gpt4all {
23+
namespace download {
24+
25+
class HashFile : public QObject
26+
{
27+
public:
28+
HashFile() : QObject(nullptr) {}
29+
void hashAndInstall(const QString &expected, QCryptographicHash::Algorithm algo, QFile *temp, const QString &file, QNetworkReply *reply);
30+
};
31+
32+
class Download : public QObject
33+
{
34+
public:
35+
static Download *instance();
36+
Q_INVOKABLE void driveFetchAndInstall();
37+
Q_INVOKABLE void downloadManifest();
38+
Q_INVOKABLE void downloadInstaller();
39+
Q_INVOKABLE void cancelDownload();
40+
Q_INVOKABLE void installInstaller(QString &expected, QFile *temp, QNetworkReply *installerResponse);
41+
42+
private Q_SLOTS:
43+
void handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
44+
void handleErrorOccurred(QNetworkReply::NetworkError code);
45+
void handleInstallerDownloadFinished();
46+
void handleReadyRead();
47+
48+
private:
49+
explicit Download();
50+
~Download();
51+
QNetworkReply * downloadInMemory(QUrl &url);
52+
QNetworkReply * downloadLargeFile(QUrl &url);
53+
QIODevice * handleManifestRequestResponse(QNetworkReply * reply);
54+
gpt4all::manifest::ManifestFile *manifest;
55+
QNetworkAccessManager m_networkManager;
56+
QMap<QNetworkReply*, QFile*> download_tracking;
57+
HashFile *saver;
58+
QDateTime m_startTime;
59+
QString platform_ext;
60+
QFile *downloadPath;
61+
friend class Downloader;
62+
};
63+
64+
}
65+
}

gpt4all-updater/include/Embedded.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <fstream>
5+
#include <sstream>
6+
7+
#include <QCoreApplication>
8+
#include <QFile>
9+
#include <QByteArray>
10+
11+
// Symbols indicating beginning and end of embedded installer
12+
extern "C" {
13+
extern char data_start_gpt4all_installer, data_stop_gpt4all_installer;
14+
extern int gpt4all_installer_size;
15+
}
16+
17+
namespace gpt4all {
18+
namespace embedded {
19+
20+
class EmbeddedInstaller : public QObject
21+
{
22+
public:
23+
EmbeddedInstaller();
24+
void extractAndDecode();
25+
void installInstaller();
26+
27+
private:
28+
char * start;
29+
char * end;
30+
int size;
31+
QByteArray data;
32+
};
33+
34+
35+
}
36+
}
37+

gpt4all-updater/include/Manifest.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#include "Package.h"
4+
5+
#include <QVersionNumber>
6+
#include <QDate>
7+
#include <QFile>
8+
#include <QXmlStreamReader>
9+
#include <QString>
10+
#include <QStringList>
11+
12+
namespace gpt4all{
13+
namespace manifest {
14+
15+
enum releaseType{
16+
RELEASE,
17+
DEBUG
18+
};
19+
20+
class ManifestFile {
21+
public:
22+
static ManifestFile * parseManifest(QIODevice *manifestInput);
23+
QUrl & getInstallerEndpoint();
24+
QString & getExpectedHash();
25+
private:
26+
ManifestFile() {}
27+
QString name;
28+
QVersionNumber release_ver;
29+
QString notes;
30+
QStringList authors;
31+
QDate release_date;
32+
releaseType config;
33+
QVersionNumber last_supported_version;
34+
QString entity;
35+
QStringList component_list;
36+
Package pkg;
37+
void parsePkgDescription();
38+
void parsePkgManifest();
39+
QXmlStreamReader xml;
40+
};
41+
42+
43+
44+
45+
}
46+
}

gpt4all-updater/include/Modify.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "Command.h"
4+
5+
6+
namespace gpt4all {
7+
namespace modify {
8+
9+
class Modify : public gpt4all::command::Command
10+
{
11+
public:
12+
Modify(QFile &installer);
13+
};
14+
}
15+
}

gpt4all-updater/include/Package.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
6+
#include <QCryptographicHash>
7+
#include <QXmlStreamReader>
8+
#include <QString>
9+
#include <QStringList>
10+
#include <QUrl>
11+
12+
namespace gpt4all {
13+
namespace manifest {
14+
15+
class Package {
16+
public:
17+
Package() {}
18+
static Package parsePackage(QXmlStreamReader &xml);
19+
20+
QString checksum_sha256;
21+
bool is_signed;
22+
QUrl installer_endpoint;
23+
QUrl sbom_manifest;
24+
QStringList installer_args;
25+
};
26+
27+
}
28+
29+
}

0 commit comments

Comments
 (0)