joseherran.info

Run pod install after every checkout

01/03/2015 08:46 | Comments

Recently the git policy changes at work, and from that point, Pods directory was globally ignored. Because of that, every time we checkout a project, we have to do pod install in the terminal. Too time comsuming.

Thanks to Tower Support guys, I knew about Git Hooks, that is a way to fire off custom scripts when certain important actions occur. From this point all become simple. I created a post-checkout script to install required cocoa pods after every checkout. To do it, you have to create a file on you project .git/hooks directory, name it post-checkout and have execution permissions.

This is the script for doing it:

1
2
3
4
5
6
#!/bin/bash
if [ -f Podfile ] && command -v pod install >/dev/null
then
        (unset GIT_DIR; exec pod install)
        true
fi

There are a couple of things to note when using hooks with Tower, so, I tweak a little my previous script to return the error (just in case):

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
if [ -f Podfile ] && command -v pod install >/dev/null
then
        OUT=$(unset GIT_DIR; exec pod install)
        if echo "$OUT" | grep -q "Integrating client project"
        then
                echo "$OUT"
                true
        else
                echo "$OUT" >&2
        fi
fi

Alfred Workflow: Hotkey to open SSH on iTerm

01/12/2014 14:07 | Comments

I created this Alfred Workflow in order to quickly open ssh on iTerm when I need it.

Let see how I did it.

Firt, you need to create a file with the command you want to run, and set the right permissions.

1
2
echo "ssh host -l username -p password" > /Users/username/Documents/Connections/ssh.command
chmod 755 /Users/username/Documents/Connections/ssh.command

On Alfred, go to Preferences → Workflows, click on + and select Blank Workflow. Fill the data and click Create.

Use the + symbol in the upper right of the window and select Triggers → Hotkey. You will have a window like the image below. For the example I used s, but feel free to change to suits your needs.

Use the + symbol again and select Actions → Run Script. Be carefull and select /usr/bin/osascript as Language.

This is the code in the image (for copy-paste purposes):

1
2
3
4
tell application "iTerm"
  open "/Users/username/Documents/Connections/ssh.command"
  activate
end tell

The new workflow must look like the following image:

You can download the ready to use workflow.

Tell Messages I’m Away

02/11/2014 00:02 | Comments

First of all, you need homebrew installed on your system. If you haven’t, visit http://brew.sh for instructions, or let me know and I will try to guide you.

Then you need to install arp-scan. To do it, open a Terminal and type brew install arp-scan.

Next step. Save the following script, I called check-iphone-available.scpt, but your can rename if you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
set IPHONE to do shell script "if /usr/local/bin/arp-scan -l | grep your-iphone-mac; then echo 1; else echo 0; fi" user name "your-username" password "your-password" with administrator privileges
tell application "System Events"
        tell process "Messages"
          tell menu bar 1
              tell menu bar item "Messages"
                  tell menu "Messages"
                      tell menu item "My Status"
                          tell menu "My Status"
                              if IPHONE is not equal to "0" then
                                  click menu item "Available"
                              else
                                  click menu item "Away"
                              end if
                          end tell
                      end tell
                  end tell
               end tell
          end tell
        end tell
end tell

Replace your-username, your-password and your-iphone-mac.

  • your-username must be an administrator user that can sudo on your computer.
  • your-password password for that user.
  • your-iphone-mac can be obtained on your iphone, go to Settings -> General -> About and copy Wi-Fi Address.

The script execute as administrator the command arp-scan. This command sends ARP packets to hosts on the local network and displays any responses that are received. The grep command look for your iphone on the answer receive by arp-scan. If the iphone is found, then return 1, otherwise, return 0. On 1, the script do click on Available menu item in Messages, on 0, the same on Away (can be changed by Offline, On the phone, etc).

So, let’s do it automatically.

