Skip to content

More git prompt - Does it work on a Mac?

So a few years ago I wrote this article about setting a custom shell prompt that is "Git aware" and shows you your current branch.

The question came up as to whether or not this works on a Mac under OS/X.

I have always advocated avoiding things like WAMP or MAMP because I don't like a bunch of services running on my workstation. I prefer using virtualization to run a *nix distro matching whatever target deployment server I'm going to run under. VMWare, Virtualbox etc. along with the popularity of Vagrant and Docker have tremendous advantages over something like MAMP in my experience. You start the environment when you need it, and stop it when you don't, and there's no problem having 5 different VM's with different stacks and php versions.

For this reason, I have never been all that concerned with setting a git aware shell prompt up on my macbook. But as it's a *nix-like operating system, it has the basics you need to make the shell prompt code work, albeit with 2 required tweaks.

First you have to edit the /etc/profile script so that it will look for and read scripts in an /etc/profile.d directory. sudo vi, nano or whatever you want to edit the /etc/profile script and add this at the bottom:


for sh in /etc/profile.d/*.sh ; do
        [ -r "$sh" ] && . "$sh"
done
unset sh
 


This is simple bourne shell code to read in scripts in the /etc/profile.d directory when you login to a shell. It is a system-wide script, so when you change this, you change it for all users on the system.

Now you just have to create the /etc/profile.d directory.


sudo mkdir /etc/profile.d
 


Once this is done, you can use the same simple method described in the original article.



Defined tags for this entry: , ,

How I found symbolic links for gitignore with find and cut

I was cleaning up a directory of old files for addition to git, and I came across a bunch of symlinks to a completely different codebase. In this particular case it was a wordpress blog that had a series of its files linked into the webroot of the site, that existed in an entirely different directory. I didn't want these files in the git repo.

Finding all the symlink files is easy enough.


cd /path/to/root
 


The find command gives us an easy way to get all symlink files:


find . -type l
 


But that's just a start -- we have to get these into the .gitignore file. Continue reading "How I found symbolic links for gitignore with find and cut"

Showing your git branch in your shell prompt

At some point I found some code to get the git branch name and insert it into the shell prompt. As it was some years ago, sadly I've forgotten the original source, but it wasn't exactly the type of prompt I'm used to, so I tweaked it until I got it the way I prefer. In the first set of brackets, I display the user@host:directory.

Once I cd into a git initialized directory a colored prompt appears with the branch name displayed in gold. For my purposes, this is simple, functional and doesn't result in overly long prompts. Here's what you can expect from any terminal that supports ansi color codes.



To implement the prompt on your server or workstation, simply cut this snippet and paste it into the end of the /etc/bashrc file.

The current way to do this is to create a new file named "gitprompt.sh" in the /etc/profile.d directory, and paste the code below into it. Using vim is a good way to accomplish this task. You need to be root or sudo for this to work of course.

Once the file exists, all the system users will benefit from the prompt in their shell sessions.

Using this prompt, whenever you're in your bash shell, there will be no question of whether or not you are in a git initialized directory, or what your currently checked out branch name is.


# Custom aliases and functions
function parse_git_branch_and_add_brackets {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \[\1\]/'
}
PS1="[\u@\h:\W]\[\033[0;33m\]\$(parse_git_branch_and_add_brackets)\[\033[0m\]\$ "
 


Does this work on a Mac? Yes of course!




Defined tags for this entry: , , ,

system-switch-mail is gone, so use alternatives

When I setup a new Centos or Amazon Linux server, one of the first tasks I want to accomplish is installing postfix. There used to be a package named system-switch-mail that made this easy to do from the shell. If you've tried to yum install this package recently you might have been surprised to find that it has disappeared. Instead there is a tool that lets you do essentailly the same thing which is part of the chkconfig package.


For this to work you need to have the chkconfig package installed:

CODE:
[root@ ~]# rpm -qa | grep chkconfig
chkconfig-1.3.49.3-2.10.amzn1.x86_64


If you don't see it, then yum install chkconfig!

With chkconfig installed you have access to the alternatives system. Of course to be able to switch your mta you first have to install your sendmail alternative. In my case it's typically:


yum install postfix
 



Now you can switch your system to use postfix as the default MTA!


alternatives --config mta
 


Choose Postfix and hit enter and you're done. Don't forget to remove sendmail, as you no longer need it.


yum remove sendmail
 


Defined tags for this entry:

AWS EC2 Amazon Linux or Where is my ephemeral storage?

I like Amazon Linux. It is basically Centos with a bit of Fedora mixed in. Thus it comes with yum, and even better yet, with amazon supported repositories preconfigured. Because amazon provides it, they support it, and answer questions about it, and fix problems with it and keep it up to date and patched so that it runs well as a paravirtualized guest inside Amazon's Xen based infrastructure. They insure that it's fairly secure, has a fairly minimal set of installed packages, and comes with the ec2 api tools already installed, which can be a daunting task to setup for people new to aws and ec2.

However one thing they don't do by default is configure their AMI's to make the ephemeral storage you are entitled to available by default. When you boot up an instance, you find yourself with a 10 GB EBS volume, and that can become pretty filled once you've installed a few packages.

What is Ephemeral storage?
Ephemeral storage is the "instance storage" that's advertised for each instance type and can range from 150 GB on up to over a terabyte depending on the instance type you're running. This storage is called "Ephemeral" by Amazon because it comes from the local hard drives in the server your instance is running on, but does not persist or survive an instance stop. If you put anything important on it, you'll need to back that data up using traditional means, and won't have the ability to snapshot it like you can with EBS volumes. Although ephemeral drives can provide excellent IO performance and a substantial storage at no extra cost, they are subject to the types of failures that you'd expect with single server disks.

In spite of these concerns, they certainly are a great place for things like temporary files, or swap files, or logs that you're going to rotate or purge anyways.

So how can you use Amazon Linux and still get access to the ephemeral disks?

You need to utilize the Amazon API and use the "-b" parameter to map one or more block devices to your ephemeral storage. Although the web interface "Launch Instance" wizard includes a tab for storage options, the instance storage tab is not accessible. I typically use the command line api to start aws instances.

For example, this command will launch a large instance based on an Amazon Linux AMI, only, while at the same time adding back mappings to the ephemeral storage that is part of the Large instance profile.


ec2-run-instances --region us-east-1 ami-aecd60c7 -k yourkey -t "m1.large" -z us-east-1d -g your-security-group -b "/dev/sdb=ephemeral0" -b "/dev/sdc=ephemeral1"
 


The important parameters to note, are the two "-b" parameters, which specify that ec2 should establish block device mappings to our two ephemeral drives.

When your instance starts, you'll find that you now have a formated ext3 volume ready for use:

/dev/xvdb 414G 199M 393G 1% /media/ephemeral0


You might have noticed that we specified a mapping for 2 different block devices. You'll find that /dev/sdc is configured, but you'll have to format it and mount it yourself.

If this is the first time you've seen the command line api used, amazon linux is a great way to get started. Just start up a micro instance using the recommended Amazon Linux AMI, add your certs, and you're ready to go.

In summary, Amazon Linux is an excellent choice for your EBS backed instances -- just remember that unless you override the default, you'll be missing out on the Instance storage you're paying for!

Centos, Amazon Linux, NFS and the dreaded "nobody" problem

I have been fighting a problem with mounting volumes from an NFS Server in AWS for a few days. With more pressing issues at hand, I had to try and google for solutions for an hour or so before bed, and nothing I was doing was having any effect. Curiously, an Ubuntu based machine that was mounting the drive using NFS3 was not having the same problem. Only the Amazon Linux servers that were using NFS4 were having the issue, and were showing all files and directories as being owned by nobody:nobody.


drwxr-xr-x    2 nobody nobody   22 Jan  9 19:58 installervc
drwxr-xr-x    2 nobody nobody 4.0K Jan  9 19:56 avatar
drwxr-xr-x    2 nobody nobody 4.0K Jan  9 19:56 accessories
 


I had previously insured that the user UID and group GID for the user that would be writing files (in my case "apache" was the same (with the same UID and GID) on the NFS server and the servers mounting the nfs volume.

As it turned out the problem was with the configuration (or lack thereof) of the rpcidmapd service. NFS4 relies on this service to map users between machines. The "idmapd" requires that the domain of both the client and server should match for the UID/GID mapping to work, and in my case it wasn't. Probably many people with proper DNS configuration don't hit this problem, but we did not have a proper DNS setup, as these machines are part of a growing cluster. Compounding the problem I had set the configuration files to have meaningless host names rather than a domain.

You can tweak this setup by editing the: /etc/idmapd.conf file, and find the "Domain" variable:


Domain =  yourdomain.com
 


Set these to be the same for the server and all the clients.

The last problem was that I had to restart the idmapd process, which has an /etc/init.d control script named /etc/rpcidmapd


[root@web1 init.d]# ls -lath | grep rpcid
-rwxr-xr-x  1 root root 2.7K Jul 28  2011 rpcidmapd
 


Restart the process on both the nfs server and any nfs clients. If the source of your problem is the same as mine, your user & group mapping problems should be solved:


sudo /etc/inti.d/rpcidmapd restart
 


Finally!!!!


drwxr-xr-x    2 apache apache   22 Jan  9 19:58 installervc
drwxr-xr-x    2 apache apache 4.0K Jan  9 19:56 avatar
drwxr-xr-x    2 apache apache 4.0K Jan  9 19:56 accessories
 

Defined tags for this entry: , , , , , , , ,