work in progress docker
authorSam <sam.saffron@gmail.com>
Wed, 30 Oct 2013 00:48:17 +0000 (11:48 +1100)
committerSam <sam.saffron@gmail.com>
Wed, 30 Oct 2013 00:48:17 +0000 (11:48 +1100)
cids/.gitkeep [new file with mode: 0644]
image/Dockerfile [new file with mode: 0644]
launcher [new file with mode: 0755]
shared/bootloader.rb [new file with mode: 0644]
shared/config/web1/conf.yml [new file with mode: 0644]

diff --git a/cids/.gitkeep b/cids/.gitkeep
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/image/Dockerfile b/image/Dockerfile
new file mode 100644 (file)
index 0000000..8f0b433
--- /dev/null
@@ -0,0 +1,57 @@
+# DOCKER-VERSION 0.6.4
+
+# 13.10 - 04 has a missing ppa for postgresql 9.2 at the moment (26/10/2013)
+FROM ubuntu:12.10
+
+MAINTAINER Sam Saffron "https://twitter.com/samsaffron"
+
+RUN apt-get -y update &&\
+    apt-get -y upgrade &&\
+    apt-get -y install software-properties-common &&\
+    add-apt-repository -y ppa:rwky/redis &&\
+    add-apt-repository -y ppa:nginx/stable &&\
+    add-apt-repository -y ppa:pitti/postgresql &&\
+    add-apt-repository ppa:chris-lea/node.js &&\
+    apt-get -y update &&\
+    apt-get install -y build-essential git curl libxml2-dev \
+                    libxslt-dev libcurl4-openssl-dev \
+                    libssl-dev libyaml-dev libtool \
+                    libxslt-dev libxml2-dev gawk curl \
+                    pngcrush imagemagick \
+                    postgresql-9.2 postgresql-client-9.2 \
+                    postgresql-contrib-9.2 libpq-dev libreadline-dev \
+                    nginx &&\
+    dpkg-divert --local --rename --add /sbin/initctl &&\
+    ln -s /bin/true /sbin/initctl &&\
+    apt-get install -y redis-server &&\
+    apt-get install -y haproxy openssh-server &&\
+    mkdir /src && cd /src &&\
+    git clone https://github.com/sstephenson/ruby-build.git &&\
+    cd ruby-build && \
+    /src/ruby-build/install.sh &&\
+    cd / &&\
+    rm -rf /src/ruby-build &&\
+    ruby-build 2.0.0-p247 /usr/local &&\
+    gem update --system &&\
+    gem install bundler &&\
+    apt-get -y install sudo &&\
+    useradd discourse &&\
+    mkdir /var/www &&\
+    cd /var/www &&\
+    git clone https://github.com/discourse/discourse.git &&\
+    cd /var/www/discourse &&\
+    RAILS4=1 bundle install &&\
+    cp /var/www/discourse/config/redis.yml.sample /var/www/discourse/config/redis.yml &&\
+    cp /var/www/discourse/config/environments/production.rb.sample /var/www/discourse/config/environments/production.rb
+
+
+
+
+# RUN cd /var/www/discourse && bundle exec rake db:test:prepare && bundle exec rspec
+
+# mkdir -p /src/godeb && cd /src/godeb && curl -O https://godeb.s3.amazonaws.com/godeb-amd64.tar.gz &&\
+# cd /src/godeb && tar -xzvf godeb-amd64.tar.gz &&\
+# cd /src/godeb && ./godeb install &&\
+# cd /src &&  git clone https://github.com/coreos/etcd.git &&\
+# cd /src/etcd && ./build &&\
+# cp /src/etcd/etcd /usr/local/bin &&\
diff --git a/launcher b/launcher
new file mode 100755 (executable)
index 0000000..27cfc4e
--- /dev/null
+++ b/launcher
@@ -0,0 +1,96 @@
+#/bin/bash
+
+command=$1
+config=$2
+config_file=shared/config/"$config"/conf.yml
+cidfile=cids/"$config".cid
+
+image=samsaffron/discourse
+
+[ $# -ne 2 ] && {
+  echo "Usage: launcher COMMAND CONFIG"
+  echo "Commands:"
+  echo "    start:    Start/initialize a container"
+  echo "    stop:     Stop a running container"
+  echo "    restart:  Restart a container"
+  echo "    destroy:  Stop and remove a container"
+  echo "    shell:    start a bash shell in a running container"
+  echo "    logs:     Docker logs for container"
+  exit 1
+}
+
+if [ ! -e $config_file ]
+  then
+    echo "Config file was not found, ensure $config_file exists"
+    exit 1
+fi
+
+if [ "$command" == "shell" ]
+  then
+
+    if [ ! -e $cidfile ]
+       then
+         echo "No cid found"
+         exit 1
+       else
+         docker attach `cat $cidfile`
+         exit 0
+    fi
+fi
+
+if [ "$command" == "stop" ]
+  then
+
+    if [ ! -e $cidfile ]
+       then
+         echo "No cid found"
+         exit 1
+       else
+         docker stop -t 10 `cat $cidfile`
+         exit 0
+    fi
+fi
+
+if [ "$command" == "logs" ]
+  then
+
+    if [ ! -e $cidfile ]
+       then
+         echo "No cid found"
+         exit 1
+       else
+         docker logs `cat $cidfile`
+         exit 0
+    fi
+fi
+
+if [ "$command" == "start" ]
+  then
+
+    if [ ! -e $cidfile ]
+       then
+         echo "No cid found, creating a new container"
+#docker run -cidfile $cidfile -p 22 -d -v `pwd`/shared:/shared samsaffron/discourse /usr/local/bin/ruby /shared/bootloader.rb
+         docker run -d samsaffron/discourse /usr/local/bin/ruby -e 'sleep 100'
+         exit 0
+       else
+         echo "cid found, ensuring container is started"
+         docker start `cat $cidfile`
+         exit 0
+    fi
+fi
+
+
+if [ "$command" == "destroy" ]
+  then
+    if [ -e $cidfile ]
+      then
+        echo "destroying container $cidfile"
+        docker stop -t 10 `cat $cidfile`
+        docker rm `cat $cidfile` && rm $cidfile
+        exit 0
+      else
+        echo "nothing to destroy cidfile does not exist"
+        exit 1
+    fi
+fi
diff --git a/shared/bootloader.rb b/shared/bootloader.rb
new file mode 100644 (file)
index 0000000..eed2688
--- /dev/null
@@ -0,0 +1,155 @@
+# trap "SIGHUP" do
+#   STDERR.puts "Trapped SIGHUP"
+#   STDERR.flush
+#   exit
+# end
+# 
+# trap "SIGINT" do
+#   STDERR.puts "Trapped SIGINT"
+#   STDERR.flush
+#   exit
+# end
+# 
+# trap "SIGTERM" do
+#   STDERR.puts "Trapped SIGTERM"
+#   STDERR.flush
+#   exit
+# end
+
+STDERR.puts "Started #{Process.pid}"
+STDERR.puts `ps aux`
+
+while true
+  gets
+    STDERR.puts "HERE"
+    sleep 1
+end
+
+STDERR.puts "HERE"
+
+exit
+
+require 'yaml'
+require 'open3'
+require 'readline'
+
+@host = ARGV[0]
+
+STDERR.puts "Started bootloader for #{@host} at: #{Time.now}"
+
+module Discourse; end
+
+class Discourse::Config
+  def initialize(config, discourse_root=nil)
+    @config = YAML.load_file(config)
+    @discourse_root = discourse_root || "/var/www/discourse"
+  end
+
+  def startup
+    load_env
+    ensure_git_version
+    ensure_database_config
+    start_roles
+  end
+
+  def load_env
+    @config["env"].each do |k,v|
+      ENV[k.to_s] = v.to_s
+    end if @config["env"]
+  end
+
+  def ensure_database_config
+    current = YAML.load_file("#{@discourse_root}/config/database.yml.production-sample")
+    current = current.merge(@config["databases"])
+    File.open("#{@discourse_root}/config/database.yml", "w"){|f| f.write current.to_yaml }
+  end
+
+  def ensure_git_version
+    STDERR.puts `cd #{@discourse_root} && git pull`
+  end
+
+  def start_roles
+    @config["roles"].each do |role|
+      case role
+      when "unicorn"
+        start_unicorn
+      end
+    end
+  end
+
+  def start_unicorn
+    STDERR.puts `cd #{@discourse_root} && RAILS_ENV=production bundle exec rake db:migrate`
+  end
+
+end
+
+class Discourse::Process
+  def self.pids
+    @@pids ||= []
+  end
+
+  def self.spawn(*args)
+    pid = Process.spawn(*args)
+    STDERR.puts "Spawned #{args.inspect} pid: #{pid}"
+    pids << pid
+
+    Thread.start do
+      Process.wait(pid)
+      pids.delete(pid)
+    end
+
+    pid
+  end
+
+  def spawn(*args)
+    self.class.spawn(*args)
+  end
+
+  # trap "HUP" do
+  #   STDERR.puts "Trapped SIGHUP"
+  # end
+
+  # trap "INT" do
+  #   STDERR.puts "Trapped SIGINT"
+  # end
+
+  # trap "TERM" do
+  #   STDERR.puts "Trapped SIGTERM"
+  #   pids.dup.each do |pid|
+  #     STDERR.puts "Sending TERM to #{pid}"
+  #     # no such process
+  #     Process.kill("TERM", pid) rescue nil
+  #   end
+
+  #   pids.dup.each do |pid|
+  #     # no such process
+  #     Process.wait(pid) rescue nil
+  #   end
+
+  #   STDERR.puts "Exiting"
+  #   exit 1
+  # end
+end
+
+class Discourse::Postgres
+  attr_accessor :data_dir
+  def start
+  end
+end
+
+class Discourse::Sshd < Discourse::Process
+  def start
+    # we need this dir to run sshd
+    `mkdir /var/run/sshd` unless File.directory? "/var/run/sshd"
+    spawn("/usr/sbin/sshd")
+  end
+end
+
+# tmp = "/home/sam/Source/discourse_docker/shared/config/web1/conf.yml"
+tmp = "/shared/config/web1/conf.yml"
+conf = Discourse::Config.new(tmp)
+#conf.startup
+
+
+#Discourse::Sshd.new.start
+
diff --git a/shared/config/web1/conf.yml b/shared/config/web1/conf.yml
new file mode 100644 (file)
index 0000000..14f34a2
--- /dev/null
@@ -0,0 +1,24 @@
+roles:
+  - unicorn
+  - nginx
+  - sidekiq
+
+databases:
+  production:
+    name: play
+    username: discourse
+    password: password
+    host: l.discourse
+    host_names:
+      - dev1.discourse.org
+
+env:
+  DEVELOPER_EMAIL: 'sam.saffron@gmail.com'
+  DISCOURSE_HOSTNAME: 'dev.samsaffron.com'
+  REDIS_PROVIDER_URL: 'redis://l.discourse:6379'
+  RAILS_ENV: 'production'
+  RAILS4: 1
+
+version: latest
+
+