From 3b357934315b55ca5963fc13e545d7388fbd47ee Mon Sep 17 00:00:00 2001 From: t-iwano Date: Wed, 13 May 2015 19:24:19 +0900 Subject: [PATCH 01/12] Added RPM spec file. --- rpmbuild/SPECS/tiny-web-example.spec | 74 ++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 rpmbuild/SPECS/tiny-web-example.spec diff --git a/rpmbuild/SPECS/tiny-web-example.spec b/rpmbuild/SPECS/tiny-web-example.spec new file mode 100644 index 0000000..dad7582 --- /dev/null +++ b/rpmbuild/SPECS/tiny-web-example.spec @@ -0,0 +1,74 @@ +%define _prefix_path opt/axsh +%define _tiny_web_example_git_uri git://github.com/axsh/tiny_web_example.git +%define oname tiny-web-example + +%define release_id 1.daily +%{?build_id:%define release_id %{build_id}} +%{?repo_uri:%define _tiny_web_example_git_uri %{repo_uri}} + +Summary: Tiny Web Example. +Name: %{oname} +Version: 0.0.1 +Release: %{release_id}%{?dist} +License: see https://github.com/axsh/tiny_web_example/blob/master/README.md +Group: Development/Languages +URL: http://example.jp/ +Source0: %{_vdc_git_uri} +Prefix: /%{_prefix_path} + +%description +Tiny Web Example + +Requires: nginx +Requires: mysql-server + +%prep +[ -d %{name}-%{version} ] && rm -rf [ -d %{name}-%{version} ] +git clone %{_tiny_web_example_git_uri} %{name}-%{version} +cd %{name}-%{version}/webapi +bundle install +cd ../frontend +bundle install +cd .. +[ -z "%{build_id}" ] || { + build_id=%{build_id} + git checkout ${build_id##*git} + unset build_id +} && : + +%setup -T -D + +%build + +%install +[ -d ${RPM_BUILD_ROOT} ] && rm -rf ${RPM_BUILD_ROOT} +mkdir -p ${RPM_BUILD_ROOT}/%{prefix}/%{oname} + +components=" + frontend + webapi +" +for component in ${components}; do + rsync -aHA --exclude=".git/*" --exclude="*~" --exclude="*/cache/*.gem" --exclude="*/cache/bundler/git/*" `pwd`/${component} ${RPM_BUILD_ROOT}/%{prefix}/%{oname}/ +done +unset components + +[ -d ${RPM_BUILD_ROOT}/etc ] || mkdir -p ${RPM_BUILD_ROOT}/etc +rsync -aHA `pwd`/contrib/etc/default ${RPM_BUILD_ROOT}/etc +rsync -aHA `pwd`/contrib/etc/init ${RPM_BUILD_ROOT}/etc +rsync -aHA `pwd`/contrib/etc/%{oname} ${RPM_BUILD_ROOT}/etc + +mkdir -p ${RPM_BUILD_ROOT}/var/log/%{oname} + +%clean +rm -rf $RPM_BUILD_ROOT + + +%files +%defattr(-,root,root,-) +%{prefix}/%{oname} +%dir /etc/%{oname} +%dir /var/log/%{oname} +%config(noreplace) /etc/default/tiny-web-example-webapi +%config(noreplace) /etc/init/tiny-web-example-webapi.conf +%config(noreplace) /etc/tiny-web-example/unicorn-common.conf From 422755ed565dcf0b8922d2658a7ac7d1709ae7b6 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Wed, 13 May 2015 21:40:32 +0900 Subject: [PATCH 02/12] Added rules file. --- rpmbuild/SPECS/tiny-web-example.spec | 10 ++++----- rpmbuild/rules | 31 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100755 rpmbuild/rules diff --git a/rpmbuild/SPECS/tiny-web-example.spec b/rpmbuild/SPECS/tiny-web-example.spec index dad7582..4a7cd2b 100644 --- a/rpmbuild/SPECS/tiny-web-example.spec +++ b/rpmbuild/SPECS/tiny-web-example.spec @@ -19,17 +19,15 @@ Prefix: /%{_prefix_path} %description Tiny Web Example +BuildRequires: mysql-devel + Requires: nginx Requires: mysql-server %prep [ -d %{name}-%{version} ] && rm -rf [ -d %{name}-%{version} ] git clone %{_tiny_web_example_git_uri} %{name}-%{version} -cd %{name}-%{version}/webapi -bundle install -cd ../frontend -bundle install -cd .. +cd %{name}-%{version} [ -z "%{build_id}" ] || { build_id=%{build_id} git checkout ${build_id##*git} @@ -39,6 +37,7 @@ cd .. %setup -T -D %build +RUBYDIR=/usr/bin/ruby rpmbuild/rules build %install [ -d ${RPM_BUILD_ROOT} ] && rm -rf ${RPM_BUILD_ROOT} @@ -61,6 +60,7 @@ rsync -aHA `pwd`/contrib/etc/%{oname} ${RPM_BUILD_ROOT}/etc mkdir -p ${RPM_BUILD_ROOT}/var/log/%{oname} %clean +RUBYDIR=/usr/bin/ruby rpmbuild/rules clean rm -rf $RPM_BUILD_ROOT diff --git a/rpmbuild/rules b/rpmbuild/rules new file mode 100755 index 0000000..13fada8 --- /dev/null +++ b/rpmbuild/rules @@ -0,0 +1,31 @@ +#!/usr/bin/make -f + +CURDIR ?= $(PWD) + +build: build-stamp +build-stamp: bundle-install + touch $@ +bundle-install: bundle-install-stamp +bundle-install-stamp: + (cd $(CURDIR)/webapi && rm -rf vendor/bundle && mkdir -p vendor/bundle && bundle install --standalone --path vendor/bundle) + (cd $(CURDIR)/frontend && rm -rf vendor/bundle && mkdir -p vendor/bundle && bundle install --standalone --path vendor/bundle) + + # replace /usr/bin/ruby shebangs. + # unicorn shebang: /this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby + egrep -m 1 -r '^#!.*/ruby' $(CURDIR) | \ + grep -v '^Binary file' | \ + awk -F: '{print $$1}' | \ + while read -r i; do \ + echo $$i; \ + cp -p $$i $$i.sed ;\ + sed -e 's|^#!.*/ruby|#!/usr/bin/ruby|g' < $$i > $$i.sed ;\ + mv $$i.sed $$i ;\ + done + + touch $@ + +clean: + rm -rf $(CURDIR)/vendor/bundle + rm -f $(CURDIR)/bundle-install-stamp + rm -f build-stamp + From 07b200da970c1599ace3a076cf26741971fe4aa1 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 15:10:48 +0900 Subject: [PATCH 03/12] Added webapp init script. --- contrib/etc/default/tiny-web-example-webapp | 11 +++++++ contrib/etc/init/tiny-web-example-webapp.conf | 33 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 contrib/etc/default/tiny-web-example-webapp create mode 100644 contrib/etc/init/tiny-web-example-webapp.conf diff --git a/contrib/etc/default/tiny-web-example-webapp b/contrib/etc/default/tiny-web-example-webapp new file mode 100644 index 0000000..9b8d90e --- /dev/null +++ b/contrib/etc/default/tiny-web-example-webapp @@ -0,0 +1,11 @@ +# tiny-web-example +EXAMPLE_ROOT=/opt/axsh/tiny-web-example + +# Commnet out to run the vdc init script. +#RUN=yes + +## rack params +RACK_ENV=development +#BIND_ADDR=0.0.0.0 +#PORT=80 +#UNICORN_CONF=/etc/tiny-web-example/unicorn-common.conf \ No newline at end of file diff --git a/contrib/etc/init/tiny-web-example-webapp.conf b/contrib/etc/init/tiny-web-example-webapp.conf new file mode 100644 index 0000000..074ced7 --- /dev/null +++ b/contrib/etc/init/tiny-web-example-webapp.conf @@ -0,0 +1,33 @@ +description "tiny web example: web application" +author "axsh Co." + +start on started tiny-web-example-webapp +stop on stopped tiny-web-example-webapp + +respawn +respawn limit 5 60 + +env NAME=webapp + +script + [ -f /etc/default/tiny-web-example-${NAME} ] && . /etc/default/tiny-web-example-${NAME} + # Make RUN=yes effective only at auto start. + [ -n "$UPSTART_EVENTS" -a "x${RUN}" != "xyes" ] && { + logger "[${NAME}] Skip auto start for ${NAME}. Edit /etc/default/tiny-web-example-${NAME} to set RUN=yes." + exit 0 + } + + ulimit -c ${DAEMON_COREFILE_LIMIT:-0} + + [ -d "${EXAMPLE_ROOT}" ] || { + logger "no such directory: ${EXAMPLE_ROOT}" + exit 1 + } + cd ${EXAMPLE_ROOT}/frontend/ + + exec bundle exec unicorn \ + -o ${BIND_ADDR:-0.0.0.0} \ + -p ${PORT:-80} \ + -c ${UNICORN_CONF:-/etc/tiny-web-example/unicorn-common.conf} ./config-${NAME}.ru \ + >> /var/log/tiny-web-example/${NAME}.log 2>&1 +end script \ No newline at end of file From 0f1ae20828b26d823f16c604f05d375c0bd2990e Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 15:22:13 +0900 Subject: [PATCH 04/12] Update RPM spec file. --- rpmbuild/SPECS/tiny-web-example.spec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rpmbuild/SPECS/tiny-web-example.spec b/rpmbuild/SPECS/tiny-web-example.spec index 4a7cd2b..3fadca5 100644 --- a/rpmbuild/SPECS/tiny-web-example.spec +++ b/rpmbuild/SPECS/tiny-web-example.spec @@ -70,5 +70,7 @@ rm -rf $RPM_BUILD_ROOT %dir /etc/%{oname} %dir /var/log/%{oname} %config(noreplace) /etc/default/tiny-web-example-webapi +%config(noreplace) /etc/default/tiny-web-example-webapp %config(noreplace) /etc/init/tiny-web-example-webapi.conf +%config(noreplace) /etc/init/tiny-web-example-webapp.conf %config(noreplace) /etc/tiny-web-example/unicorn-common.conf From 24495164052a958189652c97f1ba64371314ff75 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 15:41:22 +0900 Subject: [PATCH 05/12] Changed it to read the configuration file. --- webapi/Rakefile | 1 + webapi/lib/webapi/configurations/webapi.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/webapi/Rakefile b/webapi/Rakefile index eab97d9..402925f 100644 --- a/webapi/Rakefile +++ b/webapi/Rakefile @@ -10,6 +10,7 @@ task :environment do require 'webapi' Webapi::Configurations.load Webapi::Configurations::Webapi, [ ENV['CONF_PATH'].to_s, + '/etc/tiny-web-example/webapi.conf', File.expand_path('config/webapi.conf', ::WEBAPI_ROOT) ] end diff --git a/webapi/lib/webapi/configurations/webapi.rb b/webapi/lib/webapi/configurations/webapi.rb index f563790..19c2b4d 100644 --- a/webapi/lib/webapi/configurations/webapi.rb +++ b/webapi/lib/webapi/configurations/webapi.rb @@ -7,6 +7,7 @@ module Configurations class Webapi < Fuguta::Configuration usual_paths [ ENV['CONF_PATH'].to_s, + '/etc/tiny-web-example/webapi.conf', File.expand_path('config/webapi.conf', ::WEBAPI_ROOT) ] From 384c4e26d66c5a9bd2868859699b5ea370d52ea8 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 17:38:48 +0900 Subject: [PATCH 06/12] Added sinatra_config. --- frontend/config-webapp.ru | 3 +++ frontend/config/webapp.yml | 1 + frontend/models/comment.rb | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 frontend/config/webapp.yml diff --git a/frontend/config-webapp.ru b/frontend/config-webapp.ru index 2869e22..74d39c1 100644 --- a/frontend/config-webapp.ru +++ b/frontend/config-webapp.ru @@ -2,6 +2,9 @@ require 'rubygems' require 'sinatra' +require 'sinatra/config_file' + +config_file 'config/webapp.yml', '/etc/tiny-web-example/webapp.yml' $LOAD_PATH.unshift File.expand_path('..', __FILE__) diff --git a/frontend/config/webapp.yml b/frontend/config/webapp.yml new file mode 100644 index 0000000..73f783f --- /dev/null +++ b/frontend/config/webapp.yml @@ -0,0 +1 @@ +database_uri: 'mysql2://localhost/tiny_web_example?user=root' \ No newline at end of file diff --git a/frontend/models/comment.rb b/frontend/models/comment.rb index b6b5fb3..ff73c21 100644 --- a/frontend/models/comment.rb +++ b/frontend/models/comment.rb @@ -2,7 +2,7 @@ require 'sequel' -Sequel.connect("mysql2://localhost/tiny_web_example?user=root") +Sequel.connect(settings.database_uri) class Comment < Sequel::Model plugin :validation_helpers From 3c719710b4afad3380111df0d303e85ba48683b2 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 17:45:47 +0900 Subject: [PATCH 07/12] Added html escape. --- frontend/views/index.erb | 8 ++++---- frontend/webapp.rb | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/frontend/views/index.erb b/frontend/views/index.erb index 085e107..f400523 100644 --- a/frontend/views/index.erb +++ b/frontend/views/index.erb @@ -36,14 +36,14 @@

- No. <%= comment.id %> - Name <%= comment.display_name %> - Created at <%= comment.created_at %> + No. <%= h(comment.id) %> + Name <%= h(comment.display_name) %> + Created at <%= h(comment.created_at) %>

- <%= comment.comment %> + <%= h(comment.comment) %>
<% end %> diff --git a/frontend/webapp.rb b/frontend/webapp.rb index f75409b..4368495 100644 --- a/frontend/webapp.rb +++ b/frontend/webapp.rb @@ -3,6 +3,11 @@ require 'sinatra/base' class WebApp < Sinatra::Base + helpers do + include Rack::Utils + alias_method :h, :escape_html + end + get '/' do @comments = Comment.dataset.all erb :index From 6b3b6d105c23f6ed082f8a93ed642a0fd537cb61 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 18:25:34 +0900 Subject: [PATCH 08/12] Added gsub for the Comment display. --- frontend/views/index.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/views/index.erb b/frontend/views/index.erb index f400523..b18d6ba 100644 --- a/frontend/views/index.erb +++ b/frontend/views/index.erb @@ -43,7 +43,7 @@
- <%= h(comment.comment) %> + <%= h(comment.comment).gsub(/\r\n|\r|\n/, "
") %>
<% end %> From 94e12db29de4ee1752fca18ce6d9e3835c037e31 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 19:35:03 +0900 Subject: [PATCH 09/12] Added validation check for frontend. --- frontend/views/index.erb | 5 +++++ frontend/webapp.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/frontend/views/index.erb b/frontend/views/index.erb index b18d6ba..02565c8 100644 --- a/frontend/views/index.erb +++ b/frontend/views/index.erb @@ -7,6 +7,11 @@
+
+

+

<%= @errors %>

+

+
diff --git a/frontend/webapp.rb b/frontend/webapp.rb index 4368495..8f6300a 100644 --- a/frontend/webapp.rb +++ b/frontend/webapp.rb @@ -3,19 +3,31 @@ require 'sinatra/base' class WebApp < Sinatra::Base + + configure do + use Rack::Session::Pool + end + helpers do include Rack::Utils alias_method :h, :escape_html end get '/' do + @errors = session[:errors] + session.clear + @comments = Comment.dataset.all erb :index end post '/comment' do + begin Comment.create(:display_name => params["display_name"], :comment => params["comment"]) + rescue Sequel::ValidationFailed => e + session[:errors] = e + end redirect '/' end end From d7dbd5a2e85c86e8bdd3d527e0c7c36f5a42cee6 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 20:13:59 +0900 Subject: [PATCH 10/12] Moved Config files. --- {webapi/config => contrib/etc/tiny-web-example}/webapi.conf | 0 {frontend/config => contrib/etc/tiny-web-example}/webapp.yml | 0 frontend/config/webapp.yml.example | 1 + webapi/config/webapi.conf.example | 2 ++ 4 files changed, 3 insertions(+) rename {webapi/config => contrib/etc/tiny-web-example}/webapi.conf (100%) rename {frontend/config => contrib/etc/tiny-web-example}/webapp.yml (100%) create mode 100644 frontend/config/webapp.yml.example create mode 100644 webapi/config/webapi.conf.example diff --git a/webapi/config/webapi.conf b/contrib/etc/tiny-web-example/webapi.conf similarity index 100% rename from webapi/config/webapi.conf rename to contrib/etc/tiny-web-example/webapi.conf diff --git a/frontend/config/webapp.yml b/contrib/etc/tiny-web-example/webapp.yml similarity index 100% rename from frontend/config/webapp.yml rename to contrib/etc/tiny-web-example/webapp.yml diff --git a/frontend/config/webapp.yml.example b/frontend/config/webapp.yml.example new file mode 100644 index 0000000..73f783f --- /dev/null +++ b/frontend/config/webapp.yml.example @@ -0,0 +1 @@ +database_uri: 'mysql2://localhost/tiny_web_example?user=root' \ No newline at end of file diff --git a/webapi/config/webapi.conf.example b/webapi/config/webapi.conf.example new file mode 100644 index 0000000..fac9fc7 --- /dev/null +++ b/webapi/config/webapi.conf.example @@ -0,0 +1,2 @@ +# Database connection string +database_uri 'mysql2://localhost/tiny_web_example?user=root' From 3663298f389999419a6f9857ec0e177110a9f126 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 20:15:03 +0900 Subject: [PATCH 11/12] Update RPM spec file. --- rpmbuild/SPECS/tiny-web-example.spec | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rpmbuild/SPECS/tiny-web-example.spec b/rpmbuild/SPECS/tiny-web-example.spec index 3fadca5..fee8880 100644 --- a/rpmbuild/SPECS/tiny-web-example.spec +++ b/rpmbuild/SPECS/tiny-web-example.spec @@ -63,6 +63,8 @@ mkdir -p ${RPM_BUILD_ROOT}/var/log/%{oname} RUBYDIR=/usr/bin/ruby rpmbuild/rules clean rm -rf $RPM_BUILD_ROOT +%post +/sbin/chkconfig mysqld on %files %defattr(-,root,root,-) @@ -74,3 +76,5 @@ rm -rf $RPM_BUILD_ROOT %config(noreplace) /etc/init/tiny-web-example-webapi.conf %config(noreplace) /etc/init/tiny-web-example-webapp.conf %config(noreplace) /etc/tiny-web-example/unicorn-common.conf +%config(noreplace) /etc/tiny-web-example/webapi.conf +%config(noreplace) /etc/tiny-web-example/webapp.yml From 76bd591f25a062beb461fa7f5713f51ebe5e9995 Mon Sep 17 00:00:00 2001 From: t-iwano Date: Thu, 14 May 2015 20:31:29 +0900 Subject: [PATCH 12/12] Fixed Require packages. --- rpmbuild/SPECS/tiny-web-example.spec | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rpmbuild/SPECS/tiny-web-example.spec b/rpmbuild/SPECS/tiny-web-example.spec index fee8880..2786ae8 100644 --- a/rpmbuild/SPECS/tiny-web-example.spec +++ b/rpmbuild/SPECS/tiny-web-example.spec @@ -16,14 +16,15 @@ URL: http://example.jp/ Source0: %{_vdc_git_uri} Prefix: /%{_prefix_path} -%description -Tiny Web Example - BuildRequires: mysql-devel Requires: nginx Requires: mysql-server + +%description +Tiny Web Example + %prep [ -d %{name}-%{version} ] && rm -rf [ -d %{name}-%{version} ] git clone %{_tiny_web_example_git_uri} %{name}-%{version}