#!/bin/bash set -euxo pipefail nexus_domain=$(hostname --fqdn) # use the local nexus user database. config_authentication='nexus' # OR use LDAP. # NB this assumes you are running the Active Directory from https://github.com/rgl/windows-domain-controller-vagrant. #config_authentication='ldap' # install java. # see https://help.sonatype.com/en/sonatype-nexus-repository-system-requirements.html#SystemRequirements-Java apt-get install -y openjdk-17-jre-headless apt-get install -y gnupg # add the nexus user. groupadd --system nexus adduser \ --system \ --disabled-login \ --no-create-home \ --gecos '' \ --ingroup nexus \ --home /opt/nexus \ nexus install -d -o root -g nexus -m 750 /opt/nexus # download and install nexus. pushd /opt/nexus # see https://www.sonatype.com/download-oss-sonatype # see https://help.sonatype.com/en/download-archives---repository-manager-3.html # see https://help.sonatype.com/en/release-notes.html # see https://help.sonatype.com/en/sonatype-nexus-repository.html nexus_version=3.84.1-01 nexus_home=/opt/nexus/nexus-$nexus_version nexus_tarball=nexus-$nexus_version-linux-x86_64.tar.gz nexus_download_url=https://download.sonatype.com/nexus/3/$nexus_tarball wget -q $nexus_download_url tar xf $nexus_tarball # NB this creates the $nexus_home (e.g. nexus-3.84.1-01) and sonatype-work directories. rm $nexus_tarball sed -i -E 's,#!.+,#!/usr/bin/bash,g' "$nexus_home/bin/nexus" install -d -o nexus -g nexus -m 700 .java # java preferences are saved here (the default java.util.prefs.userRoot preference). install -d -o nexus -g nexus -m 700 sonatype-work/nexus3/etc chown -R nexus:nexus sonatype-work grep -v -E '\s*##.*' $nexus_home/etc/nexus-default.properties >sonatype-work/nexus3/etc/nexus.properties sed -i -E 's,(application-host=).+,\1127.0.0.1,g' sonatype-work/nexus3/etc/nexus.properties sed -i -E '/^(\s*(nexus-edition=.+|nexus-features=.+|nexus-pro-feature)\s*)/d' sonatype-work/nexus3/etc/nexus.properties cat >>sonatype-work/nexus3/etc/nexus.properties <<'EOF' # disable the wizard. nexus.onboarding.enabled=false # disable generating a random password for the admin user. nexus.security.randompassword=false # allow the use of groovy scripts because we use them to configure nexus. # see https://issues.sonatype.org/browse/NEXUS-23205 # see Scripting Nexus Repository Manager 3 at https://support.sonatype.com/hc/en-us/articles/360045220393 nexus.scripts.allowCreation=true # enable the database console. # see https://support.sonatype.com/hc/en-us/articles/213467158-How-to-reset-a-forgotten-admin-password-in-Sonatype-Nexus-Repository-3#DatabaseConsoleforh2Database nexus.h2.httpListenerEnabled=true nexus.h2.httpListenerPort=8082 EOF diff -u $nexus_home/etc/nexus-default.properties sonatype-work/nexus3/etc/nexus.properties || true popd # trust the LDAP server certificate for user authentication (when enabled). # NB this assumes you are running the Active Directory from https://github.com/rgl/windows-domain-controller-vagrant. if [ "$config_authentication" = 'ldap' ]; then echo '192.168.56.2 dc.example.com' >>/etc/hosts openssl x509 -inform der -in /vagrant/shared/ExampleEnterpriseRootCA.der -out /usr/local/share/ca-certificates/ExampleEnterpriseRootCA.crt update-ca-certificates -v fi # start nexus. cat >/etc/systemd/system/nexus.service <"$GNUPGHOME/apt-hosted-gpg-batch" <"$GNUPGHOME/gpg-agent.conf" </vagrant/shared/apt-hosted-public.key gpg \ --export-secret-key \ --armor \ --pinentry-mode loopback \ --passphrase abracadabra \ "apt-hosted@$nexus_domain" \ >/vagrant/shared/apt-hosted-private.key gpgconf --kill gpg-agent rm -rf "$GNUPGHOME" unset GNUPGHOME # configure nexus with the groovy script. bash /vagrant/provision/execute-provision.groovy-script.sh # set the api credentials. api_auth="admin:admin" # accept the End User License Agreement (EULA). # see https://links.sonatype.com/products/nxrm3/docs/ce-onboarding. eula_disclaimer="$(http \ --check-status \ --auth "$api_auth" \ GET \ https://$nexus_domain/service/rest/v1/system/eula \ | jq -r .disclaimer)" http \ --check-status \ --auth "$api_auth" \ --ignore-stdin \ POST \ https://$nexus_domain/service/rest/v1/system/eula \ accepted=true \ disclaimer="$eula_disclaimer" # create the adhoc-package raw repository. # NB this repository can host any type of artifact, so we disable strictContentTypeValidation. # see https://help.sonatype.com/display/NXRM3/Raw+Repositories+and+Maven+Sites#RawRepositoriesandMavenSites-UploadingFilestoHostedRawRepositories http \ --check-status \ --auth "$api_auth" \ POST \ https://$nexus_domain/service/rest/v1/repositories/raw/hosted \ <<'EOF' { "name": "adhoc-package", "online": true, "storage": { "blobStoreName": "default", "strictContentTypeValidation": false, "writePolicy": "allow_once" }, "component": { "proprietaryComponents": true } } EOF # create the apt-hosted apt repository. # see https://help.sonatype.com/en/apt-repositories.html http \ --check-status \ --auth "$api_auth" \ POST \ https://$nexus_domain/service/rest/v1/repositories/apt/hosted \ <