Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Install script #34

Merged
merged 25 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions .github/workflows/test-install-script.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Install script tester"

on:
push:
branches: [ "dev" ]
paths:
- 'install.sh'

jobs:

ubuntu:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Run install script
run: sudo bash install.sh action
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
DMCD is being used to auto deploy apps, it uses a webhook to know when there is a new image ready to pull,
then pulls it and recreats a new container ready for production. This webhook can be used in [`hub.docker.com`](https://hub.docker.com/), or what suits the best.

# 🧿 | Status
[![Install script tester](https://github.com/Tolfix/dmcd/actions/workflows/test-install-script.yml/badge.svg)](https://github.com/Tolfix/dmcd/actions/workflows/test-install-script.yml) [![CodeQL](https://github.com/Tolfix/dmcd/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/Tolfix/dmcd/actions/workflows/codeql-analysis.yml)

# 📢 | Contribute
Want to contribute? Great! You can contribute by `forking` this repository, then make changes and make a `PR`!
If you get confused and doesn't understand something, there is [`documentation`](https://github.com/Tolfix/dmcd/wiki/Documentation) *(being worked on)*.
Expand Down Expand Up @@ -42,6 +45,9 @@ Run `npm run start` and it will run as usual.
Default user is `admin`, and password is the password you got prompted to type in `setup`

# 💾 | OS Support
* Linux (Ubuntu, CentOs...)
* ~~MacOS~~
* ~~Windows~~
| OS | Supported |
| ------------- |:-------------:|
| Ubuntu | ✔ |
| CentOS | ❌ |
| Windows | ❌ |
| MacOS | ❌ |
182 changes: 182 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#!/bin/bash

set -e

# root user?
if [[ $EUID -ne 0 ]]; then
echo "* Please run this script with root. (sudo)." 1>&2
exit 1
fi

# Check if curl is installed
if ! [ -x "$(command -v curl)" ]; then
echo "* curl is not installed, please ensure it is."
exit 1
fi

# VARS

# Github
GITHUB_REPO="https://github.com/Tolfix/dmcd"
GITHUB_BRANCH="master"
GITHUB_IS_ACTION=$1

# General VARS
INSTALL_PATH="/var/dmcd"
SESSION_SECRET=""

# User information
USER_PASSWORD=""

# Database
MONGO_URI=""
MONGO_ALREADY_HAS=false

# Domain/SSL
FQDN=""
IS_SSL=false
CONFIGURE_SSL=false

# Update apt
apt_update() {
apt update -q -y && apt upgrade -y
}

# Get the newest tag with the .zip file.
install_dmcd() {
echo "* Getting the newest release"
TAG=$(curl --silent "https://api.github.com/repos/tolfix/dmcd/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/')

curl -L -o master.tar.gz https://github.com/Tolfix/dmcd/archive/refs/tags/$TAG.tar.gz
tar xf master.tar.gz -C $INSTALL_PATH --strip-components 1
rm -r master.tar.gz
echo "* Installed the DMCD"
}

npm_install() {
npm install
}

build_dmcd() {
echo "* Building DMCD"
npm run build
echo "* Done building DMCD"
}

# Get a .env file with the vars we got.
create_env_file() {
echo "* Creating .env file"
echo -e 'MONGODB_URI='$MONGO_URI'\nSESSION_SECRET='$SESSION_SECRET > .env
cat .env
echo "* Done creating .env file"
}

# Create a mongodb database
create_database() {
echo "* Installing mongodb"
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list
apt_update
apt install mongodb-org -y
systemctl start mongod
systemctl enable mongod
MONGO_URI="mongodb://localhost/dmcd"
echo "* Mongodb installed"
}

install_node() {
echo "* Installing nodejs"
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
apt -y install nodejs
echo "* Nodejs installed"
echo ""
echo "* Installing typescript"
npm install typescript -g
echo "* Installed typescript"
}

gen_random_string() {
SESSION_SECRET=$(node -e 'console.log(require("crypto").randomBytes(20).toString("hex"))')
}

install_docker() {
echo "* Installing docker"
apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
apt_update
apt install docker-ce -y
systemctl start docker
systemctl enable docker
echo "* Installed docker"
echo ""
echo "* Install docker-compose"
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
echo "* Installed docker-compose"
}

create_admin() {
npm run create-admin $MONGO_URI $USER_PASSWORD
}

main() {
# Check if already installed.
if [ -d $INSTALL_PATH ]; then
echo "* You have already installed this once."
echo -e -n "* Are you sure you want to proceed? (y/N): "
read -r CONFIRM_PROCEED
if [[ ! "$CONFIRM_PROCEED" =~ [Yy] ]]; then
print_error "Installation aborted!"
exit 1
fi
fi

mkdir $INSTALL_PATH
cd $INSTALL_PATH

if [ "$GITHUB_IS_ACTION" = "action" ]; then
apt_update
# create_database
install_node
gen_random_string
# install_docker
# install_dmcd
# npm_install
# build_dmcd
create_env_file
else
echo ""
echo -n "* Admin password: "
read -r USER_PASSWORD

echo ""
echo "* For this application to run properly it needs a mongodb database."
echo -n "* Do you already have a mongodb database? (true/false): "
read -r MONGO_ALREADY_HAS

if [ "$MONGO_ALREADY_HAS" = false ]; then
apt_update
create_database
else
echo ""
echo "* Mongodb URI: "
read -r MONGO_URI
fi

install_node
gen_random_string
install_docker
install_dmcd
npm_install
build_dmcd
create_admin
create_env_file

fi

}

main
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"install": "npm install",
"start-nobuild": "node ./build/Server.js",
"nodemon": "nodemon ./build/Server",
"create-admin": "node ./build/Setup/CreateAdmin",
"setup": "npm run build && node ./build/Setup.js"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AW from "./Lib/Async";
import ConfigModel from "./Models/Config";

export const DebugMode = process.env.DEBUG === "true" ? true : false;
export const MongoDB_URI = process.env.MONGODB_URI ?? "";
export const MongoDB_URI = process.env.MONGODB_URI ?? "mongodb://localhost/dmcd";
export const Web_Title = process.env.TITLE ?? "DMCD";
export const PORT = 56251;
export const Session_Secret = process.env.SESSION_SECRET ?? undefined;
Expand Down
39 changes: 39 additions & 0 deletions src/Setup/CreateAdmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import mongoose from "mongoose";
import ConfigModel from "../Models/Config";
import User from "../Models/User";
import bcrypt from "bcryptjs"

const myArgs = process.argv.slice(2);

bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(myArgs[1], salt, (err, hash) => {

mongoose.connect(myArgs[0], {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});

new ConfigModel({
setupDone: true,
title: "DMCD",
smtp: {
host: "",
port: 25,
secure: false,
auth: {
user: "",
password: ""
},
},
domain: "",
}).save().then(() => {return;});

new User({
username: "admin",
password: hash,
}).save().then(() => {
process.exit(0);
});
});
});
4 changes: 2 additions & 2 deletions views/Modals/Create-New-CD.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Create new CD</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
<button type="button" class="btn-close" data-dismiss="modal" aria-label="Close">
</button>
</div>
<div class="modal-body">
Expand Down Expand Up @@ -31,7 +31,7 @@
</div>
<div class="modal-footer">
<button class="btn btn-success" type="submit">Submit</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</form>
</div>
Expand Down