Skip to content

Commit

Permalink
(install) Add two separate templates for the install script
Browse files Browse the repository at this point in the history
One template is for the full Marginalia Search style install, and the other is for a barebones install with no Marginalia-related fluff.
  • Loading branch information
vlofgren committed Jan 13, 2024
1 parent d28fc99 commit 4c62065
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 20 deletions.
Expand Up @@ -3,12 +3,15 @@
import com.google.inject.Inject;
import com.zaxxer.hikari.HikariDataSource;
import nu.marginalia.nodecfg.model.NodeConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NodeConfigurationService {
private final Logger logger = LoggerFactory.getLogger(NodeConfigurationService.class);

private final HikariDataSource dataSource;

Expand Down Expand Up @@ -37,7 +40,7 @@ INSERT IGNORE INTO NODE_CONFIGURATION(ID, DESCRIPTION, ACCEPT_QUERIES, KEEP_WARC
}
}

public List<NodeConfiguration> getAll() throws SQLException {
public List<NodeConfiguration> getAll() {
try (var conn = dataSource.getConnection();
var qs = conn.prepareStatement("""
SELECT ID, DESCRIPTION, ACCEPT_QUERIES, AUTO_CLEAN, PRECESSION, KEEP_WARCS, DISABLED
Expand All @@ -60,6 +63,10 @@ public List<NodeConfiguration> getAll() throws SQLException {
}
return ret;
}
catch (SQLException ex) {
logger.warn("Failed to get node configurations", ex);
return List.of();
}
}

public NodeConfiguration get(int nodeId) throws SQLException {
Expand Down
Expand Up @@ -24,13 +24,21 @@ public ControlRendererFactory(RendererFactory rendererFactory,

@SneakyThrows
public Renderer renderer(String template) {
Map<String, Object> globalContext = Map.of(
"nodes", nodeConfigurationService.getAll(),
"hideMarginaliaApp", Boolean.getBoolean("control.hideMarginaliaApp")
);

var baseRenderer = rendererFactory.renderer(template);

return (context) -> baseRenderer.render(context, Map.of("global-context", globalContext));
// We might want to add some sort of caching here, as this is called for every request
// (but we can't cache the result forever, as the node list might change)

return (context) ->
baseRenderer.render(context,
Map.of("global-context",
Map.of(
"nodes", nodeConfigurationService.getAll(),
"hideMarginaliaApp", Boolean.getBoolean("control.hideMarginaliaApp")
)
)
);
}

public interface Renderer {
Expand Down
Expand Up @@ -30,18 +30,13 @@ public NodeConfigurationWatcher(NodeConfigurationService configurationService) {
private void pollConfiguration() {
for (;;) {
List<Integer> goodNodes = new ArrayList<>();
try {
for (var cfg : configurationService.getAll()) {

if (!cfg.disabled() && cfg.acceptQueries()) {
goodNodes.add(cfg.node());
}
for (var cfg : configurationService.getAll()) {
if (!cfg.disabled() && cfg.acceptQueries()) {
goodNodes.add(cfg.node());
}
queryNodes = goodNodes;
}
catch (SQLException ex) {
logger.warn("Failed to update node configurations", ex);
}
queryNodes = goodNodes;

TimeUnit.SECONDS.sleep(10);
}
Expand Down
32 changes: 29 additions & 3 deletions run/install.sh
Expand Up @@ -34,8 +34,22 @@ if [ -e "${1}" ]; then
exit 1
fi

INSTALL_DIR=${1}
INSTALL_DIR=$(realpath ${1})

echo "Would you like to set up a:"
echo
echo "1) barebones instance"
echo "2) full Marginalia Search instance?"
read -p "Enter 1 or 2: " INSTANCE_TYPE

## Validate
if [ "${INSTANCE_TYPE}" != "1" ] && [ "${INSTANCE_TYPE}" != "2" ]; then
echo
echo "ERROR: Invalid instance type, choose 1 or 2"
exit 1
fi

echo
echo "We're going to set up a Mariadb database in docker, please enter some details"

read -p "MariaDB user (e.g. marginalia): " MARIADB_USER
Expand All @@ -52,6 +66,7 @@ if [ "${MARIADB_PASSWORD}" != "${MARIADB_PASSWORD2}" ]; then
exit 1
fi

echo
echo "Will install to ${INSTALL_DIR}"
read -p "Press enter to continue, or Ctrl-C to abort"

Expand All @@ -63,7 +78,7 @@ mkdir -p ${INSTALL_DIR}

echo "** Copying files to ${INSTALL_DIR}"

for dir in model data conf env; do
for dir in model data conf conf/properties env; do
if [ ! -d ${dir} ]; then
echo "ERROR: ${dir} does not exist"
exit 1
Expand All @@ -73,13 +88,19 @@ for dir in model data conf env; do
find ${dir} -maxdepth 1 -type f -exec cp -v {} ${INSTALL_DIR}/{} \;
done

# for barebones, tell the control service to hide the marginalia app specific stuff
if [ "${INSTANCE_TYPE}" == "1" ]; then
echo "control.hideMarginaliaApp=true" > ${INSTALL_DIR}/conf/properties/control-service.properties
fi

echo "** Copying settings files"
cp prometheus.yml ${INSTALL_DIR}/

echo "** Creating directories"
mkdir -p ${INSTALL_DIR}/logs
mkdir -p ${INSTALL_DIR}/db
mkdir -p ${INSTALL_DIR}/index-1/{work,index,backup,storage,uploads}
mkdir -p ${INSTALL_DIR}/index-2/{work,index,backup,storage,uploads}

echo "** Updating settings files"

Expand All @@ -93,6 +114,11 @@ export uval="\$\$MARIADB_USER"
export pval="\$\$MARIADB_PASSWORD"

export INSTALL_DIR
envsubst < install/docker-compose.yml.template >${INSTALL_DIR}/docker-compose.yml

if [ "${INSTANCE_TYPE}" == "1" ]; then
envsubst < install/docker-compose-barebones.yml.template >${INSTALL_DIR}/docker-compose.yml
else
envsubst < install/docker-compose-marginalia.yml.template >${INSTALL_DIR}/docker-compose.yml
fi

popd

0 comments on commit 4c62065

Please sign in to comment.