Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: ✨ GitOps feature, preparation for the next release v0.3.2
  • Loading branch information
gnekich committed Nov 5, 2022
commit 03b1b94cdaf61d6ab097c78d6ae92a0b711eed6d
24 changes: 24 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ HLAMBDA_SWAGGER_UI_TOKEN_EXPIRES_IN_SECONDS="86400"
# Expires in value in seconds, time before JWT token expires and cookie is deleted on the client.
HLAMBDA_VSCODE_UI_TOKEN_EXPIRES_IN_SECONDS="86400"

# Constant reference in code: ENV_ENABLE_HLAMBDA_GIT_SYNC | Default value: false
# Turns on the GIT SYNC option, where the server will be able to sync metadata with remote repository. (Default: false)
ENABLE_HLAMBDA_GIT_SYNC="false"

# Constant reference in code: ENV_HLAMBDA_GIT_SYNC_URL | Default value:
# Sync repository url.
HLAMBDA_GIT_SYNC_URL=""

# Constant reference in code: ENV_HLAMBDA_GIT_SYNC_USERNAME | Default value:
# Sync repository username.
HLAMBDA_GIT_SYNC_USERNAME=""

# Constant reference in code: ENV_HLAMBDA_GIT_SYNC_PASSWORD | Default value:
# Sync repository password.
HLAMBDA_GIT_SYNC_PASSWORD=""

# Constant reference in code: ENV_HLAMBDA_GIT_SYNC_BRANCH | Default value: master
# Sync default branch. (Default: master) If falsey value, the current HEAD branch will be used.
HLAMBDA_GIT_SYNC_BRANCH="master"

# Constant reference in code: ENV_HLAMBDA_GIT_SYNC_INTERVAL_SECONDS | Default value: 240
# When sync is enabled, system will automatically try to sync with the current branch, it will work only if there is no conflicts, to not destroy live work. (Default: 240 (4m). If set to 0, disables pooling)
HLAMBDA_GIT_SYNC_INTERVAL_SECONDS="240"

# Constant reference in code: ENV_HLAMBDA_CONFIGURATION_LOADER_PREFIX | Default value: hlambda-config.yaml
# Sets the value for the name of the configuration file that will be loaded.
HLAMBDA_CONFIGURATION_LOADER_PREFIX="hlambda-config.yaml"
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Release 0.3.2

- Fixes the version bump issue.
- Updated README.md
- GitOps, remote repository sync.
- Tab complete in Hlambda Pseudo Terminal

# Release 0.3.1

- Fix the CORS issue with the vscode when requesting external data from vscode-unpkg CDN.
- Fix the issue with Webpack miss-configuration that was wrapping dist and src folder instead of only dist.

# Release 0.3.0

- Added zero downtime reload.
Expand Down
7 changes: 1 addition & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ WORKDIR /usr/src/app
# where available (npm@5+)
COPY package*.json ./

# RUN echo "`date`" > ./image-build-time.txt
RUN echo "`date +\"%Y-%m-%dT%H:%M:%S%z\"`" > ./image-build-time.txt
RUN echo "`date +%s`" > ./image-build-timestamp.txt

