Skip to content

Instantly share code, notes, and snippets.

@yuyalush
Created August 28, 2011 01:50
Show Gist options
  • Save yuyalush/1176137 to your computer and use it in GitHub Desktop.
Save yuyalush/1176137 to your computer and use it in GitHub Desktop.
Ruby1.9.2, Rails3, Sqlite3 and Create TestApp for CentOS
#!/usr/bin/env ruby
require 'rubygems'
require 'nifty'
require 'dozens'
require 'net/ssh'
NIFTY_ACCESS_KEY = "-----"
NIFTY_SECRET_KEY = "-----"
DOZENS_API_KEY = "-----"
DOZENS_DZNSID = "-----login_id-----"
DOZENS_DOMAIN_NAME = "--------"
ncs4r = NIFTY::Cloud::Base.new(:access_key => NIFTY_ACCESS_KEY, :secret_key => NIFTY_SECRET_KEY)
#
# patch for dozens's gem (0.0.2)
# fix Symbol to String
#
module Dozens
class API
def request(verb, url, headers = {}, params = {})
uri = URI.parse(url)
klass = eval "Net::HTTP::#{verb.to_s.capitalize}" # fix Symbol to String
req = klass.new(uri.path)
headers.each {|key, value| req[key] = value }
if req.request_body_permitted? && !params.empty?
req.body = params.to_json.to_s
req['Content-Type'] = 'application/json'
end
resp = nil
Net::HTTP.start(uri.host, uri.port) do |http|
resp = http.request(req)
end
JSON.parse(resp.body)
end
end
end
dzns = Dozens::API.new(DOZENS_DZNSID, DOZENS_API_KEY)
dzns.authenticate
#
# for Dozens
#
def check_domain(dzns, domainname)
dzns.zones['domain'].each {|zone| return true if zone['name'] == domainname }
return false
end
def check_record(dzns, domainname, recordname)
dzns.records(domainname)['record'].each do |record|
return record['id'] if record['name'] == recordname + '.' + domainname
end
return false
end
#
# Create images list
#
options_for_describe_images = {
#:executable_by => ["executableUser"],
#:image_id => ["imageId"],
#:image_name => ["imageName"],
#:owner => ["owner"]
}
response = ncs4r.describe_images(options_for_describe_images)
image_list = []
response.imagesSet.item.each do |image|
image_list.push image.imageId + " : " + image.name + "(" + image.imageOwnerAlias + ")"
end
image_list.each{|i| p i }
#
# Input new instance setting
#
puts "Select imageID:"
image_id = gets
puts "Input instance_id:"
instance_id = gets
puts "Input additional info:"
additional_info = gets
#
# create new instances
#
options_for_run_instances = {
:image_id => image_id.strip!,
:key_name => "-----",
:security_group => ["-----"],
:additional_info => additional_info.strip!,
:instance_type => "small",
:monitoring_enabled => true,
:disable_api_termination => false,
:accounting_type => "2",
:instance_id => instance_id.strip!,
:admin => "admin",
:password => "-----",
:ip_type => "dynamic" # or static
}
#
# Send run request
#
startTime = Time.now
p "Request for run instance ..."
response = ncs4r.run_instances(options_for_run_instances)
p "LaunchTime : " + response.instancesSet.item[0].launchTime
p "imageId : " + response.instancesSet.item[0].imageId
p "instanceType : " + response.instancesSet.item[0].instanceType
p "instanceId : " + response.instancesSet.item[0].instanceId
instanceId = response.instancesSet.item[0].instanceId
#
# Check state per 10 sec
#
ip = ""
t = 0
while true
r = ncs4r.describe_instances({ :instance_id => instanceId })
p status = r.reservationSet.item[0].instancesSet.item[0].instanceState.name
ip = r.reservationSet.item[0].instancesSet.item[0].dnsName
if status == "running"
p t.to_s + "sec"
break
else
t += 10
end
sleep 10
end
#
# Start setting shell script
#
p "Start set Server"
sh_address = 'https://raw.github.com/gist/1176137/ad0494644317ca9a138bb203b63bead31205dbe0/setup.sh'
key_path = ["-----"]
passphrase = "-----"
Net::SSH.start(ip, 'root', :keys => key_path, :passphrase => passphrase) do |ssh|
ssh.open_channel do |channel|
channel.exec("curl #{sh_address} | sh") do |ch, success|
raise 'コマンドが実行できません。' unless success
channel.on_data {|ch, data| p data }
channel.on_process do |ch|
channel.eof! if channel.output.empty?
end
channel.on_close do p "done!" end
end
end
ssh.loop
end
#
# create Dozens record
#
p "Regist Dozens"
p Time.now
ttl = '7200'
priority = ''
if check_domain(dzns, DOZENS_DOMAIN_NAME)
recordid = check_record(dzns, DOZENS_DOMAIN_NAME, instanceId)
if recordid
res = dzns.update_record(recordid, { 'prio' => '', 'content' => ip, 'ttl' => ttl})
if res['code'] == '404'
puts "The record could not be updated."
else
puts "The record is updated."
end
else
res = dzns.create_record({ 'domain' => DOZENS_DOMAIN_NAME,
'name' => instanceId,
'type' => 'A',
'prio' => priority,
'content' => ip,
'ttl' => ttl })
if res['code'] == '404'
puts "The record could not be created."
else
puts 'The record is created.'
end
end
else
puts 'There is no such a domain.'
end
#
# Print result
#
p ip
record = Time.now - startTime
p record.to_s + "sec"
rec_min = (record / 60).to_i
rec_sec = record.to_i - (rec_min * 60)
p "#{rec_min.to_s} min #{rec_sec.to_s} sec"
#
# Display
#
exec('open http://' + instanceId +'.co-meeting.com:8080')
#!/bin/sh
echo "START!!!!" > log.txt
date >> log.txt
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
yum -y install gcc kernel-devel zlib-devel openssl-devel readline-devel ncurses-devel gcc-c++ curl-devel sqlite-devel net-snmp libxslt-devel libxml2-devel git
echo "yum finished." >> log.txt
date >> log.txt
#ruby1.9.2
echo "Ruby start." >> log.txt
date >> log.txt
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p290.tar.gz
tar zxvf ruby-1.9.2-p290.tar.gz
cd ruby-1.9.2-p290
./configure
make -j
make install
echo "Ruby finished." >> log.txt
date >> log.txt
#Rails
echo "Rails start" >> log.txt
date >> log.txt
echo "gem: --no-ri --no-rdoc" > ~/.gemrc
cd ..
gem update --system
gem update rake
gem install rails
echo "Rails finished" >> log.txt
date >> log.txt
# Sqlite3
echo "Sqlite3 start." >> log.txt
date >> log.txt
wget http://www.sqlite.org/sqlite-autoconf-3070701.tar.gz
tar zxf sqlite-autoconf-3070701.tar.gz
cd sqlite-autoconf-3070701/
./configure
make -j
make install
cd ..
gem install sqlite3-ruby
gem install sqlite3
echo "Sqlite3 finished." >> log.txt
date >> log.txt
# Check
echo "ruby & rails check" >> log.txt
ruby -v >> log.txt
rails -v >> log.txt
# Test Rails App
rails new testapp --skip-bundle
cd testapp
echo "gem 'therubyracer'" >> Gemfile
echo "gem 'unicorn'" >> Gemfile
bundle install
rails g scaffold Book title:string price:integer
rake db:migrate
bundle exec unicorn_rails -D
echo "Finish!!!!!" >> ../log.txt
date >> ../log.txt
cat ../log.txt
exit
# Please Access http:// global-ip :3000/books/
#!/bin/sh
echo "START!!!!" > log.txt
date >> log.txt
apt-get install -y build-essential libssl-dev openssl libreadline6 libreadline6-dev git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion git-core
echo "apt-get finished." >> log.txt
date >> log.txt
#ruby1.9.2
echo "Ruby start." >> log.txt
date >> log.txt
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p290.tar.gz
tar zxvf ruby-1.9.2-p290.tar.gz
cd ruby-1.9.2-p290
./configure
make -j
make install
echo "Ruby finished." >> log.txt
date >> log.txt
#Rails
echo "Rails start" >> log.txt
date >> log.txt
echo "gem: --no-ri --no-rdoc" > ~/.gemrc
cd ..
gem update --system
gem update rake
gem install rails -v=3.0.9
echo "Rails finished" >> log.txt
date >> log.txt
# Sqlite3
echo "Sqlite3 start." >> log.txt
date >> log.txt
wget http://www.sqlite.org/sqlite-autoconf-3070701.tar.gz
tar zxf sqlite-autoconf-3070701.tar.gz
cd sqlite-autoconf-3070701/
./configure
make -j
make install
cd ..
gem install sqlite3-ruby
gem install sqlite3
echo "Sqlite3 finished." >> log.txt
date >> log.txt
# Check
echo "ruby & rails check" >> log.txt
ruby -v >> log.txt
rails -v >> log.txt
# Test Rails App
rails new testapp --skip-bundle
cd testapp
#echo "gem 'therubyracer'" >> Gemfile
echo "gem 'unicorn'" >> Gemfile
echo "gem 'nifty-generators'" >> Gemfile
echo "gem 'mocha'" >> Gemfile
bundle install
rails g nifty:scaffold Book title:string price:integer
rails g nifty:layout -f
rake db:migrate
bundle exec unicorn_rails -D
ufw allow 8080
echo "Finish!!!!!" >> ../log.txt
date >> ../log.txt
cat ../log.txt
exit
# Please Access http:// global-ip :8080/books/
#!/bin/sh
echo "START!!!!" > log.txt
date >> log.txt
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
yum -y install gcc kernel-devel zlib-devel openssl-devel readline-devel ncurses-devel gcc-c++ curl-devel sqlite-devel net-snmp libxslt-devel libxml2-devel git
echo "yum finished." >> log.txt
date >> log.txt
#ruby1.9.2
echo "Ruby start." >> log.txt
date >> log.txt
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p290.tar.gz
tar zxvf ruby-1.9.2-p290.tar.gz
cd ruby-1.9.2-p290
./configure
make -j
make install
echo "Ruby finished." >> log.txt
date >> log.txt
#Rails
echo "Rails start" >> log.txt
date >> log.txt
echo "gem: --no-ri --no-rdoc" > ~/.gemrc
cd ..
gem update --system
gem update rake
gem install rails -v=3.0.9
echo "Rails finished" >> log.txt
date >> log.txt
# Sqlite3
echo "Sqlite3 start." >> log.txt
date >> log.txt
wget http://www.sqlite.org/sqlite-autoconf-3070701.tar.gz
tar zxf sqlite-autoconf-3070701.tar.gz
cd sqlite-autoconf-3070701/
./configure
make -j
make install
cd ..
gem install sqlite3-ruby
gem install sqlite3
echo "Sqlite3 finished." >> log.txt
date >> log.txt
# Check
echo "ruby & rails check" >> log.txt
ruby -v >> log.txt
rails -v >> log.txt
# Test Rails App
rails new testapp --skip-bundle
cd testapp
#echo "gem 'therubyracer'" >> Gemfile
echo "gem 'unicorn'" >> Gemfile
echo "gem 'nifty-generators'" >> Gemfile
echo "gem 'mocha'" >> Gemfile
bundle install
rails g nifty:scaffold Book title:string price:integer
rails g nifty:layout -f
rake db:migrate
bundle exec unicorn_rails -D
echo "Finish!!!!!" >> ../log.txt
date >> ../log.txt
cat ../log.txt
exit
# Please Access http:// global-ip :8080/books/
#!/bin/sh
# pwd => /
# whoami => root
cd /root
echo "START!!!!"
date
apt-get install -y build-essential libssl-dev openssl libreadline6 libreadline6-dev git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion git-core
echo "apt-get finished."
date
#ruby1.9.2
echo "TITLE:Ruby start."
date
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p290.tar.gz
tar zxvf ruby-1.9.2-p290.tar.gz
cd ruby-1.9.2-p290
./configure
make -j
make install
echo "TITLE:Ruby finished."
date
#Rails
echo "TITLE:Rails start"
date
echo "gem: --no-ri --no-rdoc" > /root/.gemrc
cd ..
sudo gem update --system
sudo gem update rake
sudo gem install rails -v=3.0.9
echo "TITLE:Rails finished"
date
# Sqlite3
echo "TITLE:Sqlite3 start."
date
wget http://www.sqlite.org/sqlite-autoconf-3070701.tar.gz
tar zxf sqlite-autoconf-3070701.tar.gz
cd sqlite-autoconf-3070701/
./configure
make -j
make install
cd ..
gem install sqlite3-ruby
gem install sqlite3
echo "TITLE:Sqlite3 finished."
date
# Check
echo "TITLE:ruby & rails check"
ruby -v
rails -v
# Test Rails App
sudo rails new testapp --skip-bundle
cd testapp
#echo "gem 'therubyracer'" >> Gemfile
echo "gem 'unicorn'" >> Gemfile
echo "gem 'nifty-generators'" >> Gemfile
echo "gem 'mocha'" >> Gemfile
sudo bundle install
sudo rails g nifty:scaffold Book title:string price:integer
sudo rails g nifty:layout -f
sudo rake db:migrate
sudo bundle exec unicorn_rails -D
echo "TITLE:ufw setting"
ufw allow 8080
echo "TITLE:Finish!!!!!"
date
# Please Access http:// global-ip :8080/books/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment