Class: FromScratch

Inherits:
Object
  • Object
show all
Defined in:
lib/from-scratch.rb,
lib/from-scratch/version.rb

Constant Summary collapse

DEFAULTS =
{ ruby_installer: 'rvm' }
VERSION =
"0.6.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFromScratch

Returns a new instance of FromScratch.



12
13
14
# File 'lib/from-scratch.rb', line 12

def initialize
  @options = OpenStruct.new DEFAULTS
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/from-scratch.rb', line 10

def options
  @options
end

Instance Method Details

#generate_valuesObject



63
64
65
66
67
# File 'lib/from-scratch.rb', line 63

def generate_values
  @options.ssh_pub_key               = `cat ~/.ssh/id_rsa.pub`.strip
  @options.postgresql_admin_password = `echo -n '#{SecureRandom.base64(16)}''postgres' | openssl md5 | sed -e 's/.* /md5/'`.strip
  @options.postgresql_user_password  = SecureRandom.base64(16)
end

#get_host_and_app_nameObject



38
39
40
41
42
43
44
# File 'lib/from-scratch.rb', line 38

def get_host_and_app_name
  @options.app_name, @options.host = ARGV.select { |x| !(x =~ /^\-/) }

  unless @options.app_name && @options.host
    raise ArgumentError, 'You should specify APP_NAME and HOST. Use --help for information.'
  end
end

#parse_optionsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/from-scratch.rb', line 46

def parse_options
  opt_parser = OptionParser.new do |args|
    args.banner = "Usage: scratchify your_app_name your.host.com [options]"

    args.on '--rbenv', 'Use RBENV instead of RVM' do
      @options.ruby_installer = 'rbenv'
    end

    args.on '-h', '--help', 'Prints this help' do
      puts args
      exit
    end
  end

  opt_parser.parse!
end

#run!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/from-scratch.rb', line 16

def run!
  parse_options
  get_host_and_app_name
  generate_values

  { node: ['nodes', @options.host],
    user: ['data_bags/users', 'deploy']
  }.each do |from, to|
    FileUtils.mkdir_p File.expand_path("../../tmp/#{to[0]}", __FILE__)
    File.open(File.expand_path("../../tmp/#{to.join('/')}.json", __FILE__), 'w') do |f|
      f.write ERB.new(File.open(File.expand_path("../../templates/#{from}.json.erb", __FILE__)).read).result(binding)
    end
  end

  Dir.chdir(File.expand_path('../..', __FILE__)) do
    system "knife solo bootstrap root@#{@options.host} -c ./.chef/knife.rb"
    system "knife solo clean root@#{@options.host} -c ./.chef/knife.rb"
  end

  FileUtils.rm_rf [File.expand_path('../../tmp', __FILE__)]
end