topkit 0.1.0 → 1.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/{LICENSE.txt → LICENSE.md} +1 -1
  4. data/README.md +10 -24
  5. data/Rakefile +1 -0
  6. data/bin/ripen +248 -0
  7. data/bin/topkit +111 -7
  8. data/lib/templates/database.yml +9 -0
  9. data/lib/templates/developer/database.yml +7 -0
  10. data/lib/templates/developer/topkit.yml +3 -0
  11. data/lib/templates/nginx.conf +27 -0
  12. data/lib/templates/topkit.yml +14 -0
  13. data/lib/templates/unicorn.rb +8 -0
  14. data/lib/templates/unicorn_init +84 -0
  15. data/lib/topkit/developer/base.rb +156 -0
  16. data/lib/topkit/developer/database.rb +17 -0
  17. data/lib/topkit/developer/installer.rb +46 -0
  18. data/lib/topkit/developer/site.rb +193 -0
  19. data/lib/topkit/developer/symlinks.rb +53 -0
  20. data/lib/topkit/developer/user.rb +53 -0
  21. data/lib/topkit/server/base.rb +84 -0
  22. data/lib/topkit/server/console.rb +11 -0
  23. data/lib/topkit/server/installer.rb +117 -0
  24. data/lib/topkit/server/symlinks.rb +53 -0
  25. data/lib/topkit/server/updater.rb +21 -0
  26. data/lib/topkit/version.rb +1 -1
  27. data/lib/topkit.rb +23 -2
  28. data/topkit.gemspec +10 -10
  29. metadata +50 -46
  30. data/lib/topkit/actions.rb +0 -41
  31. data/lib/topkit/app_builder.rb +0 -112
  32. data/lib/topkit/generators/app_generator.rb +0 -109
  33. data/templates/Gemfile_clean +0 -41
  34. data/templates/_footer.html.erb +0 -2
  35. data/templates/_header.html.erb +0 -2
  36. data/templates/_status.html.erb +0 -6
  37. data/templates/application_layout.html.erb +0 -35
  38. data/templates/database.pg.yml.erb +0 -55
  39. data/templates/database_cleaner_rspec.rb +0 -21
  40. data/templates/topkit_gitignore +0 -2
  41. data/test/lib/topkit/version_test.rb +0 -7
  42. data/test/test_helper.rb +0 -3
@@ -0,0 +1,156 @@
1
+ module Topkit
2
+ module Developer
3
+ class Base
4
+
5
+ # Topkit Server
6
+ @@home = File.expand_path('~')
7
+ @@app_name = 'topkit'
8
+ @@app_dir = ".#{@@app_name}"
9
+ @@root = "#{@@home}/#{@@app_dir}"
10
+ @@projects_root = "#{@@root}/projects"
11
+ @@topkit_file = "#{@@root}/config/topkit.yml"
12
+
13
+ # Topkit CLI
14
+ @@be = 'bundle exec'
15
+ @@template_dir = "#{Topkit.root}/lib/templates/developer"
16
+
17
+ # Git
18
+ @@git_url = 'https://github.com/seancdavis/sapwood-developer.git'
19
+
20
+ # Database
21
+ @@db_file = "#{@@root}/config/database.yml"
22
+ @@db_name = "#{@@app_name}_production"
23
+
24
+ private
25
+
26
+ def create_database
27
+ system("cd #{@@root} && #{@@be} rake db:create")
28
+ end
29
+
30
+ def migrate_database
31
+ system("cd #{@@root} && #{@@be} rake db:migrate")
32
+ end
33
+
34
+ def create_and_migrate_database
35
+ create_database
36
+ migrate_database
37
+ end
38
+
39
+ def start_server
40
+ say "=> Booting Topkit Developer ..."
41
+ if system("cd #{@@root} && #{@@be} rails s -d -p 4141")
42
+ msg = "\nYou're up and running. Topkit Developer is now available at "
43
+ msg += "http://localhost:4141."
44
+ say msg
45
+ end
46
+ end
47
+
48
+ def stop_server
49
+ say "=> Topkit Developer going down..."
50
+ system("kill $(lsof -ti :4141)")
51
+ say "=> Done."
52
+ end
53
+
54
+ def restart_server
55
+ stop_server
56
+ start_server
57
+ end
58
+
59
+ def push_repo(project)
60
+ system("cd #{@@projects_root}/#{project} && git push origin master")
61
+ end
62
+
63
+ def remove_dead_symlinks
64
+ Topkit::Developer::Symlinks.new.clean
65
+ end
66
+
67
+ def generate_symlinks
68
+ Topkit::Developer::Symlinks.new.generate
69
+ end
70
+
71
+ def config
72
+ @@config ||= YAML.load_file(@@topkit_file)['development']
73
+ end
74
+
75
+ def test_api_key
76
+ @@test_api_key ||= config['test_api_key'].strip
77
+ end
78
+
79
+ def api_key
80
+ @@api_key ||= config['api_key'].strip
81
+ end
82
+
83
+ def test_server_url
84
+ @@test_server_url ||= config['test_server_url'].strip
85
+ end
86
+
87
+ def server_url
88
+ @@server_url ||= config['server_url'].strip
89
+ end
90
+
91
+ def api_call(path, data = nil, options = {})
92
+ uri = URI.parse("#{server_url}/api/v2/#{path}.json")
93
+ http = Net::HTTP.new(uri.host, uri.port)
94
+ if !options[:method].nil? && options[:method] == :get
95
+ request = Net::HTTP::Get.new(uri.request_uri)
96
+ else
97
+ request = Net::HTTP::Post.new(uri.request_uri)
98
+ end
99
+ request.set_form_data(data) unless data.nil?
100
+ request["X-Api-Key"] = api_key
101
+ response = http.request(request)
102
+ if response["status"].to_i == 401
103
+ {
104
+ :status => response["status"],
105
+ :body => { 'ERROR' => response["status"] }
106
+ }
107
+ elsif !options[:json].nil? && options[:json] == false
108
+ {
109
+ :status => response["status"],
110
+ :body => response.body
111
+ }
112
+ else
113
+ {
114
+ :status => response["status"],
115
+ :body => JSON.parse(response.body)
116
+ }
117
+ end
118
+ end
119
+
120
+ def template(name)
121
+ File.read("#{@@template_dir}/#{name}")
122
+ end
123
+
124
+ def get_site
125
+ response = api_call('sites', nil, :method => :get)
126
+ say "\nAvailable sites:\n\n"
127
+ sites = response[:body]
128
+ sites.each_with_index do |site, idx|
129
+ say " [#{idx}] #{site['title']}"
130
+ end
131
+ idx = ask("\nEnter the number of the desired site: ").to_i
132
+ if idx >= sites.size
133
+ say "Could not find site."
134
+ exit
135
+ end
136
+ { :status => response[:status], :body => sites[idx] }
137
+ end
138
+
139
+ def get_user
140
+ response = api_call('users', nil, :method => :get)
141
+ say "\nAvailable users:\n\n"
142
+ users = response[:body]
143
+ users.each_with_index do |user, idx|
144
+ say " [#{idx}] #{user['email']}"
145
+ end
146
+ idx = ask("\nEnter the number of the desired user: ").to_i
147
+ if idx >= users.size
148
+ say "Could not find user."
149
+ exit
150
+ end
151
+ { :status => response[:status], :body => users[idx] }
152
+ end
153
+
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,17 @@
1
+ module Topkit
2
+ module Developer
3
+ class Database < Base
4
+
5
+ def update
6
+ response = api_call('data/export', nil, :json => false)
7
+ data = response[:body]
8
+ data_file = "#{@@root}/db/data.yml"
9
+ FileUtils.rm(data_file) if File.exists?(data_file)
10
+ File.open(data_file, 'w+') { |f| f.write(data) }
11
+ system("cd #{@@root} && #{@@be} rake db:schema:load")
12
+ system("cd #{@@root} && #{@@be} rake db:data:load")
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,46 @@
1
+ module Topkit
2
+ module Developer
3
+ class Installer < Base
4
+
5
+ def initialize
6
+ clone_repo
7
+ # checkout_release_branch
8
+ install_bundle
9
+ add_database_config
10
+ add_topkit_config
11
+ create_and_migrate_database
12
+ start_server
13
+ end
14
+
15
+ private
16
+
17
+ def clone_repo
18
+ system("cd #{@@home} && git clone #{@@git_url} #{@@app_dir}")
19
+ end
20
+
21
+ # def checkout_release_branch
22
+ # system("cd #{@@root} && git checkout -b release origin/release")
23
+ # end
24
+
25
+ def install_bundle
26
+ system("cd #{@@root} && bundle install")
27
+ end
28
+
29
+ def add_database_config
30
+ config = template('database.yml').gsub(/\[db_name\]/, @@db_name)
31
+ File.open(@@db_file, 'w+') { |f| f.write(config) }
32
+ end
33
+
34
+ def add_topkit_config
35
+ config = template('topkit.yml')
36
+ say "\nWe just need a couple settings to work with Topkit Server..."
37
+ @@server_url = ask("Topkit Server URL: ")
38
+ config.gsub!(/\[server_url\]/, @@server_url)
39
+ @@api_key = ask("API Key: ")
40
+ config.gsub!(/\[api_key\]/, @@api_key)
41
+ File.open(@@topkit_file, 'w+') { |f| f.write(config) }
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,193 @@
1
+ module Topkit
2
+ module Developer
3
+ class Site < Base
4
+
5
+ # def generate
6
+ # create_site_title_and_slug
7
+ # get_git_url("Template URL: ")
8
+ # clone_repo
9
+ # change_remote_origin
10
+ # create_config_file
11
+ # init_commit
12
+ # generate_symlinks
13
+ # push_repo(@@site_slug)
14
+ # end
15
+
16
+ # def subl
17
+ # say "Here are the sites you have locally:\n"
18
+ # say all_sites.each_with_index.map { |s,i| " [#{i}] #{s}" }.join("\n")
19
+ # idx = ask("\nChoose site: ").to_i
20
+ # if idx >= all_sites.size
21
+ # say "Please choose one of the available numbers."
22
+ # else
23
+ # system("subl #{@@projects_root}/#{all_sites[idx]}")
24
+ # end
25
+ # end
26
+
27
+ # def import
28
+ # get_git_url("Site Repo URL: ")
29
+ # clone_repo_blind
30
+ # generate_symlinks
31
+ # end
32
+
33
+ def create
34
+ data = {
35
+ :site => { :title => ask("Title: ") }
36
+ }
37
+ @response = api_call('sites', data)
38
+ update_database_if_success
39
+ standard_response
40
+ end
41
+
42
+ def get
43
+ @response = get_site
44
+ standard_response
45
+ unless ask("\nWrite config to file? (Y/N) ").downcase == 'y'
46
+ exit
47
+ end
48
+ filename = ask("Filename (default: config.yml): ")
49
+ if filename.nil? || filename.strip == ''
50
+ file = "#{Dir.pwd}/config.yml"
51
+ else
52
+ file = "#{Dir.pwd}/#{filename}"
53
+ end
54
+ if File.exists?(file)
55
+ unless ask("\nFile exists. Overwrite? (Y/N) ").downcase == 'y'
56
+ exit
57
+ end
58
+ end
59
+ config = (@response[:body].to_yaml.split("\n")[1..-1] + ['']).join("\n")
60
+ File.open(file, 'w+') { |f| f.write(config) }
61
+ end
62
+
63
+ def update
64
+ site = get_site[:body]
65
+ file = ask('Where is your config file? ')
66
+ unless File.exists?(file)
67
+ say "Could not find file."
68
+ exit
69
+ end
70
+ data = {
71
+ :site => YAML.load_file(file)
72
+ }
73
+ @response = api_call("sites/update/#{site['uid']}", data)
74
+ update_database_if_success
75
+ @response = api_call("sites/#{site['uid']}", nil, :method => :get)
76
+ standard_response
77
+ end
78
+
79
+ def deploy
80
+ unless File.exists?('.config')
81
+ say 'Make sure you are in a project directory.'
82
+ exit
83
+ end
84
+ data = { :site => { :uid => YAML.load_file('.config')['uid'] } }
85
+ @response = api_call('sites/deploy', data)
86
+ update_database_if_success
87
+ standard_response
88
+ end
89
+
90
+ private
91
+
92
+ # ------------------------------------------ Site References
93
+
94
+ def all_sites
95
+ Dir.glob("#{@@projects_root}/*").collect { |s| s.split('/').last }
96
+ end
97
+
98
+ # ------------------------------------------ Site Attrs
99
+
100
+ def create_site_title_and_slug
101
+ @@site_title = ask("Title: ")
102
+ set_site_slug
103
+ end
104
+
105
+ def set_site_slug
106
+ @@site_slug = @@site_title.downcase
107
+ .strip
108
+ .gsub(/(\ )+/, '-')
109
+ .gsub(/[^0-9a-z\-\_]/i, '')
110
+ end
111
+
112
+ def generate_site_uid
113
+ @@site_uid = SecureRandom.hex(12)
114
+ end
115
+
116
+ # ------------------------------------------ Git URLs
117
+
118
+ def get_git_url(prompt)
119
+ @@git_url = ask(prompt)
120
+ unless system("git ls-remote #{@@git_url} > /dev/null")
121
+ say "You do not have access to that repo. Try again ..."
122
+ get_template_url
123
+ end
124
+ end
125
+
126
+ def change_remote_origin
127
+ @@new_repo_url = ask("Remote URL: ")
128
+ system("cd #{@@project_root} && git remote set-url origin #{@@new_repo_url}")
129
+ end
130
+
131
+ # ------------------------------------------ File Actions
132
+
133
+ def create_config_file
134
+ config = "title: #{@@site_title}\n"
135
+ config += "slug: #{@@site_slug}\n"
136
+ config += "git_url: #{@@new_repo_url}\n"
137
+ config += "uid: #{generate_site_uid}\n"
138
+ File.open("#{@@project_root}/.config", 'w+') { |f| f.write(config) }
139
+ end
140
+
141
+ def load_site_config
142
+ @@site_config = YAML.load_file("#{@@project_root}/.config")
143
+ set_config_vars
144
+ end
145
+
146
+ def set_config_vars
147
+ @@site_title = @@site_config['title']
148
+ @@site_slug = @@site_config['slug']
149
+ @@git_url = @@site_config['git_url']
150
+ @@site_uid = @@site_config['uid']
151
+ end
152
+
153
+ # ------------------------------------------ Git Actions
154
+
155
+ def clone_repo_blind
156
+ @@site_slug = 'topkit-import'
157
+ system("rm -rf #{@@site_slug}")
158
+ system("git clone #{@@git_url} #{@@site_slug}")
159
+ @@project_root = "#{Dir.pwd}/#{@@site_slug}"
160
+ load_site_config
161
+ @@project_root = "#{@@projects_root}/#{@@site_slug}"
162
+ system("mv topkit-import #{@@project_root}")
163
+ end
164
+
165
+ def clone_repo
166
+ system("git clone #{@@git_url} #{@@site_slug}")
167
+ @@project_root = "#{@@projects_root}/#{@@site_slug}"
168
+ system("mv #{@@site_slug} #{@@project_root}")
169
+ end
170
+
171
+ def init_commit
172
+ system("cd #{@@project_root} && git add . && git commit -am \"generate config file\"")
173
+ end
174
+
175
+ # ------------------------------------------ Responses
176
+
177
+ def update_database_if_success
178
+ if @response[:status].to_i == 200
179
+ say 'Updating local database. Please wait ...'
180
+ Topkit::Developer::Database.new.update
181
+ end
182
+ end
183
+
184
+ def standard_response
185
+ say "========================="
186
+ say "RESPONSE: #{@response[:status]}\n"
187
+ say @response[:body].to_yaml
188
+ say "========================="
189
+ end
190
+
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,53 @@
1
+ module Topkit
2
+ module Developer
3
+ class Symlinks < Base
4
+
5
+ def clean
6
+ system("find #{@@root} -type l -exec sh -c \"file -b {} | grep -q ^broken\" \\; -delete")
7
+ end
8
+
9
+ def generate
10
+ clean
11
+ Dir.glob("#{@@projects_root}/*").each do |site_dir|
12
+ site = site_dir.split('/').last
13
+ # Assets
14
+ %w{images javascripts stylesheets fonts}.each do |asset_dir|
15
+ src = "#{site_dir}/#{asset_dir}"
16
+ if Dir.exists?(src)
17
+ dest_parent = "#{@@root}/app/assets/#{asset_dir}/viewer"
18
+ FileUtils.mkdir_p(dest_parent)
19
+ dest = "#{dest_parent}/#{site}"
20
+ system("ln -s #{src} #{dest}") unless File.exists?(dest)
21
+ end
22
+ end
23
+ # Layout
24
+ src = "#{site_dir}/layouts/layout.html.erb"
25
+ dest = "#{@@root}/app/views/layouts/viewer"
26
+ FileUtils.mkdir_p(dest)
27
+ dest = "#{dest}/#{site}.html.erb"
28
+ system("ln -s #{src} #{dest}") unless File.exists?(dest)
29
+ # View Files
30
+ %w{templates partials}.each do |dir|
31
+ src = "#{site_dir}/#{dir}/*"
32
+ dest = "#{@@root}/app/views/viewer/#{site}"
33
+ FileUtils.mkdir_p(dest)
34
+ system("ln -s #{src} #{dest}") #unless File.exists?(dest)
35
+ end
36
+ # Service Object
37
+ src = "#{site_dir}/utilities/services.rb"
38
+ dest_dir = "#{@@root}/app/viewer_services"
39
+ FileUtils.mkdir_p(dest_dir)
40
+ dest = "#{dest}/#{underscore(site)}_viewer.rb"
41
+ system("ln -s #{src} #{dest}") unless File.exists?(dest)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def underscore(site)
48
+ site.downcase.gsub(/[\ \-]+/i, '_').gsub(/[^0-9a-z\-\_]/i, '')
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,53 @@
1
+ module Topkit
2
+ module Developer
3
+ class User < Base
4
+
5
+ def create
6
+ name = ask("Name: ")
7
+ email = ask("Email: ")
8
+ password = ask("Password: ")
9
+ admin = ask("Admin: (true/false) ")
10
+ admin = (admin.downcase == 'true') ? true : false
11
+ data = {
12
+ :user => {
13
+ :name => name,
14
+ :email => email,
15
+ :password => password,
16
+ :password_confirmation => password,
17
+ :admin => admin
18
+ }
19
+ }
20
+ response = api_call('users', data)
21
+ say "========================="
22
+ say "RESPONSE: #{response[:status]}\n"
23
+ say response[:body].to_yaml
24
+ say "========================="
25
+ end
26
+
27
+ def get
28
+ response = get_user
29
+ say "========================="
30
+ say "RESPONSE: #{response[:status]}\n"
31
+ say response[:body].to_yaml
32
+ say "========================="
33
+ end
34
+
35
+ def add
36
+ user = get_user[:body]
37
+ site = get_site[:body]
38
+ data = {
39
+ :site_user => {
40
+ :site => site['uid'],
41
+ :user => user['email']
42
+ }
43
+ }
44
+ response = api_call('site_users', data)
45
+ say "========================="
46
+ say "RESPONSE: #{response[:status]}\n"
47
+ say response[:body].to_yaml
48
+ say "========================="
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,84 @@
1
+ module Topkit
2
+ module Server
3
+ class Base
4
+
5
+ # Topkit Server
6
+ @@user = 'topkit'
7
+ @@home = "/home/#{@@user}"
8
+ @@app_name = 'topkit'
9
+ @@root = "#{@@home}/#{@@app_name}"
10
+ @@projects_root = "#{@@root}/projects"
11
+ @@nginx_file = "/etc/nginx/sites-enabled/#{@@app_name}"
12
+ @@service = "unicorn_#{@@app_name}"
13
+ @@unicorn_init = "/etc/init.d/#{@@service}"
14
+ @@unicorn_file = "#{@@root}/config/unicorn.rb"
15
+ @@topkit_file = "#{@@root}/config/topkit.yml"
16
+
17
+ # Topkit CLI
18
+ @@be = 'bundle exec'
19
+ @@prod = 'RAILS_ENV=production'
20
+ @@template_dir = "#{Topkit.root}/lib/templates"
21
+
22
+ # Git
23
+ @@git_url = 'https://github.com/seancdavis/sapwood'
24
+
25
+ # Database
26
+ @@db_file = "#{@@root}/config/database.yml"
27
+ @@db_user = @@app_name
28
+ @@db_pass = SecureRandom.hex
29
+ @@db_name = "#{@@app_name}_production"
30
+
31
+ # Mailer
32
+ @@mailer_address = 'smtp.sendgrid.net'
33
+ @@mailer_port = '587'
34
+
35
+ private
36
+
37
+ def create_database
38
+ system("cd #{@@root} && #{@@prod} #{@@be} rake db:create")
39
+ end
40
+
41
+ def migrate_database
42
+ system("cd #{@@root} && #{@@prod} #{@@be} rake db:migrate")
43
+ end
44
+
45
+ def create_and_migrate_database
46
+ create_database
47
+ migrate_database
48
+ end
49
+
50
+ def install_bundle
51
+ system("cd #{@@root} && bundle install --without development test")
52
+ end
53
+
54
+ def precompile_assets
55
+ system("cd #{@@root} && #{@@prod} #{@@be} rake assets:precompile")
56
+ end
57
+
58
+ def clean_assets
59
+ system("cd #{@@root} && #{@@prod} #{@@be} rake assets:clean")
60
+ end
61
+
62
+ def start_server
63
+ system("service #{@@service} start")
64
+ end
65
+
66
+ def stop_server
67
+ system("service #{@@service} stop")
68
+ end
69
+
70
+ def restart_server
71
+ system("service #{@@service} restart")
72
+ end
73
+
74
+ def create_user(name, email, password, admin = false)
75
+ puts `cd #{@@root} && #{@@prod} #{@@be} rake create_user '#{name}' '#{email}' '#{password}' '#{admin}'`
76
+ end
77
+
78
+ def template(name)
79
+ File.read("#{@@template_dir}/#{name}")
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,11 @@
1
+ module Topkit
2
+ module Server
3
+ class Console < Base
4
+
5
+ def initialize
6
+ system("cd #{@@root} && #{@@prod} #{@@be} rails console")
7
+ end
8
+
9
+ end
10
+ end
11
+ end