Testing Your Imagination
The usual blog post follows a particular format:
“I learned something new, as part of a task that I succeeded at. Here’s what I did, here’s why it worked so well, thank you for reading.”
This one’s a little different. I made a mistake, it seemed like a pretty simple thing, and then it got me thinking about why I (and in general, we software types), fall into that mistake, and how hard it is to correct.
Here’s the background: I was working on a very small bit of code that was supposed to check a ZIP code and do one of two things. The test was, in Perl,
$zip =~ /^94|95/
Screetch!
Perhaps you have already spotted the bug. Don’t feel so smug just yet. The particulars here are vital to understanding my point, but the bug could have been something more complex, or even simpler, and I am willing to bet a cubic yard of virtual cash that you have made equally embarrassing errors. I’m just more willing to talk about mine, that’s all.
Back to our tale. I wrote that mistaken bit of code (and worse, it’s not the first time I’ve made that mistake in code), and then I proceeded to test it.
- Set $zip to ‘12345’, doesn’t match, no false positive. Check!
- Set $zip to …
testing
Using JavaScript in PostgreSQL
This time I will describe two things: installing a new extension using pgxn and using JavaScript for writing a PostgreSQL stored procedure.
The last time I was describing a couple of nice features of the incoming PostgreSQL 9.3, I wrote about merging JSONs in Postgres using a stored procedure written in Python. In one of the comments there was a suggestion that I should try using JavaScript for that, as JSON is much more native there.
So let’s try JavaScript with PostgreSQL.
Installing PL/V8
PL/V8 is a PostgreSQL procedural language powered by V8 JavaScript Engine. This way we can have JavaScript backed, something funny which could be used to create something like NoSQL database, with JavaScript procedures and storing JSON.
To have this procedural language, you need to install it as a separate extension. This can be done with system packages, if your system provides them. For Ubuntu, which I use, there are packages ready, however I use PostgreSQL compiled from source, and I keep it in my local directory, so I had to install it in a little bit different way.
I keep my PostgreSQL in ~/postgres directory. The ~/postgres/bin directory is added to environmnent $PATH variable. It is …
javascript postgres
Copying Rows Between PostgreSQL Databases
A recurring question is: “how can I copy a couple of rows from one database to another”? People try to set up some replication, or dump entire database, however the solution is pretty simple.
Example
For this blog post I will create two similar tables, I will be copying data from one to another. They are in the same database, but in fact that doesn’t matter, you can use this example to copy to another database as well. Even on another server, that’s enough to change arguments for the psql commands.
The tables are:
test=# CREATE TABLE original_table (i INTEGER, t TEXT);
CREATE TABLE
test=# CREATE TABLE copy_table (i INTEGER, t TEXT);
CREATE TABLE
Now I will insert two rows, which I will copy later to the “copy_table”.
test=# INSERT INTO original_table(i, t) VALUES
(1, 'Lorem ipsum dolor sit amet'),
(2, 'consectetur adipiscing elit');
INSERT 0 2
test=# SELECT * FROM original_table ;
i | t
---+-----------------------------
1 | Lorem ipsum dolor sit amet
2 | consectetur adipiscing elit
(2 rows)
test=# SELECT * FROM copy_table;
i | t
---+---
(0 rows)
The Solution
Of course I can set up replication, which is too much effort for ad hoc copying two …
postgres
Mooving to the Mobile Web
With the rise of the myriad of mobile phones, tablets and other devices that are connected to the internet, the potential users for a given website have both increased in number and morphed in their needs in terms of a user experience. As anyone who has attempted to use a website not designed for a mobile phone browser with frantic pinch-zooms to find a tiny page control, right next to four other controls that do something totally different in a menu, can attest this is really not ideal.
And meanwhile from the other perspective, for web developers, the notion of a fragmented user base over everything from a desktop PC with a modern browser to an embedded PC built into my new LCD TV needing to view your pages gracefully can be a scary prospect. The thought of maintaining independent versions of your web infrastructure that fit each of these major use cases would likely scare everyone in your company, especially the finance people cutting all the checks.
So what is a company facing this new reality of the modern web to do? One particular solution can help to alleviate one of the more troublesome issues with new devices browsing the Internet, mobile phone display. While the phones …
browsers mobile
Piggybak Dependency & Demo Updates
Things have been quiet on the Piggybak front lately, but we recently upgraded the demo to Ruby 2.0.0 via rbenv, Rails 3.2.15, and Postgres 9.3. The Piggybak demo runs on Debian 7 with nginx and Unicorn. The upgrade went fairly smoothly, with the exception of jQuery related issues, described below.
As of jQuery 1.7, the live() method is deprecated, replaced with the on() method. As of jQuery 1.10., the live() method no longer exists. The previous version of Rails that was used on the demo, Rails 3.2.12, required the jquery-rails gem version which included an older version of jQuery. Upon upgrading to Rails 3.2.15, the attached jquery-rails gem now includes jQuery 1.10., resulting in the live() method no longer existing. As a result, several of the dependencies needed to be updated to accomodate this change (Rails_admin, the Piggybak Coupon gem, and the Piggybak Gift Cert gem, jQuery Nivo Slider).
What’s next for Piggybak? Our future plans include an upgrade to support Rails 4.0. Additional features described on our last Roadmap Update include advanced taxonomy, reviews & ratings, saved cart, wishlist functionality, and saved address support. Piggybak continues to be a great …
ecommerce piggybak ruby rails
Zero Downtime Deploys with Unicorn
I was recently deploying a new Ruby on Rails application that used NGINX and Unicorn for production. During the deploy, my common practice was to stop the Unicorn processes and then restart them. I would do this by finding the PID (process id) of the running process, stop it using the kill command and then run the unicorn_rails command from my application root directory. This worked well enough that I put together a simple unicorn_init shell script to handle running the commands for me.
After a couple of deploys using this init script, I found that there was a significant inturruption caused to the site. This was due to the approximately 20 seconds it took for the Unicorn workers to launch. This was unaccepatble and I started a search for how to perform a zero downtime deploy for Unicorn.
My search lead me to the Unicorn Signal Handling documentation. Unicorn makes use of POSIX Signals for inter-process communication. You can send a signal to a process using the unfortunately named kill system command. Reading through the different signals and what message they send to the Unicorn master and workers, I found a better approach to restarting my Unicorn processes that would result in …
rails sysadmin
SELinux fix for sudo PAM audit_log_acct_message() failed
I was just reading my co-worker Lele’s blog post about making SELinux dontaudit AVC denial messages visible and realized it was likely the solution to a mystery I ran into a few days ago.
As Lele explains, the SELinux dontaudit
flag suppresses certain very common SELinux AVC denials to keep the audit logs from bloating beyond belief and being too hard to use. But sometimes a commonly harmless denial can be the cause of further errors. You can tell this is the case if temporarily disabling SELinux enforcing (setenforce 0
) makes the problem go away, but /var/log/audit/audit.log
still doesn’t show any AVC denial actions being allowed through.
In my somewhat unusual case there is an Apache CGI shell script that calls sudo to invoke another program as a different user without using setuid or suEXEC. Everything works fine with SELinux enforcing, but there are some strange errors in the logs. In /var/log/secure
:
sudo: PAM audit_log_acct_message() failed: Permission denied
And in the Apache error_log
is the apparently strangely unbuffered output:
[error] sudo
[error] :
[error] unable to send audit message
[error] :
[error] Permission denied
[error]
To show the dontaudit
AVC denials, I ran …
redhat security selinux sysadmin
Asynchronous Page Switches with Django
Now that the newly rebuilt endpoint.com website is up and running, you may have noticed it does something fancy: internal links within the site are fetched in the background, and the page is replaced dynamically with a script. That eliminates the ‘flicker’ of normal website navigation, and removes the need for the browser to re-parse CSS and JavaScript, making it feel more responsive.
Recently I did some work on a Django project that uses jQuery for some AJAX calls to send information back to the database. It was a fairly simple $.post() call, but it got me thinking about Django’s template inheritance and how it could be used to render parts of templates and update those client-side without having to render the whole thing. The idea being, if your base template is complex and has a number of built-in queries or calculations, for example if you have a dynamic navigation menu, why put extra load on Postgres, on the web server, or have the browser reload the CSS, JS, images, or other resources, to load in what could be otherwise static data into a content column?
The idea’s a little half-baked, just the result of a little after-hours tinkering over a couple evenings. Certainly hasn’t …
django javascript python