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
# File 'lib/provisionator/ec2_userdata.rb', line 5

def initialize options={}
  [:app_name, :chef_node, :chef_repo, :config_repo, :app_repo].each do |option|
    raise "option ':#{option}' is required" if options[option].nil?
  end
  @app_dir = "/home/ubuntu/.#{options[:app_name]}"
  @chef_dir = "#{@app_dir}/#{options[:chef_dir] || 'provision/chef'}"

  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



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

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



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

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

#templateObject



30
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
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/provisionator/ec2_userdata.rb', line 30

def template
"#!/bin/bash -xe\nexec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1\n\nlocale-gen en_US.UTF-8\ndpkg-reconfigure locales\nupdate-locale LANG=en_US.UTF-8\nexport LANGUAGE=en_US.UTF-8\nexport LANG=en_US.UTF-8\nexport LC_ALL=en_US.UTF-8\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\n\nfunction add_line_to_file {\n  (test -f \"$2\" && grep \"$1\" $2) || echo \"$1\" >> $2\n}\n\nadd_line_to_file \"StrictHostKeyChecking no\" /etc/ssh/ssh_config\nadd_line_to_file \"UserKnownHostsFile=/dev/null\" /etc/ssh/ssh_config\n\nadd_line_to_file \"install: --no-document\" /etc/skel/.gemrc\nadd_line_to_file \"install: --no-document\" /root/.gemrc\nadd_line_to_file \"install: --no-document\" /home/ubuntu/.gemrc\nadd_line_to_file \"install: --no-document\" /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\nfunction install_chef {\n  #install ruby 2.0 compatible version of chef\n  cd /tmp\n  curl -o chef.tar.gz -L https://api.github.com/repos/opscode/chef/tarball/CHEF-3935\n  tar -xvzf chef.tar.gz\n  cd opscode-chef-634ad58\n  gem build chef.gemspec\n  gem install chef-11.4.0.gem\n  gem install ruby-shadow\n  mkdir -p /var/cache/chef\n  chown ubuntu:ubuntu /var/cache/chef\n}\ngem list | grep chef || install_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\necho <%= @options[:chef_node] %> > /home/ubuntu/.chef_node\nchown ubuntu:ubuntu /home/ubuntu/.chef_node\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  ln -s $APP_PATH/shared/database.yml $APP_PATH/current/config/\n\n  <% if @options[:chef_pre_bundle] %>\nchef-solo -c <%= \"\#{@chef_dir}/solo.rb\" %> -j <%= \"\#{@chef_dir}/\#{@options[:chef_pre_bundle]}.json\" %> -l debug\n  <% end %>\n\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\nchef-solo -c <%= \"\#{@chef_dir}/solo.rb\" %> -j <%= \"\#{@chef_dir}/\#{@options[:chef_node]}.json\" %> -l debug\n"
end