Making changes to provide the version and saving the console log

Our upstart configuration is located in the subdirectory scripts. With your code editor, open the file scripts/helloworld.conf.

We will edit the script section of the file as follows:

script 
    set -a 
    . /usr/local/helloworld/version 
    mkdir -p /var/log/helloworld/ 
    chown ec2-user:ec2-user /var/log/helloworld/ 
    exec su --session-command="/usr/bin/node /usr/local/helloworld/helloworld.js >> /var/log/helloworld/helloworld.log 2>&1" ec2-user 
end script 

The first set of changes will allow us to define the HELLOWORLD_VERSION environment variable. To get there, we will add a call to set -a to force our variables to be exported and source a new file, /usr/local/helloworld/version, which we will create later. The second part of the changes will allow us to log the console output on to the filesystem. To do that, we need to create a directory in /var/log and change the command that starts the application to save stdout and stderr into that new directory.

We now have a log file containing our console log. Whenever you add new logs, you should always think about how to rotate those logs. We will do that using logrotate.

We will create a new folder and configuration file for it.

In the root of the helloworld directory, create a new folder conf and a new file called logrotate:

$ mkdir conf
$ touch conf/logrotate 

We will now edit that file and put the following configuration in it:

/var/log/helloworld/*.log { 
  rotate 3 
  size=100M 
  copytruncate 
  nocompress 
} 

You may, of course, adjust it to your liking.

We will now address the creation of the /usr/local/helloworld/version file. Our goal is to generate a new version file every time new code is released with CodeDeploy. One of the functionalities that we didn't cover is that whenever CodeDeploy runs, it sets some environment variables of its own. We will use those to generate a version.

Create a new script in the script directory, call it setversion.sh, and set its permissions to be executable:

$ touch scripts/setversion.sh
$ chmod +x scripts/setversion.sh  

Open the file and simply put the following:

#!/bin/sh
echo "HELLOWORLD_VERSION=${APPLICATION_NAME}-${DEPLOYMENT_GROUP_NAME}-${DEPLOYMENT_GROUP_ID}-${DEPLOYMENT_ID}" > /usr/local/helloworld/version  

We can now make changes to CodeDeploy to incorporate all our changes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset