Skip to content

Commit

Permalink
refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
whummer committed Jul 7, 2014
1 parent 9356e13 commit 72c3316
Show file tree
Hide file tree
Showing 49 changed files with 277 additions and 2,548 deletions.
44 changes: 44 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
source 'https://rubygems.org'

# RUBY ON RAILS DEPENDENCIES

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails' #, '4.0.2'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails' #, '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier' #, '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails' #, '~> 4.0.0'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder' #, '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
# Authentication module
gem 'devise'
# Get rid of some rails warnings
gem 'thin'

# TOASTER DEPENDENCIES
gem 'hashdiff' # diff hashes
gem 'json'
gem 'jsonpath'
gem 'open4' # open processes with stdin/stdout
gem 'chef'
gem 'ohai'
gem 'rspec' # tests
gem 'ruby_parser' # parse Ruby code
gem 'bson_ext'
gem 'tidy' # tidy XML library
gem 'diffy' # comparing source files
gem 'mysql2' # for DB access
gem 'therubyracer' # required by execjs
gem 'railties'
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.1.2
2 changes: 1 addition & 1 deletion chef/cookbooks/lxc/attributes/start_lxc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
include_attribute "lxc::general"

default["lxc"]["cont"]["use_proxy"] = true
default["lxc"]["cont"]["proxy_ip"] = "192.168.100.2"
default["lxc"]["cont"]["proxy_ip"] = node["network"]["gateway"]
default["lxc"]["cont"]["name"] = "lxc1"
default["lxc"]["cont"]["ip_address"] = nil
default["lxc"]["cont"]["root_path"] = "#{node["lxc"]["root_path"]}/#{node["lxc"]["cont"]["name"]}"
Expand Down
2 changes: 2 additions & 0 deletions chef/cookbooks/lxc/files/install.chef.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,7 @@ fi
# update gem system to avoid incompatibilities later on, e.g.:
# http://efreedom.net/Question/1-15266444/Unable-Install-Bson-Ext-182-Gem
gem update --system
gem update
gem clean


45 changes: 24 additions & 21 deletions chef/cookbooks/lxc/recipes/create_lxc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@

bash "lxc_check_existence" do
code <<-EOH
echo "LXC configuration '/lxc/#{node["lxc"]["cont"]["name"]}/config' already exists."
echo "LXC configuration '#{node["lxc"]["root_path"]}/#{node["lxc"]["cont"]["name"]}/config' already exists."
echo "Please provide a different container name or use recipe lxc::start_lxc"
exit 1
EOH
only_if "test -f /lxc/#{node["lxc"]["cont"]["name"]}/config"
only_if "test -f #{node["lxc"]["root_path"]}/#{node["lxc"]["cont"]["name"]}/config"
end

