Showing posts with label Deployment. Show all posts
Showing posts with label Deployment. Show all posts

Sunday, April 6, 2008

Git the Version Control System - part one

Git is a Version Control System, it is used to keep a history of all the changes you made into your code, manage releases, makes it easier for more than one programmer to share his/her code with the rest of the group.

There are tens of good VCS out there, the most famous was CVS Concurrent Version System, then came Subversion from the same team of CVS with the same concepts but cleaner than its ancestor. Both CVS and Subversion was free. There are other sophisticated VCS out there some of them are free and the other are proprietary. Linux was developed using a free license version of Mercurial which is pretty good CVS but for more some reason Linus Torvalds was challenged enough to start writing his own VCS, and Git was born.

There are two types of VCS, Central and Distributed. In central VCS like CVS and Subversion, there is always one central repository, you checkout a copy of whatever version you like from the repository, start working on it and then submit your changes into the central repository again.

The Distributed type like Git, there is one or more central repository, and you clone the repository into your local machine, and then you will have a standalone repository. Which is more like forking the development of the code at certain point in its development line into another repository. You then maintain your repository and change to your heart content, and it is up to you or the original (upstream) repository maintainer to merge your changes.

I will write a note on the basic operation of managing my code in GIT. For the sake of simplicity I will not write in this post about how to serve or access remote GIT repository using SSH, I will just assume that all repositories are on local machine.
$ mkdir myapp
$ cd myapp
$ git init
Initialized empty Git repository in .git/
I created a folder for my application, and inside the folder I make Git initialize a new repository.

At this point we should have a folder named .git inside the 'myapp' and that .git folder by itself is a repository, in this folder GIT will store all the transaction, changes, version, releases of your code. The beauty of GIT is that it does not pollute your code folder with other files like Subversion does. Subversion was creating an .svn folder inside each folder in my app folder.

As I said .git is the repository and the rest of your folder is now then called the working copy. the working copy is an important concept. It is your current version files, in Git if you switch to another version of your code, all the files in the folder will be changed to reflect the real version you switched to. We can create a repository without a working copy and that's what we do in case we need to make a central repository where programmer are pulling and pushing changes into it, in that case we do not need a working copy with the repository, this is called a 'bare' repository.

The .git repository folder is now empty, it has no files (or changes) in it. To start populating the repository we use the 'add' Git command. but we actually do not have any files yet, so let's make some files.
$ echo "hello" > dialog.txt
I created a file 'dialog.txt'. Now i will ask Git about the status, and it will answer me with the following information, which match files in three cases:
  • The modified files in my working copy that needs to be committed.
  • The modified files that are marked for committing.
  • The files that are not yet tracked by Git, and they need to be added first to the repository.
Now that we only have one file in the third case.
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# dialog.txt
Git suggest to use the add command to start tracking the file. What tracking means? Ok, in a working copy there is either tracked files that you want to keep track of, and untracked files which are either newly created and needs to be added to the repository or non source code files like log files, object and comoiled files and in this case we need to tell Git to ignore them and not notifying me about them when we use the 'status' command.

To be more specific the 'add' command does not add the files to the repository here or even convert the untracked files into tracked ones, the 'add' command is actually preparing the files to be committed or added to the repository in the next time you use the commit command. Actually 'add' is adding the files, tracked or not, to an area which is called the 'index', and after you add all the files you need and you feel satisfied that your all the changes you need to commit is in the stage, then you can use the commit command to commit your changes into the repository.
$ git add dialog.txt
Now that we added the dialog.txt it is now in the 'index' and will be commited by the next commit command.
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: dialog.txt
#
Well, it is time of our first commit. Git is storing the name and email of the committer with each commit, so we need to identify ourselves first to Git.
git config --global user.email "ibrahim@mymail.com"
git config --global user.name "Ibrahim Ahmed"
$ git commit -m "saying hello"
I committed the dialog.txt and notice the '-m', commit always needs a log message, try to write a message describing the changes you've done to the file.
$ echo "what's your name" >> dialog.txt
$ git status
I added one more line to dialog.txt, and git status to see the changes, and Git should giving me that dialog.txt is modified and will be committed in the next comit.

One more time, and excuse my boring repetition, you will need to add first the dialog.txt to the index area, before it is ready to be commit.
$ git add dialog.txt
$ git commit -m "asking about your name"
Ignoring files
Git status will always nag about untracked files, so we should either 'add' it or ignore it.

There are always some files in your app that you do not need to track, files like logs for example.
$ touch myapp.log
$ git status
# On branch master
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# myapp.log
$ echo "*.log" >> .git/info/exclude
$ git status
# On branch master
nothing to commit (working directory clean)
Notice how git status listed myapp.log in untracked files, and when we add a line containing '*.log' to '.git/info/exclude', git status ignored the myapp.log file and gives the message 'working directory clean'

Remove files
'git rm' removes files from the working directory and from the repository. But it also used to remove files from the 'index' using the following;
$ touch delete.me
$ git add delete.me
$ git commit -m "adding delete.me"
$ git rm delete.me
$ echo "how old are you?" >> dialog.txt
$ git add dialog.txt
$ git rm --cached dialog.txt

