Uncategorized

Category Archive Uncategorized

Punked!

It looks like some rather naughty individuals have found and exploited a vulnerability in my WordPress blog. This is an inconvenience but I’ll work to restore my notes so they can remain useful to the public.

Set Background Colour with AngularJS

From the “I can’t believe I had to look it up” department:

One can use ngStyle to programmatically set the background colour of an element. This is arbitrary ugly code that demonstrates the concept:

[html light=”true”]
<span ng-style="{background: item.color}">
[/html]

The “gotcha” is if one wishes to only use this on a certain page, one must reset the colour when the controller’s $scope is destroyed:

[javascript]
$scope.$on("$destroy", function(){
    $("body").css(‘background-color’, ‘white’);
});
[/javascript]

Initializing Objects with $.extend()

If jQuery is available, $.extend() can be useful “quick and dirty” method for initializing an object with defaults, while allowing for the caller to provide values to override the defaults.

The advantage is simplicity. One disadvantage is that there is no checking of arguments. The particular situation will drive the relative importance of each.

 

JavaScript Binding

Getting Out of Binding Situations is an old article, but worth the read.

AngularJS ngLocale Error

AngularJS, like all frameworks has its warts. Its documentation is spartan but adequate for most purposed.

When AngularJS encounters an error, it tends to cough up a messy hairball of unhelpful information.

For example,

[js]
[$injector:nomod] Module ‘ngLocale’ is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.2.7/$injector/nomod?p0=ngLocale
[/js]

By all rights I read this as a failure to load ngLocale. It took me some backtracking to determine that the cause of the error has nothing to do with ngLocale.

The Cause

I simply attempted to use a service that exists in a module that hasn’t been loaded. Adding the module to the application module’s dependency list resolved the error.

My druthers would be an error along the line of “Dependency ‘foo’ not found.”

 

Installing Django on CentOS

These are notes for what I did to get Django working on CentOS.

Activate EPEL.

Use the appropriate version, of course. I used:

[bash light=”true”]
$ sudo rpm -Uhv http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[/bash]

Install Prerequisites

  1. Install Apache.
    1. Ensure it’s working, including ensuring the firewall lets traffic through.
  2. Install MySQL
  3. Install revision control or however you pull software, e.g. git.
    1. Create a non-priviledged user, if that’s you install your software.
    2. Ensure Apache can access the software directory.

Install Django

[bash light=”true”]
$ sudo yum install python Django mod_python MySQL-python
[/bash]

Test Django

[python light=”true”]
$ python
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print django.get_version()
1.4.5
>>>
[/python]

Install and Test the Software

I create a separate user for managing the code via git.
Put the code somewhere appropriate, e.g. /opt.

  1. The address 0.0.0.0 tells Django to accept external connections.
  2. Ensure the test port is open on the firewall.

[bash light=”true”]
$ cd /opt/foo
$ python manage.py runserver 0.0.0.0:8000
[/bash]

Attempt to connect to your application. Ensure, for example, that the settings allow database access.

Running Django from Apache WSGI

Create an Apache WSGI file.

[bash light=”true”]
$ cd /opt/foo
$ mkdir apache
$ vim apache/django.wsgi
[/bash]

and fill the file with something like:

[python]
import os
import sys

sys.path.append(‘/opt/foo’)
sys.path.append(‘/opt/foo/foo’)

os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘foo.settings’

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

[/python]

Inside of the Apache virtual host:

[php]
<VirtualHost *:80>
ServerName foo.bar.com

WSGIScriptAlias / /opt/foo/apache/django.wsgi

# Alias /robots.txt /opt/foo/static/robots.txt
# Alias /favicon.ico /opt/foo/static/favicon.ico
Alias /static/admin/ /usr/lib/python2.6/site-packages/django/contrib/admin/media/
Alias /static/ /opt/foo/static/

<Directory /opt/foo>
Order allow,deny
Allow from all
</Directory>

<Directory /opt/food/static>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
[/php]

Restart the Apache server.

Test to ensure that you can connect.

Credits

A big shout out to ITek Blog’s article Installing Python / Django on Centos 6.3 is Easy!.

Tags, , ,

Remote control via ssh keys

These are some notes on remotely controlling a machine with ssh keys.

On a couple of remote testing machines I wanted to automatically pull the latest updates from git and restart the dæmon. The ~/.ssh/authorized_keys file is normally used “plain” with keys to allow login.

It is, however, possible to run a command when a certain key logs in, instead of dropping to a console. This makes it possible to remotely update and restart a dæmon from a script.

Take, for example, the line

command="/usr/local/bin/update-foo-from-git",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-rsa AAAAB3NzaC1yc2EAAA... foo@bar.com

This directs ssh that when this particular key is authenticated, forwarding is disabled and the script /usr/local/bin/update-foo-from-git is run as the logged in user.

If the script needs to touch something that requires higher privileges, sudo can be configured to allow that user to execute that script as a different user. For example the line

myuser   ALL= NOPASSWD: /usr/local/bin/update-foo-from-git

could be added to /etc/sudoers, and the line in .ssh/authorized_keys would then look like

command="/usr/bin/sudo /usr/local/bin/update-from-git",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-rsa AAAAB3NzaC1yc2EAAA... foo@bar.com

and the script could, for example, restart a system dæmon.

Using the Adafruit ArduinoISP shield to bootload a 8MHz standalone ATmega328

Programming an ATmega328 using an in-system programmer (ISP) is pretty straightforward. I was tasked with using an Adafruit ArduinoISP shield to make an ATmega328 run standalone using its internal 8MHz clock. These are my notes.

Create A New Board Definition

The Arduino web site doesn’t have very clear instructions for creating a minimal circuit aren’t too clear for the Mac.

  • Close the Arduino IDE if it’s open.
  • Download the Breadboard.zip file.
  • Extract the file contained therein.
  • Append it to the list of definitions found in

[code]
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/boards.txt
[/code]

When you start the Arduino IDE, you should see ATmega328 on a breadboard (8 MHz internal clock) in the menu.

Screenshot_5_28_13_7_35_PM

Burning the Bootloader

The code to turn the host Arduino into an ISP is here. Copy/paste into a new sketch and upload. Screenshot_5_28_13_7_10_PM

  • Install an ATmega328 in the ZIF socket.
  • Set the programmer to Arduino as ISP.
  • Choose Burn Bootloader.

The board should sing, the green light should blink rapidly while burning, and finally go dark. The Arduino IDE should say Done burning bootloader in the status bar at the bottom of the sketch window.

Into Your Light

Some nice passionate playing. The song is Into Your Light by Leaves’ Eyes performed by Vika Yermolyeva.

httpv://www.youtube.com/watch?v=smxeZOPi6Zw

Cleaning Up Jammed Subversion Transactions

There was a very odd server failure while a big Subversion commit was in process. (The ISP hosting the server is kind of flaky.) To clean up the leftover files from the failed transaction, I did this:

$ svnadmin lstxns repository-path
47-1
$ svnadmin rmtxns repository-path 47-1
Transaction '47-1' removed.

%d bloggers like this: