Skip to content

Commit

Permalink
initial checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Aug 27, 2015
0 parents commit 1802579
Show file tree
Hide file tree
Showing 23 changed files with 578 additions and 0 deletions.
Empty file added .gitignore
Empty file.
1 change: 1 addition & 0 deletions .ruby-gemset
@@ -0,0 +1 @@
microservice-template
1 change: 1 addition & 0 deletions .ruby-version
@@ -0,0 +1 @@
2.1.3
9 changes: 9 additions & 0 deletions Gemfile
@@ -0,0 +1,9 @@
source 'https://rubygems.org'

gem "cucumber", "~> 1.3.10"
gem "cucumber-rest-api"
gem 'pickle-mongodb', '~> 0.1'
gem 'docker-api', :require => 'docker'
gem 'cucumber-mailcatcher', '~> 0.12'
gem 'daemons', '~> 1.2.3'
gem 'mimic', '~> 0.4.3'
100 changes: 100 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,100 @@
GEM
remote: https://rubygems.org/
specs:
activemodel (3.2.22)
activesupport (= 3.2.22)
builder (~> 3.0.0)
activesupport (3.2.22)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
builder (3.0.4)
cucumber (1.3.20)
builder (>= 2.1.2)
diff-lcs (>= 1.1.3)
gherkin (~> 2.12)
multi_json (>= 1.7.5, < 2.0)
multi_test (>= 0.1.2)
cucumber-mailcatcher (0.12)
cucumber (~> 1.3, >= 1.3.10)
rspec-expectations (~> 3.3, >= 3.3.1)
cucumber-rest-api (0.3)
cucumber (>= 1.2.1)
jsonpath (>= 0.1.2)
nokogiri (>= 1.6.0)
rspec (>= 2.12.0)
daemons (1.2.3)
diff-lcs (1.2.5)
docker-api (1.22.2)
excon (>= 0.38.0)
json
excon (0.45.4)
factory_girl (4.3.0)
activesupport (>= 3.0.0)
gherkin (2.12.2)
multi_json (~> 1.3)
i18n (0.7.0)
json (1.8.3)
jsonpath (0.5.7)
multi_json
mimic (0.4.3)
json
plist
rack
sinatra
mini_portile (0.6.2)
mongoid (3.1.7)
activemodel (~> 3.2)
moped (~> 1.4)
origin (~> 1.0)
tzinfo (~> 0.3.29)
moped (1.5.3)
multi_json (1.11.2)
multi_test (0.1.2)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
origin (1.1.0)
pickle (0.4.11)
cucumber (>= 0.8)
rake
pickle-mongodb (0.3)
cucumber (>= 1.2.1)
factory_girl (~> 4.3.0)
mongoid (~> 3.1.6)
pickle (~> 0.4.11)
rspec (>= 2.12.0)
plist (3.1.0)
rack (1.6.4)
rack-protection (1.5.3)
rack
rake (10.4.2)
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
tilt (2.0.1)
tzinfo (0.3.44)

PLATFORMS
ruby

DEPENDENCIES
cucumber (~> 1.3.10)
cucumber-mailcatcher (~> 0.12)
cucumber-rest-api
daemons (~> 1.2.3)
docker-api
mimic (~> 0.4.3)
pickle-mongodb (~> 0.1)
87 changes: 87 additions & 0 deletions Generate.rb
@@ -0,0 +1,87 @@
require 'io/console'
require 'FileUtils'

DEFAULT_NAME = "microservice-template"

def read_line
text = ""

while (char = $stdin.getch) != "\r"
text += char
text = "" if char == " "
print char
end

print "\n"

text
end

def request_name
p "What is the name of this microservice?"
read_line
end

def request_output_folder
p "Where shall i save the template?"
read_line
end

def rename_in_files name, folder

Dir.chdir folder do
file_names = [
'go/src/github.com/nicholasjackson/microservice-template/server.go',
'dockercompose/microservice-template/docker-compose.yml',
'dockerfile/microservice-template/Dockerfile',
'.ruby-gemset',
'Rakefile',
]

file_names.each do |file_name|
text = File.read(file_name)
new_contents = text.gsub(/microservice-template/, name)

# To write changes to the file, use:
File.open(file_name, "w") {|file| file.puts new_contents }
end
end
end

def confirm name,output
p "Generating Microservice template: #{name} in #{output}"
p "Is this correct? (y|n)"

char = $stdin.getch
print char
print "\n"

if char == 'y'
true
end
end

def copy_files destination
FileUtils.copy_entry './', destination
end

def rename_files_and_folders name, destination
Dir.glob("#{destination}/**/*").select.select do |f|
if File.basename(f, ".*") == DEFAULT_NAME
File.rename(f, f.gsub(/microservice-template/,name))
end
end
end

def generate_template
name = request_name
destination = request_output_folder
if confirm name,destination
copy_files destination
rename_in_files name, destination
rename_files_and_folders name, destination
end

end

generate_template
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Nicholas Jackson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

9 changes: 9 additions & 0 deletions README.md
@@ -0,0 +1,9 @@
# microservice-template
Repository to create a template setup for a Go microservice built and tested with a Docker containers.

# How to use
1. run ruby Generate.rb
2. Enter the name for your new microservice
3. Enter the location to create the service

The service will then be created in the destination folder, the Rakefile in the destination contains the details for default build settings.
113 changes: 113 additions & 0 deletions Rakefile
@@ -0,0 +1,113 @@
require 'docker'
require_relative 'rake-modules/docker'

GOPATH = Dir.pwd + "/go"
GOCMD = "/usr/local/go/bin/go"
CONFIG = Dir.pwd + "/config.json"
ROOTFOLDER = Dir.pwd

