Reinaldo Junior

Um outro tech-blog

Deploy automatizado de aplicações Rails com Capistrano e Github

| Comments

Nesse tutorial irei abordar a realização de deploy automatizado de uma aplicação Rails (3.0.X) utilizando o Github como repositório. A idéia é utilizar o capistrano para automatizar (e organizar) as tarefas de deployment. Para isso, irei criar um usuário no servidor que utilizará minha chave privada (cadastrada no Github) para fazer o deployment da aplicação.

Configurando o servidor

No servidor, crie um usuário que será utilizado para realizar o deploy. Eu utilizei uma senha gerada aleatóriamente (um MD5 de um arquivo, por exemplo). Em seguida, adicionei minha chave privada (a que eu uso para acessar meus repositórios no Github) ao arquivo authorized_keys do usuário de deploy.

Na máquina local

1
2
scp ~/.ssh/id_rsa.pub reinaldo@servidor:/home/reinaldo/my_key.pub
ssh reinaldo@servidor

No servidor

1
2
3
4
5
6
7
sudo adduser deploy
MY_PUBLIC_KEY=/home/reinaldo/my_key.pub
sudo mkdir -p /home/deploy/.ssh
sudo cat $MY_PUBLIC_KEY >> /home/deploy/.ssh/authorized_keys && rm $MY_PUBLIC_KEY
sudo chmod 600 /home/deploy/.ssh/authorized_keys
sudo chmod 700 /home/deploy/.ssh
sudo chown deploy:deploy -R /home/deploy

Configurando a aplicação

Adicione a gem capistrano no seu Gemfile

1
gem 'capistrano'

Em seguida, no diretório da aplicação

“Capistrando” a aplicação
1
2
3
bundle install
capify .
echo ".bundle\n" >> .gitignore

Isso vai criar o arquivo config/deploy.rb onde serão armazenadas as configurações de deploy, bem como as tasks.

Meu arquivo ficou assim:

Pra finalizar, você precisa criar os diretórios no servidor. Para isso simplesmente execute (no diretorio da aplicação) cap deploy:setup.

Realizando o Deploy

Depois disso, você poderá realizar seu deploy automático executando o comando: cap deploy.

Playing with Droid

| Comments

During the last weeks I’ve been working on the Droid DSL. Now I’m proud to announce that it is working!

Since the last blog post, the DSL had changed. In order to simplify the DSL some concepts have been removed, for example, ImageButton, Link and ImageLink. These concepts are still available at the Button concept, so a Button with and “src” property is a ImageButton and a Button with a “target” property is a “Link”.

Another significant change in the DSL is the merging of the Activity and Layout concepts in a single concept: Screen (although the layout concept is still existing). This made easier to write a simple application. The screen’s widgets can now be placed inside the screen (you don’t need anymore to create a layout to put the widgets and set the activity to show the created layout).

So what used to be the simplest Droid example:
application "Hello, Android!" com.example.android.hellodroid {

string hello = "Hello, Android! I am a string resource!"

activity HelloDroid {
=> show main
}

layout main {
textView: @string hello
}
}

Now is even simpler:
application "Hello, Android!" => com.example.android.hellodroid {

string hello = "Hello, Android! I am a string resource!"

screen HelloDroid {
# textView: @string hello
}
}

In order to play with the Droid DSL, you need to install some plugins:
  1. Install the ADT Eclipse plugin. The target of the DSL is creating Android applications, so you must to install the ADT. The Android Hello World tutorial is all you need to have your environment ready to the work.
  2. Install the Droid plugins, located at this Update Site: http://reinaldojunior.net/eclipse-amalgamation-tutorials/

Creating your first Droid Project

A Droid project is an Eclipse Android Project that has automatic code generation from a droid model. The droid model is inferred from a .droid file (a textual representation of the model).

To create a Droid project you must to:
  1. Create an Android Project (following the instructions at the Hello World tutorial)
  2. Create a ’model‘ folder inside the project
  3. Create a ’src-gen’ folder inside the project
  4. Right-click the ’src-gen’ folder and select “Build Path” > “Use as source folder”
  5. You can delete the content of the ’src’ folder as it will have a generated Activity stub.
Now you are ready to start playing with the Droid DSL. Inside the ’model’ folder, create a .droid file and open it. The file will be opened with the DSL textual editor (generated with Xtext).

You can use this sample code to test your environment:
application "SimpleWizard" => org.eclipse.amalgam.tutorials.android.simplewizard {

screen First {
# textView: "This is the first screen" {}
# button: "Next >" to Second {}
}

screen Second {
# textView: "And this is the second screen" {}
# button: "< Back" to First {}
# button: "Next >" to Third {}
}

screen Third {
# textView: "Now, the last one" {}
# button: "< Back" to Second {}
}
}

Generating the code

  1. Right-click in the .droid file
  2. Select “Acceleo Model to Text” > “Generate Android Code”
  3. The generated code will be available at the ’src-gen’ folder and at the ’res’ folder.
In order to run the generated application, I recommend to use an Android 2.3.3 virtual device.

    The Droid DSL

    | Comments

    The first part of my GSoC project is creating a DSL (Domain Specific Language) targeting the development of Android applications. The main objective is the building of a DSL which can be used as the basis to the development of the Amalgamation Tutorials (the project’s final objective).

    The DSL’s abstract syntax [1] (an Ecore metamodel) was created from the definition of a concrete syntax  (a Xtext grammar [2]). To this part, I used Xtext. Before that, I and my mentor had discussed about what granularity level should be used to choose what concepts should be expressed in the DSL. A too fine granularity level could lead to a very detailed DSL but would take too much time to complete, while a too coarse one could lead to an very constrained DSL (and maybe useless).


    Considering this, I chose the following concepts as main concepts to the DSL:
    • Application - The main concept. We’re talking about Android Applications.
    • Activity - Represents an application screen and the user interface displayed.
    • Layout - Represents a collection of UI elements
    • Widget - Represents the various UI elements
    • Resource - Represents values which can be assigned to widgets properties. They have a type and can be serialized to a resource file (XML) 
    The following example ilustrates the Hello World sample application (provided by Android) written using the Droid DSL (there are more Hello World examples available [3])



    In order to have some direction in the creation of the tutorials, I started to create a code-generator using a Model-to-Text transformation. The transformation [4] can generate the AndroidManifest.xml file that describes the application, the Activity Java class, the Layout XML file and the String resource XML file.
    To make this I used the Acceleo.

    If you want to play with this DSL you just need to install the generated plugins in your Eclipse and create a new file with the .droid extension. For more information check the project repository at GitHub.

    References:

    1. Harel, D., & Rumpe, B. (2000). Modeling Languages: Syntax, Semantics and All That Stuff Part I: The Basic Stuff. Citeseer. doi: 10.1.1.10.1512.
    2. To more info, see: Droid.xtext
    3. Some of the Hello Views tutorials are available: Relative Layout and Linear Layout.
    4. Available under this path