class Pushapp::Generators

Public Instance Methods

chef_solo(remote) click to toggle source
# File lib/pushapp/generators.rb, line 81
def chef_solo(remote)
  options[:remote] = remote

  template 'Cheffile.erb',    'config/deploys/chef/Cheffile'
  template 'Vagrantfile.erb', 'config/deploys/chef/Vagrantfile'
  template 'knife.rb.erb',    'config/deploys/chef/.chef/knife.rb'
  template 'node.json.erb',   "config/deploys/chef/nodes/#{app_host}.json"
  template 'user.json.erb',   "config/deploys/chef/data_bags/users/#{app_user}.json"

  template 'chef.gitignore',  'config/deploys/chef/.gitignore'
end
unicorn(remote) click to toggle source
# File lib/pushapp/generators.rb, line 48
def unicorn(remote)
  options[:remote] = remote
  template 'unicorn.rb.erb', 'config/unicorn.rb'
  puts "NOTE: add tmp/pids to your git repo. ( touch tmp/pids/.keep && git add tmp/pids/.keep -f )"
end
unicorn_nginx(remote) click to toggle source
# File lib/pushapp/generators.rb, line 36
def unicorn_nginx(remote)
  options[:remote] = remote
  template 'unicorn_nginx.conf.erb', "config/deploys/#{app_name}.nginx.conf"
end
unicorn_upstart() click to toggle source
# File lib/pushapp/generators.rb, line 42
def unicorn_upstart
  template 'unicorn_upstart.erb', 'bin/unicorn_upstart'
  chmod 'bin/unicorn_upstart', 'a+x'
end
web(remote) click to toggle source
# File lib/pushapp/generators.rb, line 16
def web(remote)
  options[:remote]
  options[:listen] = "80"

  uncomment_lines 'Gemfile', /gem 'unicorn'/
  uncomment_lines 'Gemfile', /gem 'therubyracer'/
  insert_into_file 'Gemfile', "\ngem 'pushapp'\ngem 'foreman'\ngem 'dotenv-rails'", after: /gem 'unicorn'/
  unicorn_upstart
  unicorn_nginx(remote)
  unicorn(remote)

  template 'Procfile'
  template '.env.erb', ".env.#{app_env}"
end

Private Instance Methods

app_env() click to toggle source
# File lib/pushapp/generators.rb, line 111
def app_env
  remote.env[:RACK_ENV] || remote.env[:RAILS_ENV] || 'production'
end
app_host() click to toggle source
# File lib/pushapp/generators.rb, line 103
def app_host
  options[:host] || remote.host || '127.0.0.1'
end
app_name() click to toggle source
# File lib/pushapp/generators.rb, line 95
def app_name
  remote.path.split('/').last
end
app_path() click to toggle source
# File lib/pushapp/generators.rb, line 107
def app_path
  remote.path
end
app_user() click to toggle source
# File lib/pushapp/generators.rb, line 99
def app_user
  remote.user
end
authorization_config() click to toggle source
# File lib/pushapp/generators.rb, line 163
def authorization_config
  {
    sudo: {
      users: [app_user],
      passwordless: true
    }
  }
end
chef_config() click to toggle source
# File lib/pushapp/generators.rb, line 191
def chef_config
  common_config.merge({
    authorization: authorization_config,
    rbenv: rbenv_config(app_user)
  })
end
common_config() click to toggle source
# File lib/pushapp/generators.rb, line 172
def common_config
  cfg = {
    nginx: {
      dir:     '/etc/nginx',
      log_dir: '/var/log/nginx',
      binary:  '/usr/sbin/nginx',
      user:    'www-data',
      pid:     '/var/run/nginx.pid',
      worker_connections: '1024'
    },
    git: {
      prefix: '/usr/local'
    },
  }
  cfg[:mysql] = mysql_config if mysql?
  cfg[:postgresql] = postgresql_config if postgresql?
  cfg
end
config() click to toggle source
# File lib/pushapp/generators.rb, line 119
def config
  @config ||= Pushapp::Config.parse(options[:file])
end
config_json() click to toggle source
# File lib/pushapp/generators.rb, line 198
def config_json
  chef_config.merge({
    run_list: run_list
  })
end
mysql?() click to toggle source
# File lib/pushapp/generators.rb, line 123
def mysql?
  options[:database] == 'mysql'
end
mysql_config() click to toggle source
# File lib/pushapp/generators.rb, line 144
def mysql_config
  {
    :server_root_password   => options[:db_password],
    :server_repl_password   => options[:db_password],
    :server_debian_password => options[:db_password],
    :service_name           => 'mysql',
    :basedir                => '/usr',
    :data_dir               => '/var/lib/mysql',
    :root_group             => 'root',
    :mysqladmin_bin         => '/usr/bin/mysqladmin',
    :mysql_bin              => '/usr/bin/mysql',
    :conf_dir               => '/etc/mysql',
    :confd_dir              => '/etc/mysql/conf.d',
    :socket                 => '/var/run/mysqld/mysqld.sock',
    :pid_file               => '/var/run/mysqld/mysqld.pid',
    :grants_path            => '/etc/mysql/grants.sql'
  }
end
postgresql?() click to toggle source
# File lib/pushapp/generators.rb, line 127
def postgresql?
  options[:database] == 'postgresql'
end
postgresql_config() click to toggle source
# File lib/pushapp/generators.rb, line 131
def postgresql_config
  {
    :users => [
      {
        :username => app_user,
        :superuser => true,
        :createdb => true,
        :login => true
      }
    ]
  }
end
rbenv_config(user) click to toggle source
# File lib/pushapp/generators.rb, line 242
def rbenv_config(user)
  {
    user_installs: [{
      user: user,
      rubies: [ options[:ruby] ],
      global: options[:ruby],
      environment: { CFLAGS: '-march=native -O2 -pipe' },
      gems: {
        options[:ruby] => [{name: 'bundler', version: '1.5.0'}]
      }
    }]
  }
end
remote() click to toggle source
# File lib/pushapp/generators.rb, line 115
def remote
  @remote ||= config.remotes_named_by(options[:remote]).first
end
run_list() click to toggle source
# File lib/pushapp/generators.rb, line 211
def run_list
  [
    'apt',
    'chef-solo-search',
    'locale',
    'users::sysadmins',
    'sudo',
    'runit',
    'memcached',
    mysql? ? 'mysql::server' : nil,
    postgresql? ? ['postgresql::server', 'postgresql::contrib', 'postgresql::client', 'postgresql::libpq'] : nil,
    'imagemagick',
    'ruby_build',
    'rbenv::user',
    'nginx::repo',
    'nginx',
    'git'
  ].flatten.compact
end
user_json() click to toggle source
# File lib/pushapp/generators.rb, line 231
def user_json
  {
    id:        app_user,
    comment:   'Application User',
    ssh_keys:  [File.read(options[:ssh_pub_key])],
    groups:    %w{sysadmin sudo staff},
    shell:     '/bin/bash'
  }
end
vagrant_config() click to toggle source
# File lib/pushapp/generators.rb, line 204
def vagrant_config
  # common_config.merge({
  #   rbenv: rbenv_config('vagrant')
  # })
  chef_config
end