RUN ["npm", "install"]
Expand All @@ -40,9 +40,4 @@ RUN cp -r ./metadata/* ./data/metadata-examples
RUN npm install pm2 -g

# CMD ["npm", "run", "start"]

CMD [ "pm2-runtime", "start", "ecosystem.config.cjs" ]

# This is not ready, there are known bugs with cluster mode (state management between nodes, like path in remote shell etc...)
#CMD ["npm", "run", "cluster-runtime"]

10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ In production deployments, you can bake metadata and disable the UI console.
You can check ["Getting started"](https://www.hlambda.io/getting-started/) on how to get started with Hlambda. In short, it goes like this:

- Install latest hlambda CLI. (Hint: `npm i -g hlambda-cli`)
- Spin up the Docker instance of the Hlambda server. (Hint: Use `hl snip d` to get docker snippet)
- Spin up the Docker instance of the Hlambda server. (Hint: Use `hl snip d` to get the docker snippet)
- Use CLI to generate a new Hlambda app. (Hint: `hl init my-app`)
- (Optional) Configure multiple environments. (Hint: `hl env add staging`)
- Deploy metadata (Hint: `hl m a`)
Expand All @@ -74,10 +74,14 @@ Any contributions are greatly appreciated.

## Authors

Gordan Nekić <gordan@neki.ch>
- Gordan Nekić <gordan@neki.ch>

<a href="https://github.com/sponsors/gnekich" title="Sponsor Gordan Nekić"><img src="https://img.shields.io/github/sponsors/gnekich?style=plastic" alt="Github Sponsors button" /></a>

## How to support the project

I've recently created Patreon that will enable me to work more on the Hlambda services.
I've recently created Patreon which will enable me to work more on the Hlambda services.

<span class="badge-patreon"><a href="https://www.patreon.com/bePatron?u=70751523" title="Donate to this project using Patreon"><img src="https://img.shields.io/badge/patreon-donate-green.svg" alt="Patreon donate button" /></a></span>

You can also sponsor the developers directly through the GitHub Sponsors program.
2 changes: 1 addition & 1 deletion ecosystem.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
name: 'Backend Microservice',
script: 'src/index.js',
// script: 'npm', // Do not use this, express port binding will be problematic with npm start
// args: 'run start', // Look abowe
// args: 'run start', // Look above

exec_mode: 'cluster',

Expand Down
6 changes: 6 additions & 0 deletions metadata/apps/github-hook/hlambda-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
env:
HLAMBDA_GIT_SYNC_URL: 'https://github.com/hlambda/example-metadata-sync'
HLAMBDA_GIT_SYNC_USERNAME: ''
HLAMBDA_GIT_SYNC_PASSWORD: ''
ENABLE_HLAMBDA_GIT_SYNC: false
HLAMBDA_GIT_SYNC_INTERVAL_SECONDS: '240'
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hlambda-core",
"version": "0.3.0",
"version": "0.3.2",
"description": "Hlambda core",
"type": "module",
"main": "src/index.js",
Expand Down Expand Up @@ -61,4 +61,4 @@
"eslint-plugin-prettier": "^4.0.0",
"nodemon": "^2.0.15"
}
}
}
34 changes: 34 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,40 @@ export const constants = {
description: 'Expires in value in seconds, time before JWT token expires and cookie is deleted on the client.',
},

// GitOps
ENV_ENABLE_HLAMBDA_GIT_SYNC: {
name: 'ENABLE_HLAMBDA_GIT_SYNC',
default: 'false',
description:
'Turns on the GIT SYNC option, where the server will be able to sync metadata with remote repository. (Default: false)',
},
ENV_HLAMBDA_GIT_SYNC_URL: {
name: 'HLAMBDA_GIT_SYNC_URL',
default: '',
description: 'Sync repository url.',
},
ENV_HLAMBDA_GIT_SYNC_USERNAME: {
name: 'HLAMBDA_GIT_SYNC_USERNAME',
default: '',
description: 'Sync repository username.',
},
ENV_HLAMBDA_GIT_SYNC_PASSWORD: {
name: 'HLAMBDA_GIT_SYNC_PASSWORD',
default: '',
description: 'Sync repository password.',
},
ENV_HLAMBDA_GIT_SYNC_BRANCH: {
name: 'HLAMBDA_GIT_SYNC_BRANCH',
default: 'master',
description: 'Sync default branch. (Default: master) If falsey value, the current HEAD branch will be used.',
},
ENV_HLAMBDA_GIT_SYNC_INTERVAL_SECONDS: {
name: 'HLAMBDA_GIT_SYNC_INTERVAL_SECONDS',
default: '240',
description:
'When sync is enabled, system will automatically try to sync with the current branch, it will work only if there is no conflicts, to not destroy live work. (Default: 240 (4m). If set to 0, disables pooling)',
},

// Loader configurations
ENV_HLAMBDA_CONFIGURATION_LOADER_PREFIX: {
name: 'HLAMBDA_CONFIGURATION_LOADER_PREFIX',
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ import execScriptCommand from './utils/execScriptCommand.js';
// Create Hlambda event emmitter
import hlambdaEventEmitter from './emitter/index.js';

// Import git sync
import { startGitSync } from './utils/gitSync.js';

// Customized npm packages
import swaggerUi from './custom/swagger-ui-express/index.js';
// --------------------------------------------------------------------------------
Expand Down Expand Up @@ -434,6 +437,14 @@ const spinServer = async () => {
console.log(`${Array(80 + 1).join('#').yellow}`);
});
// --------------------------------------------------------------------------------
if (isEnvTrue(constants.ENV_ENABLE_HLAMBDA_GIT_SYNC)) {
// console.log('Git sync is starting...'.green);
await startGitSync();
} else {
console.log('Git sync is disabled.'.red);
}
console.log(`${Array(80 + 1).join('-').yellow}`);
// --------------------------------------------------------------------------------
// For some packages like socket.io we need reference to the server instance to attach
global.HLAMBDA_SERVER_INSTANCE = server; // Legacy
hlambdaEventEmitter.emit('server-ready', server);
Expand Down
47 changes: 47 additions & 0 deletions src/utils/gitSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import path from 'path';
import execScriptCommand from './execScriptCommand.js';
import reloadServer from './reloadServer.js';

// Define constants and constants defaults
import { constants, isEnvTrue, getEnvValue } from './../constants/index.js';

// Define timer reference
let timer;

export const gitSync = async () => {
// TODO: Implement actuall git pull and check for the results, including auth.
const result = await execScriptCommand('git pull');
// console.log(result);
if (!result.stdout.includes('Already up to date.') && result.code === 0) {
console.log('GitSync - Reloading...'.yellow);
await reloadServer();
}
};

export const stopGitSync = async () => {
if (timer) {
clearInterval(timer);
}
};

export const startGitSync = async () => {
const HLAMBDA_GIT_SYNC_INTERVAL_SECONDS = parseInt(getEnvValue(constants.ENV_HLAMBDA_GIT_SYNC_INTERVAL_SECONDS), 10);
if (typeof HLAMBDA_GIT_SYNC_INTERVAL_SECONDS === 'number' && HLAMBDA_GIT_SYNC_INTERVAL_SECONDS !== 0) {
// All good
console.log('Git sync started!'.green);
} else {
console.log('Git sync is disabled. Git sync interval seconds are set to 0.'.red);
return;
}

timer = setInterval(async () => {
try {
console.log('[tick] Git Sync!'.yellow);
await gitSync();
} catch (error) {
console.log(error);
}
}, HLAMBDA_GIT_SYNC_INTERVAL_SECONDS * 1000);
};

export default startGitSync;