Last active
August 29, 2015 14:23
-
-
Save ganmacs/a1c81c37fa023516d23e to your computer and use it in GitHub Desktop.
cloud create lxc an git repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# const | |
USAGE="[USAGE] | |
$ ./setup <username> <reponame> <ip> | |
[Requirement] | |
exec by root | |
[Example] | |
$ ./setup seri sandbox 167 | |
=> craete 'seri/sandbox' git repository | |
=> create linux container that name is 'sandbox' and ip is '157.82.3.167' | |
" | |
NAME=$1 | |
REPO=$2 | |
IP=$3 | |
L_NAME="$REPO$NAME" | |
LXC_USER_NAME="obana" | |
create_lxc() { | |
sudo lxc-create -n $L_NAME -t ubuntu | |
sudo cat "./interfaces" | awk '{gsub(/<%=ip%>/, '${IP}'); gsub(/<%=name%>/, "'${L_NAME}'");print $0;}' | sudo tee "/var/lib/lxc/$L_NAME/rootfs/etc/network/interfaces" | |
sudo lxc-start -n $L_NAME -d # error | |
echo "[FINISH] --------- created lxc\n" | |
} | |
setup_lxc () { | |
sleep 2 | |
lxc-attach -n "$L_NAME" -- apt-get update | |
lxc-attach -n "$L_NAME" -- apt-get install -y apache2 | |
lxc-attach -n "$L_NAME" -- apt-get install -y git | |
echo "[FINISH] --------- setup lxc\n" | |
} | |
create_and_copy_lxc_pubkey() { | |
local pub_key_path="/home/gitadmin/gitolite-admin/keydir/$L_NAME.pub" | |
lxc-attach -n "$L_NAME" -- mkdir -m 700 "/home/$LXC_USER_NAME/.ssh" | |
cp /root/.ssh/config "/var/lib/lxc/$L_NAME/rootfs/home/$LXC_USER_NAME/.ssh/config" | |
cp /root/.ssh/id_rsa.pub "/var/lib/lxc/$L_NAME/rootfs/home/$LXC_USER_NAME/.ssh/authorized_keys" | |
cat /home/git/.ssh/id_rsa.pub >> "/var/lib/lxc/$L_NAME/rootfs/home/$LXC_USER_NAME/.ssh/authorized_keys" | |
lxc-attach -n "$L_NAME" -- chown -R "$LXC_USER_NAME:$LXC_USER_NAME" "/home/$LXC_USER_NAME/.ssh" | |
ssh [email protected].$IP "ssh-keygen -t rsa -N '' -f /home/$LXC_USER_NAME/.ssh/id_rsa" | |
lxc-attach -n "$L_NAME" -- cat "/home/$LXC_USER_NAME/.ssh/id_rsa.pub" > "$pub_key_path" | |
chmod 700 "$pub_key_path" | |
chown gitadmin:gitadmin "$pub_key_path" | |
echo "[FINISH] --------- create and copy lxc pub key\n" | |
} | |
create_git_repository() { | |
local new_line="repo $NAME/$REPO | |
RW+ = gitadmin | |
RW = @developers | |
R = @all" | |
echo "$new_line" >> "/home/gitadmin/gitolite-admin/conf/gitolite.conf" | |
pushd /home/gitadmin/gitolite-admin/ | |
git add . | |
git commit -m "[new repo] created $NAME/$REPO" | |
su - gitadmin -c "cd gitolite-admin; git push origin master" | |
popd | |
echo "[FINISH] --------- create git repository\n" | |
} | |
create_post_hook() { | |
local bash_script="#!/bin/sh | |
ssh [email protected].$IP \"cd /home/$LXC_USER_NAME/$REPO; git pull\"" | |
local git_dir="/home/git/repositories/$NAME/$REPO.git/" | |
local post_hook_path="$git_dir/hooks/post-receive" | |
for i in $(seq 1 3); do | |
if [[ -d "$git_dir" ]]; then | |
break | |
fi | |
sleep 3 | |
done | |
echo "$bash_script" > "$post_hook_path" | |
chmod 700 "$post_hook_path" | |
chown git:git "$post_hook_path" | |
echo "[FINISH] --------- create post hook\n" | |
} | |
clone_repo() { | |
local path="[email protected]:$NAME/$REPO.git" | |
ssh [email protected].$IP "git clone $path /home/$LXC_USER_NAME/$REPO" | |
echo "[FINISH] --------- clone repo\n" | |
} | |
restart_apache() { | |
sudo cat "./apache.conf" | awk '{gsub(/<%=lxc%>/, "'${LXC_USER_NAME}'");gsub(/<%=repo%>/, "'${REPO}'");print $0;}' | sudo tee "/var/lib/lxc/$L_NAME/rootfs/etc/apache2/sites-enabled/000-default.conf" | |
lxc-attach -n "$L_NAME" -- service apache2 restart | |
} | |
main() { | |
if [[ $# > 2 ]]; then | |
create_lxc | |
setup_lxc | |
create_and_copy_lxc_pubkey | |
create_git_repository | |
create_post_hook | |
clone_repo | |
restart_apache | |
exit 0 | |
else | |
printf "$USAGE" | |
exit 1 | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment