Wednesday, September 23, 2015

on destructuring

[5:42 PM] Darien Valentine: [ x, y ] = z || []
[5:44 PM] Darien Valentine: and when destructuring in parameters, for the same reason sometimes you'll want to supply an empty default
[5:44 PM] Darien Valentine: function poop({ color, length, aroma }={}) {}
[5:46 PM] Darien Valentine: you can also supply defaults in destructured assignments that occur anywhere
[5:47 PM] Darien Valentine: const { poopCount=0 } = event;
[5:48 PM] Darien Valentine: the trickiest part of destructuring syntax at first is the distinction between the bindings you're creating & actual property names. effectively, in the examples above and most that you find (with objects), those bindings are actually a form of shorthand
they are doing double duty as both the binding (var, const, let) as well as the property name
the long form would look like this:
[5:49 PM] Darien Valentine: const { poopCount: poopCount=0 } = event;
meaning the binding identifier there -- the second poopCount -- could actually be another identifier; you're not stuck with the property name
[5:51 PM] Darien Valentine: last bit of info I can think of to confer:
you can destructure to things that already were declared, but if it's an object pattern, you need parens or another means to expressionize it (just like with object literals)
[5:53 PM] Darien Valentine: ({ cats } = obj);

Tuesday, November 18, 2014

tail a *nix command in your browser

if you have bash or zsh:

command | cat -u <( echo $'HTTP/1.0 200 OK\nContent-type:text/plain\n\n' ) - | nc -l 8080

otherwise:

echo $'HTTP/1.0 200 OK\nContent-type:text/plain\n\n' > header.txt
command | cat -u header.txt - |  nc -l 8088

enjoy the output on http://localhost:8080

Saturday, August 16, 2014

rasbian default ssh root password

This shouldnt be so hard to find. I had a hell of a time trying to find the root password for rasbian on my raspberry pi. There are a lot of places that claim that the username is "pi" but this is not true. use root.

user: root
pass: raspberry

once you ssh in, change it using the passwd command. to ssh in, find the thing's IP and type ssh root@THE_IP

Thursday, November 14, 2013

Building a custom Vagrant box with Parallels Desktop as the Vagrant Provider

I recently needed to build myself an ubuntu 12.04-lts (amd64) vagrant box that would work in parallels. The problem was that the vagrant-parallels plugin does not provide you that box, and there is very little  information on how to build one yourself. (note - that link is not up to date).

The link above got me part way there, but I found that I did not need to install the parallels SDK or do any voodoo restarts.

The idea is the same though, use veewee to build the vagrant-parallels box. Unfortunately veewee does not have very good docs about how to do this.

The solution was to modify the veewee 'ubuntu 12.04-lts' definition and to replace the virtualbox.sh setup with a parallels.sh setup which I found elsewhere.


So here is how you can build a vagrant parallels ubuntu server box:

Requirements:

vagrant plugin install vagrant-parallels
sudo gem install veewee
bundle exec veewee parallels define 'ubuntu-12.04-lts' 'https://github.com/mkoryak/vagrant-parallels-ubuntu-12.04-lts.git/definitions/parallels-ubuntu-12.04-server-amd64'
bundle exec veewee parallels build 'ubuntu-12.04-lts'  --workdir=.
bundle exec veewee parallels export 'ubuntu-12.04-lts' --workdir=.
Now you have a vagrant parallels box.
To import it into vagrant type:
vagrant box add 'ubuntu' './ubuntu-12.04-lts.box'
To use it:
vagrant init 'ubuntu'
vagrant up --provider=parallels
vagrant ssh

Updates to this process may be found on my github page for this: https://github.com/mkoryak/vagrant-parallels-ubuntu-12.04-lts

Tuesday, September 25, 2012

vertically center content in bootstrap

This is how you vertically center stuff in bootstrap:



This doesnt work in IE8 or less.

Monday, September 24, 2012

install syntax highlighter 3.x on blogger

To install syntax highligher on blogger here what you need to do:

Edit raw template html and put this after the HEAD tag:


and put this at the bottom of the template before the closing HTML tag: ]


NOTE:
If the javascript snippet above you must escape all html entities. They are shown as non-escaped becase of limitations of blogger. You will have to replace all of ' with @quot; and < with <

Monday, September 17, 2012

A/B Testing with nginx via rewrite

Here is how you can setup 50% of your users to see a different homepage via nginx.

We split them based on the last digit of their IP address:

location / {
    if ($remote_addr ~ "[02468]$") {
        rewrite ^(.+)$ /a$1 last;
    }
    rewrite ^(.+)$ /b$1 last;
}
location /a {
    alias   /Users/dogself/site;
    index  index.html index.htm;
    ssi    on;
}
location /b {
    alias   /Users/dogself/site;
    index  index2.html index2.htm;
    ssi    on;
}

The trick here is that instead of using the root directive you should use the alias directive. Using alias means that you dont need to create an 'a' and 'b' directories on your server for this to work.

The downside is that now you have /a/ and /b/ in your path. If you know a way around that, let me know!

How To Install Node.js on RasberryPi

This will let you install the latest version of node.js on a Raspberry Pi running occidentalis 0.2:


git clone https://github.com/joyent/node.git
cd node
export GYP_DEFINES="armv7=0"
export CXXFLAGS='-march=armv6 -mfpu=vfp -mfloat-abi=hard -DUSE_EABI_HARDFLOAT'
export CCFLAGS='-march=armv6 -mfpu=vfp -mfloat-abi=hard -DUSE_EABI_HARDFLOAT'
./configure --shared-openssl --without-snapshot
make
make install


Compiling node will take about 4-5 hours.

but at the end you will be rewarded with:

pi@raspberrypi ~ $ node --version
v0.9.2-pre

Wednesday, August 15, 2012

Instrument javascript functions with before and after hooks

Use-Case:

I have a javascript class that i want to intrument in an aspect oriented programming (AOP) style by adding before and after hooks to every method in the class. I want to do this generically.

The before function should take the same arguments as the instrumented function and return a set of modified (or not) arguments for the instrumented function.

The after function should take the same arguments as the arguments passed to the instrumented function.

You can also do other things, for example the before function could abort the call to the instrumented function and the after function could munge the returned value.

Solution:

I decided to add the before and after functions as properties of the instrumented function. Remember, in javascript functions are also objects and can have properties added just like any javascript object.

Once my class's functions are instrumented the code that uses the class remains the same, but new code can now define the new before and after functions:
MyClass.someMethod.before = function(...) { ... }

Code:
Note: This code makes use of underscore.js

Final Thoughts:
The code as it stands now does not allow the programmer to define multiple before and after functions, only a single one can exist at one time. In the future I may consider supporting binding multiple functions.

Wednesday, July 25, 2012

get table sizes of MySQL database

mysql> SELECT count(*) tables,
    ->   concat(round(sum(table_rows)/1000000,2),'M') rows,
    ->   concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,
    ->   concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
    ->   concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
    ->   round(sum(index_length)/sum(data_length),2) idxfrac
    -> FROM information_schema.TABLES;
+--------+---------+--------+--------+------------+---------+
| tables | rows    | data   | idx    | total_size | idxfrac |
+--------+---------+--------+--------+------------+---------+
|    389 | 325.92M | 74.15G | 26.63G | 100.78G    |    0.36 |
+--------+---------+--------+--------+------------+---------+
1 row in set, 14 warnings (15.45 sec)

To get the details for one table, using something like:

mysql> SELECT count(*) tables,
    ->        concat(round(sum(table_rows)/1000000,2),'M') rows,
    ->        concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,
    ->        concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
    ->        concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
    ->        round(sum(index_length)/sum(data_length),2) idxfrac
    ->        FROM information_schema.TABLES
    ->        WHERE  table_name like "users";
+--------+-------+-------+-------+------------+---------+
| tables | rows  | data  | idx   | total_size | idxfrac |
+--------+-------+-------+-------+------------+---------+
|      1 | 3.47M | 0.58G | 0.21G | 0.78G      |    0.36 |
+--------+-------+-------+-------+------------+---------+
1 row in set (0.27 sec)

Saturday, December 03, 2011

configure boxee to work as a second monitor with sound over hdmi

My HTPC setup is simple:
I ran a 40 foot HDMI cable from my computer room into my bedroom through the basement. I hooked up my TV in the bedroom to be the 2nd monitor on my PC and set boxee to run on that monitor.

The problem:
I wanted to have Boxee use sound over hdmi, but also be able to listen to to music and play games on my computer as usual without the sound coming from the bedroom TV.

Solution:
In your windows sound prefs set:
  • "Speakers" as your default audio device
  • "HDMI digital output" as your default communication device
In boxee sound config set:
  • primary soundcard: HDMI digial sound
restart boxee. Now you should be able to hear HDMI sound only on the TV, and all other sound will come from your speakers. 

Tuesday, October 18, 2011

Photosmart CN216 no LCD or power lights

I have a brand new (POS) HP Photosmart CN216 all in one print/scan/copy/web printer.
The only trouble is that sometimes it will power on, but no lights come on. The power light is not not blue, and the LCD is off. At the same time, the printer is on because its making the normal noises.

I was about to return it to HP since once this happens no amount of pressing the power button will "turn it back on". I found a solution:

The fix is to open the printer as if you were about to replace the ink cartridges, then SLAM it shut. It helps if you are are angry, but you will be when your brand new 100$ wireless printer doesnt work. This will cause it to turn on properly.

The downside to this solution is that it cannot be done at night when people are sleeping

Thursday, September 29, 2011

mysql function example

delimiter //
create function mytest () returns char(50) deterministic
begin
declare c int;
select count(*) into c from table;
return c;
end
//
delimiter ;

Friday, July 08, 2011

Python: remove some entries from a dict

Problem:
Remove a bunch of entries from a dict in an elegant way

Solution 1:

map(lambda name: dict.pop(name, None), ['some_key', 'some_other_key', 'bleee'])


Solution 2:
My coworker Dave came up with an even better way to do this thing:

map(dict.pop, ['some_key', 'some_other_key', 'bleee'], [])

Wednesday, April 06, 2011

Grails: Securing CKEditor URLs

Problem:
I am using the grails CKEditor plugin and i wanted to secure the file manager urls to be accessed only by a site admin. I am using the Spring Security Core plugin for site security, but i was already using annotations elsewhere on my site, so using a InterceptUrlMap was not an option.

So, how to keep using annotations and secure a plugin's urls?

Solution:
The trick is properly using the controllerAnnotations.staticRules (which really isnt that hard if you pay attention to the docs).
Here is the mapping that works for ANY url mapping you define to CKEditor:



Thursday, March 24, 2011

manually set id of grails domain object

Problem:
if you create a new domain object and set its id in the constructor like this:
Cat catman = new Cat(id:1000, name: "catman", kind: "domestic short haired")
later, if you look at the object the id will be null. what gives?!


Solution:
Its madness, but it works to assign the id after the object is made. Just add
catman.setId(1000);
and you are in business.

Note: if you want to save the object into db after doing this, you must set the id generator to 'assigned'

Sunday, March 13, 2011

lg cdma usb modem driver

The lg mobile phone driver is hard to find. i am using it to make bitpim work with my phone.

It will work with all LG VX-something phones and also is sometimes called this:

Prolific USB-to-Serial Comm Port Driver
LGE CDMA Composite USB Device Driver
LGE CDMA USB Modem Driver
LGE CDMA USB Modem Diagnostic Serial Port Driver
LGE CDMA USB Modem (VCOMM) Driver
LGE CDMA USB Modem Diagnostic Serial Port (VCOMM) Driver
LGE CDMA USB Serial Port Driver


get it here:
http://programmingdrunk.com/random-stuff/LGUSBModemDriver_4.6_WHQL.zip

Monday, February 21, 2011

Class needed by SvnTask cannot be found

problem:
getting this error when trying to build:

taskdef A class needed by class org.tigris.subversion.svnant.SvnTask cannot be found: org/tigris/subversion/svnclientadapter/SVNClientException

solution:

I didnt unzip all libs that came with svnant into my lib folder. Only unzipped svnant.jar. need to put them all in there.

Sunday, February 06, 2011

Multiple domains with tomcat behind apache

This is how you would host multiple domains/webapps on a single tomcat behind apache via mod-jk.

first setup tomcat behind apache

I am setting up a new domain called dogself.com:

in server.xml add this under last host:

<Host name="dogself.com"  appBase="webapps-dogself"
  unpackWARs="true" autoDeploy="true"
  xmlValidation="false" xmlNamespaceAware="false">
<alias>www.dogself.com<alias>
</Host> 
 
and also add another connector port:
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> 

create a webapps-dogself directory next to webapps and give it the same permissions

in your virtualhost apache config:
ServerName dogself.com 
JkMount  /* dogself 
 
in workers.properties add dogself worker to list:
worker.list=another,dogself

# Define dogself
worker.dogself.port=8010
worker.dogself.host=dogself.com
worker.dogself.type=ajp13 

If you want the manager app to work on the domain you need to:
mkdir /etc/tomcat6/Catalina/dogself.com 

and copy the manager.xml into it from /etc/tomcat6/Catalina/localhost

in /var/lib/tomcat6 create a webapps-dogself dir and give it 0775 perms and chown it to tomcat:tomcat

restart tomcat and apache

Troubleshooting:
if you get error:
worker dogself can't resolve tomcat address dogself.com
Check that your dns a/aaaa records point to your ip. Comment out the worker in workers.properties and see if you can get to the apache vhost.

if any of the domains seems to point to another domain, ie the war that is running on one domain is incorrect. check if you are in the www.domain and if you have aliased the www both in tomcat and in apache. if you dont alias the www correctly tomcat will send you the the defaultDomain as defined in server.xml

Thursday, February 03, 2011

Linode: how to install mod_jk, tomcat behind apache

This "tutorial" is for Ubuntu 10.04 LTS (Lucid).
This is how you would set up Tomcat6 behind Apache2 via mod_jk

I am assuming that you have apache2 installed and tomcat6 installed. I also assume you have deployed and can find your tomcat app possibly on port 8080 of your linode.

  1. Install mod_jk:
    apt-get install libapache2-mod-jk
  2. Create workers.properties in /etc/apache2/ and add the following into it:
    # this lives in /etc/apache2/workers.properties
    # workers.properties - ajp13
    #
    # List workers
    worker.list=worker1
    
    # Define worker1
    worker.worker1.port=8009
    worker.worker1.host=localhost
    worker.worker1.type=ajp13
    
  3.  Edit your apache2 config in /etc/apache2/apache2.conf and add this to the end:
    # tomcat connector stuff:
    JkWorkersFile /etc/apache2/workers.properties
    # Where to put jk shared memory
    JkShmFile     /var/log/apache2/mod_jk.shm
    # Where to put jk logs
    JkLogFile     /var/log/apache2/mod_jk.log
    # Set the jk log level [debug/error/info]
    JkLogLevel    info
    # Select the timestamp log format
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] 
    
  4. In your /etc/apache2/sites-enabled/ dir find the vhost you want to use tomcat and edit it, at the end of the vhost declaration put:
    #Everything under root goes to tomcat
    JkMount  /* worker1
    #html files should be served by apache2
    JkUnMount /*.html worker1
    
  5. Edit /etc/tomcat6/server.xml and make sure that the ajp connector is uncommented:
    <connector port="8009" protocol="AJP/1.3" redirectport="8443" />
    
  6. restart tomcat: /etc/init.d/tomcat6 restart
  7. restart apache: /etc/init.d/apache2 restart
  8. navigate to yourdomain.com/ and you should be all tomcatted