WATCH AND LEARN

I learn by doing and seeing

EC2 Adventurous

[ ] [ Tags geek, git, tools ]

It’s already July! Omg time flies when it comes to summer time.

Update: recently I’ve been very involved in my internship project. I can’t expose the detail but basically I am working with AngularJS extensively. Though I am still very far from an experienced AngularJS developer, I must say IT IS A BEAUTIFUL WEB FRONT-END FRAMEWORK.

Currently I am working on a project called “yolyo” (temporarily name), which is based on an open source forum Discourse. Since Discourse requires quite a bit of computing resources, I decided to use Amazon EC2 as our starting point. As I realized how important it is to configure a virtual instance, I would like to dedicate this post to document all the works I’ve put into to make things work smoothly.

First of all, I would like to thank BitNami providing one-click Discourse installation to deploy on EC2. Otherwise, I would’ve still struggled with all the nitty gritty deployment detail.

How to deploy/update by Git

This has confused me for a long while. I had been used to deploy virtually everything by drag and drop for so long. Though I’ve learnt how to use Git to do versioning, I do not know how to setup the cloud server so that I can simply do git push origin master to deploy the changes.

Although I finally figured out how to use Git to deploy remote instance, I realized this workflow doesn’t work really well with Discourse. Discourse itself already contains a git repo, so I would need to push to a remote repo and deploy the instance by pulling from this remote repo. (If anyone knows a better deployment strategy, please let me know)

On remote machine

create a git repo as a transition to deploy to a designated directory

  • Copy local machine public key to the remote server (ec2 instance)
1
$ cat ~/.ssh/id_rsa.pub | ssh -i ~/.ssh/amazonKey.pem ubuntu@example.com "cat >> ~/.ssh/authorized_keys"
  • Create and initialize a new git repo
1
2
$ mkdir website.git && cd website.git
$ git init --bare
  • Configure the post-receive hook in order to put the files into desired directory
1
2
3
4
5
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/home/ubuntu/website
export GIT_WORK_TREE
git checkout -f
  • Make the post-receive file executable
1
$ chmod +x hooks/post-receive
  • Configure hooks/post-update file
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/sh
#
# This hook does two things:
#
#  1. update the "info" files that allow the list of references to be
#     queries over dumb transports such as http
#
#  2. if this repository looks like it is a non-bare repository, and
#     the checked-out branch is pushed to, then update the working copy.
#     This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update".

git update-server-info

is_bare=$(git config --get --bool core.bare)

if [ -z "$is_bare" ]
then
        # for compatibility's sake, guess
        git_dir_full=$(cd $GIT_DIR; pwd)
        case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi

update_wc() {
        ref=$1
        echo "Push to checked out branch $ref" >&2
        if [ ! -f $GIT_DIR/logs/HEAD ]
        then
                echo "E:push to non-bare repository requires a HEAD reflog" >&2
                exit 1
        fi
        if (cd $GIT_WORK_TREE; git diff-files -q --exit-code >/dev/null)
        then
                wc_dirty=0
        else
                echo "W:unstaged changes found in working copy" >&2
                wc_dirty=1
                desc="working copy"
        fi
        if git diff-index --cached HEAD@{1} >/dev/null
        then
                index_dirty=0
        else
                echo "W:uncommitted, staged changes found" >&2
                index_dirty=1
                if [ -n "$desc" ]
                then
                        desc="$desc and index"
                else
                        desc="index"
                fi
        fi
        if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
        then
  • Make hooks/post-update executible
1
$ chmod +x post-update

On local machine

I’m lazy to explicitly write out the steps here. I will just refer to the StackOverflow page that helped me solve this problem.

Side note

After all the above hassle I suddenly realized that I do not need any of these setup. Because I shall rely on a remote (e.q. GitHub) to host the repo, so the remote machine can easily pull from this remote repo. Though it is true that I am not currently working collaboratively with anybody else, this is a good deployment/development practice.

Reference