Friday, May 16, 2014

Provisioning Vagrant with Chef-Zero on Ubuntu 14.04 Trusty

I'm building a Vagrant instance for development and I want to use Chef to provision the machine.

I'm on Ubuntu Trusty 64. Running with the packaged versions of Vagrant and Ruby is a fool's errand. I used the Vagrant 1.5.4 package. Vagrant has an embedded Ruby which causes all kinds of grief cross-linking with stuff in the Ubuntu versions of the packages. I ended up removing them wholesale, but you may be ok if you be sure and always use the binaries from: /opt/vagrant/embedded/bin/

It took me almost a week to figure this out thanks to a lot of very unhelpful errors and red herrings. I figured I'd do a write-up and save you some suffering.
# Add the base vagrant box
vagrant box add ubuntu/trusty64 http://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

# Setup the vagrant instance
cd my-project
vagrant init ubuntu/trusty64

# Setup berkshelf and fix some path grief
env SSL_CERT_FILE=/opt/vagrant/embedded/cacert.pem /opt/vagrant/embedded/bin/berks
rm -rf ~/.berkshelf/default/cookbooks/
ln -s ~/.berkshelf/cookbooks/ ~/.berkshelf/default/cookbooks

# Get dependencies
sudo /opt/vagrant/embedded/bin/gem install bundler
/opt/vagrant/embedded/bin/bundle
vagrant plugin install vagrant-chef-zero
vagrant plugin install vagrant-berkshelf --plugin-version '>= 2.0.0'
Update your Vagrantfile:
config.berkshelf.enabled = true
config.chef_zero.enabled = true
config.chef_zero.environments = "./environments/"
config.chef_zero.data_bags = "./data_bags/"
config.chef_zero.roles = "./roles/"
config.chef_zero.cookbooks = "./cookbooks/"

config.vm.provision :chef_client do |chef|
  chef.run_list = [
    "role[base]" # Yours may be different
  ]
end
Then:
vagrant up

Monday, March 17, 2014

Spring MVC Serving Images - An Unreasonable PITA

I tried returning a @ResponseBody BufferedImage, but Spring kept returning an HTTP status 406.

Here's how I ended up solving the problem WITHOUT using the BufferedImageHttpMessageConverter (which seems to be a poorly documented method).
   
    @RequestMapping(value = "/{id}.jpg",
                    method = RequestMethod.GET)
    @ResponseBody
    @Secured("ROLE_READ")
    public ResponseEntity<byte[]> get(@PathVariable Long id)
                                  throws Exception {

        // Load it from disk
        File imageFile = new File(...);
        
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "image/jpg");

            byte[] bytes = FileUtils.getBytes(imageFile);

            return new ResponseEntity(bytes, headers,
                                      HttpStatus.NOT_FOUND);
        } catch (IOException e) {
            log.log(Level.INFO, "Couldn't load image file: " +
                                imageFile.getAbsolutePath(), e);

            throw e;
        }
    }

Wednesday, February 26, 2014

The Handiest GitHub Trick I know: ?w=no

Add it to the URL when you're looking at the diff of a pull request, and the diff will ignore whitespace changes.

It can make big ugly diff look really obvious and neat.

Use with care.

Wednesday, February 19, 2014

Access your AngularJS services with a handy JS console script

angular.element(document)
       .injector()
       .invoke(function($window, Todos, Account) {

  $window.MyNS = {
    Todos: Todos,
    Account: Account
  }
});


Now you have access to MyNS.Todos and MyNS.Account in the console!

Sunday, February 9, 2014

Magento 403 Forbidden at /.html

The Magento installation I've been working on has a somewhat unique property - many of the products don't have names. Magento normally uses the names to generate rewrite URLs, so that causes a weird quirk: The first product with no name gets a url like store.example.com/.html

Apache2 has an explicit DENY for files beginning with '.ht' that can be overridden in your .htaccess:

###########################################
## Fixing /.html
<Files ~ "^\.html">
    Order allow,deny
    Allow from all
    Satisfy any
</Files>
HTH someone else!