Skip to content

Commit

Permalink
add install.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
playground committed Jan 28, 2022
1 parent 7fd4016 commit 1d80fd4
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 14 deletions.
4 changes: 3 additions & 1 deletion bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
const fs = require('fs');
const os = require('os');
const jsonfile = require('jsonfile');
const hznConfig = '/etc/default/config';
const homePath = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
const hznConfig = `${homePath}/config`;
const constants = fs.constants;

const template = {
envHzn: {
Expand Down
1 change: 1 addition & 0 deletions build/commands/deploy.js

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

2 changes: 1 addition & 1 deletion build/commands/deploy.js.map

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

1 change: 1 addition & 0 deletions build/common/src/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Observable } from 'rxjs';
export declare class Utils {
homePath: string | undefined;
hznConfig: string;
constructor();
init(): void;
Expand Down
8 changes: 5 additions & 3 deletions build/common/src/utils.js

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

2 changes: 1 addition & 1 deletion build/common/src/utils.js.map

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/sh
set -e
# Install node and npm via nvm - https://github.com/nvm-sh/nvm

# Run this script like - bash script-name.sh

# Define versions
INSTALL_NODE_VER=16
INSTALL_NVM_VER=0.39.1

# You can pass argument to this script --version 8
if [ "$1" = '--version' ]; then
echo "==> Using specified node version - $2"
INSTALL_NODE_VER=$2
fi

echo "==> Ensuring .bashrc exists and is writable"
touch ~/.bashrc

echo "==> Installing node version manager (NVM). Version $INSTALL_NVM_VER"
# Removed if already installed
rm -rf ~/.nvm
# Unset exported variable
export NVM_DIR=

# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v$INSTALL_NVM_VER/install.sh | bash
# Make nvm command available to terminal
source ~/.nvm/nvm.sh

echo "==> Installing node js version $INSTALL_NODE_VER"
nvm install $INSTALL_NODE_VER

echo "==> Make this version system default"
nvm alias default $INSTALL_NODE_VER
nvm use default

#echo -e "==> Update npm to latest version, if this stuck then terminate (CTRL+C) the execution"
npm install -g npm

echo "==> Checking for versions"
nvm --version
node --version
npm --version

echo "==> Print binary paths"
which npm
which node

echo "==> List installed node versions"
nvm ls

nvm cache clear
echo "==> Now you're all setup and ready for development. If changes are yet to take effect in the current shell, try source ~/.bashrc or open a new shell"

echo "==> Installing oh cli"
npm i -g hzn-cli

echo "==> Checking oh version"
oh --version

echo "==> Setting up hzn environment"
oh deploy setup

# Tested on Ubuntu, MacOS
# @playground
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hzn-cli",
"version": "0.2.9",
"version": "0.3.0",
"description": "Open Horizon CLI toolkit helps streamline the process of preparing node agents and perform tasks between orgs environments",
"main": "./build/index.js",
"bin": {
Expand Down
1 change: 1 addition & 0 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const handler = (argv: Arguments<Options>): void => {
if(env.length == 0) {
let value = utils.getPropValueFromFile(`${utils.getHznConfig()}/.env-local`, 'DEFAULT_ORG')
env = value.length > 0 ? value : 'biz'
console.log('$$$env', env)
}
const proceed = () => {
if(existsSync(`${utils.getHznConfig()}/.env-hzn.json`) && existsSync(`${utils.getHznConfig()}/.env-local`)) {
Expand Down
17 changes: 10 additions & 7 deletions src/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const env = process.env.npm_config_env || 'biz';
const notRequired = ['SERVICE_CONTAINER_CREDS', 'MMS_CONTAINER_CREDS', 'MMS_OBJECT_FILE', 'HZN_CUSTOM_NODE_ID', 'UPDATE_FILE_NAME'];

export class Utils {
hznConfig = '/etc/default/config';
etcDefault = '/etc/default';
homePath = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
hznConfig = `${this.homePath}/config`;
constructor() {}
init() {
}
Expand Down Expand Up @@ -126,7 +128,7 @@ export class Utils {
this.shell(`curl -sSL https://raw.githubusercontent.com/open-horizon/devops/master/mgmt-hub/deploy-mgmt-hub.sh --output deploy-mgmt-hub.sh && chmod +x deploy-mgmt-hub.sh && sudo -s -E -b ./deploy-mgmt-hub.sh`)
.subscribe({
next: (res: any) => {
writeFileSync(`/etc/default/.secret`, res)
writeFileSync(`${this.hznConfig}/.secret`, res)
},
complete: () => observer.complete(),
error: (err) => observer.error(err)
Expand Down Expand Up @@ -278,6 +280,7 @@ export class Utils {
prompt.get({name: 'answer', required: true}, (err: any, question: any) => {
if(question.answer.toUpperCase() === 'Y') {
hznJson[org] = Object.assign({}, hznJson.biz);
console.log('$$$new org', org, hznJson)
this.updateOrgConfig(hznJson, org, true)
.subscribe({
next: () => observer.next({env: org}),
Expand Down Expand Up @@ -310,7 +313,7 @@ export class Utils {
console.log(`\nWould you like to save config files: Y/n?`)
prompt.get({name: 'answer', required: true}, (err: any, question: any) => {
if(question.answer.toUpperCase() === 'Y') {
this.copyFile(`sudo cp -rf ${__dirname}/config /etc/default`).then(() => {
this.copyFile(`sudo cp -rf ${__dirname}/config ${this.homePath}`).then(() => {
let content = '';
for(const [key, value] of Object.entries(result)) {
content += `${key}=${value}\n`;
Expand Down Expand Up @@ -358,7 +361,7 @@ export class Utils {
})
}
getHznInfo() {
return readFileSync('/etc/default/horizon').toString().split('\n');
return readFileSync(`${this.etcDefault}/horizon`).toString().split('\n');
}
showHznInfo() {
return new Observable((observer) => {
Expand Down Expand Up @@ -413,7 +416,7 @@ export class Utils {
}
updateHznInfo() {
return new Observable((observer) => {
let props = this.getPropsFromFile('/etc/default/horizon');
let props = this.getPropsFromFile(`${this.etcDefault}/horizon`);
console.log('\nKey in new value or (leave blank) press Enter to keep current value: ')
prompt.get(props, (err: any, result: any) => {
console.log(result)
Expand All @@ -425,9 +428,9 @@ export class Utils {
for(const [key, value] of Object.entries(result)) {
content += `${key}=${value}\n`;
}
this.copyFile('sudo cp /etc/default/horizon /etc/default/.horizon').then(() => {
this.copyFile(`sudo cp ${this.etcDefault}/horizon ${this.etcDefault}/.horizon`).then(() => {
writeFileSync('.horizon', content);
this.copyFile(`sudo mv .horizon /etc/default/horizon`).then(() => {
this.copyFile(`sudo mv .horizon ${this.etcDefault}/horizon`).then(() => {
observer.next();
observer.complete();
})
Expand Down

0 comments on commit 1d80fd4

Please sign in to comment.