From 1d6ab90fad31c10c4f03a43270133fd615ba8aff Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 9 Jul 2019 12:40:18 +1000 Subject: [PATCH] builds --- Makefile | 20 ++++++++ VERSION | 1 + bin/build.sh | 48 +++++++++++++++++++ bin/deps.sh | 5 ++ bin/package.sh | 27 +++++++++++ client/assets/styles/styles.css | 2 + client/package.json | 4 +- etc/.gitignore | 2 + etc/mnml.SAMPLE.env | 6 +++ .../sites-available/mnml.gg.DEV.nginx.conf | 31 ++++++++++++ .../mnml.gg.PRODUCTION.nginx.conf | 2 +- .../mnml.gg.STAGING.SAMPLE.nginx.conf | 2 +- etc/systemd/system/mnml.service | 14 ++++++ ops/knexfile.js | 2 +- ops/package.json | 5 +- server/.env | 2 +- server/src/account.rs | 11 +++-- server/src/construct.rs | 2 + server/src/main.rs | 2 +- server/src/mtx.rs | 27 ++++++----- server/src/skill.rs | 2 +- 21 files changed, 192 insertions(+), 25 deletions(-) create mode 100644 Makefile create mode 100644 VERSION create mode 100755 bin/build.sh create mode 100755 bin/deps.sh create mode 100755 bin/package.sh create mode 100644 etc/.gitignore create mode 100644 etc/mnml.SAMPLE.env create mode 100644 etc/nginx/sites-available/mnml.gg.DEV.nginx.conf rename {ops => etc/nginx/sites-available}/mnml.gg.PRODUCTION.nginx.conf (97%) rename ops/mnml.gg.DEV.nginx.conf => etc/nginx/sites-available/mnml.gg.STAGING.SAMPLE.nginx.conf (96%) create mode 100644 etc/systemd/system/mnml.service diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..71521bd5 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +SHELL:=/bin/bash + +all: mnml + +deps: + ./bin/deps.sh + +package: + ./bin/package.sh + +mnml: + ./bin/build.sh + +# test: test-unit test-integration + +# test-unit: +# ./test/test-unit.sh + +# test-integration: +# ./test/test-integration.sh diff --git a/VERSION b/VERSION new file mode 100644 index 00000000..6c6aa7cb --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1.0 \ No newline at end of file diff --git a/bin/build.sh b/bin/build.sh new file mode 100755 index 00000000..16ff329a --- /dev/null +++ b/bin/build.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# bless you chris and andy <3 +DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +MNML_PATH=$(realpath "$DIR/../") + +if [ ! -f $MNML_PATH/etc/mnml.env ]; then + echo "create an env file in $MNML_PATH/etc/mnml.env" + echo "see $MNML_PATH/etc/mnml.SAMPLE.env for details" + exit 1; +fi + +source $MNML_PATH/etc/mnml.env + +# DIRECTORY SETUP +sudo mkdir -p /opt/mnml +sudo chown $MNML_USER: /opt/mnml + +sudo mkdir -p /var/lib/mnml +sudo chown $MNML_USER: /var/lib/mnml +mkdir -p /var/lib/mnml/public + +sudo mkdir -p /var/log/mnml +sudo chown $MNML_USER: /var/log/mnml + +sudo ln -nfs $MNML_PATH/current /opt/mnml + +# SERVICES +sudo cp $MNML_PATH/etc/systemd/system/mnml.service /etc/systemd/system + +# POSTGRES SETUP +sudo -u postgres dropdb mnml +sudo -u postgres createdb mnml +sudo -u postgres createuser --encrypted mnml + +echo "DATABASE_URL=postgres://mnml:$MNML_PG_PASSWORD@$MNML_PG_HOST/mnml" > $MNML_PATH/server/.env + +sudo -u postgres psql -c "alter user mnml with encrypted password '$MNML_PG_PASSWORD';" + +cd $MNML_PATH/ops && npm run migrate + +# RUST SETUP +# cargo build +# cp -r $MNML_PATH/server/target/release /opt/mnml/bin + +# NGINX +cp $MNML_PATH/etc/nginx/sites-available/mnml.gg.nginx.conf /etc/nginx/sites-available +cd $MNML_PATH/client && npm run build diff --git a/bin/deps.sh b/bin/deps.sh new file mode 100755 index 00000000..4a2abdc8 --- /dev/null +++ b/bin/deps.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +sudo apt-get install -y postgresql postgresql-contrib +sudo apt-get install -y nginx +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh \ No newline at end of file diff --git a/bin/package.sh b/bin/package.sh new file mode 100755 index 00000000..02960166 --- /dev/null +++ b/bin/package.sh @@ -0,0 +1,27 @@ +# bless you chris and andy <3 + +# DOES NOT WORK AT MOMENT + +DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +MNML_PATH=$(realpath "$DIR/../") +VERSION=$(<"$MNML_PATH/VERSION") + +# copy this dir to the tmp build directory and build it +[[ -n "$BUILD_DIR" ]] || BUILD_DIR="/tmp/mnml/$VERSION" +[[ -n "$BUILD_PREFIX" ]] || BUILD_PREFIX="/opt/mnml" +[[ -n "$REPO_DIR" ]] || REPO_DIR="$HOME/mnml" + +rm -rf "$BUILD_DIR" && mkdir -p "$BUILD_DIR/$BUILD_PREFIX" && cd "$BUILD_DIR/$BUILD_PREFIX" + +( + rsync -a --delete --delete-excluded \ + --exclude=".git/" \ + --exclude=".gitignore" \ + --exclude="packaging" \ + "$REPO_DIR/" "$BUILD_DIR/$BUILD_PREFIX/" + + rsync -a --delete --delete-excluded "$DIR/postinstall" "$BUILD_DIR/$BUILD_PREFIX/" + + cd "$BUILD_DIR/$BUILD_PREFIX" && + make +) diff --git a/client/assets/styles/styles.css b/client/assets/styles/styles.css index cde93b48..5e36a0f8 100644 --- a/client/assets/styles/styles.css +++ b/client/assets/styles/styles.css @@ -539,6 +539,8 @@ header { grid-area: create; flex: 1; + justify-self: flex-end; + display: flex; flex-flow: row wrap; margin-bottom: 1.5em; diff --git a/client/package.json b/client/package.json index cdcc5ce7..e8635a23 100644 --- a/client/package.json +++ b/client/package.json @@ -4,9 +4,9 @@ "description": "", "main": "index.js", "scripts": { - "start": "parcel index.html --host 0.0.0.0 --port 40080 --no-source-maps", + "start": "parcel watch index.html --out-dir /var/lib/mnml/public", "anims": "parcel animations.html --host 0.0.0.0 --port 40080 --no-source-maps", - "build": "rm -rf dist && parcel build index.html && cp -r assets/molecules/ dist/", + "build": "parcel build index.html --out-dir /var/lib/mnml/public", "scss": "node-sass --watch assets/scss -o assets/styles", "lint": "eslint --fix --ext .jsx src/", "test": "echo \"Error: no test specified\" && exit 1" diff --git a/etc/.gitignore b/etc/.gitignore new file mode 100644 index 00000000..eaac129c --- /dev/null +++ b/etc/.gitignore @@ -0,0 +1,2 @@ +mnml.env +nginx/sites-available/mnml.gg.nginx.conf \ No newline at end of file diff --git a/etc/mnml.SAMPLE.env b/etc/mnml.SAMPLE.env new file mode 100644 index 00000000..c2ee78ae --- /dev/null +++ b/etc/mnml.SAMPLE.env @@ -0,0 +1,6 @@ +#!/bin/bash + +export MNML_USER=$(whoami) +export MNML_PG_PASSWORD=$(openssl rand -base64 16) +export MNML_PG_HOST="localhost" + diff --git a/etc/nginx/sites-available/mnml.gg.DEV.nginx.conf b/etc/nginx/sites-available/mnml.gg.DEV.nginx.conf new file mode 100644 index 00000000..a70c95d1 --- /dev/null +++ b/etc/nginx/sites-available/mnml.gg.DEV.nginx.conf @@ -0,0 +1,31 @@ +upstream mnml { + server 127.0.0.1:40000; +} + +map $http_upgrade $connection_upgrade { + default upgrade; + '' close; +} + +# DEV +server { + root /var/lib/mnml/public/; + index index.html; + + location / { + try_files $uri $uri/ =404; + } + + location /api/ws { + proxy_pass http://mnml; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + proxy_read_timeout 600s; + } + + location /api/ { + proxy_pass http://mnml; + proxy_read_timeout 600s; + } +} diff --git a/ops/mnml.gg.PRODUCTION.nginx.conf b/etc/nginx/sites-available/mnml.gg.PRODUCTION.nginx.conf similarity index 97% rename from ops/mnml.gg.PRODUCTION.nginx.conf rename to etc/nginx/sites-available/mnml.gg.PRODUCTION.nginx.conf index c4da6d7e..4b87e27d 100644 --- a/ops/mnml.gg.PRODUCTION.nginx.conf +++ b/etc/nginx/sites-available/mnml.gg.PRODUCTION.nginx.conf @@ -9,7 +9,7 @@ map $http_upgrade $connection_upgrade { # PRODUCTION server { - root /home/git/mnml/client/dist/; + root /var/lib/mnml/public/; index index.html; server_name mnml.gg; # managed by Certbot diff --git a/ops/mnml.gg.DEV.nginx.conf b/etc/nginx/sites-available/mnml.gg.STAGING.SAMPLE.nginx.conf similarity index 96% rename from ops/mnml.gg.DEV.nginx.conf rename to etc/nginx/sites-available/mnml.gg.STAGING.SAMPLE.nginx.conf index 7634a96b..349950ef 100644 --- a/ops/mnml.gg.DEV.nginx.conf +++ b/etc/nginx/sites-available/mnml.gg.STAGING.SAMPLE.nginx.conf @@ -9,7 +9,7 @@ map $http_upgrade $connection_upgrade { # DEV server { - root /home/git/mnml/client/dist/; + root /var/lib/mnml/public/; index index.html; server_name dev.mnml.gg; # managed by Certbot diff --git a/etc/systemd/system/mnml.service b/etc/systemd/system/mnml.service new file mode 100644 index 00000000..f5f66fd2 --- /dev/null +++ b/etc/systemd/system/mnml.service @@ -0,0 +1,14 @@ +[Unit] +Description=OpenBSD Secure Shell server +After=network.target auditd.service +ConditionPathExists=!/etc/ssh/sshd_not_to_be_run + +[Service] +ExecStart=/opt/mnml/bin/mnml +KillMode=process +Restart=on-failure +RestartPreventExitStatus=255 +Type=notify + +[Install] +WantedBy=multi-user.target diff --git a/ops/knexfile.js b/ops/knexfile.js index 7c5e1ce0..9004afd4 100755 --- a/ops/knexfile.js +++ b/ops/knexfile.js @@ -5,7 +5,7 @@ const local = { connection: { database: 'mnml', user: 'mnml', - password: 'craftbeer' + password: process.env.MNML_PG_PASSWORD, }, pool: { min: 2, diff --git a/ops/package.json b/ops/package.json index 03ab795a..1eb603d9 100755 --- a/ops/package.json +++ b/ops/package.json @@ -1,5 +1,5 @@ { - "name": "constructs-ops", + "name": "mnml-ops", "version": "1.0.0", "description": "", "main": "index.js", @@ -7,7 +7,8 @@ "split:molecules": "cd molecules; csplit -f mol bulk.sdf /\\$\\$\\$\\$/ {*} --suppress-matched -z", "migrate": "knex migrate:latest", "migrate:make": "knex migrate:make --", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "nginx:dev": "sudo cp mnml.gg.DEV.SAMPLE.nginx.conf /etc/nginx/sites-available/mnml.gg.DEV.nginx.conf && sudo ln -nfs /etc/nginx/sites-available/mnml.gg.DEV.nginx.conf /etc/nginx/sites-enabled" }, "author": "", "license": "UNLICENSED", diff --git a/server/.env b/server/.env index c5e1d4fe..4d2a4990 100644 --- a/server/.env +++ b/server/.env @@ -1 +1 @@ -DATABASE_URL=postgres://mnml:craftbeer@localhost/mnml +DATABASE_URL=postgres://mnml:ECfcIvPdsGDMg+E3Wbwdig==@localhost/mnml diff --git a/server/src/account.rs b/server/src/account.rs index 0873e175..b21dd440 100644 --- a/server/src/account.rs +++ b/server/src/account.rs @@ -12,6 +12,7 @@ use names::{name as generate_name}; use rpc::{ConstructSpawnParams}; use construct::{Construct, construct_recover, construct_spawn}; use instance::{Instance, instance_delete}; +use mtx::{Mtx, FREE_MTX}; use failure::Error; use failure::{err_msg, format_err}; @@ -230,11 +231,13 @@ pub fn account_create(name: &String, password: &String, code: &String, tx: &mut None => return Err(err_msg("account not created")), }; - // slow but neat - let account = Account::select(tx, id)?; - for _i in 0..3 { - construct_spawn(tx, ConstructSpawnParams { name: generate_name() }, account.id)?; + construct_spawn(tx, ConstructSpawnParams { name: generate_name() }, id)?; + } + + for mtx in FREE_MTX.iter() { + Mtx::new(*mtx, id) + .insert(tx)?; } info!("registration account={:?}", name); diff --git a/server/src/construct.rs b/server/src/construct.rs index e1b48d76..6e2c7ceb 100644 --- a/server/src/construct.rs +++ b/server/src/construct.rs @@ -197,6 +197,7 @@ pub struct ConstructRecover { pub struct Construct { pub id: Uuid, pub account: Uuid, + pub img: Uuid, pub red_power: ConstructStat, pub red_life: ConstructStat, pub blue_life: ConstructStat, @@ -218,6 +219,7 @@ impl Construct { return Construct { id, account: id, + img: Uuid::new_v4(), red_power: ConstructStat { base: 256, value: 256, max: 256, stat: Stat::RedPower }, red_life: ConstructStat { base: 0, value: 0, max: 0, stat: Stat::RedLife }, blue_power: ConstructStat { base: 256, value: 256, max: 256, stat: Stat::BluePower }, diff --git a/server/src/main.rs b/server/src/main.rs index 312652f4..7ee34ef6 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -63,7 +63,7 @@ fn setup_logger() -> Result<(), fern::InitError> { .level_for("actix_web", log::LevelFilter::Info) .level(log::LevelFilter::Info) .chain(std::io::stdout()) - .chain(fern::log_file("log/mnml.log")?) + .chain(fern::log_file("/var/log/mnml/mnml.log")?) .apply()?; Ok(()) } diff --git a/server/src/mtx.rs b/server/src/mtx.rs index 1cb4a8f9..d938efc0 100644 --- a/server/src/mtx.rs +++ b/server/src/mtx.rs @@ -7,21 +7,19 @@ use postgres::transaction::Transaction; use failure::Error; use failure::err_msg; +pub const FREE_MTX: [MtxVariant; 2] = [ + MtxVariant::Rename, + MtxVariant::Reimage, +]; + #[derive(Debug,Copy,Clone,Serialize,Deserialize)] pub enum MtxVariant { + Rename, + Reimage, ArchitectureMolecular, ArchitectureInvader, } -impl MtxVariant { - fn new(self, account: Uuid) -> Mtx { - match self { - MtxVariant::ArchitectureInvader => Mtx { id: Uuid::new_v4(), account, variant: self }, - MtxVariant::ArchitectureMolecular => Mtx { id: Uuid::new_v4(), account, variant: self }, - } - } -} - #[derive(Debug,Copy,Clone,Serialize,Deserialize)] pub struct Mtx { id: Uuid, @@ -30,12 +28,19 @@ pub struct Mtx { } impl Mtx { + pub fn new(variant: MtxVariant, account: Uuid) -> Mtx { + match variant { + _ => Mtx { id: Uuid::new_v4(), account, variant }, + // MtxVariant::ArchitectureInvader => Mtx { id: Uuid::new_v4(), account, variant: self }, + // MtxVariant::ArchitectureMolecular => Mtx { id: Uuid::new_v4(), account, variant: self }, + } + } + pub fn account_list(tx: &mut Transaction, account: Uuid) -> Result, Error> { let query = " SELECT data, id FROM mtx - WHERE account = $1 - FOR UPDATE; + WHERE account = $1; "; let result = tx diff --git a/server/src/skill.rs b/server/src/skill.rs index 1fd09206..3b9f180f 100644 --- a/server/src/skill.rs +++ b/server/src/skill.rs @@ -183,7 +183,7 @@ pub fn resolve(skill: Skill, source: &mut Construct, target: &mut Construct, mut Skill::AbsorbI | Skill::AbsorbII | - Skill::AbsorbIII => absorbtion(source, target, resolutions, skill), + Skill::AbsorbIII => absorb(source, target, resolutions, skill), Skill::HybridI | Skill::HybridII |