A saner alternative to SimpleForm's :grouped_select input type
SimpleForm is a great approach to simplifying your forms, and it comes with lots of well-defined input types. However, the :grouped_select type seems to be overly complicated for most use cases.
Example
Consider this example, from the documentation:
form.input :country_id, collection: @continents,
as: :grouped_select, group_method: :countries
While that looks easy enough at a first glance, look closer. The example passes @continents for a country_id.\
SimpleForm actua...
Capistrano 3: How to deploy when a firewall blocks your git repo
Sometimes, through some firewall or proxy misconfiguration, you might have to deploy to a server that cannot access the git repository.
Solution 1: HTTP Proxy (this is the preferred fix)
SSH can be tunneled over an HTTP Proxy. For example, when the repo is on github, use this:
-
Install
socat -
Add a
~/.ssh/configon the target server(s) with permission 0600 and this content:Host github.com ssh.github.com User git Hostname ssh.github.com Port 443 ProxyCommand socat - PROXY:<your proxyhost>:%h:%p,...
Debug flaky tests with an Unpoly observeDelay
The problem
Unpoly's [up-observe], [up-autosubmit] and [up-validate] as well as their programmatic variants up.observe() and up.autosubmit() are a nightmare for integration tests.
Tests are usually much faster than the configured up.form.config.observeDelay. Therefore, it may happen that you already entered something into the next field before unpoly updates that field with a server response, discarding your changes.
The steps I wait for active ajax requests to complete (if configured) and capybara-lockstep can catch some ...
Carrierwave: Using a nested directory structure for file system performance
When storing files for lots of records in the server's file system, Carrierwave's default store_dir approach may cause issues, because some directories will hold too many entries.
The default storage directory from the Carrierwave templates looks like so:
class ExampleUploader < CarrierWave::Uploader::Base
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
If you store files for 500k records, that store_dir's parent directory will have 500k sub-directories which will cause some...
Loading dumps via SSH, unpacking and sourcing them, all with a progress bar
Here is a hacky way to load dumps directly from the source server, without fully copying them over and extracting them first.
It may break horribly for you. This is the dark side of the force.
- Install pipe viewer, if you don't have it already:
sudo apt-get install pv - Know the location of the dump file on the remote server. We'll use
/mnt/dumps/my_project.dump.bz2in the example below. - Find out the size of the (bzipped) file in by...
Sending TCP keepalives in Ruby
When you make a simple TCP connection to a remote server (like telnet), your client won't normally notice when the connection is unexpectly severed on the remote side. E.g. if someone would disconnect a network cable from the server you're connected to, no client would notice. It would simply look like nothing is being sent.
You can detect remote connection loss by configuring your client socket to send TCP keepalive signals after some period of inactivity. If those signals are not acknowledged by the other side, your client will terminat...
Geordi 1.0 released
Geordi 1.0 features a command line application geordi, that holds most of Geordi's previous commands.
New features
-
command help and usage examples right within
geordi(geordi helpandgeordi help <command>) -
quick command access: type just the first few letters of a command, e.g.
geordi rsorgeordi dev[server] -
command dependencies, e.g.
geordi rspecinvokesgeordi bundle-install(which bundles only if needed) -
no cluttered
/usr/bin, but all commands in one handy tool -
template for easily adding new...
Install MySQL 5.6 in Ubuntu 16.04
Instead of using this hack you might want to use MariaDB 10.x which can work with both old and new apps.
An alternative could be to use the MySQL Docker image which is still updated for 5.6.
Ubuntu 16.04 only provides packages for MySQL 5.7 which has a range of backwards compatibility issues with code written against older MySQL versions.
Oracle maintains a list of official APT repositories for MySQL 5.6, but those repositories do...
Debug Ruby code
This is an awesome gadget in your toolbox, even if your test coverage is great.
-
gem install ruby-debug(Ruby 1.8) orgem install debugger(Ruby 1.9) - Start your server with
script/server --debugger - Set a breakpoint by invoking
debuggeranywhere in your code - Open your application in the browser and run the code path that crosses the breakpoint
- Once you reach the breakpoint, the page loading will seem to "hang".
- Switch to the shell you started the server with. That shell will be running an irb session where you can step thr...
Net::SSH::Exception: could not settle on encryption_client algorithm
TL;DR: Update the 'net-ssh' gem by adding to your Gemfile:
gem 'net-ssh', '=2.9.1'
Now run bundle update net-ssh. It has no dependencies so it shouldn't update other gems.
If you're using Ruby 1.8.7 and want to update net-ssh to a version > 2.9.1 you also need to add this to your gemfile:
gem 'backports', :require => false
... and in your deploy.rb add this:
require 'backports/1.9.2/array/select'
Background
You propably have an older version of Capistrano and thereby an older version of `n...
Working around OpenSSL::SSL::SSLErrors
If your requests blow up in Ruby or CURL, the server you're connecting to might only support requests with older SSL/TLS versions.
You might get an error like: OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=unknown state
SSL Server Test
This SSL Server Test can help finding out which SSL/TLS versions the server can handle.
Ruby
In Ruby, you can teach Net::HTTP to use a specific SSL/TLS version.
uri = URI.parse(url)
ssl_options = {
use_ssl: true,
ssl_version...
vague puppet error messages with broken yaml files
If you get one of this errors:
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: (<unknown>): found character that cannot start any token while scanning for the next token at line 1297 column 3
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: undefined method `empty?' for nil:NilClass at /etc/puppet/environments/production/manifests/nodes.pp:1 on node example.makandra.de
Warning: ...
Apache: Log the original client IP when your site sits behind a reverse proxy
When your site is mapped into the URL-space of another server using mod_proxy, ProxyPass and ProxyPassReverse, all requests in your Apache logs are logged with the IP address of the proxying server. The IP address of the original client doing the request is not logged, making it difficult to trace problems and run statistics.
Short answer
There is no easy way to fix this. Use the log of the proxying server instead, which logs the original client IPs you're looking for.
Long answer
You can fix this for your ac...
Workflow: How to use a key management service to encrypt passwords in the database
This is an extract from the linked article. It shows an approach on how to implement encrypted passwords with the AWS Key Management Service (KMS).
For most applications it's enough to use a hashed password with a salt (e.g. the gem devise defaults to this).
Upon password creation
-
Generate hash as hash of password + salt.
-
Encrypt the hash with a public key from KMS (you can store the public key in your server code).
-
In your database sto...
Zeus promises to make rails development faster
I am talking about development speed. When your application starts growing and you start adding gems, it starts to take really long to start up, be it the server, console or just running a single spec.
Zeus is smart, you don’t have to put it in your Gemfile or run it with Bundler, all you need to do is create a JSON config file via
zeus initand then start the serverzeus start.
After that, you’re ready to go, all you need to do is prefix every command with zeus. That means
rails serverbecomeszeus server, `rails console...
Cronjobs: "Craken" is dead, long live "Whenever"
Our old solution for cronjobs, the "craken" plugin, is no longer maintained and does not work on Rails 3.2+.
We will instead use the whenever gem.
"Whenever" works just like "craken", by putting your rake tasks into the server's cron table. Everything seems to work just like we need it.
Installation for new projects
-
Add "whenever" to your
Gemfile:group :deploy do gem 'whenever', require: false end -
Add it to your
config/deploy.rb:
...
IMAP: Check credentials
Connect to your IMAP server. If you have SSL enabled:
openssl s_client -connect your-server:993
if your server supports STARTTLS:
openssl s_client -starttls imap -connect your-server:143
otherwise use netcat or telnet (you shouldn't do cleartext stuff nowadays...).
You should see something like this:
...
. OK Pre-login capabilities listed, post-login capabilities have more.
To log into IMAP, send the following string (incl. "01"!):
01 LOGIN user@domani.io $password
The server should return something like:
* C...
makandra tech survey - results
These are the results of the "personal tech stack survey". I've included only the most popular mentions, maybe it can help you find one or two useful tools for your own usage.
Desktop environment
pie title Desktop environment
"Gnome" : 16
"i3": 2
"sway": 2
"awesome": 1
"bspwm": 1
"mate": 1
"xfce": 1
Gnome dominates (unsuprising, it's the Ubuntu default), but quite a few people use tiling window managers, most popular i3 and the mostly i3-compatible [sway](https://swaywm....
Never use SET GLOBAL sql_slave_skip_counter with a value higher than 1
If you have a replication error with MySQL and you know the "error" is okay (e.g. you've executed the same statement at the same time on 2 masters which sync each other), you can skip this error and continue with the replication without having to set up the slave from the ground up.
stop slave;
set global sql_slave_skip_counter = 1;
start slave;
But what if you have multiple errors which you want to skip? (e.g. you've executed multiple statement at the same time on 2 masters which sync each other)
Still do not use a value highe...
Deal with certain travis CI failures
Travis changed their default distribution from Ubuntu 14.04 (trusty) to 16.04 (precise). This might break your test setup for new builds.
You can solve this issue by freezing your test distribution in the .travis.yml to Ubuntu 14.04 until you have the time to solve all the issues you will have in 16.04:
dist: trusty
Error details
Here are few indicators that you ran into this issue:
Connection to the PostgreSQL database does not work anymore
Your travis-ci builds might have started failing on the usual
psql -c...
Resque: Clearance authentication for dashboard
Resque comes with its own dashboard (Resque server) that you can mount inside your Rails 3 application with
#config/routes.rb:
require 'resque/server'
My::Application.routes.draw do
# ...
mount Resque::Server => '/resque'
end
Unfortunately, since this bypasses the filters in your ApplicationController, everyone can access this dashboard now (unless you have some Rack-based authentication in place, like Devise).
If you're using ...
Make an HTTP request to a machine but fake the hostname
Consider you have a website vhost listening to www.example.com, redirecting all incoming requests that do not talk about the configured hostname (this is often used to redirect users to http://www.example.com when entering only http://example.com/).
If you want to make a request to that site's web server without actually talking to www.example.com (e.g. because this is a load balancer's address but you want to access one specific machine), you cannot just request machine1.example.com or localhost as the above vhost will redirect...
Rails: Concurrent requests in development and tests
With puma you can have concurrent requests. There are two concepts on how Puma can handle two incoming requests: Workers and Threads.
Workers
Puma can have multiple workers. Each worker is a process fork from puma and therefore a very heavy instance and can have multiple threads, that handle the incoming requests.
Example: A Puma server with 2 workers and 1 thread each can handle 2 request in parallel. A third request has to wait until the thread of one of the workers is free.
Threads
Rails is thread-safe since version 4 (n...
Temporary solution for connection errors with rubygems
The problem
If you're experiencing that your bundle install command fails with an error message like this, rubygems.org might have issues with their ipv6 connectivity:
$ bundle install
Fetching source index from https://rubygems.org/
Retrying fetcher due to error (2/4): Bundler::HTTPError Could not fetch specs from https://rubygems.org/ due to underlying error <timed out (https://rubygems.org/specs.4.8.gz)>
The (a little bit dirty) possible solution
If that's actually the case, then you can try to deprioritize the ipv...