That's it for the first part of my note about Git, on the second note I will write about diff, clone, pull, adding remote repository, merge, and conflict resolution.

Thursday, July 5, 2007

Subversion, SSH and Capistrano for a fully automated deployment

Deployment could be defined as; uploading an application to the hosting server and make any required modification or configuration to the file system, web server, database server, or whatever for the application to run properly.

For deployment, I use Capistrano, Subversion and Putty. Notice that I did not mention an FTP client. My minimal typical hosting environment consists of at least one instance of production web server and another instance as a staging web server, and each of them has its own file structure spanning to more than 3 levels and all the web servers are behind a reverse proxy server as the front end for web request, and I may run clusters of these instances. It could take me more than 10 minutes to do but the simplest tasks of deployment, like updating 3 files in 2 different folders, for example. As simple as it seems, but that kind of task is troublesome, especially that after uploading those files in their respective paths, and preview the updates on the staging server, and iterating a little bit in a preview, comments, fix, deploy loop, and finally redeploy in the production server, and restart the cluster instances. Well, at best, if half of your brain is actually a computer machine and you didn't make any silly overwrite or other nasty stuff to your application, your other brain half will get bored of these brain dead repetitive tasks, or you will simply screw yourself bad when you break your production hosting environment or worse destroy your production database.

We have to admit it; a programmer should always train himself to automate everything repetitive and boring he does, in order to spare his brain power on otherwise useful stuff that needs his attention like application architecture design and algorithms. Or simply to spare the world the kind of destruction our minds able to do when faced with boring repetitive tasks. This is a very popular concept which is called DRY, abbreviation of Don't Repeat Yourself.

Subversion

I believe that the centerpiece of any successful and decently automated deployment lies in the Version Control tool you use. Coupled with a decent SSH client and you're up to the deployment task. I use Subversion. It really makes my life easier. Imagine the following scenario;
• Write your application
• Import it to your online code repository.
• Continuously commit any changes you do in your application to your code repository.
• Need to deploy a development server; just check out a version of your application from the repository
• Changed anything in your application; just update your development server with any recent changes, and it will be in sync with your latest application code.
• Satisfied with your current development version, make a release tag in your code repository, and check out this version to a new production server.
• Need fixing, change the code, commit it, update your hosting server.
• Did a bad decision and decided to go back to a previous version; just update your application to the required version.

It is even simpler than it sounds. All these steps are done using simple commands like;
On local system:
$ mkdir myapp
$ cd myapp
$ svn import http://svn.myrepository.com/myapp
$ echo "puts 'hello'" > hello.rb
$ svn add hello.rb
$ svn commit hello.rb -m "Added Hello module that says hello"
On hosting server using secure shell:
$ svn checkout hello
$ svn update
$ svn update –r $required_revision_number

The beauty of this process of check in, check out, commit and update, is that you do not have to keep track which files are changed and need to be uploaded, or worse upload all your application folder each time you make an update.

Useful link: A Novices Tutorial on Subversion


Capistrano
Capistrano is the deployment tool i use for deploying my Rails applications on the hosting server. As quoted from Capistrano manual; Application deployment is one of those things that becomes more and more complicated as the scale of your application increases. With just a single box running your database and your application, it’s quite simple. But when you start putting your database on a different server, and then separating your web servers from your app servers, and eventually splitting your database into master and slave servers… It can get to where you almost don’t want to deploy your application any more.

Simply put, Capistrano makes it even easier to do tasks like fire up your ssh client, type some commands on the remote server to move, update, restart, or whatever task you would normally do by hand, you can write your commands in a script called "recipe" for Capistrano and as the next decent software, it will submit to your command and do every task you tell it to do.

Tasks like; login to remote server, update your application code, migrate your database, restart your server , are very likely will be repeated over and over, and that should notify you to start a new recipe for Capistrano to cook at your command.

Useful link: Capistrano: Automating Application Deployment

Putty
I like Putty, it is a secure shell client. Nothing bad with openssh client, but when i use Windows, it's pathetic command prompt console leave no choice but to use Putty with its own terminal window. By the way, I wonder why for God sake, there is no any alternatives for the bloody windows command prompt console, is there any out there? And I mean terminal or console not just another shell, cause i found many (Cygwin, bash, sh). [update: I found a great console for Windows, it is called Console. It is a great tool, it even support transparent background! when used with unix tools like sh, tar, cp, rm...etc and all you unix geeks will find yourself almost at home.]

Among the servers of the hosting data center we have at work, there is only one server that open a Secure Shell port to the internet. For a hosting data center, this is not a good idea. But we have to have a remote shell to login to, in case of emergencies or remote administration. My system admin collegue at work, had generated a pair key for my account on the hosting server. And that was a good idea. Because, it requires a private key for your account on the linux box for you to be able to login. No username/password login prompt anymore. This technique is both secure and easier than the regular login procedure.

Using key authorization is very useful if you intend to use any kind of automated deployment tools. Because such tools will almost always use key authenticated secure shell for login to the deployment server. And scattering your username/password all over your deployment scripts is not something you should do if you care about your hosting server security.


bundlephobia

JavaScript bloat is a more real problem today than it ever was. Bundlephobia lets you understand the performance cost of adding a npm pack...