expressでBasic認証

expressを使用してBASIC認証を試してみたよ。

var app = module.exports = express.createServer(express.basicAuth(authorize));
// Basic Auth
function authorize(username, password) {
    return 'username' === username & 'password' === password;
}

3行目以降を次のように変更すればパスを分けてBasic認証することが可能っす。

app.all('/tyome/*', express.basicAuth(function(user, password) {
  return user === 'username' && password === 'password';
}));

あっさりとBasic認証の設定ができます。

カテゴリー: Nodejs | コメントをどうぞ

Arduino1.0の変更点とか。

久しぶりにArduinoを触ったら1.0がリリースされていたのを忘れてた。。
ちょうどいい機会だから変更点とかをまとめておこうかと。

1.0は Arduino Software からダウンロードしてね。

【変更点】
・拡張子
  拡張子が .pde から .ino に変更。

・標準ライブラリ
 ・Serial周り
  Serial.print()などデータの送信はバックグラウンドで実施。
  Serial.flush():受信データを破棄する処理から、送信データがすべて送信されるまで待つ処理に変更。
  Serial.print(byteVal):数値を文字として送信
  byte型を数値のまま送信したい場合:Serial.write(byteVal) を使います。あわせてBYTEキーワードも廃止。
 
 ・ネットワーク周り
  ネットワークアクセス用の抽象クラスClient、Server、UDPを追加
  EthernetClient、EthernetServer、EthernetUDPはこれからのクラスを継承

 ・Arduino APIのヘッダ名変更
  WProgram.h → Arduino.h に変更。
  0022の開発環境で新しいライブラリを入れると「Arduino.hがないよ」って怒られる。
  次のようにARDUINOマクロをチェックするコードを入れることで
  Arduino 0022以前のIDEでもコンパイルを通すことは可能。
  

  #if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #else
  #include "WProgram.h"
  #endif
  

  
  動く保証はないので1.0で作り直す方がいいのかな。
  
他にもいろいろあるけど自分がいろいろ作って遊んでいたところで関わりそうなのはこのへんなのかなと。
詳しい変更点は Release Notes を見てください。

カテゴリー: Arduino | コメントをどうぞ

Leopardにdotcloud CLIのインストール失敗

dotCloud CLIをLeopardに入れたときにエラーが発生。。。
結論から言うと、macportsを使用してのPythonでdotCloud CLIを動かすのはダメみたい。
それなりに時間を使ったのでメモを。

【環境】
 Mac 10.5
 MacPorts 2.0.4

DotCloud DocumentationのMacOSタブを選択して
dotCloud CLI (Command Line Interface)をインストールする。
$ sudo easy_install pip && sudo pip install dotcloud
Searching for pip
Best match: pip 1.1
Processing pip-1.1-py2.5.egg
pip 1.1 is already the active version in easy-install.pth
Installing pip script to /usr/local/bin
Installing pip-2.5 script to /usr/local/bin

Using /Library/Python/2.5/site-packages/pip-1.1-py2.5.egg
Processing dependencies for pip
Finished processing dependencies for pip
Downloading/unpacking dotcloud
Running setup.py egg_info for package dotcloud
Traceback (most recent call last):
File ““, line 14, in
File “/Users/takeda/build/dotcloud/setup.py”, line 47, in
install_requires = ['dotcloud.cli == {0}'.format(VERSION)],
AttributeError: ‘str’ object has no attribute ‘format’
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File ““, line 14, in

File “/Users/takeda/build/dotcloud/setup.py”, line 47, in

install_requires = ['dotcloud.cli == {0}'.format(VERSION)],

AttributeError: ‘str’ object has no attribute ‘format’

—————————————-
Command python setup.py egg_info failed with error code 1 in /Users/takeda/build/dotcloud
Storing complete log in /Users/hogehoge/Library/Logs/pip.log

pipはうまく入ったっぽいが、dotcloudのインストールでエラーが発生してるくさい。
strにはformatなんていう関数は無いよって。

調べたらpythonのバージョンが古いのが原因っぽい。
確認してみると
$ python –version
Python 2.5.1

やっぱり古かった。Pythonのバージョンは2.6以上じゃないとダメなようなので
MacPortsを使ってバージョンアップしてみる。

・Pythonのインストール
 $ sudo port install python27
 —> Fetching archive for expat
 —> Attempting to fetch expat-2.1.0_0.darwin_9.i386.tgz from http://packages.macports.org/expat
       省略
 To make python 2.6 the default (i.e. the version you get when you run ‘python’), please run:
 sudo port select –set python python27

・使用するバージョン選択するための準備
 インストールしただけだとpython2.6は使えないので、使えるようにするために
 python_selectをインストールする。
 $ sudo port install python_select
 —> Cleaning python_select

 インストールできて内容な気がするけど念のためコマンドをたたいてみる。

・選択できるバージョンを確認
 $ python_select -l
 やっぱり出来てなかった。Command not foundって怒られる。
 何やらpython_selectは使えないようだ。。
 
 改めて、
 $ sudo port select –list python
  python25-apple (active)
  python26
  python27

・バージョンを選択
 $ sudo python_select python27

・バージョンを確認
 python -V
Python 2.5.2

なぜ。。。Pathが通ってないようなので.bash_profileに以下を追加

export PATH=/opt/local/bin:/opt/local/shin:$PATH

$ source ./bash_profile

これで再度バージョン確認。
$ python -V
Python 2.7.2

これでやっとdotCloud CLIをインストールできると思ったら、今度はpipでダメだと。
pipも2.7に設定して、

$ sudo easy_install-2.7 pip && sudo pip-2.7 install dotcloud
としてインストールには成功してるみたいだけど、

$ dotcloud
-bash: dotcloud: command not found

だって。これ以上時間をかけたくないので他の方法でPythonを入れて試そう。

カテゴリー: Cloud | コメントをどうぞ

ubuntu11.10 に Jenkinsをいれるための準備

javaのバージョンを確認
$ java -varsion
java version “1.6.0_23″
OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10.2)
OpenJDK Client VM (build 20.0-b11, mixed mode, sharing)

入ってなければ以下のコマンドでjavaをインストール。
sudo apt-get install openjdk-6-jdk openjdk-6-jre

次にgitのインストール
$ sudo apt-get install git-core

$ git –version
git version 1.7.5.4

次にGitHubアカウントのセットアップ
 http://github.comの[Pricing and Signup] をクリック
 Free (無料) アカウント用の [Sing Up!] ボタンをクリックします。
 [Username]、[Email Address]、[Password]、[Confirm Password] を入力します。
 [I agree, sign me up!] ボタンをクリックして、アカウントを作成します。

■レポジトリを作成
 鍵の準備
GitHubに登録する公開鍵の作成
  $ cd .ssh
  $ ssh-keygen -t rsa -C “your github mail address”
  $ Generating public/private rsa key pair.
  $ Enter file in which to save the key (/Users//.ssh/id_rsa): /Users//.ssh/github_rsa
  Enter passphrase (empty for no passphrase):
  Enter same passphrase again:

 鍵をGitHubへ登録
  GitHubにアクセスしてログインする。
  画面右上の「Account Settings」をクリック。
  SSH Public Keysを選択。
  タイトルに鍵を識別する任意の名前をつける。
  作った公開鍵(id_rsa.pubとか)の中身を、まるっとコピー&ペーストする。
  Add keyボタンクリックで登録完了。

  以下でGitHubにアクセスできたらOK。
  $ ssh -T git@github.com
  Hi アカウント名! You’ve successfully authenticated, but GitHub does not provide shell access.

ここまできてやってとレポジトリの作成
 GitHubのページから[Dashboard]をクリックし、画面中段右の[New Repository]ボタンをクリック。
 リポジトリ登録画面が表示されるのでプロジェクト名(HelloWorld)を入力する。
 オプシンでプロジェクトの説明とホームページも入力出来る。
 [Create Repojitory]ボタンで作成完了。
 後は画面に従って操作する。

続いてレポジトリのフォーク
 GutHubのアカウントにログイン
 レポジトリのURLにアクセス
 Forkボタンをクリック

確認のためにローカルコピーをフォーク(レポジトリの個人用コピー)する。
$ git clone git@github.com:アカウント名/HelloWorld.git
Cloning into HelloWorld…
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Receiving objects: 100% (3/3), done.
$ ls
HelloWorld

HelloWorldがClone(SVNでのチェックアウト)されている。

とりあえず今日はここまで。

カテゴリー: Jenkins, Linux | コメントをどうぞ

お勉強

クラウドコンピューティングに触れる機会が多くなってきたから
この辺でしっかりお勉強をしておこうかと。

まずは定義からしっかりと。数学の基本すな。
【構成定義】
 IaaS(Infrastructure as a Service):サーバ/CPU/ストレージなどのインフラをサービスとして提供する。
 PaaS(Platform as a Service):アプリケーションを稼働させるためのプラットフォームをサービスとして提供する。
 SaaS(Software as a Service):アプリケーション(ソフトウェア)をサービスとして提供する。

【特徴】
 IaaS:ネットワーク/ハードウエア(CPU・メモリー・ハードディスク)/OSを提供するサービス。
    ユーザーは何もインストールされていないサーバー環境を提供され、その上にユーザーが必要とするミドルウエアや、
    アプリケーションソフトウエアをインストールする。
   ★レンタルサーバとの違い
    サーバーのCPU・メモリー・ハードディスクを仮想化技術などによって、ユーザーが必要とする分だけ提供する。
   ★具体例
    Amazon EC2(EC2はOSも提供しているので厳密な意味でのIaaSではないけど)

 PasS:IaaSの構成要素に加え、データベースソフトやWebサーバーソフトといったWebアプリケーションを
     稼働させるためのソフトウエアや、アプリケーション開発に必要となるソフトウエアを提供。
     ユーザーは、必要なアプリケーションのソースコードをPaaS環境にアップロードし、
     必要なデータベース定義を設定することで、アプリケーションを動作させることが可能。
   ★IaaSとの違い
    OSの各種設定や保守作業をユーザー自身が行う必要がないため、ユーザが直接操作することはない。
   ★注意点
    アプリケーションで利用できる開発言語が限定される点や、PaaSにおいて提供されていないミドルウエアについては
    ユーザー自身が準備しなければならない。
   ★具体例
    dotcloud

 SaaS:ユーザーはネットワーク・ハードウエア・OS・ミドルウエアのすべての要素について、自身で準備をする必要なし。
     ユーザーはWebブラウザを利用して、 SaaS提供事業者のWebアプリケーションにアクセスし、サービスを利用する。

カテゴリー: Cloud | コメントをどうぞ

Node + webSocket on dotCloud

dotCloud上でNodejsを使ってみたときのメモ。

まずはアプリケーションの作成
 $ dotcloud create nodewebsocket

ビルドファイル(dotcloud.yml)の作成

#dotcloud.yml
www:
  type: nodejs
  approot: chat

packge.jsonはこんな感じ

#packge.json
{
  "author": "Jxck",
  "name": "nodechat",
  "version": "0.0.0",
  "repository": {
    "url": ""
  },
  "engines": {
    "node": "*"
  },
  "dependencies": {
    "express": "*",
	"ejs": "*",
    "socket.io": "*"
  },
  "devDependencies": {}
}

続いてsupervisord.confをこんな感じに設定する

#supervisord.conf
[program:node]
command = node server.js
directory = /home/dotcloud/current

必要なファイルを以下のように設定
$ tree
nodechat
├── chat
│   ├── server.js
│   ├── node_modules
│   │   ├── ejs -> /opt/local/lib/node_modules/ejs
│   │   ├── express -> /opt/local/lib/node_modules/express
│   │   └── socket.io -> /opt/local/lib/node_modules/socket.io
│   ├── package.json
│   ├── public
│   │   ├── images
│   │   ├── index.html
│   │   └── javascripts
│   │      └── client.js
│   ├── supervisord.conf
│   └── views
└── dotcloud.yml

最後にPush!
$ ls
nodechat
$ dotcloud push nodechat nodechat/

完成かと思ったら、全然ダメでした。
client.jsを次のように修正

var socket = io.connect('http://アプリケーション名-アカウント名.dotcloud.com/');

これで再度Push!
ちゃっとができたっす!

カテゴリー: Cloud, Nodejs | コメントをどうぞ

php on dotcloud

dotcloudを試してみました。
dotcludはPHPやPerl、Ruby、Java、Python、Node.jsなど複数の言語と、MySQL、PostgreSQL、MongoDB、CouchDB、Redisなど複数のデータベースを開発者が
自由に組み合わせてプラットフォームを構成できて、クラウド上のPaaSとして利用できるみたい。
料金はどのプログラミング言語とデータベースを選択しても同一で、2サービスまでは無料で利用可能とのこと。

これは試すしかないと思ってとりあえず、チュートリアルに従ってやったときの手順をまとめておきます。

まずはdotcloudへの登録
dotcloudへアクセスしてのFreeの「Sing up now」を選択。
いろいろ登録

次にクライアントCLIをインストール
書いてあるけど念のためここにも書いておく
【Linux】
$ sudo easy_install pip && sudo pip install dotcloud
$ dotcloud
Enter your api key (You can find it at http://www.dotcloud.com/account/settings):APIキーを入力

【Mac】
$ sudo easy_install pip && sudo pip install dotcloud
$ dotcloud
Enter your api key (You can find it at http://www.dotcloud.com/account/settings):APIキーを入力

これでクライアントCLIツールのインストールが完了

次は実際にphpで書いてみよう。

まずはファイルを作成するフォルダの作成
$ cd /Users/hoge/Documents/
$ mkdir 04_dotcloud
$ cd 04_dotcloud

phpファイルの作成
$ vi index.php

<?php
    echo "Hellow World";
?>

ビルドファイルの設定
$ vi dotcloud.yml

www:
 type: php

アプリケーションの作成
$ dotcloud create helloworld

ここまで出来たらCloud上にファイルをPushしてやります。
$ dotcloud push helloworldapp

するとURLが返ってくるので公開完了
www: http://アプリケーション名-ユーザ名.dotcloud.com/

$ open http://アプリケーション名-ユーザ名.dotcloud.com/

で Hello worldが表示されればOK!

次はNodejsをいれてみよ。

カテゴリー: Cloud, Nodejs | コメントをどうぞ

oF to Arduino

openframeworksからArduinoへシリアルケーブルを使用してデータをなげる方法
ちょっと修正すればArduinoからopenframewoksへの転送も可能かな。

【oF側】
まずはデータを転送するArduinoの情報を設定

void testApp::setup(){
  serial.enumerateDevices();
  serial.setup("/dev/tty.usbserial-A800evPW",9600);
}

次に転送する契機の箇所に以下を追加

serial.writeByte(key);

データを受信するArduino側はこんな感じ

void loop()
{
  rcv = Serial.read () - 48;
  Serial.print("rcv byte is: ");
  Serial.println(rcv, DEC); // 10進数で表示
}
カテゴリー: Arduino, openframeworks | コメントをどうぞ

Arduino Ethernet Shield + SD

Ethernet ShieldにくっついてるSDカードスロットが
飾りのようになっていて寂しいので使ってみたよ。

書き込みように開くファイル名に文字数制限があるみたい。
8文字以内にしないとファイルオープンに失敗するから注意が必要。

【環境】
ArduinoIDE 0022 + Ethernet Library

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetDHCP.h>
#include <EthernetDNS.h>
#include <SD.h>

File myFile;
byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x12, 0xDE }; // Ethernet ShieldのMACアドレス
byte server[] = { 74, 125, 31, 106 };                 // Google
Client client(server, 80);

void setup() {

  // start the serial library:
  Serial.begin(9600);

  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);

  if( !SD.begin(4) ) {
    Serial.println("initialization failed!");
    return;
  }  

  // start the Ethernet connection:
  EthernetDHCP.begin( mac );
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect()) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  }
  else {
    Serial.println("connection failed");
  }
}

void loop()
{
  Serial.println("initialization done.");
  myFile = SD.open("Sample2.txt", FILE_WRITE);    // SDカードを書き込みように開く

  if (client.available()) {
    char c = client.read();
    if (myFile) {
      Serial.println("Writing to test.txt...");
      myFile.write(c);                // googleから取得した文字列をファイルに書き込み
    }
    else {
      // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  myFile.close();             // 書き込みが終わったらちゃんと閉じる
  Serial.println("SD Save Completed");
}
カテゴリー: Arduino | コメントをどうぞ

Arduino Ethernet Shield

書こう書こうと思ってほったらかしになってたので、
Arduino + Ethernet Shieldを情報をまとめておきます。

【環境】
ArduinoIDE 0022

とりあえずはTwitterから自分の投稿をゲットしてくる方法
XML解析は自分でお願いします。そんなに難しくないし、ライブラリもあるので。

#define SSR_PIN  9
#define MSG_LEN 140
byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x12, 0xDE };      // Ethernet ShieldのMACアドレス
byte server[] = { 199, 59, 148, 10 }; // www.twitter.com   // TwitterのグローバルIP
Client client(server, 80);

void setup()
{
  // 初期化
  pinMode(SSR_PIN, OUTPUT);
  digitalWrite(SSR_PIN, LOW);

  // start the Ethernet connection:
  EthernetDHCP.begin(mac);
  // start the serial library:
  Serial.begin(9600);
  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop()
{
  if( !client.connected() )
  {
    Serial.println("connecting...");

    // if you get a connection, report back via serial:
    if (client.connect()) {
      Serial.println("connected");
      // Make a HTTP request:
      client.println("GET /statuses/user_timeline/Twitterのアカウント名.xml HTTP/1.0");
      client.println();
    }
    else {
      Serial.println("connection failed");
    }
  }

  delay(1000);

  // 取得した文字列を出力
  while( client.available() ){
    char c = client.read();
    Serial.print(c);
  }

  Serial.println("disconnecting.");
  client.stop();

  // 60秒後に再接続
  Serial.println("waiting 60s.");
  delay(60000);
}
カテゴリー: Arduino | コメントをどうぞ