Double Shot #1885

  • smith - Command line utility from Oracle designed to build containers with only a single process & its dependencies, reading and writing from standard locations.
  • Announcing Graylog v2.3.0-rc.1 - Notably with Elasticsearch 5 compatibility.
  • Syntax. - New podcast from Wes Bos and Scott Tolinski, kicking off with an episode about React tools.
  • Exclude Some Stylesheets in Rails 4 - Rails helpfully provides different stylesheets for different controllers - and then insists on concatenating them all together. Here are some thoughts on a more modular setup.
  • Redux isn't slow, you're just doing it wrong - An optimization guide - Some fairly detailed guidance, much of it revolving around how to avoid unnecessary rendering.
  • GitNotifier - Get email notifications when someone stars or forks one of your repos on GitHub, or when they follow or unfollow you.
  • Bitcoin, Ethereum, Blockchain, Tokens, ICOs: Why should anyone care? - Despite the title, this is actually a clear explanation of a lot of the basic cryptocurrency concepts and ideas floating around. It is not however in the least skeptical about them.
  • Ship - Native macOS client for GitHub, focused on code review and issue tracking.
  • Ruby on Rails Code Audits: 8 Steps to Review Your App - Quick ways to judge the quality of a codebase that you just stepped in. I'd add that you should look at routes.rb early on; if the routing is craptacular the rest of the app won't be any better.
  • Wildcard Certificates Coming January 2018 - From Let's Encrypt, that is. Free of charge from one of their automated endpoints.

Double Shot #1884

  • Why Serverless? - An introduction to the idea behind things like OpenWhisk, Amazon Lambda, and Google Cloud Functions. I need to dig in a bit here.
  • ES2017's async/await is the best thing to ever happen to JavaScript - There are days when I feel like I'll never catch up with innovation in JavaScript. Here's the latest way to handle async code.
  • Elvish - Another option for a shell on Linux, macOS, or BSD. It features nicer control structures and pipelines that can handle more than plain text.
  • Project Guidelines - For JavaScript projects, from Hive. I'm not always happy with formal guidelines, but I welcome thinking about the tradeoffs. Lots of good stuff here.
  • Running feature specs with Capybara and Chrome Headless - How to set up the latest refinements in Rails system tests.
  • Suicide Linux - The distro equivalent of "do you feel lucky, punk?" Now available as a Docker image.
  • The future of querying json in PostgreSQL - Looks yummy, though certainly not the SQL I learned lo these many years ago. SELECT * FROM my_table WHERE JSON_EXISTS(ranges, '$[*] ? (@.min < $x && $x <= @.max)' PASSING number AS x);
  • Announcing Gatsby 1.0.0 - A major milestone for this React-based static site generator. Lots of spiffy advanced features are built in, too.

Double Shot #1883

Introduction to Active Storage

Today DHH announced Active Storage: a new gem that makes it easy to attach Amazon S3 or Google Cloud Storage files to Active Record model instances. Looks like this is slated to become part of Rails in 5.2. I happen to have a Rails 5.2 application with a user model around, so here's a quick look at adding an avatar to that model with Active Storage.

NOTE: This is early days for this library, so don't count on this post to be perfectly accurate if you're reading it months from now.

1. Add the gem to your Gemfile and run bundle install

gem 'activestorage', github: 'rails/activestorage'

2. Add require "active_storage" to config/application.rb.

3. Run rails activestorage:install. This will create the storage and tmp/storage directories in your application, copy a default configuration file to config/storage_services.yml, and create a new migration. The migration will build the active_storage_blobs and active_storage_attachments tables in your database.

4. Run rails db:migrate

NOTE: I had to change the class name in the migration from ActiveStorage::CreateTables to ActiveStorageCreateTables to get it to run without errors.

5. Add config.active_storage.service = :local to your development.rb file. This will use the preconfigured local-file storage when you're in the development environment.

6. Tell your User model that it has an attached file:

class User < ApplicationRecord
  has_one_attached :avatar
  ...
end

7. Make a copy of your config/storage_services.yml file to config/storage_services_backup.yml. Then delete the amazon, google, and mirror sections from the original file. Otherwise, your server won't start, because it will be looking for keys and files that don't exist.

8. Add a field to your user edit form to input an avatar file:

<%= f.file_field :avatar, class: "form-control" %>

9. Add a control to your user show view to display the avatar:

<%= image_tag(@user.avatar.url) %>

10. Update your users controller to attach the file:

  def update
    @user = User.find(params[:id])
    avatar = params[:user][:avatar]
    if @user.update(user_params)
      if avatar
        @user.avatar.attach(avatar)
      end
      redirect_to @user, notice: "Your profile has been updated"
    else
      render 'edit'
    end
  end

11. Restart your application and navigate to /users/<id>edit to edit an existing user. Select a file and save the change. You should see the avatar image displayed on the edited user.

Of course, in production you'll want to use an actual cloud service. If you check your backed-up configuration file you'll see how to set up the keys for either S3 or AWS. Then it should just be a matter of setting the appropriate configuration in production.rb (note that I haven't tried this yet!)

Introduction to Active Storage, Part 4: Mirroring

If you've been following along:

Then you're ready for one of the cool features of Active Storage: mirroring. This one is really easy to set up:

1. Ensure that you have created amazon, local, and google sections in your storage_services.yml file, and that you have tested both of them.

2. Update your storage_services.yml file with a configuration stanza for mirroring:

mirror:
  service: Mirror
  services: [ amazon, local, google ]

3. Edit your production.rb file:

config.active_storage.service = :mirror

4. Deploy the application. You should still be able to add user avatars.

What does mirroring do? It gives you built-in redundancy against cloud service failures. In the case of the configuration above:

  • Files are uploaded to Amazon and Google, and stored locally.
  • Files are served from Amazon (the first member of the services array)

Should AWS go down for an entire region, you'd only need to change the services array in your config file, restart your server, and you'd be up and running again.

BUT: Note the "should" in step 4. As I write this, this doesn't actually work with the code in the Active Storage repo, because the MirrorService doesn't get configured properly. See issue 9, which includes a quick and hacky patch for the Active Storage internals to get things working.

Introduction to Active Storage Part 3: Google Cloud Storage

And to complete the trifecta, here's how you can update the Active Storage code to use Google Cloud Storage:

1. Sign in to your Google Cloud Platform account and go to your console (the URL will be something like https://console.cloud.google.com/home/dashboard).

2. You need to create a new project, and then a storage bucket inside of the project. Record the names for both so you can add them to your storage_services.yml file.

3. You'll need to use GCS's API Manager to create credentials for your new bucket. Create a set of credentials that use the Google Cloud Datastore API. Download your credentials as JSON and store a copy of the file in your project at config/gcs.json.

NOTE: Obviously, you need to keep the keys in this file confidential. Don't check them into a public repository, for example. Manage it the same way you manage your database.yml or other files containing confidential information.

4. Update your storage_services.yml file with a configuration stanza for Google:

google:
  service: GCS
  project: gstroop-******
  keyfile:
  bucket: ****-*******-9999 

5. Add a line to your production.rb file:

config.active_storage.service = :google

6. Add the google-cloud-storage gem to your Gemfile and run bundle install.

7. Deploy the application. You should now be able to add user avatars and have then stored in the GCS bucket that you configured.

Introduction to Active Storage Part 2: Amazon S3

Moving along, here's how to move your Active Storage files over to Amazon S3:

1. Sign in to your AWS account and go to your S3 management console (the URL will be something like https://console.aws.amazon.com/s3/home?region=us-west-2# depending on your region).

2. Create a new bucket (for this tutorial, I'll use gstroop-production). Grant public read access to the bucket.

3. Retrieve the Access Key ID and Secret Access Key for your AWS account. Better yet, create a new pair just for this application.

4. Update your storage_services.yml file with a configuration stanza for Amazon:

amazon:
  service: S3
  access_key_id: AKIA************
  secret_access_key: *********************************************
  region: us-west-2
  bucket: gstroop-production

NOTE: Obviously, you need to keep those keys confidential. Don't check them into a public repository, for example. You can use the Rails secrets file to store them, or whatever other mechanism you prefer for production secrets.

5. Add a line to your production.rb file:

config.active_storage.service = :amazon

6. Add the aws-sdk gem to your Gemfile and run bundle install.

7. Deploy the application. You should now be able to add user avatars and have then stored in the S3 bucket that you configured.

Double Shot #1882

Double Shot #1881

Double Shot #1880

Double Shot #1879

Double Shot #1878

  • An Overview of the Logging Ecosystem in 2017 - Really good centralized logging is something I've always wanted and never convinced management to pay for. A personal failing I'm sure.
  • Serverspec - "RSpec tests for your servers configured by CFEngine, Puppet, Ansible, Itamae or anything else." A nice addition to the devops arsenal. Though of course configuration tools work perfectly every time, right?
  • MongoDB Indexing, Monitoring & Backup - A look at some of the supporting bits of the MongoDB story.

Double Shot #1876

Double Shot #1873

Double Shot #1872

Double Shot #1871

I'm at Girl Scout camp for the next two weeks. However, that doesn't mean I'm completely unavailable -- and I'm still job-hunting. So please, if you have work for a flexible Rails developer & team lead, do drop me an email at MikeG1 [at] larkfarm.com. There are plenty of times I can talk even from camp.

  • Refactoring Rails - A video course coming from Ben Orenstein. Sign up for news & a discount on the product when it comes out.
  • Debugging Design - And speaking of coming ebooks, here's one that promises to teach developers a design process they can understand. I just blew an interview over this very topic.
  • Reduce your javascript bundle size by 77% - Wiring up compressors into your web pack build pipeline to pre-create the compressed version before uploading.
  • DevDocs - Searchable API documentation in the browser. It covers a whole raft of things from CSS and HTML to jQuery, Jasmine, Lua, Meteor, and on and on. You can pick and choose the collections you're interested in.

Double Shot #1870

subscribe via RSS