Go to folder /Users/your-username/Library/LaunchAgents and save there the following plist file. I named it com.username.checkiphone.plist, but again, feel free to change it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.your-username.checkiphone</string>

  <key>ProgramArguments</key>
  <array>
    <string>/usr/bin/osascript</string>
    <string>/Users/your-username/bin/check-iphone-available.scpt</string>
  </array>

  <key>Nice</key>
  <integer>1</integer>

  <key>StartInterval</key>
  <integer>60</integer>

  <key>RunAtLoad</key>
  <true/>

  <key>StandardErrorPath</key>
  <string>/tmp/com.your-username.checkiphone-available.err</string>

  <key>StandardOutPath</key>
  <string>/tmp/com.your-username.checkiphone-available.out</string>
</dict>
</plist>

The file is pretty self-explanatory. We will launch the command /usr/bin/osascript /Users/your-username/bin/check-iphone-available.scpt every 60 seconds, will be launched at load, will save errors on /tmp/com.username.checkiphone-available.err and logs on /tmp/com.username.checkiphone-available.out.

Again, replace your-username appropriately.

Last step, tell the Mac launchd daemon to load it.

1
launchctl load com.your-username.checkiphone.plist

To stop the script, just replace the word load with unload in the above sentence. When your restart your computer the script will be load again. To prevent it, move it to another folder.

Originally posted in AskDifferent as answer to this question.

Install SSL Certificates on AWS Cloudfront

15/10/2014 16:35 | Comments

Installing an SSL Certificate on AWS Cloudfront can be done easily in just one line of code, but unfortunatelly rarely happens. Let’s see why and how to deal with it.

First step, you need AWS command line tools installed and configured, is the only way. Have a look here if you don’t know who to install it on your system. I installed it on my mac via homebrew.

1
brew install awscli

Configure AWS CLI by entering your credentials. To do it, you need your aws_access_key_id and aws_secret_access_key.

1
aws configure

In order to purchase a SSL certificate your need to generate a server key and csr file.

1
openssl req -new -newkey rsa:2048 -nodes -keyout <your-domain>.key -out <your-domain>.csr

I purchased the certificate on https://ssls.com, it’s the cheaper site I found. You can get one from $8.95 if you pay for a year to $4.99/year if you pay for five years. In the buying process you must paste your previously generated <your-domain>.crs. I don’t want to enter in the certificate purchase, so, move on. When the certificate is ready, you will receive an email with a file containing the following:

1
2
3
4
AddTrustExternalCARoot.crt
COMODORSAAddTrustCA.crt
COMODORSADomainValidationSecureServerCA.crt
<your-domain>.crt

AWS require a RSA version of the server key file.

1
openssl rsa -in <your-domain>.key -out <your-domain>.key.rsa

And last step before the upload. The certificate chain, this file contains all the intermediate certificates and the root certificate of the CA. The files order matters.

1
2
3
cat COMODORSADomainValidationSecureServerCA.crt \
    COMODORSAAddTrustCA.crt \
    AddTrustExternalCARoot.crt > certificate-chain.crt

Almost ready, let’s upload the certificate.

1
2
3
4
5
6
aws iam upload-server-certificate \
        --server-certificate-name <your-domain>.com \
        --certificate-body file://<your-domain>.crt  \
        --private-key file://<your-domain>.key.rsa \
        --certificate-chain file://certificate-chain.crt \
        --path /cloudfront/<your-domain>.com/

When you see file:// is because you are in the same directory as your certificate files. If not the case, use file:///path/to/your/files. Path parameter must end with /, otherwise you will get an error.

Final step, edit your Cloudfront distribution, and select your certificate under Custom SSL Certificate (stored in AWS IAM).

Update a Github Fork from the Original Repo

15/10/2014 12:28 | Comments

When you fork a repo, you usually want to keep it updated with newer changes. Unfortunatelly github hasn’t got an option to allow this on a single click. So, let’s see how to do it.

First, create local repository, add your own remote and retrieve it.

1
2
3
git init
git remote add origin git@github.com:<username>/<repo>.git
git pull origin master

Next step, add a remote branch to your repository that points to the original repo you forked from.

1
git remote add --track master <repo-name> git://github.com/<username>/<source-repo>.git

Note that master will be the branch you want to track in the forked repo. <repo-name> is up to you. If you want to verify, simply run git remote and you must see <repo-name> and origin as output.

Now, you are ready to get all new changes from original repo.

1
git fetch <repo-name>

A new branch called <repo-name>/master will be created with latest changes.