Skip to content
This repository has been archived by the owner on Mar 18, 2021. It is now read-only.

Updater tool for Tomoe, protects databases from destruction :) #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 14 additions & 6 deletions Tomoe/build/core/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import crypto from 'crypto';
import fs from 'fs';

import packageDetail from '../../package.json';

import { Definitions } from '../templates';

const TOMOE_CONFIG_PATH = `./${Definitions.configName}`

export class Config{
export class Config {
constructor(params = {}){
this.hackathon = params.hackathon || '';
this.beta = params.beta || false;
Expand All @@ -17,10 +17,11 @@ export class Config{
this.server = params.server || this.database;
this.apiVersion = params.apiVersion || Definitions.apiVersion;
this.userTypes = params.userTypes || Definitions.userTypes;
this.hackerStatuses = params.hackerStatuses || Definitions.defaultHackerStatuses;
this.volunteerStatuses = params.hackerStatuses || Definitions.defaultVolunteerStatuses;
this.hackerStatuses = params.hackerStatuses || Definitions.hackerStatuses;
this.volunteerStatuses = params.hackerStatuses || Definitions.volunteerStatuses;
this.build = params.build || ((fs.existsSync('../.hackmerced')) ? Definitions.build.default : Definitions.build.import)
this.adminPermissions = params.adminPermissions || Definitions.defaultAdminPermissions;
this.adminPermissions = params.adminPermissions || Definitions.adminPermissions;
this.version = packageDetail.version;
}

get(){
Expand All @@ -36,7 +37,9 @@ export class Config{
userTypes: this.userTypes,
hackerStatuses: this.hackerStatuses,
volunteerStatuses: this.volunteerStatuses,
adminPermissions: this.adminPermissions
adminPermissions: this.adminPermissions,
build: this.build,
version: this.version,
}
}

Expand All @@ -55,6 +58,11 @@ export class Config{
})
}


static current(){
return require(`../.${TOMOE_CONFIG_PATH}`).Tomoe;
}

delete(){
fs.unlink(TOMOE_CONFIG_PATH);
}
Expand Down
2 changes: 1 addition & 1 deletion Tomoe/build/core/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const stage = {

term('\nLearn how to create local ENV files at: https://github.com/HackMerced/HackMerced/tree/master/Tomoe#local-environmnet-variables-in-development'.red);
term('\n');
process.exit();
process.exit(1);
}

stage.next();
Expand Down
8 changes: 4 additions & 4 deletions Tomoe/build/templates/tomoe.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const standardDBCollections = [
'hacker', // hacker users
'volunteer', // volunteer users
'oauth', // oauth keys information
'meta' // meta Tomoe information
'meta', // meta Tomoe information
]

const dbCollections = {
Expand All @@ -32,7 +32,7 @@ export const Definitions = {
server: serverNames[process.env.NODE_ENV],
userTypes:['admin', 'hacker', 'volunteer'],
apiVersion: '2.0',
defaultHackerStatuses:[
hackerStatuses:[
'registered',
'applied',
'accepted',
Expand All @@ -41,12 +41,12 @@ export const Definitions = {
'attending',
'inactive'
],
defaultVolunteerStatuses: [
volunteerStatuses: [
"applied",
"denied",
"approved",
],
defaultAdminPermissions: [
adminPermissions: [
'read',
'read+write', // read + write
'admin' // super admin (controls group settings)
Expand Down
11 changes: 11 additions & 0 deletions Tomoe/build/update/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs'),
ENV_PATH = '.env';

// support for local .env files

if(fs.existsSync(ENV_PATH)){
require('dotenv').config({path: ENV_PATH})
}

require('babel-core/register');
require('./updater.js')
48 changes: 48 additions & 0 deletions Tomoe/build/update/updater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
This code will upgrade the Tomoe.config.js and Arangodb without destroying the database/your updates
*/
import packageDetail from '../../package.json';
import { Config } from '../core/config';
import { Definitions } from '../templates/tomoe.template';
import { Database } from 'arangojs';
import { createCollections } from '../util'


const term = console.log; // easier to type

export function updateConfig() {
term(`Updating tomoe.config.js`);

let config = new Config(Config.current());

for(let option in Definitions) {
if(!config[option]) {
config[option] = Definitions[option];
}
}

config.save();
}

export function updateCollections() {
global.db = new Database({
url: process.env.DB_URI,
});

db.useDatabase(Definitions.server);

createCollections().then(() => {

}).catch(err => {
term(err);
})
}

export function TomoeUpdate() {
term(`Upgrading Tomoe to version ${packageDetail.version}`);

updateConfig();
updateCollections();
}

TomoeUpdate();
19 changes: 19 additions & 0 deletions Tomoe/build/util/check-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const packageDetail = require('../../package.json');
const colors = require('colors');
const LATEST_MAJOR_RELEASE = 20.2;

function checkVersion(resolve) {
if(parseFloat(packageDetail.version.replace('.', '')) < LATEST_MAJOR_RELEASE) {
console.log(colors.red('\n\You are running a deprecated version of Tomoe!\nPlease delete your "tomoe.config.js" file and run the following:\n\n'),colors.green(' yarn setup'))
process.exit(1);
}

if(TOMOE_CONFIG.version === packageDetail.version) {
return resolve();
}

console.log(colors.red('\n\Version mismatch in Tomoe instances, please run:\n'),colors.green(' yarn renew'))
process.exit(1);
}

module.exports = checkVersion;
3 changes: 2 additions & 1 deletion Tomoe/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "Tomoe",
"version": "2.0.0",
"version": "2.0.2",
"description": "Centralized API user platform for Hackathons",
"main": "production.js",
"scripts": {
"clean": "rimraf dist",
"compile": "node ./src/app/build/scripts/compile",
"setup": "npm run generate",
"generate": "node ./build/core",
"renew": "node ./build/update",
"build": "npm run clean && cross-env NODE_ENV=production npm run compile",
"start": "node ./build/imports && cross-env NODE_ENV=development node ./src/app/build/scripts/start",
"api": "node ./src/server/index.js",
Expand Down
10 changes: 9 additions & 1 deletion Tomoe/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Access the server and gui locally at:
http://localhost:4925
```

### Local environmnet variables in development
### Local environment variables in development
If you do not want to setup environmental variables globally, Tomoe allows you to locally set environmental variables by creating file called `.env` in the root directory of Tomoe

```terminal
Expand All @@ -68,6 +68,14 @@ NODE_ENV={Either DEVELOPMENT or PRODUCTIOn}

[<b>⬆ back to top](#table-of-contents)</b>

### Updating
In the case of updating to a newer version of Tomoe, please run the following commands after pulling a new repo:

```
yarn # installs all new pacakges
yarn renew # updates your tomoe.config.js and databases
```

# Documentation
## Shortcuts
[**Hacker Object**](#the-hacker-object)<br>
Expand Down
10 changes: 6 additions & 4 deletions Tomoe/src/server/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs'),
ENV_PATH = './.env';
const colors = require('colors');
const checkVersion = require('../../build/util/check-version.js');

// support for local .env files
if(!fs.existsSync(ENV_PATH)){
Expand All @@ -11,10 +12,11 @@ if(!fs.existsSync(ENV_PATH)){
if(fs.existsSync('./tomoe.config.js')){
global.TOMOE_CONFIG = require('../../tomoe.config.js').Tomoe;

require( 'babel-core/register' );
require('./src/database.js')
require('./src/server.js');

checkVersion(function(){
require( 'babel-core/register' );
require('./src/database.js')
require('./src/server.js');
})
} else {
console.log(colors.red('\n\nWe cannot find your tomoe.config.js file!'));
console.log(colors.grey('Please install Tomoe before turning on the server by entering the following command:'));
Expand Down