Launching Node.js app on OpsWorks - node.js

I'm having trouble getting my OpsWorks custom recipe to launch my node application. I'm just starting to learn the fundamentals of Chef, so I may be missing something rather obvious.
To get me started I forked a copy of AWS nodejs cookbook and made very minor changes. I was also sure to copy the node deploy script here.
My layers are now fully configured and I can trigger node manually via SSH, however it seems that upon deploy it's not triggered by the Ruby script.
I'm assuming the start program line (which is the only place in the cookbook where I can see node being started) isn't getting run:
check host node_web_app_<%= #application_name %> with address 127.0.0.1
start program = "/bin/bash -c 'cd <%= #deploy[:deploy_to] %>/current; source <%= #deploy[:deploy_to] %>/shared/app.env; /usr/bin/env PORT=<%= #deploy[:nodejs][:port] %> NODE_PATH=<%= #deploy[:deploy_to] %>/current/node_modules:<%= #deploy[:deploy_to] %>/current /usr/local/bin/node <%= #monitored_script %>' --NODE_ENV=prod"
stop program = "/usr/bin/pkill -f 'node <%= #monitored_script %>'"
<% if #deploy[:ssl_support] -%>
if failed port <%= #deploy[:nodejs][:port] %> type TCPSSL protocol HTTP
<% else -%>
if failed port <%= #deploy[:nodejs][:port] %> protocol HTTP
<% end -%>
request /
with timeout 10 seconds
then restart
So I'm really at a loss for why it's not doing it's thing.
It almost seems as though this deploy cookbook wasn't fully completed. If there's a better cookbook available that someone can suggests, I'd consider changing.
Thanks.

Using the AWS opsworks nodejs layer, you should be able to set environment variables during App registration. You should see a section like this:
under app settings:
Where you can set all your variables to be included in the deployment.

Related

Webpacker can't find chartkick

I followed the documentation from https://chartkick.com/ on a fresh Rails 6 install but I get the following:
Webpacker can't find chartkick in /home/sbstn/codes/desafio_latam/g24/chart_demo/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
"application.js": "/packs/js/application-0db4c39e559fa03658c6.js",
"application.js.map": "/packs/js/application-0db4c39e559fa03658c6.js.map",
"entrypoints": {
"application": {
"js": [
"/packs/js/application-0db4c39e559fa03658c6.js"
],
"js.map": [
"/packs/js/application-0db4c39e559fa03658c6.js.map"
]
}
}
}
I've made a partial with the following and rendered inside an ActiveAdmin panel:
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag "application", "chartkick" %>
<% my_chart = {"ejemplo1": 20, "ejemplo2": 30} %>
<%= bar_chart my_chart %>
But plain simple examples form de oficial documentation didn't work on a normal view either.
Could you please provide some advice on how to integrate chartkick with webpacker and activeadmin
It was my fault. Adding the gem is a must even if you add the requirements with yarn.
Thanks

Set webpack environmental variable for apache2

Is it possible to fetch a webpack envvar outside the node / js scope?
I'm developing with vueJS and TYPO3 and want to load the JS files from node server, when it runs. Otherwise, I want to load the built JS files from the project folder.
TYPO3 has conditions for file loading, in dependence of an Apache envvar.
SetEnv TYPO3_CONTEXT Development
Questions:
Is it possible to set an apache2 envvar when launching a node devserver by webpack?
If not, is it possible to hook in that process and write a temporary htaccess file with the var in it, place it in a specific directory and delete it, when I stop the node server?
Background:
In TYPO3, js and css includes are configured by TypeScript.
In Prod Env:
page.includeJSFooter.app = path/to/build/name.js
This load can be changed by condition:
[applicationContext = Development]
page.includeJSFooter.app = http://192.168.100.38:8080/app.build.js
[end]
Now I want to set this context, as soon as I start my node dev server that builds the files on the fly:
webpack-dev-server --open --config webpack.dev.js
I doubt this is possible but as a solution I would propose to implement a custom condition (described here in the manual https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/TypoScriptSyntax/TypoScriptParserApi/CustomConditions.html) and check within that e.g with a request if the node server is responding.
Hi Georg and all others,
a custom condition would be a solution, but I don't want to influence the production environment (e.g. pinging for a response from node server).
I made it with two shell scripts, executed by webpack-shell-plugin.
The first script is triggered "onBuildStart", looking for an .htaccess file and adding
SetEnv TYPO3_CONTEXT Development/NodeServer
to the end of the file.
Unfortunately there is no event fired when the node server is shut down. As a workaround, I added a new npm run script, which just executes a shell script to delete that line from .htaccess again.
I guess there is a besser solution, but at this time, it solves my (very little) problem to avoid changing the JS includes every time I start to develop.

Heroku can't find this particular image path. Why?

I have deployed an app at heroku.
Everything works fine, locally. However, when deployed heroku can't find two img. index.html contains exactly this, at different places :
FOUND <img src="img/bamboo-black.png" height="27"> <div class="loaderWrap">
NOT FOUND <img class="loader" src="img/ajax-loader-black.gif"></div>
NOT FOUND <img src="img/person-placeholder.jpg" class="mainPicture" width="150">
Why would it work locally and fail on heroku ? I don't understand.
Did you commit those two files to the repo?
Do a git status and see if they're in the 'new files' section.
If you've committed the files, then login to the Heroku dyno and verify they are there.
$ heroku run bash
You can also try a restart.
$ heroku restart
Fixed it.
For those who might encounter the same problem someday : i used a CDN (cloudinary)
I replaced :
<div class="loaderWrap"> <img class="loader" src="img/ajax-loader-black.gif"></div>
By this :
var $loader = $.cloudinary.image( LJ.cloudinary.loader_id, LJ.cloudinary.displayParamsLoader );
$loader.appendTo( $('.loaderWrap') );
It works fine now.

Server side includes (SSI) with grunt connect web server

We are using yeoman for our dev process and currently using the "grunt server" command to run the grunt connect web server for local development. Every time we save a file, grunt will run all its tasks and reload the browser.
The problem is with Server side includes we use to include the header and footer. We had it previously working with Apache, IIS and Tomcat but have no idea how to get connect to do the same. It just treats it as an html comment.
eg include:
<!--#include virtual="header.html" -->
So,
1. Is there a way to get grunt/connect to include these files?
2. If not can we use Apache with yeoman/grunt?
3. If all fails, is there another way to include files with connect?
You can have express handle SSI with the help of the ssi node module.
I put together a github repo with this simple example: https://github.com/sfarthin/express-ssi-example .
I deployed this app to heroku so you can see it in action: http://intense-basin-9464.herokuapp.com/
app.use(function(req,res,next) {
var filename = __dirname+(req.path == "/" ? "/index.shtml" : req.path);
if(fs.existsSync(filename)) {
res.send(parser.parse(filename, fs.readFileSync(filename, {encoding: "utf8"})).contents);
} else {
next();
}
});
you can easily use connect-ssi:
https://github.com/soenkekluth/connect-ssi
I also used the ssi module for that.
for now I includes are only allowed for .shtml files. 'will change that soon.
Thanks a lot for all your help #steve-farthing and #soenke I finally ended up using a much simpler solution which was to install Apache with SSI enabled and add the following JS tag to the footer.
<script type="text/javascript">
document.write('<script src="//localhost:35729/livereload.js?snipver=1" type="text/javascript"><\/script>')
</script>
Now when we run grunt serve we still need to manually navigate to http://localh0st/app/ but everything else seems to work fine after that.

Can I tell foreman to reload the web app every time a request is made so I can develop decently?

A web app I am writing in JavaScript using node.js. I use Foreman, but I don't want to manually restart the server every time I change my code. Can I tell Foreman to reload the entire web app before handling an HTTP request (i.e. restart the node process)?
Here's an adjusted version of Pendlepants solution. Foreman looks for an .env file to read environment variables. Rather than adding a wrapper, you can just have Foreman switch what command it uses to start things up:
In .env:
WEB=node app.js
In dev.env:
WEB=supervisor app.js
In your Procfile:
web: $WEB
By default, Foreman will read from .env (in Production), but in DEV just run this:
foreman start -e dev.env
You can use rerun for this purpose
You might implement just 2 commands for this:
gem install rerun
rerun foreman start
Then rerun will automatically restart process after any change in your files.
If you use nodemon
, you can do
nodemon --exec "foreman start"
The problem isn't with Foreman so much as it's with how node doesn't reload code on new requests. The solution is to use an npm package like supervisor along with an environment wrapper for Foreman.
First, install supervisor:
npm install -g supervisor
Then, write a wrapper shell script that Foreman can call:
if [ "$NODE_ENV" == "production" ]; then
node /path/to/app.js
else
supervisor /path/to/app.js
fi
Set the wrapper script's permissions to executable by running chmod a+x /path/to/wrapper_script.sh
Lastly, update foreman to use the wrapper script. So in your Procfile:
web: /path/to/wrapper_script.sh
Now when you run Foreman and your node app isn't running in production, it should reload on every request.
I feel like Peter Ehrlich's comment on the original question deserves to be an answer on its own. I think a different Procfile for local/dev is definitely the best solution: https://stackoverflow.com/a/10790514/133720
You don't even need to install anything new if you use node-dev.
Your .env file loaded from Procfile:
NODECMD=node-dev
Your Procfile:
web: $NODECMD app/server.js
Your foreman command
foreman start -e dev.env -p 9786
And in your production env (heroku) set an environment variable:
NODECMD=node

Resources