Archive for category tutorial
Distributed revision control was a breakthru
I strongly believe in revision control. Back in the days when there wasn’t any other choice that CVS I spent countless hours setting up CVS servers and re-freshing my mind on its obscure commands and arguments, which were almost all of them. If you drop my copy of Professional Linux Programming it’ll open on the CVS chapter by itself (much like my copy of The C Programming Language
will open itself on the pointers chapter, somewhere on the pointers to pointers section).
Then came Subversion and it was somewhat better. After that we got the explosion of revision control systems with the introduction of distributed ones. We’ve got Darcs, Git, Mercurial, Arch, Bazaar, etc. My choice right now is Git. It used to be Darcs, but unfortunately it stagnated for a long time and I moved on to the next new thing. I’ve used Mercurial as well. From my perspective Mercurial and Git are almost equivalent.
For me distributed revision control was a breakthru. There’s one small feature that makes a huge difference for me. In the old days I used to set up CVS or Subversion servers/repositories (in those, a repository is essentially a server-side thing). I had to decide where that repository was going to reside, how it was going to be named, how it was going to be accessed (ssh, http, custom protocol?), by whom, etc.
Today with Git I just do this
git init
That’s it. I’m done. The directory where I am is now a repository. I can start committing, branching, rolling back, looking at the history, generating patches, stashing, etc. It’s that simple. The fact that it’s so simple goes from a quantitative advantage to a qualitative advantage. What I mean is that I not only revision-control things faster but that I revision-control things I wouldn’t have otherwise. For example the configuration directories on my servers.
I know many people will complain: “But with no centralized repository chaos will ensue”. The fact that it’s distributed doesn’t mean there can’t be a central repository. When I want to collaborate with someone using Git I create one repo somewhere, in my servers or GitHub and we both push and pull from there. The difference is that I commit a lot locally, and when the chances are ready I push them. That means that I can commit broken code, without worrying.
At work we don’t use a distributed revision control system, we use a centralized one and we have a very string peer reviewing policy. My current tasks involve touching code in many different files in many different systems never getting familiar to any of them. That means that it’s common for my peer reviews to say things like “all this methods don’t belong here, these two should go there, that one should be broken into three different ones going here, there and somewhere else”.
Now I have a problem. I can’t commit because my peers don’t consider my code ready. My code works but it has to be refactored in a very destructive way. What happens if during the refactoring it stops working. For example copying and pasting I loose a piece of code. I can’t roll back to my working state and start over. If we were using a distributed revision control system I could.
So, being able to commit non-finished code locally while colaborating with other people is one of my other crucial features in DVCS.
The third one is being able to branch locally. In a similar vein as the last example. When I find myself thinking about a very destructive refactoring that I’m not sure if it’s going to get me anywhere and worst than that is going to take me three days to do; I just create a local branch. I experiment in that branch. If at any time I get tired or I need to do anything else I go back to the main one. That is what branching is about.
Why is locally branching better than globally or centralized branching? Well, one reason is that a local branch doesn’t have to make sense to anyone else. I don’t have to pick a name that’s descriptive for anyone else than me. I don’t have to justify myself for creating a branch with anyone else. Let’s suppose I had an argument with a co-worker where I believe something is doable and (s)he believes is not. Do I want him/her to see that I created a branch to prove him/her wrong? I don’t. And if I prove myself wrong I want to quietly delete that branch and never ever talk about it.
But I am starting to go into the very hypothetical realm. In short, for me, DVCS is about this:
git init
and get to code.
Erlang gen_server template
I like Erlang and I like gen_servers, I end up coding a lot of stuff as gen_servers and I like behaviors in generally. I even wrote some myself. I haven’t used Erlang for a while. I’ve been playing with it in the last couple of days, hopefully I’ll announce something soon.
I ended up creating my own gen_servers template to get started. It’s a very verbose one and I’m putting it here more for me to know where it is that anything else; but, maybe it’s of use for someone else:
-module(module). -compile(export_all). -behaviour(gen_server). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% Public API start() -> gen_server:start({local, ?MODULE}, ?MODULE, [], []). stop(Module) -> gen_server:call(Module, stop). stop() -> stop(?MODULE). state(Module) -> gen_server:call(Module, state). state() -> state(?MODULE). %% Server implementation, a.k.a.: callbacks init([]) -> say("init", []), {ok, []}. handle_call(stop, _From, State) -> say("stopping by ~p, state was ~p.", [_From, State]), {stop, normal, stopped, State}; handle_call(state, _From, State) -> say("~p is asking for the state.", [_From]), {reply, State, State}; handle_call(_Request, _From, State) -> say("call ~p, ~p, ~p.", [_Request, _From, State]), {reply, ok, State}. handle_cast(_Msg, State) -> say("cast ~p, ~p.", [_Msg, State]), {noreply, State}. handle_info(_Info, State) -> say("info ~p, ~p.", [_Info, State]), {noreply, State}. terminate(_Reason, _State) -> say("terminate ~p, ~p", [_Reason, _State]), ok. code_change(_OldVsn, State, _Extra) -> say("code_change ~p, ~p, ~p", [_OldVsn, State, _Extra]), {ok, State}. %% Some helper methods. say(Format) -> say(Format, []). say(Format, Data) -> io:format("~p:~p: ~s~n", [?MODULE, self(), io_lib:format(Format, Data)]).
Update 2010-01-04: I keep changing the actual contents of the template as I’m coding a little Erlang app. I’ve recently added, among other things, a call to get the state of the process, useful for debugging.
Revisioning /etc with Git
First and foremost I’m a coder, a coder who strongly believes in revision control. Second I am also a sysadmin; but only by accident. I have some servers and someone has to take care of them. I’m not a good sysadmin because I don’t have any interest on it so I learn as little as possible and also because I don’t want to be good at it and get stuck doing that all the time. What I love is to code.
I’m always scare of touching a file in /etc. What if I break something? I decided to treat /etc the same way I treat my software (where I am generally not afraid of touching anything). I decided to use revision control. So far I’ve never had to roll back a change in /etc, but it gives me a big peace of mind knowing that I can.
In the days of CVS and Subversion I thought about it, but I’ve never done it. It was just too cumbersome. But DVCS changed that, it made it trivial. That’s why I believe DVCS is a breakthru. Essentially what you need to revision-control your /etc with Git is to go to /etc and turn it into a repository:
cd /etc git init
Done. It’s now a repo. Then, submit everything in there.
git add . git commit -am "Initial commit. Before today it's prehistory, after today it's fun!"
From now on every time you modify a file you can do
git add <filename> git commit -m "Description of the change to <filename>"
where <filename> is one or many files. Or if you are lazy you can also do:
git commit -am "Description to all the changes."
which will commit all pending changes. To see your pending changes do:
git status
and
git diffWhen there are new files, you add them all with
git add .
and if some files were remove, you have to run
git add -u .to be sure that they are remove in the repo as well (otherwise they’ll stay as pending changes forever).
That’s essentially all the commands I use when using git and doing sysadmin. If you ever have to do a rollback, merge, branch, etc, you’ll need to learn git for real.
One last thing. I often forget to commit my changes in /etc, so I created this script:
#!/usr/bin/env sh cd /etc git status | grep -v "On branch master" | grep -v "nothing to commit" true # Don't return 1, which is what git status does when there's nothing to do.
on /etc/cron.daily/git-status which reminds me, daily, of my pending changes.
Hope it helps!
Ensuring the displaying of flash messages in Ruby on Rails
Ruby on Rails has a special object called flash which hold its contents for one more request. It’s particularly useful to show messages after a redirect. Since it’s good style to redirect after each succesful form post, that’s where you put the messages such as: “You’ve logged in”, “Thank you for your feedback”, “The book has been added”, etc.
This flash object looks like a hash table. I normally use two items in there: notice, for good stuff and errors, for bad stuff. I want these messages to be displayed in all pages, so whenever something bad or good happens, I just drop stuff in the notice or error and forget about it. This is trivial to do, I just put this:
<% if flash[:error] -%> <p class='error'><%=h flash[:error] %></p> <% end -%> <% if flash[:notice] -%> <p class='notice'><%=h flash[:notice] %></p> <% end -%>
on the application layout.
The problem with doing that is that it doesn’t look nice. I expect error messages and success messages to be near the forms or UI elements I’m interacting with. That means that every view or page will put it in a different location. No problem, you just add that same snippet where you want the messages to appear.
Then there’s a second problem: you get messages twice. If you remove them from the application layout, then you have to remember to put in absolutely every view, even those that you don’t expect to never show an error or notice. I don’t trust myself to always remember to do anything, so what happens is that I can’t just drop something in the flash and expected it to be show, I have to check every time.
I’m doing this in for a pet projects with less than 10 views, but I think big. I think of the project having 100 views and three coders than don’t know all the implicit rules, like adding the display message snippet to every view they create.
I’ve came up with this solution. I created a partial view with this content:
<% if not @messages_rendered -%> <% if flash[:error] -%> <p class='error'><%=h flash[:error] %></p> <% end -%> <% if flash[:notice] -%> <p class='notice'><%=h flash[:notice] %></p> <% end -%> <% end -%> <% @messages_rendered = true -%>
That partial view is rendered from the views and also from the application layout, but it displays the messages only once. Thankfully Rails renders the partials inside the views first, so that the messages gets displayed according to the view, and if the view didn’t display them, the application layout will.
Reviewers in a WordPress blog
In an effort to increase the quality of this blog I’ve engaged a couple of friends in reviewing my posts before they go out. I’m after typos, grammar and also “Are you serious? are you going to publish that crap?” or “You are going to get into trouble with that”.
The best way to do this, in my and in the opinion of many, is to let the reviewer modify the post freely and then check the diff. I suppose we are too used to work with source code, where diffs are a necessity.
I was positively surprised that WordPress can provide very nice colored diffs between each save of each post. Praise to WordPress! I was also surprised, but negatively, that WordPress doesn’t have a reviewer role. Users with the reviewer role would be able to edit unpublished posts but wouldn’t be able to modify published posts or publish drafts.
I was pointed to Role Scope, an amazing plug in for WordPress. I spent an hour creating a group and trying to give it the proper access, and as that failed, trying to limit the Editor role to only edit drafts. I failed at that too.
When I gave up and I went back to the usual blogging I found that Role Scoper allows you to give edit access to each post to any user. And that’s it. That’s what I’m using. Whenever I want something reviewed I give access only to that post to the user I want to review it. Quite simple.
Reviewed by Daniel Magliola. Thank you!
File input for forms in ASP.NET MVC
I’m not sure why ASP.NET MVC was shipped without a file input type for forms. Maybe it’ll come in MVC 2.0 or 3.0. Meanwhile, I created one. I spent two or three hours trying to figure out how to go from Object to IDictionary<String, Object> to follow the same ASP.NET MVC style where you have methods like:
TextBox(HtmlHelper, String, Object, IDictionary); TextBox(HtmlHelper, String, Object, Object);
which are essentially the same. The last argument is a dictionary of extra HTML attributes, like style=”float: left;”. The good thing about accepting Object Is that you can call it this way:
Html.TextBox("email", new { style="float: left;" })
which is very handy for forms. The bad thing is that it is a pain in the ass to do that hocus pocus in C# using reflection. Thankfully ASP.NET MVC is open source. I downloaded the source and after 15 minutes I got it working nicely (and without manually using reflection). Use the source Luke!
In a recent episode of Hansel Minutes podcast someone argued what was the value of releasing the code of ASP.NET MVC at all. Well, this is the value. You help developers, you build a better developing community.
Without further ado, here’s the code:
public static class HtmlHelperExtensions { /// <summary> /// Returns a file input element by using the specified HTML helper and the name of the form field. /// </summary> /// <param name="htmlHelper">The HTML helper instance that this method extends.</param> /// <param name="name">The name of the form field and the <see cref="member">System.Web.Mvc.ViewDataDictionary</see> key that is used to look up the validation errors.</param> /// <returns>An input element that has its type attribute set to "file".</returns> public static string FileBox(this HtmlHelper htmlHelper, string name) { return htmlHelper.FileBox(name, (object)null); } /// <summary> /// Returns a file input element by using the specified HTML helper, the name of the form field, and the HTML attributes. /// </summary> /// <param name="htmlHelper">The HTML helper instance that this method extends.</param> /// <param name="name">The name of the form field and the <see cref="member">System.Web.Mvc.ViewDataDictionary</see> key that is used to look up the validation errors.</param> /// <param name="htmlAttributes">An object that contains the HTML attributes for the element. The attributes are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.</param> /// <returns>An input element that has its type attribute set to "file".</returns> public static string FileBox(this HtmlHelper htmlHelper, string name, object htmlAttributes) { return htmlHelper.FileBox(name, new RouteValueDictionary(htmlAttributes)); } /// <summary> /// Returns a file input element by using the specified HTML helper, the name of the form field, and the HTML attributes. /// </summary> /// <param name="htmlHelper">The HTML helper instance that this method extends.</param> /// <param name="name">The name of the form field and the <see cref="member">System.Web.Mvc.ViewDataDictionary</see> key that is used to look up the validation errors.</param> /// <param name="htmlAttributes">An object that contains the HTML attributes for the element. The attributes are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.</param> /// <returns>An input element that has its type attribute set to "file".</returns> public static string FileBox(this HtmlHelper htmlHelper, string name, IDictionary<String, Object> htmlAttributes) { var tagBuilder = new TagBuilder("input"); tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.MergeAttribute("type", "file", true); tagBuilder.MergeAttribute("name", name, true); tagBuilder.GenerateId(name); ModelState modelState; if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState)) { if (modelState.Errors.Count > 0) { tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName); } } return tagBuilder.ToString(TagRenderMode.SelfClosing); } }
Reviewed by Daniel Magliola. Thank you!
SSL with multiple virtual hosts and only one IP

I was writing this long post about how to use rewrite rules to make Apache query itself and serve various sites through the same SSLed virtual host using only one IP. After about four hours of struggling with it I thought I was done but then while writing this article I found out I was wrong. Something was not working as expected. Did I face another 4 hours of Apache struggling? Read the rest of this entry »
Converting the ASP.NET MVC project into OpenID
When you create an ASP.NET MVC project it comes with a controller called AccountController that manages logging in, logging out, registering, changing password and so on. Since usernames and passwords are dead I converted it into OpenID and I’m just pasting it here for everybody to use.
I’m using the DotNetOpenAuth library which you have to download, put in your project and refer. The difference between what I’m pasting and the example provided by DotNetOpenAuth is that I’m actually storing the user in the membership database, like the original AccountController.
My work is based on the on the blog post Adding OpenID to your web site in conjunction with ASP.NET Membership. I really had to put a couple of hours on top of that, so I consider it worth it to post it. Scott Hanselman also provides useful information for integrating OpenID. I’m using the jQuery OpenID plug-in but I’m not going to post my views. They are really trivial and left as an exercise to the reader.
I’m not using any extra tables, I’m storing the OpenID identifier (the URI) in the field for the username. This has the advantage of not requiring any other fields but the disadvantage that you can have only one identifier per user. There are some unfinished parts but since you are likely to customize them anyway, I don’t feel too guilty about not finishing yet. If you find a bug, please, let me know.
Full body on feeds (RSS) in Plone 3
A friend of mine complained that after switching to my new site (from Wordpress to Plone 3) my feeds stopped containing the full article and only the body. So this is how I fix it.
Searching for how to fix it on the Internet I’ve found several articles (and I’m adding one more!):
- PLONE 3 RSS: this solution fails in that it outputs the text as description and doesn’t output the description at all.In my case, the description is the start of the article, so that didn’t work. Now I’m not 100% sure that’s the case, but I think so (sorry, I won’t break my site again to check it out). This article points to another three.
- Full RSS-feeds for Plone: this one adds the content in a separate tag after the description. This one is better, but pay close attention to the comment, otherwise it’ll fail.One problem I’ve had with this one is that readers show either the description or the content; they interpret description to be meta-data and it’s not shown when the content is shown. That’s probably correct, but in Plone, the description is like an introduction to the article, being shown just below the title and above the main body text, and that’s what I needed in my feeds.
- Add the body text to your rss feed: outdated, I ignored it. It might have worked but it would have required a lot of tweaking.
- Add the body text to your rss feed (2): it’s also marked as outdated, but it is the same as “Full RSS-feeds for Plone”, just more graphical and integrating the important.
So, my solution is essentially 2 or 4, with the addendum of the description to the content:
<content :encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"> <span tal:replace="structure python: '\074![CDATA[\074p\076' + obj_item.Description() + '\074/p\076' + obj_item.getText() + ']]\076'">content</span> </content>
If you are wondering, “\074″ is “<” and “\076″ is “>”.

Recent Comments