Skip to content

Instantly share code, notes, and snippets.

@nellshamrell
Last active October 4, 2018 20:03
Show Gist options
  • Save nellshamrell/3976f9a55faaf90b2ea09b55954e1cda to your computer and use it in GitHub Desktop.
Save nellshamrell/3976f9a55faaf90b2ea09b55954e1cda to your computer and use it in GitHub Desktop.

Creating the app

mkdir my-node-app
cd my-node-app
touch app.js

app.js

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Creating the plan

habitat/plan.sh

pkg_name=01-basic
pkg_origin=core
pkg_version="0.1.0"
pkg_maintainer="The Habitat Maintainers <[email protected]>"
pkg_license=("Apache-2.0")
# pkg_source="http://some_source_url/releases/${pkg_name}-${pkg_version}.tar.gz"
# pkg_filename="${pkg_name}-${pkg_version}.tar.gz"
# pkg_shasum="TODO"
pkg_deps=(core/node)
pkg_build_deps=(core/make core/gcc)
# pkg_lib_dirs=(lib)
# pkg_include_dirs=(include)
# pkg_bin_dirs=(bin)
# pkg_pconfig_dirs=(lib/pconfig)
# pkg_svc_run="haproxy -f $pkg_svc_config_path/haproxy.conf"
# pkg_exports=(
#   [host]=srv.address
#   [port]=srv.port
#   [ssl-port]=srv.ssl.port
# )
# pkg_exposes=(port ssl-port)
# pkg_binds=(
#   [database]="port host"
# )
# pkg_binds_optional=(
#   [storage]="port host"
# )
# pkg_interpreters=(bin/bash)
# pkg_svc_user="hab"
# pkg_svc_group="$pkg_svc_user"
# pkg_description="Some description."
# pkg_upstream_url="http://example.com/project-name"

do_build() {
  # The mytutorialapp source code is in a relative directory, so you must copy the
  # contents of the source directory into your $CACHE_PATH as this
  # is the same path that Habitat would use if you downloaded a tarball of the source code.
  cp -vr $PLAN_CONTEXT/../app.js $CACHE_PATH
}

do_install() {
    return 0
}
hab studio enter
(studio) build
# watch build successfully
(studio) exit

habitat/plan.sh

pkg_name=01-basic
pkg_origin=core
pkg_version="0.1.0"
pkg_maintainer="The Habitat Maintainers <[email protected]>"
pkg_license=("Apache-2.0")
# pkg_source="http://some_source_url/releases/${pkg_name}-${pkg_version}.tar.gz"
# pkg_filename="${pkg_name}-${pkg_version}.tar.gz"
# pkg_shasum="TODO"
pkg_deps=(core/node)
pkg_build_deps=(core/make core/gcc)
# pkg_lib_dirs=(lib)
# pkg_include_dirs=(include)
# pkg_bin_dirs=(bin)
# pkg_pconfig_dirs=(lib/pconfig)
# pkg_svc_run="haproxy -f $pkg_svc_config_path/haproxy.conf"
# pkg_exports=(
#   [host]=srv.address
#   [port]=srv.port
#   [ssl-port]=srv.ssl.port
# )
# pkg_exposes=(port ssl-port)
# pkg_binds=(
#   [database]="port host"
# )
# pkg_binds_optional=(
#   [storage]="port host"
# )
# pkg_interpreters=(bin/bash)
# pkg_svc_user="hab"
# pkg_svc_group="$pkg_svc_user"
# pkg_description="Some description."
# pkg_upstream_url="http://example.com/project-name"

do_build() {
  # The mytutorialapp source code is in a relative directory, so you must copy the
  # contents of the source directory into your $CACHE_PATH as this
  # is the same path that Habitat would use if you downloaded a tarball of the source code.
  cp -vr $PLAN_CONTEXT/../app.js $CACHE_PATH
}

do_install() {
  # Our source files were copied over to $CACHE_PATH in do_build(),
  # and now they need to be copied from that directory into the root directory of our package
  # through the use of the pkg_prefix variable.
  cp $CACHE_PATH/app.js ${pkg_prefix}
}
hab studio enter
(studio) build
# watch build successfully
(studio) hab start core/01-basic
# errors because it cannot find run file 
(studio) exit

habitat/hooks/init

#!/bin/sh
#
ln -sf {{pkg.path}}/app.js {{pkg.svc_var_path}}

habitat/hooks/run

#!/bin/sh
#

cd {{pkg.svc_var_path}}

# `exec` makes it so the process that the Habitat supervisor uses is
# `node server.js`, rather than the run hook itself. `2>&1` makes it so both
# standard output and standard error go to the standard output stream,
# so all the logs from the application go to the same place.

exec node app.js 2>&1

Running the plan

habitat/default.toml

# Use this file to templatize your application's native configuration files.
# See the docs at https://www.habitat.sh/docs/create-packages-configure/.
# You can safely delete this file if you don't need it.

listening_port = 3000

habitat/config/config.json

{
	"port": "{{cfg.listening_port}}"
}
pkg_name=01-basic
pkg_origin=core
pkg_version="0.1.0"
pkg_maintainer="The Habitat Maintainers <[email protected]>"
pkg_license=("Apache-2.0")
# pkg_source="http://some_source_url/releases/${pkg_name}-${pkg_version}.tar.gz"
# pkg_filename="${pkg_name}-${pkg_version}.tar.gz"
# pkg_shasum="TODO"
pkg_deps=(core/node)
pkg_build_deps=(core/make core/gcc)
# pkg_lib_dirs=(lib)
# pkg_include_dirs=(include)
# pkg_bin_dirs=(bin)
# pkg_pconfig_dirs=(lib/pconfig)
# pkg_svc_run="haproxy -f $pkg_svc_config_path/haproxy.conf"
# pkg_exports=(
#   [host]=srv.address
#   [port]=srv.port
#   [ssl-port]=srv.ssl.port
# )
# pkg_exposes=(port ssl-port)
# pkg_binds=(
#   [database]="port host"
# )
# pkg_binds_optional=(
#   [storage]="port host"
# )
# pkg_interpreters=(bin/bash)
# pkg_svc_user="hab"
# pkg_svc_group="$pkg_svc_user"
# pkg_description="Some description."
# pkg_upstream_url="http://example.com/project-name"

pkg_exports=( [port]=listening_port )
pkg_exposes=(port)

do_build() {
  # The mytutorialapp source code is in a relative directory, so you must copy the
  # contents of the source directory into your $CACHE_PATH as this
  # is the same path that Habitat would use if you downloaded a tarball of the source code.
  cp -vr $PLAN_CONTEXT/../app.js $CACHE_PATH
}

do_install() {
  # Our source files were copied over to $CACHE_PATH in do_build(),
  # and now they need to be copied from that directory into the root directory of our package
  # through the use of the pkg_prefix variable.
  cp $CACHE_PATH/app.js ${pkg_prefix}
}

Running in VM

Have AWS Ubuntu VM ready to go:

Need to expose these ports in security group:

  • 3000 (http)
  • 22 (ssh)
  • 9631 (tcp)
  • 9638 (tcp)
  • 9638 (udp)
scp -i ~/.aws/nshamrell.pem ./results/<thing>.hart ubuntu@<instance_public_ip>:.
$ curl https://raw.githubusercontent.com/habitat-sh/habitat/master/components/hab/install.sh | sudo bash
$ sudo groupadd hab
$ sudo useradd -g hab hab
$ sudo hab install <thing>.hart 
$ sudo hab sup run core/01-basic

Access public ip of AWS VM:3000

Running in Container

hab studio enter
(studio) hab pkg export docker ./results/<thing>.hart
(studio) exit
docker run -it -p 3000:3000 core/01-basic

Running from tarball

Make new AWS VM (Don't install anything)

hab studio enter
(studio) hab pkg export tar ./results/<thing>.hart
(studio) exit
scp -i ~/.aws/nshamrell2.pem ~/<package-id>.tar.gz ubuntu@<vm id>:.
ssh ubuntu@<vm id>
sudo adduser --group hab
sudo useradd -g hab hab
sudo tar xf your-origin-package-version-timestamp.tar.gz
sudo cp -R hab /hab 
sudo /hab/bin/hab sup run &
sudo /hab/bin/hab svc load <ORIGIN>/<NAME>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment