Skip to content

Instantly share code, notes, and snippets.

@palcu
Created February 27, 2014 18:03
Show Gist options
  • Save palcu/9255419 to your computer and use it in GitHub Desktop.
Save palcu/9255419 to your computer and use it in GitHub Desktop.
Blog post Vagrant
Do you hate coming at the office, opening your text editor, looking over the Github issues, seeing a bug you can easily fix and bang... you forgot to open Vagrant. So, you type like a robot <i>vagrant up</i>, you wait 90 seconds for the beast to load and bang... you forgot to connect to the VPN and Puppet cannot sync your packages.
You code the whole day at work, pack your things, arrive home, start browsing threads on Hacker News and bang... you have low battery because you forgot to close Vagrant. Damn the technology stack is too big to fit my tiny brain cache. Well, for those of you on OSX, I made <a href="https://github.com/palcu/dotfiles/tree/master/applescripts">some tiny little AppleScripts</a> that solve these annoying little pesky problems.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="http://2.bp.blogspot.com/-tBswMBVY02k/Uv-1LjKELxI/AAAAAAAAFxs/EieUNzsdp9U/s1600/automation.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://2.bp.blogspot.com/-tBswMBVY02k/Uv-1LjKELxI/AAAAAAAAFxs/EieUNzsdp9U/s1600/automation.png" height="320" width="318" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><a href="https://xkcd.com/1319/">https://xkcd.com/1319/</a></td></tr>
</tbody></table>
The before work script contains two parts: one for opening a list of apps found in <i>apps.txt</i> and another other one for running your custom scripts:<br />
<pre class="brush: applescript">
set current_path to POSIX path of ((path to me as text) &amp; "::")
-- get work apps
set appList to {}
set myApps to paragraphs of (read current_path &amp; "apps.txt")
repeat with nextLine in myApps
if length of nextLine is greater than 0 then
copy (nextLine as string) to the end of appList
end if
end repeat
-- open work apps
repeat with i in appList
tell application i to activate
end repeat
-- run other scripts from folder
set scriptList to {"connect_tunnelblick", "open_vagrant", "open_chrome_tabs"}
repeat with i in scriptList
run script current_path &amp; i &amp; ".applescript"
end repeat
</pre>
The <i>open_vagrant</i>&nbsp;script just opens an iTerm session and runs a <i>vagrant up</i>&nbsp;command:<br />
<pre class="brush: applescript">set open_vagrant to "z puppet; vagrant up;"
tell application "iTerm"
activate
try
set mysession to current session of current terminal
on error
set myterm to (make new terminal)
tell myterm
launch session "Default"
set mysession to current session
end tell
end try
tell mysession
write text open_vagrant
end tell
end tell
</pre>
AppleScript is a pretty funny language to code in. However neither Linux, nor Windows have a similar equivalent, making the Mac a hacker friendly environment. Unfortunately, I think it is rough on some edges: for closing iTerm, I had to first ignore the system events, then issue the exit for the app, catch system events and finally press enter.
<pre class="brush: applescript">set close_vagrant to "z puppet; vagrant halt -f;"
-- close vagrant
tell application "iTerm"
activate
set myterm to (make new terminal)
tell myterm
launch session "Default"
set mysession to current session
end tell
tell mysession
write text close_vagrant
end tell
delay 5
end tell
-- quit iTerm
ignoring application responses
tell application "iTerm"
quit
end tell
end ignoring
delay 2
tell application "System Events" to keystroke return
</pre>
Another thing I constantly forget is to update the master branch of the repos, because when I do _vagrant up_ it takes uses the config from the _puppet_ folder.
<pre class="brush: bash">
repos=("thehole" "deploy" "puppet" "uberbot")
repo_path="/Users/alex/uberVU/"
exists=`git show-ref refs/heads/master`
no_stash_save="No local changes to save"
for repo in ${repos[*]}
do
path=$repo_path$repo
cd $path
echo $path
current_branch=$(git branch | awk '/^\*/{print $2}')
if [ "$current_branch" == "master" ]; then
git pull
else
stash_message=$(git stash)
git checkout master
git pull
git checkout $current_branch
if [ "$stash_message" != "$no_stash_save" ]; then
git stash pop
fi
fi
done
</pre>
At the end of he workday you could do some Git [cleaning](http://alblue.bandlem.com/2011/11/git-tip-of-week-gc-and-pruning-this.html) so you maintain your operations are done faster. Add this to your _.gitconfig_:
<pre>
[alias]
sweep = !git fsck && git gc && git remote update --prune && git stash clear
</pre>
And use the same logic as the bash script above:
<pre>
repos=("thehole" "deploy" "puppet" "uberbot")
repo_path="/Users/alex/uberVU/"
exists=`git show-ref refs/heads/master`
no_stash_save="No local changes to save"
for repo in ${repos[*]}
do
path=$repo_path$repo
cd $path
echo $path
git sweep
done
</pre>
Is it worth the time? XKCD has the answer:
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="http://3.bp.blogspot.com/-NDzw-RnHjl8/UwCfeCf8O7I/AAAAAAAAFx8/A1iakMZNqiA/s1600/is_it_worth_the_time.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://3.bp.blogspot.com/-NDzw-RnHjl8/UwCfeCf8O7I/AAAAAAAAFx8/A1iakMZNqiA/s1600/is_it_worth_the_time.png" height="521" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><a href="http://xkcd.com/1205/">http://xkcd.com/1205/</a></td></tr>
</tbody></table>
Given that I do these things daily and I may save between 1 and 5 minutes per day, then I save between 1 and 6 days per 5 years. It doesn't look too much, but given that time is <a href="http://www.quora.com/What-are-some-important-and-generalizable-life-lessons/answer/Yishan-Wong">highly, uniquely and equitably limited</a>, we should strive doing our best in squashing annoying things that chop it.
<iframe width="560" height="315" src="//www.youtube.com/embed/BOksW_NabEk" frameborder="0" allowfullscreen></iframe>
Update: got a tip on <a href="https://www.facebook.com/palcuiealex/posts/10201097721599776?stream_ref=10">Facebook</a> about <a href="https://github.com/tmuxinator/tmuxinator">Tmuxinator</a> which automates opening programs in your terminal. Also, put the scripts somewhere searchable by Spotlight so you can launch them easily, or use my <a href="https://www.dropbox.com/s/2los6wcqaqyxywn/ubervu_applescripts.alfredworkflow">Alfred workflow</a>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment