• This repository has been archived on 11/Aug/2021
  • Stars
    star
    181
  • Rank 212,110 (Top 5 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created over 10 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ndm allows you to deploy OS-specific service-wrappers directly from npm-packages.

ndm

Build Status

ndm makes it easy to deploy a complex service-oriented-architecture by allowing you to deploy OS-specific service-wrappers directly from an npm package.

ndm currently supports Centos, OS X, and Ubuntu.

Table of Contents:

Installing

  • npm install ndm -g

You might need to run that as root with sudo.

Quick Start

How to build an ndm-ready package:

  1. Install ndm: sudo npm install ndm -g
  2. Create a project directory with a package.json: npm init
  3. Add service dependencies to your package.json: npm install my-service-module --save
  4. Generate your service.json: ndm init.
  5. Edit service.json to add appropriate args and envs for your server.
  6. When you're ready, generate service wrappers (upstart, initctl, etc): ndm install.
  7. Start the service wrappers you've just generated: ndm start.

Anatomy of an ndm service

ndm can run a single services or a collection of services. It's structured like an npm package, with a package.json file listing its dependencies. Each service you want to run with ndm should be packaged as its own separate npm module that the ndm wrapper depends on. Then a service.json file describes how to run each service.

An ndm wrapper package looks like this:

wrapper/
  package.json
  service.json
  logs/
  node_modules/

Service dependencies

A node-packaged service built for ndm can provide some hints in its package.json about how to run itself. Here's an example ndm-ready package.json:

{
  "name": "be-awesome",
  "version": "0.0.0",
  "description": "a service designed to be run with ndm.",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ssh2": "^0.2.25"
  },
  "devDependencies": {
    "mocha": "^1.20.0"
  },
  "script": {
    "start": "node ./bin/awesome.js"
  },
  "service": {
    "args": {
      "--verbose": "false"
    },
    "env": {
      "PORT": "5000"
    }
  },
}

Note the environment field and its subfields. environment.args is a map of arguments that should be passed to the ndm service. ndm.env is a map of environment variables that should be passed to your ndm service.

The service.json file

The ndm wrapper must also have a service.json file, which describes how to run the services. Run ndm init to generate service.json from your installed npm dependencies. The init script will copy default values from the environment stanza in each service's package.json. You can then edit the defaults if you need to change anything.

Here's an example:

{
  "baby-animals": {
    "description": "baby animal thumbnailing service",
    "scripts": {
      "start": "./baby-animal.js"
    },
    "env": {
      "PORT": "8000",
      "USER": "bcoe"
    },
    "args": {
      "--kitten": "cute"
    }
  },
  "ndm-test2": {
    "description": "the awesome service",
    "processes": 3,
    "module": "be-awesome",
    "scripts": {
      "start": "./bin/foo.js"
    },
    "env": {
      "PORT": "5000"
    },
    "args": {
      "--verbose": "false"
    }
  },
  "env": {
    "APP": "my-test-app-%i",
    "NODE_ENV": "production"
  },
  "args": {
    "--batman": "greatest-detective"
  }
}
  • module: the name of the npm module that should be the working directory for the service. If no module is specified, the key of the service will be used as the module name to look for.
  • description: description of the service.
  • scripts: scripts that can be executed by ndm. When generating service wrappers the start script is used.
  • env: string environment variables available within the script executed by the ndm wrapper.
  • args: command-line-arguments available to the script executed by the ndm wrapper.
  • processes: how many copies of the service should be run.
    • useful for taking advantage of multiple cpus, defaults to 1 process.
  • %i: %i is a place-holder for the process # if you're running multiple processes.
    • this can be useful if you want each process to bind to a different port, e.g., 800%i.

Defaults for all services are in the top-level env and args fields. Each services can override and extend the defaults in its own options stanza.

Updating dependencies

To add new dependencies:

  • Add a new dependency to the wrapper's package.json the way you would for any npm package:
    npm install <new-service> --save
  • Add the new service to service.json:
    ndm update.

Installing the services

To install your ndm-wrapped services, copy the package directory to your host system using whatever means you prefer. Then from inside the directory, run ndm install.

On systems like Ubuntu, you'll need to run this as root so ndm has permission to add the upstart config file to /etc/init. On OS X, you can run it as any user to create a local launch control script.

Command line arguments can be passed to the service wrapper at generation time, by appending them after --:

ndm install -- --verbose true

Starting and stopping

You can start and stop the services manually using your host's native daemon control: upstart, launchctl, or initctl. Or you can use ndm start and ndm stop from inside an ndm wrapper directory to start & stop all the wrapped services.

Tailing Logs

All console.log and console.error output is recorded in the logs/ directory, in files named <service-name>.log. This is separate from whatever internally-managed logging the service might do.

Interviewing the User

Rather than providing set-in-stone default values, you can opt to interview your user. To interview a user for important variables, write your default values in this form:

"env": {
  "HOST": {
    "default": "localhost",
    "description": "what host should I bind to?"
  }
}

By running ndm interview, a user will then be asked to fill in these values in an interactive manner:

Benjamins-MacBook-Air:ndm benjamincoe$ node ./bin/ndm.js interview
starting interview:
[?] url of front facing server: www.example.com
[?] what environment should we run the app in: test
[?] what do you think of dogs? I like 'em.

.ndmrc

Add an .ndmrc file to your home directory, to override ndm's default settings.

Variable names should be camel-case. As an example, the following .ndmrc would change the default logging location:

; override ndm CLI variables by adding
; them to a .ndmrc file. Variables should be
; cammel case.
logsDirectory=/foo/bar/logs

The ndm API

Rather than using the ndm bin to manage services, you can use the ndm API to create a self-installable service:

  1. add ndm to your package.json dependencies.
  2. add a service.json to the root of your module with a scripts stanza which includes:
  • a start script, which is what ndm will run by default.
  • any other scripts that you'd like to expose via runScript.

service.json example:

{
  "ndm-test": {
    "description": "ndm test service",
    "scripts": {
      "start": "node ./test.js",
      "echo": "echo hello",
      "node-echo": "node ./test2.js"
    },
    "env": {
      "PORT": 8000,
      "USER": {
        "description": "enter a username."
      }
    }
  }
}
  1. update your package's bin to look something like this (the argument passed to ndm's require is the name of the module in the sevice.json that you'd like to run):
#!/usr/bin/env node

var argv = require('yargs').argv,
  ndm = require('ndm')('ndm-test');

switch(argv._[0]) {
  case 'install':
    ndm.install();
    break;
  case 'remove':
    ndm.remove();
    break;
  case 'start':
    ndm.start();
    break;
  case 'restart':
    ndm.restart();
    break;
  case 'stop':
    ndm.stop();
    break;
  case 'list-scripts':
    ndm.listScripts();
    break;
  case 'run-script':
    ndm.runScript();
    break;
}

ndm-test is published to npm, try it out:

npm install ndm-test -g
ndm-test install
ndm-test start

Setting Node Flags

For each service in your service.json file, you can optionally set the following flags.

  • maxOldSpaceSize: sets the --max-old-space-size flag to to N megabytes.
{
  "foo": {
    "maxOldSpaceSize": "4096"
  }
}

Disclaimer

ndm is an experiment, based on ops challenges we've been facing at npm. This is a dot release. I'll be moving things around a lot in this library, as we use it for our own deployments.

The ndm stanza is not officially supported by npm.

LICENSE

ISC

More Repositories

1

npm

This repository is moving to: https://github.com/npm/cli
17,473
star
2

cli

the package manager for JavaScript
JavaScript
8,303
star
3

node-semver

The semver parser for node (the one npm uses)
JavaScript
4,772
star
4

npm-expansions

Send us a pull request by editing expansions.txt
JavaScript
2,209
star
5

tink

a dependency unwinder for javascript
JavaScript
2,153
star
6

ini

An ini parser/serializer in JavaScript
JavaScript
733
star
7

npx

npm package executor
JavaScript
731
star
8

rfcs

Public change requests/proposals & ideation
JavaScript
726
star
9

npm-registry-couchapp

couchapp bits of registry.npmjs.org
JavaScript
615
star
10

nopt

Node/npm Option Parsing
JavaScript
527
star
11

registry

npm registry documentation
449
star
12

npmlog

The logger that npm uses
JavaScript
423
star
13

marky-markdown

npm's markdown parser
JavaScript
405
star
14

arborist

npm's tree doctor
JavaScript
369
star
15

documentation

Documentation for the npm registry, website, and command-line interface.
MDX
369
star
16

pacote

npm fetcher
JavaScript
349
star
17

download-counts

Background jobs and a minimal service for collecting and delivering download counts
JavaScript
328
star
18

gauge

A terminal based horizontal guage aka, a progress bar
JavaScript
319
star
19

node-which

Like which(1) unix command. Find the first instance of an executable in the PATH.
JavaScript
305
star
20

init-package-json

A node module to get your node module started
JavaScript
284
star
21

validate-npm-package-name

Is the given string an acceptable npm package name?
JavaScript
282
star
22

cacache

npm's content-addressable cache
JavaScript
274
star
23

npm-merge-driver

git merge driver for resolving conflicts in npm-related files
JavaScript
271
star
24

npm-registry-client

JavaScript
264
star
25

lockfile

A very polite lock file utility, which endeavors to not litter, and to wait patiently for others.
JavaScript
259
star
26

registry-issue-archive

An archive of the old npm registry issue tracker
250
star
27

write-file-atomic

Write files in an atomic fashion w/configurable ownership
JavaScript
217
star
28

read-package-json

The thing npm uses to read package.json files with semantics and defaults and validation and stuff
JavaScript
214
star
29

roadmap

Public roadmap for npm
213
star
30

hosted-git-info

Provides metadata and conversions from repository urls for Github, Bitbucket and Gitlab
JavaScript
206
star
31

fstream

Advanced FS Streaming for Node
JavaScript
205
star
32

make-fetch-happen

making fetch happen for npm
JavaScript
190
star
33

read

read(1) for node.
JavaScript
187
star
34

normalize-package-data

normalizes package metadata, typically found in package.json file.
JavaScript
184
star
35

are-we-there-yet

Track complex hiearchies of asynchronous task completion statuses.
JavaScript
173
star
36

abbrev-js

Like ruby's Abbrev module
JavaScript
158
star
37

statusboard

Public monitor/status/health board for @npm/cli-team's maintained projects
JavaScript
152
star
38

security-holder

An npm package that holds a spot.
145
star
39

osenv

Look up environment settings specific to different operating systems.
JavaScript
137
star
40

feedback

Public feedback discussions for npm
133
star
41

npm-registry-fetch

like fetch() but for the npm registry
JavaScript
130
star
42

npm-package-arg

Parse the things that can be arguments to `npm install`
JavaScript
116
star
43

libnpm

programmatic npm API
JavaScript
113
star
44

npm-collection-staff-picks

JavaScript
112
star
45

npm-packlist

Walk through a folder and figure out what goes in an npm package
JavaScript
110
star
46

promzard

A prompting json thingie
JavaScript
101
star
47

npm-remote-ls

Examine a package's dependency graph before you install it
JavaScript
89
star
48

npmconf

npm config thing
JavaScript
75
star
49

cmd-shim

The cmd-shim used in npm
JavaScript
75
star
50

npm-tips

A collection of short (5 words or so) tips and tricks that can be sprinkled about the npm site.
JavaScript
73
star
51

www

community space for the npm website
68
star
52

policies

Privacy policy, code of conduct, license, and other npm legal stuff
Shell
67
star
53

package-json

Programmatic API to update package.json
JavaScript
65
star
54

git

a util for spawning git from npm CLI contexts
JavaScript
62
star
55

npm_conf

A conference about npm, maybe. Not to be confused with npmconf.
59
star
56

ignore-walk

Nested/recursive `.gitignore`/`.npmignore` parsing and filtering.
JavaScript
57
star
57

registry-follower-tutorial

write you a registry follower for great good
JavaScript
56
star
58

ssri

subresource integrity for npm
JavaScript
55
star
59

minipass-fetch

An implementation of window.fetch in Node.js using Minipass streams
JavaScript
54
star
60

ci-detect

Detect what kind of CI environment the program is in
JavaScript
53
star
61

run-script

Run a lifecycle script for a package (descendant of npm-lifecycle)
JavaScript
53
star
62

read-installed

Read all the installed packages in a folder, and return a tree structure with all the data.
JavaScript
52
star
63

mute-stream

Bytes go in, but they don't come out (when muted).
JavaScript
49
star
64

fs-write-stream-atomic

Like `fs.createWriteStream(...)`, but atomic.
JavaScript
48
star
65

read-package-json-fast

Like read-package-json, but faster
JavaScript
48
star
66

libnpmpublish

programmatically publish and unpublish npm packages
JavaScript
46
star
67

logical-tree

Calculates a nested logical tree using a package.json and a package lock.
JavaScript
44
star
68

read-package-tree

Read the contents of node_modules
JavaScript
42
star
69

jobs

41
star
70

unique-filename

Generate a unique filename for use in temporary directories or caches.
JavaScript
40
star
71

lock-verify

Report if your package.json is out of sync with your package-lock.json
JavaScript
38
star
72

fstream-ignore

JavaScript
37
star
73

benchmarks

The npm CLI's benchmark suite
JavaScript
36
star
74

npm-lifecycle

npm lifecycle script runner
JavaScript
36
star
75

wombat-cli

The wombat cli tool.
JavaScript
35
star
76

npme-installer

npm Enterprise installer
JavaScript
35
star
77

couch-login

A module for doing logged-in requests against a couchdb server
JavaScript
33
star
78

libnpmexec

npm exec (npx) Programmatic API
JavaScript
33
star
79

npm-audit-report

npm audit security report
JavaScript
33
star
80

ansible-nagios

Ansible role for building Nagios 4.
Perl
32
star
81

config

Configuration management for https://github.com/npm/cli
JavaScript
32
star
82

parse-conflict-json

Parse a JSON string that has git merge conflicts, resolving if possible
JavaScript
32
star
83

npm-profile

Make changes to your npmjs.com profile via cli or library
JavaScript
31
star
84

unique-slug

Generate a unique character string suitible for use in files and URLs.
JavaScript
31
star
85

fstream-npm

fstream class for creating npm packages
JavaScript
30
star
86

redsess

Yet another redis session thing for node.
JavaScript
30
star
87

concurrent-couch-follower

a couch follower wrapper that you can use to be sure you don't miss any documents even if you process them asynchronously.
JavaScript
28
star
88

npm-registry-mock

mock the npm registry
JavaScript
27
star
89

lint

lint the npmcli way
JavaScript
26
star
90

fs

filesystem helper functions, wrappers, and promisification for the npm cli
JavaScript
25
star
91

template-oss

a template package for npm CLI team development
JavaScript
25
star
92

libnpmsearch

programmatic API for the shiny new npm search endpoint
JavaScript
25
star
93

bin-links

.bin/ script linker
JavaScript
24
star
94

libnpmaccess

programmatic api for `npm access`
JavaScript
24
star
95

logos

official logos for npm, Inc
22
star
96

types

Typescript definitions for npm registry content
JavaScript
22
star
97

public-api

21
star
98

deprecate-holder

An npm package that holds a spot.
21
star
99

map-workspaces

Retrieves a name:pathname Map for a given workspaces config
JavaScript
20
star
100

libnpmversion

library to do the things that 'npm version' does
JavaScript
20
star