bash "lxc_prepare_rootdir" do
if node["lxc"]["use_copy_on_write"]
code <<-EOH
name=#{node["lxc"]["cont"]["name"]}
prototype_name=#{node["lxc"]["proto"]["name"]}
rm -rf /lxc/$name
/sbin/btrfs subvolume snapshot /lxc/$prototype_name /lxc/$name
rm -rf #{node["lxc"]["root_path"]}/$name
/sbin/btrfs subvolume snapshot #{node["lxc"]["root_path"]}/$prototype_name #{node["lxc"]["root_path"]}/$name
EOH
else
code <<-EOH
name=#{node["lxc"]["cont"]["name"]}
prototype_name=#{node["lxc"]["proto"]["name"]}
mkdir -p /lxc/$name
cp /lxc/$prototype_name/config /lxc/$name/config
mkdir -p #{node["lxc"]["root_path"]}/$name
cp #{node["lxc"]["root_path"]}/$prototype_name/config #{node["lxc"]["root_path"]}/$name/config
EOH
end
end
Expand All @@ -39,8 +39,9 @@
code <<-EOH
name=#{node["lxc"]["cont"]["name"]}
prototype_name=#{node["lxc"]["proto"]["name"]}
echo "INFO: Copying container root directory from /lxc/$prototype_name to /lxc/$name"
cp -r /lxc/$prototype_name/* /lxc/$name/
root_path=#{node["lxc"]["root_path"]}
echo "INFO: Copying container root directory from $root_path/$prototype_name to $root_path/$name"
cp -r $root_path/$prototype_name/* $root_path/$name/
EOH
end
# don't execute if we use docker.io tools
Expand All @@ -51,33 +52,35 @@
code <<-EOH
name=#{node["lxc"]["cont"]["name"]}
prototype_name=#{node["lxc"]["proto"]["name"]}
root_path=#{node["lxc"]["root_path"]}
# adjust values in config files
sed -i "s|lxc.utsname = $prototype_name|lxc.utsname = $name|g" /lxc/$name/config
sed -i "s|lxc.rootfs = /lxc/[/]*$prototype_name/[/]*rootfs|lxc.rootfs = /lxc/$name/rootfs|g" /lxc/$name/config
sed -i "s|lxc.mount = /lxc/[/]*$prototype_name/[/]*fstab|lxc.mount = /lxc/$name/fstab|g" /lxc/$name/config
sed -i "s|lxc.network.ipv4 = .*\\$|lxc.network.ipv4 = #{node["lxc"]["cont"]["ip_address"]}|g" /lxc/$name/config
if [ -f /lxc/$name/fstab ]; then
sed -i "s|/lxc/[/]*$prototype_name/|/lxc/$name/|g" /lxc/$name/fstab
sed -i "s|lxc.utsname = $prototype_name|lxc.utsname = $name|g" $root_path/$name/config
sed -i "s|lxc.rootfs = $root_path/[/]*$prototype_name/[/]*rootfs|lxc.rootfs = $root_path/$name/rootfs|g" $root_path/$name/config
sed -i "s|lxc.mount = $root_path/[/]*$prototype_name/[/]*fstab|lxc.mount = $root_path/$name/fstab|g" $root_path/$name/config
sed -i "s|lxc.network.ipv4 = .*\\$|lxc.network.ipv4 = #{node["lxc"]["cont"]["ip_address"]}|g" $root_path/$name/config
if [ -f $root_path/$name/fstab ]; then
sed -i "s|$root_path/[/]*$prototype_name/|$root_path/$name/|g" $root_path/$name/fstab
fi
# create a file which contains the container's prototype name
echo "$prototype_name" > /lxc/$name/container.prototype.name
echo "$prototype_name" > #{node["lxc"]["root_path"]}/$name/container.prototype.name
EOH
end

bash "lxc_fix_network_config" do
code <<-EOH
name=#{node["lxc"]["cont"]["name"]}
root_path=#{node["lxc"]["root_path"]}
# turn /etc/resolv.conf symlink to an actual file
cp /lxc/$name/rootfs/etc/resolv.conf /lxc/$name/rootfs/etc/resolv.conf.bak
rm /lxc/$name/rootfs/etc/resolv.conf
cp /lxc/$name/rootfs/etc/resolv.conf.bak /lxc/$name/rootfs/etc/resolv.conf
cp $root_path/$name/rootfs/etc/resolv.conf $root_path/$name/rootfs/etc/resolv.conf.bak
rm $root_path/$name/rootfs/etc/resolv.conf
cp $root_path/$name/rootfs/etc/resolv.conf.bak $root_path/$name/rootfs/etc/resolv.conf
# fix hostname files
echo "$name" > /lxc/$name/rootfs/etc/hostname
echo "127.0.0.1 $name $name" > /lxc/$name/rootfs/etc/hosts
echo "$name" > #{node["lxc"]["root_path"]}/$name/rootfs/etc/hostname
echo "127.0.0.1 $name $name" > #{node["lxc"]["root_path"]}/$name/rootfs/etc/hosts
EOH
# don't execute if we use docker.io tools
Expand All @@ -92,7 +95,7 @@
name=#{node["lxc"]["cont"]["name"]}
# make /tmp directory writable to everyone
chmod 777 /lxc/$name/rootfs/tmp
chmod 777 #{node["lxc"]["root_path"]}/$name/rootfs/tmp
EOH
end

Expand Down
68 changes: 41 additions & 27 deletions chef/cookbooks/lxc/recipes/init_proto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
cache="#{node["lxc"]["bare_os"]["cachedir"]}"

# include code in Ruby LOAD_PATH
code_dir = File.join(__FILE__, "..","..","..","..", "lib")
code_dir = File.join(File.dirname(__FILE__), "..","..","..","..", "lib")
$:.unshift(code_dir)
require 'toaster/util/config'

Expand All @@ -24,10 +24,10 @@

bash "proto_init_cow" do
code <<-EOH
btrfs subvolume create /lxc/#{node["lxc"]["proto"]["name"]}
btrfs subvolume create #{node["lxc"]["root_path"]}/#{node["lxc"]["proto"]["name"]}
EOH
only_if do node["lxc"]["use_copy_on_write"] end
not_if "test -f /lxc/#{node["lxc"]["proto"]["name"]}"
not_if "test -f #{node["lxc"]["root_path"]}/#{node["lxc"]["proto"]["name"]}"
end

bash "proto_copy_os" do
Expand Down Expand Up @@ -165,38 +165,41 @@
not_if do node["lxc"]["use_docker.io"] end
end


bash "proto_docker_create" do
if node["lxc"]["bare_os"]["distribution"] == "ubuntu"
code <<-EOH
proto_name=#{node["lxc"]["proto"]["name"]}
root_path=#{node["lxc"]["root_path"]}
mkdir -p #{node["lxc"]["proto"]["root_path"]}
cat <<EOF > #{node["lxc"]["proto"]["root_path"]}/Dockerfile
FROM ubuntu
RUN mkdir /var/run/sshd
RUN grep -v rootfs /proc/mounts > /etc/mtab
#RUN ip addr add #{node["lxc"]["proto"]["ip_address"]}/24 dev eth0
#RUN bash -c 'echo "iface eth0 inet static" >> /etc/network/interfaces'
#RUN bash -c 'echo "address #{node["lxc"]["proto"]["ip_address"]}" >> /etc/network/interfaces'
#RUN bash -c 'echo "gateway #{node["network"]["gateway"]}" >> /etc/network/interfaces'
#RUN bash -c 'echo "netmask 255.255.255.0" >> /etc/network/interfaces'
RUN bash -c "ip addr add #{node["lxc"]["proto"]["ip_address"]}/24 dev eth0; ip route del default; ip route add default via #{node["network"]["gateway"]}; apt-get install -y net-tools openssh-server iptables dnsutils iputils-ping vim"
# (auto-)start ssh daemon
RUN update-rc.d ssh defaults
ADD dockerfiles/.ssh/ /root/
#RUN grep -v rootfs /proc/mounts > /etc/mtab
#RUN bash -c "ip addr flush dev eth0; ip addr add #{node["lxc"]["proto"]["ip_address"]}/24 dev eth0; ip route del default; ip route add default via #{node["network"]["gateway"]}; apt-get install -y net-tools openssh-server iptables dnsutils iputils-ping vim"
# add ssh dir
RUN mkdir -p /root/.ssh
EOF
proto_name=#{node["lxc"]["proto"]["name"]}
mkdir -p /lxc/$proto_name/dockerfiles/.ssh/
cp $HOME/.ssh/id_rsa.pub /lxc/$proto_name/dockerfiles/.ssh/authorized_keys
mkdir -p #{node["lxc"]["root_path"]}/$proto_name/dockerfiles/.ssh/
if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
ssh-keygen -f $HOME/.ssh/id_rsa.pub -P ""
end
fi
cp $HOME/.ssh/id_rsa.pub #{node["lxc"]["root_path"]}/$proto_name/dockerfiles/.ssh/authorized_keys
imgID=`docker build -t prototypes:$proto_name #{node["lxc"]["proto"]["root_path"]} | grep "Successfully built" | tail -n 1 | sed "s/Successfully built //g"`
echo "INFO: new docker image ID: '$imgID'"
if [ "$imgID" == "" ]; then
echo "WARN: Docker image creation unsuccessful..."
docker build #{node["lxc"]["proto"]["root_path"]}
exit 1
fi
echo "$imgID" > /lxc/$proto_name/docker.image.id
echo "$imgID" > #{node["lxc"]["root_path"]}/$proto_name/docker.image.id
cidfile=$root_path/$proto_name/docker.container.id
rm -f $cidfile
# the following commands can only be run in "privileged" docker mode:
docker run --privileged --cidfile=$cidfile $imgID bash -c "ip addr flush dev eth0; ip addr add #{node["lxc"]["proto"]["ip_address"]}/24 dev eth0; ip route del default; ip route add default via #{node["network"]["gateway"]}; apt-get update; apt-get install -y net-tools openssh-server iptables dnsutils iputils-ping vim; update-rc.d ssh defaults"
EOH
else
code <<-EOH
Expand All @@ -208,6 +211,15 @@
only_if do node["lxc"]["use_docker.io"] end
end

# terminate prototype container
# (lxc::stop_lxc also saves/commits the changes made to the prototype container)
node.set["lxc"]["cont"]["name"] = node["lxc"]["proto"]["name"]
node.set["lxc"]["cont"]["ip_address"] = node["lxc"]["proto"]["ip_address"]
node.set["lxc"]["cont"]["root_path"] = "#{node["lxc"]["root_path"]}/#{node["lxc"]["cont"]["name"]}"
node.set["lxc"]["cont"]["config_file"] = "#{node["lxc"]["cont"]["root_path"]}/config"
node.set["lxc"]["cont"]["root_fs"] = node["lxc"]["proto"]["root_fs"]
include_recipe "lxc::stop_lxc"

bash "proto_cp_resolv_conf" do
code <<-EOH
# cp resolv.conf from host to LXC container
Expand Down Expand Up @@ -252,12 +264,6 @@
end

# start prototype container
node.set["lxc"]["cont"]["name"] = node["lxc"]["proto"]["name"]
node.set["lxc"]["cont"]["ip_address"] = node["lxc"]["proto"]["ip_address"]
node.set["lxc"]["cont"]["root_path"] = "#{node["lxc"]["root_path"]}/#{node["lxc"]["cont"]["name"]}"
node.set["lxc"]["cont"]["config_file"] = "#{node["lxc"]["cont"]["root_path"]}/config"
node.set["lxc"]["cont"]["root_fs"] = node["lxc"]["proto"]["root_fs"]

include_recipe "lxc::start_lxc"

# make sure /tmp directory is preserved over time (365 days) and not flushed on every boot
Expand All @@ -284,7 +290,9 @@
#!/bin/bash
# make sure we have a default route
route add default gw #{node["network"]["gateway"]}
ip route del default
ip route add default via #{node["network"]["gateway"]}
#route add default gw #{node["network"]["gateway"]}
# sometimes the DNS is not immediately available and lookup of google.com fails
for i in {1..10}; do
Expand Down Expand Up @@ -331,9 +339,10 @@
gem install --no-ri --no-rdoc chef
fi
# install prerequisite for gem mysql2, needed by cloud-toaster
sudo apt-get install -y libmysqlclient-dev
# install toaster gem
#wget #{Toaster::Config.get("testing.gem_url")} -O /tmp/toaster.gem
#gem install --no-ri --no-rdoc /tmp/toaster.gem
gem install --no-ri --no-rdoc cloud-toaster
EOH
Expand Down Expand Up @@ -400,4 +409,9 @@

# terminate prototype container
# (lxc::stop_lxc also saves/commits the changes made to the prototype container)
include_recipe "lxc::stop_lxc"
bash 'commit_docker_final' do
code 'echo'
notifies :run, 'bash[lxc_docker_commit]', :immediately
notifies :run, 'bash[lxc_stop]', :immediately
notifies :run, 'bash[lxc_stop_proto]', :immediately
end
4 changes: 2 additions & 2 deletions chef/cookbooks/lxc/recipes/install_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
bash "ruby_install_gems" do
code <<-EOH
# install common gems
gem install rspec
gem install rspec -v 1.3.2 # required for, e.g., cassandra installation
#gem install rspec
#gem install rspec -v 1.3.2 # required for, e.g., cassandra installation
EOH
not_if "gem list | grep rspec"
end
Expand Down
1 change: 1 addition & 0 deletions chef/cookbooks/lxc/recipes/setup_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

bash 'db_create' do
code "echo 'create database toaster;' | mysql -u root -p#{node['mysql']['server_root_password']}"
not_if "echo \"show databases;\" | mysql -u root -p#{node['mysql']['server_root_password']} | grep toaster"
end

else
Expand Down
4 changes: 2 additions & 2 deletions chef/cookbooks/lxc/recipes/setup_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@
code <<-EOH
mkdir -p /mnt/btrfs
mount -t btrfs -o loop #{node["lxc"]["host"]["btrfs_img_path"]} /mnt/btrfs
if [ ! -e /lxc ]; then
ln -s /mnt/btrfs /lxc
if [ ! -e #{node["lxc"]["root_path"]} ]; then
ln -s /mnt/btrfs #{node["lxc"]["root_path"]}
fi
EOH
only_if do node["lxc"]["host"]["use_copy_on_write"] end
Expand Down
24 changes: 13 additions & 11 deletions chef/cookbooks/lxc/recipes/start_lxc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,38 @@
proto_name=#{node["lxc"]["proto"]["name"]}
echo "$proto_name" >> /tmp/tmp.lxc.protos
if [ "$proto_name" == "" ]; then
proto_name=`cat /lxc/$name/container.prototype.name`
proto_name=`cat #{node["lxc"]["root_path"]}/$name/container.prototype.name`
fi
echo "$proto_name" >> /tmp/tmp.lxc.protos
if [ "$proto_name" == "" ]; then
proto_name=#{node["lxc"]["cont"]["name"]}
fi
screen -m -d docker run --privileged prototypes:$proto_name bash -c "ifconfig eth0 $ip_addr && route add default gw #{node["network"]["gateway"]} && /usr/sbin/sshd -D"
cidfile=#{node["lxc"]["root_path"]}/$name/docker.container.id
rm -f $cidfile
screen -m -d docker run --cidfile=$cidfile --privileged prototypes:$proto_name bash -c "ifconfig eth0 $ip_addr && route add default gw #{node["network"]["gateway"]} && /usr/sbin/sshd -D"
echo "INFO: LXC container '#{node["lxc"]["cont"]["name"]}' started in the background using 'screen'."
sleep 2
contID=`docker ps | grep -v IMAGE | head -n 1 | awk '{print $1}'`
#contID=`docker ps | grep -v IMAGE | head -n 1 | awk '{print $1}'`
contID=`cat "$cidfile"`
if [ "$contID" == "" ]; then
echo "WARN: Container could not be started. Name '$name', prototype '$proto_name', IP '$ip_addr'"
exit 1
fi
contID=`ls /var/lib/docker/containers/ | grep "$contID"`
echo "$contID" >> /tmp/tmp.lxc.cont.ids
rm -f /lxc/$name/rootfs
#contID=`ls /var/lib/docker/containers/ | grep "$contID"`
echo "$contID" >> /tmp/tmp.lxc.cont.ids
rm -f #{node["lxc"]["root_path"]}/$name/rootfs
# create a symlink to the rootfs folder. This
# differs in different versions of toaster.. :/
# differs in different versions of docker.. :/
if [ -d /var/lib/docker/aufs/mnt/$contID ]; then
ln -s /var/lib/docker/aufs/mnt/$contID /lxc/$name/rootfs
ln -s /var/lib/docker/aufs/mnt/$contID #{node["lxc"]["root_path"]}/$name/rootfs
elif [ -d /var/lib/docker/containers/$contID/rootfs ]; then
ln -s /var/lib/docker/containers/$contID/rootfs /lxc/$name/rootfs
ln -s /var/lib/docker/containers/$contID/rootfs #{node["lxc"]["root_path"]}/$name/rootfs
else
echo "ERROR: Unable to determine container root directory."
exit 1
fi
echo "$contID" > /lxc/$name/docker.container.id
EOH
end
else
Expand Down
Loading

0 comments on commit 72c3316

Please sign in to comment.