包详细信息

foreman

strongloop372.6kMIT3.0.1

Node Implementation of Foreman

foreman, upstart, commandline, env

自述文件

Node Foreman Build Status

Node Foreman is a Node.js version of the popular Foreman tool, with a few Node specific changes.

Foreman is a manager for Procfile-based applications. Its aim is to abstract away the details of the Procfile format, and allow you to either run your application directly or export it to some other process management format.

Install

Install the command line tool

$ npm install -g foreman

Get usage

$ nf --help

Deviations from the original Foreman

  • Each worker has an additional automatic environment variable, FOREMAN_WORKER_NAME, that contains the process name and worker number.
    • example: web.1, worker.1

How to Contribute

I encourage anyone and everyone to help. If you have a specific change in mind, open an issue; we can talk about it there.

If you would like to make a code change, go ahead. Fork the repository, open a pull request. Do this early, and talk about the change you want to make. Maybe we can work together on it.

Refactor Refactor Refactor! You are free to add features, or just help clean things up.

Usage

Node Foreman can be run with as little as nf start, as long as npm start has been defined. For more complicated applications you will want to define a Procfile for your various server processes and and a .env file to preload environmental variables.

Your module directory should end up looking like the following:

List Foreman Directory

Once your Procfile is defined, run your application with nf start:

Start Foreman

Node Foreman always starts in the foreground and expects your applications to do the same. If your processes exit, Node Foreman will assume an error has occurred and shut your application down.

Instead of daemonizing, you should use nf export to ready your application for production.

For more information try any of the following:

$ nf --help
$ nf start --help
$ nf run --help
$ nf export --help

Procfile

The Procfile format is a simple key : command format:

web: node web_server.js
api: node api_server.js
log: node log_server.js

Each line should contain a separate process.

Environmental Variables

Create a .env file to pre-load environmental variables with the format:

MYSQL_NAME=superman
MYSQL_PASS=cryptonite

The equivalent .env file may alternatively be a valid JSON document:

{
    "mysql":{
        "name": "superman",
        "pass": "cryptonite"
    }
}

The above JSON document will be flattened into env variables by concatenating the nested values with an underscore. Environmental variables are passed in fully capitalized.

{
    "mysql":{
        "name": "superman",     # => MYSQL_NAME=superman
        "pass": "cryptonite"    # => MYSQL_PASS=cryptonite
    }
}

There is no need to specify which type of file you wish to use.

The PATH environment variable

The PATH variable is given special treament and is always read from the environment that the nf command has been executed from, rather than a .env file. To set a different PATH execute nf with the PATH variable set appropriately.

PATH=/opt/foo:/opt/bar:$PATH nf export

Best Practices

Generally you should not check your .env file into version control. The .env file contain only parameters that depend on where the application gets deployed. It should not contain anything related to how the application is deployed.

For example, good candidates for the .env file are MySQL connection information, port bindings, and other passwords.

The Run Command

Tasks or commands that require the environment variables from the .env file can be initiated by using nf run <command>.

Advanced Usage

Node Foreman lets you start multiple jobs of the same type:

$ nf start web=5

18:51:12: web.1     |  Web Server started listening on 0.0.0.0:5000
18:51:12: web.2     |  Web Server started listening on 0.0.0.0:5001
18:51:12: web.3     |  Web Server started listening on 0.0.0.0:5002
18:51:12: web.4     |  Web Server started listening on 0.0.0.0:5003
18:51:12: web.5     |  Web Server started listening on 0.0.0.0:5004

Each job will be started as its own process, receiving a different PORT environmental variable. The port number for processes of the same type will be offset by 1. The port number for processes of different types will be offset by 100.

$ nf start web=2,api=2

18:51:12: web.1     |  Web Server started listening on 0.0.0.0:5000
18:51:12: web.2     |  Web Server started listening on 0.0.0.0:5001
18:51:12: api.1     |  Api Server started listening on 0.0.0.0:5100
18:51:12: api.2     |  Api Server started listening on 0.0.0.0:5101

Export to Production

Node Foreman is designed to be in a development environment, however it can export an Upstart job for use in production. The Upstart file has no dependency on Node Foreman.

$ nf export
Loaded ENV .env File as JSON Format
Wrote  :  ./foreman-web-1.conf
Wrote  :  ./foreman-web.conf
Wrote  :  ./foreman-api-1.conf
Wrote  :  ./foreman-api.conf
Wrote  :  ./foreman-log-1.conf
Wrote  :  ./foreman-log.conf
Wrote  :  ./foreman.conf

You can inspect your upstart files before placing them in the right directory, or have foreman do it for you:

$ sudo nf export -o /etc/init
Loaded ENV .env File as JSON Format
Wrote  :  /etc/init/foreman-api-1.conf
Wrote  :  /etc/init/foreman-web.conf
Wrote  :  /etc/init/foreman-api.conf
Wrote  :  /etc/init/foreman-log.conf
Wrote  :  /etc/init/foreman-log-1.conf
Wrote  :  /etc/init/foreman-web-1.conf
Wrote  :  /etc/init/foreman.conf

Start and stop your jobs with

$ sudo start foreman
$ sudo stop foreman

The export will occur with whatever environmental variables are listed in the .env file.

Systemd Support

This section is beta

Optionally specify a type -t systemd during export for systemd support.

Supervisord Support

You can also use a type -t supervisord during export for supervisord support.

This will generate a APP.conf file grouping the application processes and a APP-PROCESS-N.conf file for each process.

$ nf export --type supervisord
Loaded ENV .env File as JSON Format
Wrote  :  ./foreman-web-1.conf
Wrote  :  ./foreman-api-1.conf
Wrote  :  ./foreman-log-1.conf
Wrote  :  ./foreman.conf

You can start / stop / restart individual processes.

$ sudo supervisorctl start 'foreman:foreman-web-1'
$ sudo supervisorctl stop 'foreman:foreman-web-1'
$ sudo supervisorctl restart 'foreman:foreman-web-1'

Or the entire group of processes

$ sudo supervisorctl start 'foreman:*'
$ sudo supervisorctl stop 'foreman:*'
$ sudo supervisorctl restart 'foreman:*'

Advanced Exports

You can specify the type and number of processes exported using the type=num syntax:

$ nf export web=2,api=2

Use -u <USER> to have the exported job run as USER. Note that if you need to bind to privileged ports, you must start as root. In such a case, we advise you to drop user permissions after binding.

If you want to call your upstart job something other than foreman, use -a <JOBNAME> and manage your jobs with sudo start <JOBNAME>.

Reverse Proxy

Node.js processes are supposed to be stateless. Application scale by starting multiple processes that either share a socket, or sit behind a load balancer. Node Foreman can help you test the parallel capabilities of your application by spawning multiple processes behind a round-robin proxy automatically.

$ nf start -x 8888 web=5
[OKAY] Starting Proxy Server 8888 -> 5000-5004

Access your application from port 8888 and the connections will be balanced across the servers started from ports 5000 - 5004.

If your application gets its port number from process.env.PORT the proxy setup will ocurr automatically.

Multiple Reverse Proxies

If you have multiple processes in your Procfile you can start multiple proxies.

$ nf start -x 8888,8080,9090

This will start 3 separate proxies and bind each to a separate process group. Proxies are bound based on their order specified, their order in the Procfile, or by their order on the command line.

$ nf start -x 8888,9999 web,api

Privileged Ports

Node Foreman disallows applications from starting on privileged ports. It does however allow proxies to be bound to lower ports, such as port 80.

If you require access to a privileged port, start Node Foreman with sudo:

$ sudo nf start -x 80 web=5
[OKAY] Starting Proxy Server 80 -> 5000-5004

Your application will then be accessible via port 80, but it will be running as root.

Forward Proxy

Local development and testing has huge advantages, but sometimes one needs to test web applications agains their real-world domain name. Editing /etc/hosts is a pain however, and error prone.

Node Foreman can start up an HTTP forward proxy which your browser can route requests through. The forward proxy will intercept requests based on domain name, and route them to the local application.

$ nf start -f 9999 -i nodefly.com
[OKAY] Forward Proxy Started in Port 9999
[OKAY] Intercepting requests to nodefly.com through forward proxy

A forward proxy is useful when testing OAuth, or other external services with application callbacks.

For users with Google Chrome, this can be paired with FelisCatus SwitchyOmega for great results.

更新日志

2018-06-28, Version 3.0.1

  • remove unnecessary use of regex (Ryan Graham)

  • Fixed typo on forward proxy (Cemal Eker)

  • Fixed typo (Leopold Bradley)

  • don't log warning if env file doesn't exist (Ryan Graham)

2018-05-21, Version 3.0.0

  • Update outdated dependencies to latest (Miroslav Bajtoš)

  • Fix error "Unknown signal: null" (Kaspar Schiess)

  • Update supported Node.js versions (Miroslav Bajtoš)

2016-08-25, Version 2.0.0

  • Update URLs in CONTRIBUTING.md (#123) (Ryan Graham)

  • update copyright statements (Ryan Graham)

  • replace mu2 with mustache (Ryan Graham)

  • enable node v6 on CI (Ryan Graham)

2016-04-30, Version 2.0.0-1

  • output environment varialbes in predicatable order (Ryan Graham)

  • Allow loading multiple ENV files. (Joe Esposito)

  • add --raw option to suppress log decorations (Andrew Herrington)

  • Changed handler name to error instead of proxyError (Nazar Hussain)

  • improve error messages when loading a procfile (toastynerd)

  • fix exported systemd unit dependencies (Guten Ye)

  • test: refactor tests to use ephemeral ports (Ryan Graham)

  • clean up CLI argument handling (Ryan Graham)

  • deps: upgrade commander (Ryan Graham)

  • fix padding of keys (Jeff Dickey)

  • fix 'nf' command name (Jeff Dickey)

  • disable trimming by default (Jeff Dickey)

  • reorderd colors (Jeff Dickey)

  • disable black/white colors (Jeff Dickey)

  • display exit code (Jeff Dickey)

  • test: use OS-specific env expansion (Ryan Graham)

  • test: improve assertion message (Ryan Graham)

  • disable node 6.x in Travis and Appveyor (Ryan Graham)

  • update Travis config and add Appveyor (Ryan Graham)

  • Remove note about dropping privileges (#108) (Ari Pollak)

  • fix jshint issues (Jeff Dickey)

  • jshint all local js files (Jeff Dickey)

  • added jshintignore (Jeff Dickey)

  • Add '' argument to sed -i to fix mac os tests (Ransom Briggs)

  • Small typo fix in README (Simon Taranto)

2015-11-18, Version 2.0.0-0

  • Dont' drop sudo (Vasiliy Yorkin)

  • Cleanup: No need to duplicate the color keys, can use Object.keys() to ext from colors object (Sathish Kumar)

  • Adds support for SSL proxy (Jeff Kolesky)

  • update package scripts to be more portable (Ryan Graham)

  • Fix README to reflect Proxy SwitchSharp is no longer mantained (Leo Gallucci)

  • fix forward proxy http-proxy usage (Victor Kotseruba)

  • fix spawning multiple proxies (Victor Kotseruba)

2015-06-09, Version 1.4.1

  • package: don't include tests and fixtures (Ryan Graham)

  • update http-proxy (Victor Kotseruba)

2015-06-03, Version 1.4.0

  • Lint in travis (jigsaw)

  • fix iojs version (jigsaw)

  • support node 0.12.x and iojs 1.6.x (jigsaw)

  • refactor lib/requirements (jigsaw)

  • refactor lib/proxy (jigsaw)

  • refactor lib/procfile (jigsaw)

  • refactor lib/proc (jigsaw)

  • refactor lib/forward (jigsaw)

  • refactor lib/exporters (jigsaw)

  • refactor lib/envs (jigsaw)

  • refactor console (jigsaw)

  • refactor lib/colors.js (jigsaw)

  • refactor forward.js (jigsaw)

  • refactor proxy.js and allow snake case (jigsaw)

  • refactor nf.js for jshint (jigsaw)

  • add jshint configure file (jigsaw)

2015-01-23, Version 1.3.0

  • Preserve indentation, but trim trailing whitespace (Ryan Graham)

  • style: tabs to 2 spaces, clean up indentation only (Ryan Graham)

2015-01-12, Version 1.2.1

  • Fix bad CLA URL in CONTRIBUTING.md (Ryan Graham)

2014-12-26, Version 1.2.0

  • fix: honour PORT if set in .env file (Ryan Graham)

  • cli: simplify version loading (Ryan Graham)

2014-11-04, Version 1.1.0

  • Propagate signals on exit (Ryan Graham)

  • Fix proxy errors on Windows (Ryan Graham)

  • Exit with non-zero on fatal error (Ryan Graham)

  • Adds a run command for running one-off processes (Jeff Jewiss)

  • Add contribution guidelines (Ryan Graham)

2014-09-29, Version 1.0.0

  • Don't delete files (Ryan Graham)

  • [api] SMF export (for Solaris, Illumos, SmartOS) (Charles Phillips)

2014-08-15, Version 0.4.2

  • Remove quotes from spawn on Windows (Ryan Graham)

2014-08-11, Version 0.4.1

  • Regen CHANGES.md with updated script (Ryan Graham)

  • Use spawn for worker processes (Ryan Graham)

  • Update README so CLI's name, nf, is prominent (Sam Roberts)

2014-07-08, Version 0.4.0

  • Allow overriding supervisord templates (Ryan Graham)

  • Update supervisord export instructions (Ryan Graham)

  • Fix the tests (Bret Little)

  • Add the ability to comment out tasks in a Procfile satisfying request #42 (Bret Little)

  • Fix inconsistent indentation style (Sam Roberts)

  • Remove package reference to non-existent index.js (Sam Roberts)

  • test: Update to test --cwd option (Ryan Graham)

  • export: Let --cwd accept absolute or relative path (Ryan Graham)

  • Add informations on supervisord export to README.md (Paul d'Hubert)

  • Allow cwd to be passed on CLI. (Paul d'Hubert)

  • Add --template option to export command (Ryan Graham)

  • Ignore ANSI escapes when doing line trimming (Ryan Graham)

  • Expand tabs to spaces (Paul d'Hubert)

  • Add test files for supervisord templates. (Paul d'Hubert)

  • Do not set a default cwd to '.' (Paul d'Hubert)

  • Fix an error when escaping numbers.. (Paul d'Hubert)

  • Escape supervisord envs values (Paul d'Hubert)

  • Correctly export supervisord group file (Paul d'Hubert)

  • Scope the 'i' variable in export's foreman_app_n loop (Paul d'Hubert)

  • Add 'n' to writeout.foreman's conf (Paul d'Hubert)

  • Add supervisord exporter (Paul d'Hubert)

2014-04-10, Version 0.3.0

  • Handle env values with = in them (Ryan Graham)

  • Add new FOREMAN_WORKER_NAME automatic variable (Ryan Graham)

  • export: Improve comments in Upstart-single jobs (Ryan Graham)

  • export: Don't export PATH env var to subjobs (Ryan Graham)

  • env: Don't override PATH if set in .env (Ryan Graham)

  • export: Quote env var values in Upstart jobs (Ryan Graham)

  • upstart-single: Add coredump limits (Ryan Graham)

  • Fixed typo (Brian Gonzalez)

2014-03-28, Version 0.2.1

  • proxy: Fix regression caused by http-proxy upgrade (Ryan Graham)

2014-03-28, Version 0.2.0

  • Release v0.2.0 (Ryan Graham)

  • Ignore some CI files (Ryan Graham)

  • test: Some useful example files (Ryan Graham)

  • Disable colours if stdout is not a TTY (Ryan Graham)

  • procfile: Use real shell expansion for commands (Evan Owen)

  • test: Make PATH CI portable (Ryan Graham)

  • Add Travis-CI (Ryan Graham)

  • test: Use sed as fallback because sed may === gsed (Ryan Graham)

  • test: Improve test portability (Ryan Graham)

  • export: Use runlevels in Upstart jobs (Ryan Graham)

  • Version dependencies (Ryan Graham)

2014-03-27, Version 0.1.2

  • Ignore npm packed tarballs (Ryan Graham)

2014-03-27, Version 0.1.1

  • env: Ignore quoted #'s (Ryan Graham)

  • test: Failing test for # handling in .env (Ryan Graham)

2014-03-27, Version 0.1.0

  • git: Ignore test/sandbox (Ryan Graham)

  • export: Don't auto-set PORT for upstart-single (Ryan Graham)

  • export: Use system logger for upstart-single (Ryan Graham)

  • export: Initial support for flattend upstart layout (Ryan Graham)

  • test: use strict PATH for tests (Ryan Graham)

  • test: Basic tests for current exporters (Ryan Graham)

  • refactor: Rename lib/upstart.js to be more accurate (Ryan Graham)

  • fix: regression from colors replacement (Ryan Graham)

  • test: Add basic tests for console logger (Ryan Graham)

  • console: Parameterize console for better testing (Ryan Graham)

  • src: replace dependency on colors module (Ryan Graham)

  • src: refactor .env parsing/loading (Ryan Graham)

  • test: Initial tests for .env parsing (Ryan Graham)

  • doc: Add CHANGES.md (Ryan Graham)

2014-03-18, Version 0.0.27

  • fix: Allow - in app-name in Procfile (Ryan Graham)

2014-02-06, Version 0.0.26

  • Bump for license and home change (Ryan Graham)

  • Update imaage URLs in README (Ryan Graham)

  • Change to MIT license (Ryan Graham)

  • Update owner and URLs (Ryan Graham)

2013-02-13, Version 0.0.25

  • Version Bump (Jacob Groundwater)

  • Warn on Unmatched Variable Substitutions in Procfile (Jacob Groundwater)

  • Support both $var and ${var} style variables (Larz Conwell)

  • Procfile can use environment variables from process.env, and any defined in the loaded env file. variables defined in a .env file take precendence over process.env vars. (Larz Conwell)

2013-02-01, Version 0.0.24

  • Version Bump (Jacob Groundwater)

  • Add Forward Proxy Section to README (Jacob Groundwater)

  • Rename Reverse Proxy Titles in README (Jacob Groundwater)

  • Handles Windows Procfiles with CRLF (Ethan J. Brown)

2013-01-23, Version 0.0.23

  • Version Bump (Jacob Groundwater)

  • Fix My Foolishness (Jacob Groundwater)

2013-01-23, Version 0.0.22

  • Version Bump (Jacob Groundwater)

  • Not being so precautious with quot marks (John Wright)

2013-01-22, Version 0.0.21

  • First release!