REGISTRY_USER = ENV['DOCKER_REGISTRY_USER']
REGISTRY_PASS = ENV['DOCKER_REGISTRY_PASS']
REGISTRY_EMAIL = ENV['DOCKER_REGISTRY_EMAIL']

DOCKER_IMAGE_NAME = 'microservice-template'

task :test do
p "Test Application"
container = get_container

begin
# Get go packages
ret = container.exec(['go','get','-t','-v','./...']) { |stream, chunk| puts "#{stream}: #{chunk}" }
raise Exception, 'Error running command' unless ret[2] == 0

# Test application
ret = container.exec(['go','test','./...']) { |stream, chunk| puts "#{stream}: #{chunk}" }
raise Exception, 'Error running command' unless ret[2] == 0
ensure
container.delete(:force => true)
end
end

task :build => [:test] do
p "Build for Linux"
container = get_container

begin
# Build go server
ret = container.exec(['go','build','-o','server']) { |stream, chunk| puts "#{stream}: #{chunk}" }
raise Exception, 'Error running command' unless ret[2] == 0
ensure
container.delete(:force => true)
end
end

task :build_server => [:build] do
p "Building Docker Image:- #{DOCKER_IMAGE_NAME}"

FileUtils.cp "./go/src/github.com/nicholasjackson/#{DOCKER_IMAGE_NAME}/server", "./dockerfile/#{DOCKER_IMAGE_NAME}/server"

Docker.options = {:read_timeout => 6200}
image = Docker::Image.build_from_dir "./dockerfile/#{DOCKER_IMAGE_NAME}", {:t => DOCKER_IMAGE_NAME}
end

task :run => [:build_server] do
begin
sh "docker-compose -f ./dockercompose/#{DOCKER_IMAGE_NAME}/docker-compose.yml up"
rescue SystemExit, Interrupt
sh "docker-compose -f ./dockercompose/#{DOCKER_IMAGE_NAME}/docker-compose.yml stop"
# remove stopped containers
sh "echo y | docker-compose -f ./dockercompose/#{DOCKER_IMAGE_NAME}/docker-compose.yml rm"
end
end

task :build_go_build_server do
p "Building Docker Image:- Dev Server"

Docker.options = {:read_timeout => 6200}
image = Docker::Image.build_from_dir './dockerfile/gobuildserver', {:t => 'gobuildserver'}
end

task :e2e do
feature = ARGV.last
if feature != "e2e"
feature = "--tags #{feature}"
else
feature = ""
end

host = get_docker_ip_address

puts "Running Tests for #{host}"

ENV['WEB_SERVER_URI'] = "http://#{host}:8001"
#ENV['MONGO_URI'] = "#{host}:27017"
#ENV['EMAIL_SERVER_URI'] = "http://#{host}:1080"

begin
pid = Process.fork do
exec "docker-compose -f ./dockercompose/#{DOCKER_IMAGE_NAME}/docker-compose.yml up > serverlog.txt"
end

sleep 5

sh "cucumber #{feature}"
ensure
# remove stop running application
sh "docker-compose -f ./dockercompose/#{DOCKER_IMAGE_NAME}/docker-compose.yml stop"
# remove stopped containers
sh "echo y | docker-compose -f ./dockercompose/#{DOCKER_IMAGE_NAME}/docker-compose.yml rm"
end
end

task :push do
p "Push image to registry"

image = find_image "#{DOCKER_IMAGE_NAME}:latest"
image.tag('repo' => "tutum.co/nicholasjackson/#{DOCKER_IMAGE_NAME}", 'force' => true) unless image.info["RepoTags"].include? "tutum.co/nicholasjackson/#{DOCKER_IMAGE_NAME}:latest"

#p Docker.authenticate!('serveraddress' => 'https://tutum.co', 'username' => "#{REGISTRY_USER}", 'password' => "#{REGISTRY_PASS}", 'email' => "#{REGISTRY_EMAIL}")
sh "docker login -u #{REGISTRY_USER} -p #{REGISTRY_PASS} -e #{REGISTRY_EMAIL} https://tutum.co"
sh "docker push tutum.co/nicholasjackson/#{DOCKER_IMAGE_NAME}:latest"
#image.push() { |stream, chunk| puts "#{stream}: #{chunk}" }
end
Empty file.
4 changes: 4 additions & 0 deletions dockercompose/microservice-template/docker-compose.yml
@@ -0,0 +1,4 @@
microservice-template:
image: microservice-template
ports:
- "8001:8001"
19 changes: 19 additions & 0 deletions dockerfile/gobuildserver/Dockerfile
@@ -0,0 +1,19 @@
FROM golang:latest

MAINTAINER Nic Jackson <jackson.nic@gmail.com>

RUN apt-get update

RUN echo y | apt-get install git

# install ruby
RUN echo y | apt-get install ruby
RUN echo y | apt-get install ruby-dev
RUN apt-get install zlib1g-dev
RUN echo y | apt-get install build-essential

RUN gem install bundle

ADD ./Gemfile Gemfile

RUN bundle
5 changes: 5 additions & 0 deletions dockerfile/gobuildserver/Gemfile
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem "cucumber", "~> 1.3.10"
gem "cucumber-rest-api"
gem 'pickle-mongodb', '~> 0.1'
11 changes: 11 additions & 0 deletions dockerfile/microservice-template/Dockerfile
@@ -0,0 +1,11 @@
FROM ubuntu:14.04

EXPOSE 8001

RUN mkdir /microservice-template

ADD ./server /microservice-template/server

RUN chmod 755 /microservice-template/server

CMD ["/microservice-template/server"]

0 comments on commit 1802579

Please sign in to comment.