Go Dep October 25, 2017
As I'm going through and editing all of these blurbs now, I'm reminded of some things that I thought were good at the time, and have now been replaced by things that are a million times better. Go Dep is one of those! Go modules are a thing of pure genius.
As of this moment I've updated all of my Go code to use "dep". It's useful, and it works. It's slow but that'll be fixed. Phew! Slight pain updating all of my repos to it. But man, it'll make life easy going forward.
First of all, I had to get all of my dependencies, code libraries that I've written to be shared, into my public github account. After that was complete, I had to update all of the imports. For instance, instead of "conf", which was a folder directly inside my Go path. Which makes it interesting. For a few reasons.
- I had to create a repo (in my private server) for each dependency.
- If I didn't clone that repo on another machine, the build would fail.
- If I forgot to commit the dependency repo but committed the repo that was depending on it, the build would fail on my other computer
These are problems. Here are a few solutions...
- For dependency repos, I may only have them in github going forward. Until it's easy to go get private server repositories. All of my repos are literally on the same server this site is running on.
- Doing dep ensure will get all of my dependencies, at the version it was locked at.
- Using dep and the dep paths (github.com/jasontconnell/____) willl ensure that the project will only build if it's in github.
You'll see all of my dependency repos within github. There are a few full fledged projects out there as well. (Including scgen :). It is just a matter of updating the code to use the github url instead of the local file url, running dep init, and waiting :)
One tricky thing is I have a bash script to automatically deploy websites on that server. It looked like this:
#!/bin/bash
cd src/$1
git fetch
deploybranch=`git branch -a | grep deploy`
if [ -z "$deploybranch" ]; then
echo no branch named deploy. exiting.
exit
fi
git checkout deploy
git pull
cd ..
echo building $1
go build -o bin/$1 $1
outdir=/var/www/$1
echo $outdir
PID=`pgrep $1`
echo found pid $PID
if [ -n "$PID" ]; then
echo killing process $PID
sudo kill $PID
fi
sudo cp bin/$1 $outdir/
if [ -d "$PWD/$1/content" ]; then
echo copying content
sudo cp -R $PWD/$1/content/ $outdir/content
fi
if [ -d "$PWD/$1/site" ]; then
echo copying site
sudo cp -R $PWD/$1/site/ $outdir/site
fi
cd $outdir
sudo nohup ./$1 > $1.out 2> $1.err < /dev/null & > /dev/null
echo $1 started with pid $!
exit
I'm very noobish when it comes to shell scripting. Anyway, this will checkout a deploy branch if it exists, pull latest, run go build, kill the current process and then start the process. It'll then copy contents over to the website. Simple build and deploy script.
It is named "deploy.sh". It exists in /home/jason/go and it is run just like this, "./deploy.sh jtccom" It finds the folder "jtccom" inside of src and does all of the operations there. However, since I'm now using "dep", and none of the files exist within the "vendor" folder (you really shouldn't commit that... dep creates reproducible builds), I will have to modify it to run dep first. This has to happen after the pull. I've included the entire contents of the new deploy.sh here.
#!/bin/bash
cd src/$1
git fetch
deploybranch=`git branch -a | grep deploy`
if [ -z "$deploybranch" ]; then
echo no branch named deploy. exiting.
exit
fi
git checkout deploy
git pull
if [ -f Gopkg.toml ]; then
echo Running dep ensure
dep=`which dep`
$dep ensure
fi
cd ..
echo building $1
GOCMD=`which go`
$GOCMD build -o bin/$1 $1
outdir=/var/www/$1
echo $outdir
PID=`pgrep $1`
echo found pid $PID
if [ -n "$PID" ]; then
echo killing process $PID
sudo kill $PID
fi
sudo cp bin/$1 $outdir/
if [ -d "$PWD/$1/content" ]; then
echo copying content
sudo cp -R $PWD/$1/content/ $outdir/content
fi
if [ -d "$PWD/$1/site" ]; then
echo copying site
sudo cp -R $PWD/$1/site/ $outdir/site
fi
cd $outdir
sudo nohup ./$1 > $1.out 2> $1.err < /dev/null & > /dev/null
echo $1 started with pid $!
exit
I've updated how it calls go and dep, since calling just "go" didn't work anymore for some reason. Command not found. Anyway, here's the output.
[jason@Setzer go]$ ./deploy.sh jtccom
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ssh://myserver/~git/jtccom
03999eb..7c49dc6 deploy -> origin/deploy
03999eb..7c49dc6 develop -> origin/develop
Already on 'deploy'
Your branch is behind 'origin/deploy' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Updating 03999eb..7c49dc6
Fast-forward
Gopkg.lock | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Running dep ensure
building jtccom
/var/www/jtccom
found pid 29767
killing process 29767
copying content
jtccom started with pid 30260
That Gopkg.lock was updated because I had to update a dependency to also use the github version of the dependency it was importing, since I deleted all dependencies on this server. So that was it. It's very easy to use and will make my life a lot easier, albeit a little bit more tedious. BUT! I really can't complain because the old way of doing things was painful. Forgetting to commit the dependency, now my code doesn't build on my work computer, so I have to wait until I get home :P Plus everyone can look at the little dumb Go code I use across multiple projects! Enjoy.