Class: Provisionator::Ec2Userdata

Inherits:
Object
  • Object
show all
Defined in:
lib/provisionator/ec2_userdata.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Ec2Userdata

Returns a new instance of Ec2Userdata.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/provisionator/ec2_userdata.rb', line 5

def initialize options={}
  [:app_name, :chef_node_definition, :chef_repo, :config_repo, :app_repo].each do |option|
    raise "option ':#{option}' is required" if options[option].nil?
  end

  options[:chef_solo_rb_path] ||= "/home/ubuntu/.#{options[:app_name]}/provision/chef/solo.rb"
  options[:chef_node_definition_base_file] ||= "/home/ubuntu/.#{options[:app_name]}/provision/chef/base.json"
  options[:chef_node_definition_file] ||= "/home/ubuntu/.#{options[:app_name]}/provision/chef/#{options[:chef_node_definition]}.json"
  options[:ruby_patch] ||= 'p195'
  options[:ebs_mount_paths] = ebs_mount_paths(options[:ebs_volumes]) if options[:ebs_volumes]
  options[:rails_env] ||= 'production'
  @options = options
end

Instance Method Details

#ebs_mount_paths(ebs_volumes) ⇒ Object



20
21
22
23
24
25
# File 'lib/provisionator/ec2_userdata.rb', line 20

def ebs_mount_paths ebs_volumes
  available_device_mappings = ("/dev/xvdf".."/dev/xvdp").to_a
  ebs_volumes.map do |ebs_volume|
    [available_device_mappings.shift, ebs_volume[:mount_location]]
  end
end

#renderObject



27
28
29
# File 'lib/provisionator/ec2_userdata.rb', line 27

def render
  ERB.new(template).result(binding)
end

#templateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/provisionator/ec2_userdata.rb', line 31

def template
"#!/bin/bash -xe\nexec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1\n\n<% if @options[:ebs_mount_paths] %>\n  <% @options[:ebs_mount_paths].each do |device, mount_point| %>\nmkfs.ext4 <%= device %>\nmkdir -p -m 000 <%= mount_point %>\necho \"<%= device %> <%= mount_point %> auto noatime 0 0\" >> /etc/fstab\nmount <%= device %> <%= mount_point %>\n  <% end %>\n<% end %>\n\napt-get update\napt-get upgrade -y\nntpdate ntp.ubuntu.com\nexport LC_CTYPE=en_US.UTF-8\nexport LANG=en_US.UTF-8\nunset LC_ALL\n\necho \"StrictHostKeyChecking no\" >> /etc/ssh/ssh_config\necho \"UserKnownHostsFile=/dev/null\" >> /etc/ssh/ssh_config\necho \"gem: --no-ri --no-rdoc\" >> /etc/skel/.gemrc\necho \"gem: --no-ri --no-rdoc\" >> /root/.gemrc\necho \"gem: --no-ri --no-rdoc\" >> /home/ubuntu/.gemrc\necho \"gem: --no-ri --no-rdoc\" >> /etc/gemrc\n\n#install ruby\nRUBY_PATCH=<%= @options[:ruby_patch] %>\nfunction install_ruby {\n  apt-get -y install build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libcurl4-openssl-dev\n  cd /tmp\n  wget ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-$RUBY_PATCH.tar.gz\n  tar -xvzf ruby-2.0.0-$RUBY_PATCH.tar.gz\n  cd ruby-2.0.0-$RUBY_PATCH/\n  ./configure --prefix=/usr/local\n  make\n  make install\n}\nruby -v | grep 2.0.0$RUBY_PATCH || install_ruby\n\n#install ruby 2.0 compatible version of chef\ncd /tmp\ncurl -o chef.tar.gz -L https://api.github.com/repos/opscode/chef/tarball/CHEF-3935\ntar -xvzf chef.tar.gz\ncd opscode-chef-634ad58\ngem build chef.gemspec\ngem install chef-11.4.0.gem\ngem install ruby-shadow\nmkdir -p /var/cache/chef\nchown ubuntu:ubuntu /var/cache/chef\n\napt-get install git-core -y\n\ngem install bundler\n\ncd /home/ubuntu\n\n<% if @options[:ssh_key] %>\n  mkdir -p /home/ubuntu/.ssh\n  echo \"<%= @options[:ssh_key] %>\" > /home/ubuntu/.ssh/id_rsa\n  chown -R ubuntu:ubuntu /home/ubuntu/.ssh\n  chmod 400 /home/ubuntu/.ssh/id_rsa\n<% end %>\n\nAPP_NAME=<%= @options[:app_name] %>\nAPP_PATH=<%= @options[:app_path] || \"/u/apps/\#{@options[:app_name]}\" %>\n\nsu ubuntu -c 'git clone <%= @options[:chef_repo] %> /home/ubuntu/.chef'\nsu ubuntu -c 'git clone <%= @options[:config_repo] %> /home/ubuntu/.config'\nsu ubuntu -c 'git clone <%= @options[:app_repo] %> /home/ubuntu/.<%= @options[:app_name] %>'\n\nln -s /home/ubuntu/.config/$APP_NAME/<%= @options[:rails_env] %>/app.sh /etc/profile.d/$APP_NAME.sh\n\n. /etc/profile.d/$APP_NAME.sh\n\nchef-solo -c <%= @options[:chef_solo_rb_path] %> -o recipe[base] -l debug\n\n<% unless @options[:no_release] %>\n  mkdir -p $APP_PATH $APP_PATH/releases $APP_PATH/shared $APP_PATH/shared/system $APP_PATH/shared/log $APP_PATH/shared/pids $APP_PATH/shared/sockets\n  chmod g+w $APP_PATH $APP_PATH/releases $APP_PATH/shared $APP_PATH/shared/system $APP_PATH/shared/log $APP_PATH/shared/pids $APP_PATH/shared/sockets\n  chown -R ubuntu:ubuntu /u/\n  su ubuntu -c \"git clone -q \#{@options[:app_repo]} $APP_PATH/shared/cached-copy\"\n  $(cd $APP_PATH/shared/cached-copy && APP_REVISION=$(git rev-parse HEAD))\n  cp -RPp $APP_PATH/shared/cached-copy $APP_PATH/releases/20130516070710\n  echo $APP_REVISION > $APP_PATH/releases/20130516070710/REVISION\n  ln -s $APP_PATH/releases/20130516070710 $APP_PATH/current\n  test -d $APP_PATH/current/log && rm -R $APP_PATH/current/log\n  ln -s $APP_PATH/shared/log $APP_PATH/current/log \n  mkdir $APP_PATH/current/tmp\n  ln -s $APP_PATH/shared/pids/ $APP_PATH/current/tmp/pids\n  bundle install --gemfile $APP_PATH/releases/20130516070710/Gemfile --path $APP_PATH/shared/bundle --deployment --without development test\n  chown -R ubuntu:ubuntu /u/\n<% end %>\n\necho <%= @options[:chef_node_definition] %> > /home/ubuntu/.chef_node_definition\nchown ubuntu:ubuntu /home/ubuntu/.chef_node_definition\nchef-solo -c <%= @options[:chef_solo_rb_path] %> -j <%= @options[:chef_node_definition_file] %> -l debug\n"
end