Skip to content

Instantly share code, notes, and snippets.

@makotoshimazu
Created March 22, 2015 04:20
Show Gist options
  • Save makotoshimazu/b7223e684631f3caa02e to your computer and use it in GitHub Desktop.
Save makotoshimazu/b7223e684631f3caa02e to your computer and use it in GitHub Desktop.
gogsを簡単に永続化させるためのパッチ
diff --git a/.gitignore b/.gitignore
index ba878d9..265d8fc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,5 +34,6 @@ config.codekit
docker/fig.yml
docker/docker/Dockerfile
docker/docker/init_gogs.sh
+docker/docker/misc/
gogs.sublime-project
gogs.sublime-workspace
diff --git a/docker/assemble_blocks.sh b/docker/assemble_blocks.sh
index 852064d..c4fcbb1 100755
--- a/docker/assemble_blocks.sh
+++ b/docker/assemble_blocks.sh
@@ -5,6 +5,7 @@ docker_dir=docker
template_dir=templates
docker_file=Dockerfile
+docker_misc_dir=misc
gogs_config_file=conf.tmp
gogs_config=config
@@ -13,7 +14,8 @@ gogs_init_file=$docker_dir/init_gogs.sh
fig_file=fig.yml
fig_config=fig
-gogs_init_template=$template_dir/init_gogs.sh.tpl
+gogs_init_template=$template_dir/init_gogs
+gogs_init_template_suffix=.sh.tpl
if [ "$#" == 0 ]; then
blocks=`ls $blocks_dir`
@@ -35,6 +37,7 @@ for file in $gogs_config_file $fig_file; do
fi
done
+init_gogs_target=""
for dir in $@; do
current_dir=$blocks_dir/$dir
if [ ! -d "$current_dir" ]; then
@@ -45,6 +48,12 @@ for dir in $@; do
if [ -e $current_dir/$docker_file ]; then
echo "Copying $current_dir/$docker_file to $docker_dir/$docker_file"
cp $current_dir/$docker_file $docker_dir/$docker_file
+ init_gogs_target=$dir
+ fi
+
+ if [ -d $current_dir/$docker_misc_dir ]; then
+ echo "Copying $current_dir/$docker_misc_dir to $docker_dir/$docker_misc_dir"
+ cp -r $current_dir/$docker_misc_dir $docker_dir/$docker_misc_dir
fi
if [ -e $current_dir/$gogs_config ]; then
@@ -61,6 +70,13 @@ for dir in $@; do
done
echo "Creating $gogs_init_file"
+
+# search for target-specific template file
+gogs_init_specific_file="${gogs_init_template}_${init_gogs_target}${gogs_init_template_suffix}"
+if [ -f $gogs_init_specific_file ]; then
+ gogs_init_template=$gogs_init_specific_file
+fi
+
sed "/{{ CONFIG }}/{
r $gogs_config_file
d
@@ -69,4 +85,4 @@ d
if [ -e $gogs_config_file ]; then
echo "Removing temporary GoGS config"
rm $gogs_config_file
-fi
\ No newline at end of file
+fi
diff --git a/docker/blocks/docker_gogs_persistent/Dockerfile b/docker/blocks/docker_gogs_persistent/Dockerfile
new file mode 100644
index 0000000..d0c4580
--- /dev/null
+++ b/docker/blocks/docker_gogs_persistent/Dockerfile
@@ -0,0 +1,65 @@
+FROM ubuntu:14.04
+
+# This part is taken from the official docker image --------------------
+
+RUN apt-get update && apt-get install -y \
+ build-essential ca-certificates curl \
+ bzr git mercurial openssh-client\
+ --no-install-recommends
+
+ENV GOLANG_VERSION 1.3
+
+RUN curl -sSL http://golang.org/dl/go$GOLANG_VERSION.src.tar.gz \
+ | tar -v -C /usr/src -xz
+WORKDIR /usr/src/go
+
+RUN cd src && ./make.bash --no-clean 2>&1
+
+ENV PATH /usr/src/go/bin:$PATH
+
+RUN mkdir -p /go/src
+ENV GOPATH /go
+ENV PATH /go/bin:$PATH
+WORKDIR /go
+
+# ----------------------------------------------------------------------
+
+
+
+ENV GOGS_PATH $GOPATH/src/github.com/gogits/gogs
+ENV GOGS_CUSTOM_CONF_PATH $GOGS_PATH/custom/conf
+ENV GOGS_CUSTOM_CONF $GOGS_CUSTOM_CONF_PATH/app.ini
+
+RUN go get -u -d github.com/gogits/gogs
+# WORKDIR $GOGS_PATH
+WORKDIR /go/src/github.com/gogits/gogs
+RUN go build github.com/gogits/gogs
+
+RUN useradd git
+RUN chown -R git $GOGS_PATH
+
+ADD misc/start.sh /tmp/
+RUN chmod +x /tmp/start.sh
+
+ADD misc/init_storage.sh /tmp/
+RUN chmod +x /tmp/init_storage.sh
+
+ADD init_gogs.sh /tmp/
+RUN chown git /tmp/init_gogs.sh
+RUN chmod +x /tmp/init_gogs.sh
+
+ADD misc/database_settings_merge.py /tmp/
+RUN chown git /tmp/database_settings_merge.py
+
+
+# USER git
+# ENV HOME /home/git
+# ENV USER git
+# ENV PATH $GOGS_PATH:$PATH
+
+ENTRYPOINT []
+CMD ["/tmp/start.sh"]
+#CMD ["/tmp/start.sh"]
+#ENTRYPOINT ["/tmp/init_gogs.sh"]
+# CMD ["/tmp/init_storage.sh"]
+# CMD ["gogs", "web"]
diff --git a/docker/blocks/docker_gogs_persistent/misc/database_settings_merge.py b/docker/blocks/docker_gogs_persistent/misc/database_settings_merge.py
new file mode 100644
index 0000000..73bf685
--- /dev/null
+++ b/docker/blocks/docker_gogs_persistent/misc/database_settings_merge.py
@@ -0,0 +1,75 @@
+#!/bin/env python
+
+import sys
+import ConfigParser
+
+if (len(sys.argv) < 3):
+ print('Usage: {} database target'.format(sys.argv[0]))
+ quit(1)
+
+database_settings_path = sys.argv[1]
+target_settings_path = sys.argv[2]
+
+# Prepare for config parser
+class FakeSecHead(object):
+ def __init__(self, fp):
+ self.fp = fp
+ self.sechead = '[dummy]\n'
+
+ def readline(self):
+ if self.sechead:
+ try:
+ return self.sechead
+ finally:
+ self.sechead = None
+ else:
+ return self.fp.readline()
+
+
+db_conf = ConfigParser.SafeConfigParser()
+db_conf.optionxform = str
+target_conf = ConfigParser.SafeConfigParser()
+target_conf.optionxform = str
+
+db_conf.read(database_settings_path)
+target_conf.readfp(FakeSecHead(open(target_settings_path)))
+
+def get_section(conf, section):
+ dic = {}
+ for option in conf.options(section):
+ dic[option] = conf.get(section, option)
+ return dic
+
+def take_diff(la, lb):
+ set_ab = set(la) - set(lb)
+ return list(set_ab)
+
+def overwrite(dest_dict, from_dict):
+ for k, v in from_dict.items():
+ dest_dict[k] = v
+ return dest_dict
+
+def add_options(conf, section, options):
+ if not conf.has_section(section):
+ conf.add_section(section)
+ for k, v in options.items():
+ conf.set(section, k, v)
+
+db_conf_db = get_section(db_conf, 'database')
+target_conf_db = get_section(target_conf, 'database')
+merged_conf_db = overwrite(target_conf_db, db_conf_db)
+
+target_conf.remove_section('database')
+add_options(target_conf, 'database', merged_conf_db)
+
+with open(target_settings_path, 'wb') as configfile:
+ target_conf.write(configfile)
+
+with open(target_settings_path, 'r') as f:
+ settings_string = f.read()
+
+final_settings_string = settings_string.replace('[dummy]\n','')
+
+with open(target_settings_path, 'w') as f:
+ f.write(final_settings_string)
+
diff --git a/docker/blocks/docker_gogs_persistent/misc/init_storage.sh b/docker/blocks/docker_gogs_persistent/misc/init_storage.sh
new file mode 100644
index 0000000..7c7e7ec
--- /dev/null
+++ b/docker/blocks/docker_gogs_persistent/misc/init_storage.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+create_dir() {
+ dir_path=$1
+ [ ! -d $dir_path ] && mkdir -p $dir_path && echo "create $dir_path"
+ return $?
+}
+
+for d in /data/gogs/data /data/gogs/conf /data/gogs/custom /data/gogs/log /data/git
+do
+ create_dir $d
+done
+
+# This script must be executed on gogs dir
+ln -sf /data/git /home/git
+ln -sf /data/gogs/data $GOGS_PATH/data
+ln -sf /data/gogs/log $GOGS_PATH/log
+ln -sf /data/gogs/conf $GOGS_PATH/conf
+ln -sf /data/gogs/custom $GOGS_PATH/custom
+
+[ ! -d ~git/.ssh ] && mkdir ~git/.ssh && chown git:git ~git/.ssh && chmod 700 ~git/.ssh
+if [ ! -f ~git/.ssh/environment ]; then
+ echo "GOGS_CUSTOM=/data/gogs" > ~git/.ssh/environment
+ chown git:git ~git/.ssh/environment
+ chmod 600 ~git/.ssh/environment
+fi
+
+chown -R git:git /data $GOGS_PATH
+
+
diff --git a/docker/blocks/docker_gogs_persistent/misc/start.sh b/docker/blocks/docker_gogs_persistent/misc/start.sh
new file mode 100644
index 0000000..db997f7
--- /dev/null
+++ b/docker/blocks/docker_gogs_persistent/misc/start.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+/tmp/init_storage.sh
+
+exec su git -c "/tmp/init_gogs.sh; cd $GOGS_PATH; ./gogs web"
diff --git a/docker/blocks/gogs_storage/fig b/docker/blocks/gogs_storage/fig
new file mode 100644
index 0000000..e374dad
--- /dev/null
+++ b/docker/blocks/gogs_storage/fig
@@ -0,0 +1,2 @@
+gogsstorage:
+ build: container
diff --git a/docker/blocks/w_db_persistent/fig b/docker/blocks/w_db_persistent/fig
new file mode 100644
index 0000000..cf25499
--- /dev/null
+++ b/docker/blocks/w_db_persistent/fig
@@ -0,0 +1,8 @@
+gogs:
+ build: docker
+ links:
+ - db
+ ports:
+ - "3000:3000"
+ volumes_from:
+ - gogsstorage
diff --git a/docker/container/Dockerfile b/docker/container/Dockerfile
new file mode 100644
index 0000000..122dac7
--- /dev/null
+++ b/docker/container/Dockerfile
@@ -0,0 +1,5 @@
+FROM busybox
+RUN mkdir -p /data
+RUN echo "Start gogs container!"
+VOLUME /data
+CMD /bin/sh
diff --git a/docker/templates/init_gogs_docker_gogs_persistent.sh.tpl b/docker/templates/init_gogs_docker_gogs_persistent.sh.tpl
new file mode 100644
index 0000000..19c7061
--- /dev/null
+++ b/docker/templates/init_gogs_docker_gogs_persistent.sh.tpl
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+CONNECTION_CONF=/tmp/mysql.conf
+
+echo $(pwd)
+if [ ! -d "$GOGS_CUSTOM_CONF_PATH" ]; then
+ mkdir -p $GOGS_CUSTOM_CONF_PATH
+fi
+
+echo "
+{{ CONFIG }}
+" > $CONNECTION_CONF
+
+if [ -f $GOGS_CUSTOM_CONF ]; then
+ python /tmp/database_settings_merge.py $CONNECTION_CONF $GOGS_CUSTOM_CONF
+else
+ mv $CONNECTION_CONF $GOGS_CUSTOM_CONF
+fi
+
+git config --global user.name "GoGS" && git config --global user.email "[email protected]"
+
+exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment