Module: Waxx::Init

Extended by:
Init
Included in:
Init
Defined in:
waxx/init.rb

Overview

Command line init script to create a Waxx app

Instance Method Summary collapse

Instance Method Details

#ascii_artObject

Some nice ASCII Art to get people excited



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'waxx/init.rb', line 78

def ascii_art
  # http://www.patorjk.com/software/taag/
  %(
`8.`888b                 ,8' .8.          `8.`8888.      ,8' `8.`8888.      ,8' 
 `8.`888b               ,8' .888.          `8.`8888.    ,8'   `8.`8888.    ,8'  
`8.`888b             ,8' :88888.          `8.`8888.  ,8'     `8.`8888.  ,8'   
 `8.`888b     .b    ,8' . `88888.          `8.`8888.,8'       `8.`8888.,8'    
  `8.`888b    88b  ,8' .8. `88888.          `8.`88888'         `8.`88888'     
   `8.`888b .`888b,8' .8`8. `88888.         .88.`8888.         .88.`8888.     
    `8.`888b8.`8888' .8' `8. `88888.       .8'`8.`8888.       .8'`8.`8888.    
     `8.`888`8.`88' .8'   `8. `88888.     .8'  `8.`8888.     .8'  `8.`8888.   
      `8.`8' `8,`' .888888888. `88888.   .8'    `8.`8888.   .8'    `8.`8888.  
       `8.`   `8' .8'       `8. `88888. .8'      `8.`8888. .8'      `8.`8888.

                              Version #{Waxx::Version}
  )
end

#ask(x, input) ⇒ Object

Ask questions about the config



139
140
141
142
143
144
# File 'waxx/init.rb', line 139

def ask(x, input)
  get_site(x, input)
  get_key_iv(x, input)
  get_db(x, input)
  input
end

#create_waxx_table(x, input) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'waxx/init.rb', line 240

def create_waxx_table(x, input)
  # Require the db lib
  case (input/:databases/:app).split(":").first.downcase
    when 'postgresql'
      require 'pg'
    when 'mysql2'
      require 'mysql2'
    when 'sqlite3'
      require 'sqlite3'
  end
  create_sql = %(
    CREATE TABLE waxx (
      name character varying(254) NOT NULL PRIMARY KEY,
      value character varying(254) NOT NULL,
      CONSTRAINT waxx_uniq UNIQUE(name)
    )
  )
  insert_sql = %(INSERT INTO waxx (name, value) VALUES ('db.app.migration.last', '0'))
  puts "  Connecting to: #{input/:databases/:app}"
  begin
    db = Waxx::Database.connect(input/:databases/:app)
    db.exec(create_sql)
    db.exec(insert_sql)
    puts "  Waxx table created successfully."
  rescue => e
    puts %(
      \nERROR: Could not create waxx table. Please create manually:
      \n#{create_sql}
      \n#{insert_sql}
      \nError Detail: #{e}
    )
  end
end

#default_input(x, opts) ⇒ Object

Defines the default YAML config file



20
21
22
23
24
25
26
27
28
29
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
# File 'waxx/init.rb', line 20

def default_input(x, opts)
%(---
server:
host: localhost
port: 7777
processes: 1
min_threads: 4
max_threads: 4
log_dir: log
pid_dir: tmp/pids

site:
name: #{`whoami`.chomp.capitalize}'s Website
support_email: #{`whoami`.chomp}@#{`hostname`.chomp}
url: http://localhost:7777

encryption:
cipher: AES-256-CBC
key: #{SecureRandom.base64(32)[0,32]}
iv: #{SecureRandom.base64(16)[0,16]} 

cookie:
user:
  name: wxu
  expires_after_login_mins: 1440
  expires_after_activity_mins: 480
  secure: true
agent:
  name: wxa
  expires_years: 30
  secure: true

debug:
level: 9
on_screen: true
send_email: false
email: #{`whoami`.chomp}@#{`hostname`.chomp}
auto_reload_code: true

databases:
app: 

default:
app: website
act: index
ext: html

file:
serve: true
path: public

init:
website: true
html: true
)
end

#get_db(x, input) ⇒ Object

Get config options about the db



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'waxx/init.rb', line 167

def get_db(x, input)
  default_db = "postgresql://#{`whoami`.chomp}@localhost/#{`whoami`.chomp}"
  puts ""
  puts "Enter your database connection string (type://user:pass@host:port/db)"
  puts "Types include: postgresql, mysql2, sqlite3, mongodb"
  puts "You can edit or add more database connections by editing opt/dev/config.yaml"
  puts "Enter 'none' if you do not want to connect to a database now"
  puts "[#{input['databases']['app'] || default_db}] "
  print "  db: "
  db = $stdin.gets.chomp
  if db.downcase == "none"
    input['databases'] = {}
  else
    input['databases']['app'] = db == '' ? (input['databases']['app'] || default_db) : db
    puts "Create the standard waxx table? (The waxx table is used for migration management.)"
    puts "The databse must already exist and the user must have create table privileges."
    print "  Create waxx table (y|n) [y]:"
    initdb = $stdin.gets.chomp
    initdb = initdb == '' or not (initdb =~ /[Yy]/).nil?
    input['init']['db'] = initdb
  end
  input
end

#get_key_iv(x, input) ⇒ Object

Get config options about the encryption



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'waxx/init.rb', line 192

def get_key_iv(x, input)
  puts ""
  puts "The AES key and initiation vector for encryption."
  puts "The default was generated with SecureRandom.base64()."
  puts "Accept the default or enter 48 (or more) random characters."
  puts "Can not start with an ampersand: &"
  puts "See https://www.grc.com/passwords.htm for inspiration."
  puts "[#{input['encryption']['key']}#{input['encryption']['iv']}] "
  print "  Random string: "
  random = $stdin.gets.chomp
  if random.size >= 48
    input['encryption']['key'] = random[0,32]
    input['encryption']['iv'] = random[33,16]
  end
  input
end

#get_site(x, input) ⇒ Object

Get config options about the site



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'waxx/init.rb', line 147

def get_site(x, input)
  puts ""
  puts "Website/App options:"
  print "  Name [#{input/:site/:name}]: "
  name = $stdin.gets.chomp
  print "  Support Email [#{input/:site/:support_email}]: "
  support_email = $stdin.gets.chomp
  print "  Liston on IP/Host [#{input/:server/:host}]: "
  host = $stdin.gets.chomp
  print "  Listen on Port [#{input/:server/:port}]: "
  port = $stdin.gets.chomp
  input['site']['name'] = name unless name == ''
  input['site']['support_email'] = support_email unless support_email == ''
  input['server']['host'] = host unless host == ''
  input['server']['port'] = port unless port == ''
  input['site']['url'] = "http://#{input['server']['host']}:#{input['server']['port']}"
  input
end

#init(x, opts, input = nil) ⇒ Object

Start the init process. Fired when `waxx init folder` is called



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'waxx/init.rb', line 97

def init(x, opts, input=nil)
  puts ""
  puts ascii_art
  puts ""
  # Set defaults
  input ||= YAML.load(default_input(x, opts))
  make_dir(x, opts/:sub_command)
  # Ask a few questions
  input = ask(x, input)
  puts ""
  puts "Here is your config..."
  puts ""
  puts input.to_yaml
  puts ""
  puts ""
  puts "Does this look right? You can edit this YAML file later in #{opts/:sub_command}/opt/dev/config.yaml."
  print "Install? (y|n) [y]: "
  proceed = $stdin.gets.chomp
  if proceed == '' or (proceed =~ /[yY]/) == 0
    install_waxx(x, input, opts)
  else
    init(x, opts, input)
  end
end

#install_waxx(x, input, opts) ⇒ Object

Install Waxx in the target folder. Copy skel and update the config file



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'waxx/init.rb', line 210

def install_waxx(x, input, opts)
  skel_folder = "#{File.dirname(__FILE__)}/../../skel/"
  install_folder = opts/:sub_command
  puts ""
  puts "Copying files from #{skel_folder} to #{install_folder}"
  FileUtils.cp_r(skel_folder, install_folder, verbose: false)
  if input/:init/:db
    puts ""
    puts "Setup Database"
    create_waxx_table(x, input)
  end
  if not (input/:databases).empty?
    # Require the correct db lib in app/app.rb
    db_libs = {pg: 'pg', postgresql: 'pg', mysql: 'mysql2', mysql2: 'mysql2', sqlite: 'sqlite3', sqlite3: 'sqlite3', mongo: 'mongodb', mongodb: 'mongodb'}
    db_lib = db_libs[(input/:databases/:app).split(":").first.to_sym]
    puts "Requiring lib '#{db_lib}' in app/app.rb"
    app_rb = File.read("#{install_folder}/app/app.rb")
    File.open("#{install_folder}/app/app.rb","w"){|f|
      f.puts app_rb.sub("# require '#{db_lib}'","require '#{db_lib}'") 
    }
  end
  puts ""
  puts "Installing dev config"
  input.delete "init"
  File.open("#{install_folder}/opt/dev/config.yaml","w"){|f| f << input.to_yaml}
  puts ""
  puts "Waxx installed successfully."
  puts "cd into #{install_folder} and run `waxx on` to get your waxx on"
end

#make_dir(x, name) ⇒ Object

Make the Waxx::Root / app folder



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'waxx/init.rb', line 123

def make_dir(x, name)
  puts ""
  if File.exist? name
    if not Dir.empty?(name)
      puts "Error. The directory '#{name}' already exists and is not empty."
      puts "I don't want to destroy anything. Bailing out."
      exit 4
    end
    puts "Installing into existing directory: #{name}"
    Dir.unlink name
  else
    puts "Make directory: #{name}"
  end
end