# bhoos-cli

Bhoos cli for internal use. Bhoos CLI can

+ install packages & applications
+ run applications throught Agent.
  + Agent takes care of respawing the worker process if they die
  + sends logs to ManagementServer
  + watches for updates, and gracefully updates running application
+ publish packages to the internal registry

also, you can add new commands to Bhoos CLI if they serve useful purpose to everyone (see CONTIBUTING#New Commands).

## Installation
1. Install the package. Either
```
	yarn global add bhoos-cli
```
2. Register your auth token and create config files:
```
bhoos auth --token <<TOKEN>> --name <<YOUR NAME>>
```
This will register your token to management server, and create/update `~/.bhoosrc` and `~/.npmrc` config files.

### Config files
These files are created by `bhoos auth` automatically.

1. ~/.bhoosrc file is created with following content:
```
	{
		"packageUrl":"https://pkg.bhoos.com",
		"cloudUrl":"https://cloud.bhoos.com",
		"accessToken" : "<<TOKEN>>"
	}
```
It specifies the registry and management server url, and your authentication token.

2. And .npmrc file will look like:

```
//npm.pkg.github.com/:_authToken=<<TOKEN>>
//npm.pkg.github.com/bhoos/:_authToken=<<TOKEN>>
//pkg.bhoos.com/:_authToken=<<TOKEN>>
@bhoos:registry=https://pkg.bhoos.com/

```

This directs yarn and npm to use `https://pkg.bhoos.com` as the registry for `@bhoos/` packages. If the requested pacakge is not found there the registry will itself redirect yarn|npm to the main registry `https://npm.pkg.github.com`. So, the token for both the registries must be same.


## Use

1. Installing a package

```
	bhoos install @bhoos/engine-server@latest
```
Some example pacakge name:
	- @bhoos/engine-server@dev
	- @bhoos/engine-server@1.0.1
	- @bhoos/engine-server@1.0.1-bpanthi


this will install the pacakge inside ~/.bhoos directory

2. Running the installed application
```
	bhoos run @bhoos/engine-server@latest
```

this will run the installed application (at ~/.bhoos) directory under the supervision of bhoos-cli Agent. It will then send logs, and other stats to the management server, and auto update itself if new updates arrive.


Note: Agent tries to store logs inside `/var/log/bhoos/` at `/var/log/bhoos/@bhoos` directory. So, make sure those directories exist and the application has write premissions. If they don't exist, log won't be presisted locally. However error logs are always send to management server (at the `cloudUrl` specified in `.bhoorc`). Also if you want logrotation create a config at /etc/logrotate.d/ accordingly (See CONTRIBUTING#LogRotation for a sample)

3. Publishing a package

```
	bhoos publish ./path/to/package/dir -m "publish message" -t "tag"
```

Publishes the application to registry. See `bhoos help publish` for further details.

There are three types of packages. And the publish action depends on the type of application being published.
+ `node-app` : These are application thay need to be run. node_modules is included in the bundle along with other files. So, there are no dependencies to install. Just the bundle file is sufficient.
+ `npm` : node_modules is not included. There are libraries intended to be imported in other projects.
+ `react-native`: Android and iOS bundles which can be loaded by mobile application are published.

Note:
+ once package is published to latest or staging, it is locked. And pacakges with same version cannot be published again.
+ `yarn build` command must be defined
+ For `react-native` application, `yarn setup` must also be defined. (See the FAQ section)
+ Published bundles contain only `dist` and `es6` files by default. If other files need to be bundled you have to specify them in package.json `files` field.
+ Bhoos CLI tries to determine the type of package but if it guesses wrong (shown during publish operation) , you have to specify the type of package in `bhoos`->`pkg-type` field of package.json


4. Changing tag of published package (i.e. tagging package)

```
	bhoos tag ./ -t latest
```
Changes the tag of package at `./` to `latest`. (name and version number is taken from package.json)
```
	bhoos tag -pkg @bhoos/management-server@1.0.2 -t staging
```
Changes the tag of `@bhoos/management-server@1.0.2` package to `staging`


## FAQ

### Checksum / Integrity check failed
Q: I tried to `yarn` or `yarn install` in my repo and yarn says checksum failed for a package I had published to registry proxy.
Solution: remove your yarn.lock file. Even better, remove the entry for the offending package from the yarn.lock file.

Reason: This happens when you have published a version multiple times in the registry proxy, and `yarn install`ed in the past. So, yarn remembers the checksum of the older package it installed. This is completely normal, and a result of our intented use cases.

### Publish your react native bundle to Management Server
`bhoos publish` on a react native project is intended to create react native bundles for ios and android and then upload them to server. But this requires some hacks, and configuration.

1. Create a `metro-asset-plugin.js` [See at doc/] file in your react native project and include this plugin in the `metro.config.js`:

```javascript
module.exports = {
  transformer: {
	//...
	assetPlugins: [require.resolve('./metro-asset-plugin')],
	//...
  },
  // ...
};
```

2. In your `package.json` make following changes:

 2.1. Specify that your package is a react-native package and be published by `bhoos publish` as such. To do this, add a field in the package.json:
```json
  "bhoos": {
	"pkg-type": "react-native"
  },

```
 2.2. Specify the root react-native directory (default: './')  relative to package.json and the  location of react native entry file (default: './index.js') relative to the root directory. E.g.
```json
	"bhoos" : {
	   //...
	   "react-native" : {
		   "root" : "./",
		   "entry-file" : "./index.js"
	   }
	}

```

 2.3. Add a `setup` script to `package.json` that runs the `fixResolveAssetSource.js` (See at doc/) script. This script patches the react-native package to fit our needs.
```json
  "scripts" : {
	// ...
	"setup" : "node fixResolveAssetSource.js"
  }
```
