Skip to content

Commit

Permalink
WIP #17 Rultor Simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
tannakartikey committed Nov 21, 2017
1 parent e1e4926 commit e3e0463
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bin/rultor
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require 'rultor'
require 'slop'
require 'rultor/version'
require 'rultor/encrypt'
require 'rultor/deploy'

Slop.parse(ARGV, strict: true, help: true) do
banner "Usage (#{Rultor::VERSION}): rultor command [options]"
Expand All @@ -48,4 +49,12 @@ Slop.parse(ARGV, strict: true, help: true) do
Rultor::Encrypt.new(opts[:project], args.first).run
end
end

command 'deploy' do
description 'Starts deploy process'
on :deploy
run do |opts, args|
Rultor::Deploy.run
end
end
end
31 changes: 31 additions & 0 deletions entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
set -x
set -e
set -o pipefail
mkdir -p ~/.ssh
echo -e "Host github.com\n\tStrictHostKeyChecking no\n" > ~/.ssh/config
chmod 600 ~/.ssh/config
git clone "${head}" repo
cd repo
git config user.email "[email protected]"
git config user.name "rultor"
if [ "${as_root}" = "true" ]; then
mkdir /home/r
cp -R ./* /home/r
rm -rf repo
chmod a+x /home/r/script.sh
/home/r/script.sh
mv /home/r/repo .
else
shopt -s dotglob
useradd -m -G sudo r
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
cp -R /root/* /home/r
cp -R ./* /home/r
rm -rf repo
chown -R r:r /home/r
chmod a+x /home/r/script.sh
su --login r --command /home/r/script.sh
mv /home/r/repo .
chown -R `whoami` repo
fi
57 changes: 57 additions & 0 deletions lib/rultor/deploy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'shellwords'
require 'English'
require 'yaml'

module Rultor
class Deploy
def self.run
create_script_sh
rultor_yml = File.join(Dir.pwd, '.rultor.yml')
@config = YAML.load_file(rultor_yml)
execute(
"
docker run -it --rm -v `pwd`:/main -w='/main' #{self.env} --name=rultor-remote #{@config["docker"]["image"]} /main/entry.sh
"
)
end

private

def self.execute(command)
puts 'Executing command:'
puts command
system(command)
end

def self.create_script_sh
system(
"
cat <<EOT > script.sh
#!/bin/bash
set -x
set -e
set -o pipefail
shopt -s expand_aliases
alias 'sudo=sudo -i'
export HOME=/home/r
cd \$HOME/repo
EOT
echo '${scripts[@]}' >> script.sh
"
)
end

def self.env
str = ""
variables = @config["env"]
variables.merge!({
head: (%x[ git remote get-url origin ]).strip,
head_branch: "master"
})
variables.each do |key, value|
str += "--env=#{key}=#{value} "
end
str
end
end
end

0 comments on commit e3e0463

Please sign in to comment.