Technology Musings

July 14, 2018

Platforms / Deploying a Rails Application to Elastic Beanstalk


I'm not a big AWS fan because it is a lot of work.  However, sometimes a client requires it.  In any case, if you are wanting to deploy to AWS Elastic Beanstalk using a Rails app, here are your step-by-step instructions!

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545; min-height: 14.0px} li.li1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} span.s1 {color: #e4af0a} ol.ol1 {list-style-type: decimal}

Preparing a Rails app for EB deployment:


  1. Install the elastic beanstalk tools:
    From Python: sudo pip install awscli awsebcli --upgrade
    From homebrew: brew install aws-elasticbeanstalk
  2. AWS Config
    Set ~/.aws/config as follows:

    region = us-east-1

    [profile eb-cli]
    aws_access_key_id = YOUR_ACCESS_KEY_ID
    aws_secret_access_key = YOUR_SECRET

    The name of the profile "eb-cli" will need to match future steps.
  3. Create an AWS EB application in the EB GUI:
    1. Create the application and give it a name
    2. Create an environment (select "web server environment") and name it whatever you want
    3. Select "Ruby" for the preconfigured platform
    4. Leave the application as "sample application"
    5. Hit "Create Environment" and wait for it to finish (this takes forever)
  4. Enable load balancing so that you have more control (optional, but required for HTTPS I think):
    1. Go to "configuration" then "Capacity"
    2. Change "Single Instance" to "Load Balanced"
    3. Reduce "max" to "1" to prevent additional instances from launching
    4. Hit "apply" and "confirm" and then wait (this takes forever)
  5. Change the instance type to "t2.small" at the lowest.  If it is smaller than this, you will get strange errors when deploying because it is running out of RAM to do the deployment.
    1. "Configuration" -> "Instances" -> "Modify" -> "Instance Type".  Set to t2.small.
  6. Add a data tier
    1. "Configuration" -> "Database" -> "Modify"
    2. Options:
      Engine: postgres
      Engine version: 9.6.8
      Username/password: set them and write them down, but you don't really need them
  7. Configure Environment Variables
    1. "Configuration" -> "Software" -> "Modify"
    2. BUNDLE_WITHOUT: test:development
    3. RACK_ENV: production
    7. SECRET_KEY_BASE: use "rake secret" to generate one (note that leaving this out can lead to some weird errors that seem senseless)
    8. Any other configuration (check your .env file if you have one)
  8. Modify your app to use your data tier.  In config/database.yml, make sure that the adapter is postgresql for "default", then set production to be like the following:

      <<: *default
      database: <%= ENV['RDS_DB_NAME'] %>
      username: <%= ENV['RDS_USERNAME'] %>
      password: <%= ENV['RDS_PASSWORD'] %>
      host: <%= ENV['RDS_HOSTNAME'] %>
      port: <%= ENV['RDS_PORT'] %>

  9. In your app, Create a .elasticbeanstalk/config.yml file in your project's home directory
    1. File contents:

        application_name: your-app-name
        default_region: us-east-1
        profile: eb-cli
          environment: your-staging-environment

    2. The region should probably match the region of the application (though I am not certain), the profile should match the one in ~/.aws/config, and the application_name should match the actual name of the application in elastic beanstalk.
    3. "branch-defaults" allows you to set some parameters per-branch (based on the git branch).  Here, we are saying that when using the master git branch, the environment we should push to is "your-staging-environment".
  10. Create a file in your app .ebextensions/010_packages.config (loads packages we will probably need - may need others based on requirements):

        git: []
        gcc: []
        libxml2-devel: []
        libxslt-devel: []
        patch: []
        sqlite-devel: []
        libffi-devel: []
        postgresql96-devel: []

  11. Make sure that everything in your repository is committed and ready to go.  Then, run

    eb deploy

    and everything should work.
  12. Add a CNAME for your environment in your DNS manager, whatever that is.  It should CNAME to the name listed in your environment (i.e., (optional - you can also just leave it with the generated name, but then you won't be able to add SSL).
  13. Turn on SSL (optional)
    1. Go to AWS Certificate Manager
    2. Click "Request a Certificate" -> "Request a Public Certificate" 
    3. Enter in your domain name (the CNAME you created)
    4. Choose "DNS Validation"
    5. When it says "Pending Validation", open up the "domain" area and create the requested CNAME in your DNS in order to validate your domain.  When complete, click "Continue"
    6. Go to your EB environment page
    7. Go to "Configuration" -> "Load Balancer" -> "Modify"
    8. Click "Add Listener":
      Listener Port: 443
      Listener Protocol: HTTPS
      Instance Port: 80
      Instance Protocol: HTTP
      SSL Certificate: select the certificate previously generated
    9. Note that after adding the listener, there is an "apply" button buried on the webpage.
    10. You can now access the site using SSL on the CNAME you created.


March 02, 2018

Snippets / Hot-Patching Ruby for TLS 1.2

JB recently started denying TLS requests that were less than 1.2.  I thought I was fine, but I had one server running CentOS 5.  It was not fine.  I had a very ancient Rails application on an ancient box unable to connect to

Anyway, if you are in a similar situation, here is how I solved it.  This may not be exact, but should hopefully get you far enough along to figure it out yourself:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545; min-height: 14.0px}

For's TLS upgrade, it is based on your server's OpenSSL library version.  To see if you are compatible, run the following command:


    openssl s_client -connect


If you get a one-line error message like this:

    6641:error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure:s23_clnt.c:586:


Then you are NOT compatible.  To fix, you need to either update your OpenSSL in Linux or get the latest OpenSSL from here -


For building, you may have to configure it something like this:

    export CFLAGS=-fPIC
    ./config shared
    make install     # this one as root


I also copied the generated shared libs into /lib64.  In theory, after updating, it would be best to rebuild ruby from source.  However, absent that, there is a trick you can do:


Create a new file, I called mine ruby_openssl in the same directory as the old ruby.  The file should have the following contents:


    export LD_PRELOAD=/lib64/
    exec /opt/ruby-1.8.7-p352/bin/ruby $*


The LD_PRELOAD should go to the shared files that were installed by installing OpenSSL, wherever you happened to install them.


Then, in your apache config, reference your new ruby_newopenssl wherever you were referencing ruby.

October 24, 2017

Snippets / Calculating an Even Baseline


So, I've been working in Xamarin for cross-platform phone development.  I was using their AbsoluteLayout class to try to get a bar graph working using relative sizes (AbsoluteLayout with relative sizes gives everything an absolute position from 0, top or left of the screen, to 1, the bottom or right of the screen).  The goal was to have the bottom of the bar graph halfway down the screen (to allow room for negative values to point down).  The goal was to look like this:


The problem, though is getting the boxes to align at the center line.  Now, part of the problem may be my inexperience with the platform.  There may be a really easy way to do this.  However, here is what I was running into:

I thought that Xamarin worked by aligning the your specified position to the middle of the box.  Therefore, I started by calculated the box height, and then calculating the origin of the box by setting it above the 0.5 (i.e., 50%) line by half of the box height.  Therefore, when it extended, it should extend down to the 0.5 line.

The problem, though, is that Xamarin doesn't  align to the middle of the box.  It varies the alignment by where in the positioning it is positioned.  So, if you position a box at 0.2, not only is it positioned at 0.2, but the anchoring point is also 0.2 down the box!

Therefore, I needed a way to calculate where I needed to actually position the box to get it to rest on the 0.5 line.

The way I calculated it is this - let's call the y-position that you want the box to rest on as P.  Let's call the y-position which you are going to set your box to as A (for the anchoring position).  Let's call the height of the box itself H.

What we want to happen is for the bottom of the box to hit P.

Now, we can split up the measurements from the top of the box to P into two lengths - the length from the top to the anchoring point (which is actually just A) and the length from the anchoring point to the bottom of the box.

Since the anchoring point sits A down on the box, the length of the top half of the box is given by A*H.  However, this is not the distance we want.  We want the distance down the rest of the box.  That will be (1 - A)*H.

So, our two distances are: A and (1 - A)*H.  They should add together to be P.  Therefore, our equation is:

A + (1 - A)*H = P

However, we want to solve this for A - we already know our box height (H), and we know what position we want this to extend to (P).  Therefore, we have to solve for A:

A + (1 - A)*H = P
A + H - A*H = P
A - A*H = P - H
A*(1 - H) = P - H
A = (P - H) / (1 - H)

And this gives a perfect calculation for A.  In my case, since P is 0.5, I just do A = (0.5 - H) / (1 - H) and it comes out perfectly.

It should be fairly simple to come up with similar calculations for top alignment and center alignment.  Additionally, these work with right-to-left alignments as well.

February 11, 2016

General / Finding Derivatives without Limits or Differentials


I always enjoy finding new ways of doing things.  I was reading Henle/Kleinberg's Infinitesimal Calculus, which is a great book.  It has, throughout the book, a main text and a side-text.  The side-text is infinitely more interesting than the main text.

Anyway, on pages 65-66, it gives a method for finding the slope of y = x^2 at a specific point, let's say its at (2,4).  The way you do it is kind of strange, but it seems to work.  You start by defining a line to intercept the equation:

y = mx + b

Now, we don't know what m and b are, but we *do* know what x and y are.  Therefore,

4 = 2m + b

We don't really care about b in the long run, so we want to find an equation for b to remove it from the mix:

b = 4 - 2m

Now our equation for the line becomes:

y = mx + 4 - 2m

Interestingly, we actually have a value for y - y = x^2, so this becomes:

x^2 = mx + 4 -2m

We can rearrange this into quadratic form with:

x^2 - mx + (2m - 4) = 0

Using the quadratic formula, we can do:

x = (m +- sqrt(m^2 - 4(2m - 4))) / 2

x = (m +- sqrt(m^2 - 8m + 16)) / 2

x = (m +- sqrt((m - 4)^2)) / 2

x = (m +- (m - 4)) / 2

Now, we want to take the "+" side of the "+-" since we are solving for "m".  Otherwise, we will lose "m".  Therefore, this becomes:

x = (2m - 4) / 2

x = m - 2

So, at the point we are looking at, what is the value of x?  At (2, 4), x is 2.  Therefore

2 = m - 2

m = 4

Therefore, the slope at (2, 4) is 4.  

Interestingly, we can also generalize this to get the full derivative for y = x^2.  To do this, we will introduce the variables p and q to be the specific x and y values at a specific point.  Therefore, the slope at (p, q) for y = x^2 is:

y = mx + b, therefore q = pm + b, therefore b = q - pm

q = p^2, therefore b = p^2 - pm

y = mx + p^2 - pm

y = x^2, therefore:

x^2 = mx + p^2 - pm

x^2 - mx + (pm - p^2) = 0

x = (m +- sqrt(m^2 - 4(pm - p^2)))/2

x = (m +- sqrt(m^2 - 4pm + 4p^2))/2

x = (m +- sqrt((m - 2p)^2))/2

x = (m +- (m - 2p))/2

x = (2m - 2p) / 2

x = m - p

m = x + p

Since p = x, this gives

m = x + x = 2x, which is the derivative of x^2

I don't know how many classes of functions you can do this successfully for, but I imagine it should cover quadratics at least.


January 13, 2016

General / Ratios of Infinity


A lot of people do a lot of thinking about whether or not infinities exist, and wonder how real numbers can add up to infinity.

Sometimes I wonder if that sort of thought might be the inverse of reality - perhaps the infinities are the foundational realities, and all finite numbers are merely infinities in ratio.

January 01, 2016

General / A Meaning for d^2y/d^2x?


I have recently been randomly curious about random calculus-y things.  Anyway, the notion for the second derivative of a function is d^2y/dx^2.  This is the second differential of y divided by the first differential of x squared.

However, there is, technically, also a d^2x, though it doesn't get much attention.  And, d^2y and d^2x can be put into ratio with each other, but I don't really know what it means.  But it is an interesting operation nonetheless.

So, the derivative of an equation is dy/dx and the derivative of its inverse is dx/dy.

The second derivative of an equation is d^2y/dx^2 and the second derivative of its inverse is d^2x/dy^2.

Therefore, to get the d^2y/d^2x you just do:

(2nd Derivative of y wrt x / 2nd derivative of x wrt y) * (First derivative of the inverse)^2


(d^2y/dx^2) / (d^2x/dy^2) * (dx/dy)^2

I will post an example later when I have a good one.

December 27, 2015

General / The derivative of u^v


I don't know why this isn't listed as a standard rule.  The differential of the exponent function, u^v (u raised to the v power), is pretty basic, and you can use it to formulate the other exponent rules for differentiation. However, for some reason it seems to be left off of most rules for differentiation.

The basic rule is this:

d(u^v) = v*u^(v-1)*du + u^v*ln(u)*dv

This can be clearly seen to be a more general form of u^n, because if n is a constant, this becomes:

d(u^n) = n*u^(n - 1)*du + u^v*ln(u)*0

And that zero drops it to the rule we all know and love:

d(u^n) = n*u^(n - 1)*du

This can be derived as follows:

z = u^v
ln(z) = ln(u^v)

Using log rules, we get:

ln(z) = v*ln(u)

Now take the differential of both sides:

d(ln(z) = d(v*ln(u))

Differentiating both sides, we get:

dz/z = v*du/u + ln(u) * dv

Then we multiply by z:

dz = z*v*du/u + z * ln(u) * dv

Substituting in z = u^v:

dz = u^v * v * du / u + u^v * ln(u) * dv

Now, u^v/u simplifies to u^(v-1), giving us:

dz = v*u^(v-1)*du + u^v*ln(u)*dv

Since z = u^v, dz = d(u^v), so

d(u^v) = v*u^(v-1)*du + u^v*ln(u)*dv

December 22, 2015

Platforms / Rails and Updated Postgres not Performing Migrations


Apparently, there was a change to something in postgres 9.5, or maybe just my install of it.  If you upgrade your Postgres database version and suddenly your rails app stops doing migrations, add the following line to config/database.yml database config to get it working again:


  schema_search_path: public

The error message i was getting was:


ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR:  relation "schema_migrations" already exists

Basically, sometimes Rails was finding the schema_migrations table, and sometimes it wasn't, but setting the schema_search_path fixed it.

I'm still not sure where exactly the problem came from, but setting the setting fixed it.

December 13, 2015

General / Explanation of Liebniz Notation of the Second Derivative


The notation for the second (and higher) derivatives in Liebniz notation has always troubled me.  The second derivative is usually notated as d^y/dx^2.  And I always wondered, why is the the 2 in relation to the d on the top, but in relation to the whole term on the bottom?

This puzzled me for a while, and I looked through at least 10 Calculus textbooks to find the answer, all to no avail.  Finally, I put pen to paper and figured it out.  The answer was straightforward, and I am sure that whoever invented the 2nd derivative Liebniz notation knew why they did it that way, it's just that every Calculus book since then seems to have forgotton.

Anyway, the usual notation for the derivative operation id d/dx.  I eventually came to realize that this is not a single operation, but TWO operations in one.  The first is to take the differential (not derivative) of the equation.  For that, I mean, let's say you have the equation y = 2x.  The differential of that equation is dy = 2xdx.  Doing a differential instead of a derivative is powerful, because it allows a more natural approach to both implicit differentiation and multivariable differentiation.

Now, the derivative is simply the differential divided by dx.  So, if our differential is dy = 2xdx, then when we divide by dx we get our normal notation dy/dx = 2x.

Now, let's leave off dividing by dx, and just look at the differential.  What happens if we take a second differential?

In this case, we have to treat dy as a separate variable.  So, if the differential of y is dy, what is the differential of dy?  This is where the d^2y comes in.  The 2 is by the "d" not because it is "d squared", but it is "d applied twice".  So, if we take our initial differential dy = 2xdx and take another differential, what do we have?

By the product rule, we get:

d^2y = 2((x)(d^2x) + (dx)(dx))

Now, since we want the second derivative, we divide both sides by dx twice (once for the first derivative, and once for the second), which is the same as dividing by dx^2.

That gives us:

d^2y / dx^2 = 2((x)(d^2x) + (dx)(dx)) / dx^2

The (dx)(dx) simplifies to dx^2, and we can separate this out at the plus sign to give us:

d^2y / dx^2 = 2(x)(d^2x)/dx^2 + 2(dx^2)/dx^2

This further simplifies to:

d^2y / dx^2 = 2(x)(d^2x)/dx^2 + 2

Now, we know that the second derivative is 2, so that means that 2(x)(d^2x)/dx^2 must equal zero.  But why?

Well, let's rewrite this term a little:

2x * (d^2x)/(dx^2)

d^2x/dx^2 is simply the second derivative of x with respect to itself!

To understand why this must be zero, first imagine the first derivative of x with respect to itself: dx/dx

This must always be 1!  Another way of stating this is that "for every change in x, the corresponding change in x is equivalent", which seems obvious.

Now, 1 is a constant, so the second derivative must be zero!  This zeroes out the whole term, leaving:

d^2y/dx^2 = 2

Thus, we have the notation of the derivative from first principles.

Another way to look at this is to use the quotient rule.

If we start with dy/dx, then take the differential, what do we get?

((dx)(d^2y) - (dy)(d^2x)) / dx^2

If we separate, we get:

(dx)(d^2y) / dx^2 - (dy)(d^2x)/dx^2

The first term simplifies to d^2y / dx.  The second term is our second derivative of x with respect to itself again, so it becomes (dy)*0, giving us:

d^2y/dx - (dy)*0

Which gives us just


Now, the second step of the derivative is to divide by dx.  Doing so gives us:


Which is our second derivative!

November 14, 2015

General / A General Solution for Line Integrals


Update: I found that this is a standard formula, but it is referred to as an "arc length" formula, not a line integral.  I knew it had to be somewhere, I was just searching the wrong terms.

I was playing around last night, and I came up with, what appears to be, a general solution for line integrals.  I have looked around and I have not found this equation anywhere, although I am certain in must be well-known somewhere.  In any case, I will give it here, because it is definitely not well-known on the Internet.

This is an equation for determining the line integral of y = f(x), where f(x) is a true function of x (i.e. it passes the vertical line test), and is differentiable on the portion of the line where you want to find the length of the line segment.  Most line integrals work by adding in an auxiliary parameter, t, to the equation.  This operates directly on the function itself.

The short version:

To get a line integral from x=a to x=b on y=f(x), we can use the following formula:

Evaluate this from x=a to x=b and you will have the length of the line.  Note, the software I was using gives this as a partial derivative on the inside.  I don't know enough to know if it is only limited to partials or not.

So, if you know the derivate, you can figure out (in theory) the line integral.  Whether or not this actually helps you find a solution to the integral is another story.

Now, how does one arrive at that?

Well, first, remember, that an integral is an infinite sum of infinitesimals.  A curve is simply an infinite sum of tiny, infinitesimal lines.  So, all we need to do is find the length of each line segment and add it up.

How big are our line segments?

Well, if y=f(x), then the line segment is going to be the line between the points x, y and x + dx, y + dy.  The distance formula states that the line length is sqrt( (x1 - x0)^2 + (y1 - y0)^2).  The difference between x + dx and x is simply dx, and the difference between y + dy and y is simply dy.  So, each of our infinitesimal line segments (dL) will be:

dL = sqrt(dx^2 + dy^2)

If we square both sides, we get the following:

dL^2 = dx^2 + dy^2

dL^2 - dx^2 = dy^2

Now we can divide both sides by dx^2 and get:

dL^2/dx^2 - 1 = dy^2/dx^2

dL^2/dx^2 = 1 + dy^2/dx^2

dL^2/dx^2 = 1 + (dy/dx)^2

Now we can square root both sides:

dL/dx = sqrt(1 + (dy/dx)^2)

dL = sqrt(1 + (dy/dx)^2) dx

And this is the final form of our equation.  I tested it out with a few simple integrals (a line and the top half of a circle) and it seems to work as expected.  And, as I said, if you have a line that loops back around, you still need to use the parameterized methods.  But this is an easier way to setup line integrals where f(x) is a true function of x.