incline 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 300e4465ce0f50f40f653c0745360883e4dbac04
4
- data.tar.gz: 7687e46f578f349f292dbf2509822ae8a6657d88
3
+ metadata.gz: 9fafa45ca265ae7c13ca542ea9d4dd01ab5a052c
4
+ data.tar.gz: be6b2e8f999070090c9c8367981897e8bd11a069
5
5
  SHA512:
6
- metadata.gz: 12756d6bbb6f3031afbf5d192319577ef1ef1162327b10f3e4f239377d2090ff0d8bb2471be5577fb92d9a3e624241fb358656db3f0f8cffd8aadaa6a780e704
7
- data.tar.gz: 1df7d314dd004917c93a2bf9de2c0016cd2bf187043fef9e2228708abc3fe67cec164a4208104031e08b1c586e96ad654e728653a3936780376a2df653928e19
6
+ metadata.gz: '03698557ded76bf19a3afdc29e262ffffabbd232fcb4da4d382a854c297893c573ef88c7b1f91cd099e53b50862fbb2a22c2b801362725a9f93a257e0631e494'
7
+ data.tar.gz: dd2902766035cec343433b373e61b5d6f7f4ca4d376945fe67ba870d674c9b615baf21c3517a47dc7238685603dd8e01b389ad3ca4ef06dde7f0c8186c534762
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- incline (0.2.3)
4
+ incline (0.2.4)
5
5
  ansi (~> 1.5.0)
6
6
  bcrypt
7
7
  bootstrap-sass
@@ -13,52 +13,52 @@ PATH
13
13
  rails (~> 4.2.8)
14
14
  redcarpet (~> 3.4.0)
15
15
  sass-rails (~> 5.0.6)
16
- shells (~> 0.1.9)
16
+ shells (~> 0.1.15)
17
17
  spawnling (~> 2.1.6)
18
18
  uglifier
19
19
 
20
20
  GEM
21
21
  remote: https://rubygems.org/
22
22
  specs:
23
- actionmailer (4.2.8)
24
- actionpack (= 4.2.8)
25
- actionview (= 4.2.8)
26
- activejob (= 4.2.8)
23
+ actionmailer (4.2.9)
24
+ actionpack (= 4.2.9)
25
+ actionview (= 4.2.9)
26
+ activejob (= 4.2.9)
27
27
  mail (~> 2.5, >= 2.5.4)
28
28
  rails-dom-testing (~> 1.0, >= 1.0.5)
29
- actionpack (4.2.8)
30
- actionview (= 4.2.8)
31
- activesupport (= 4.2.8)
29
+ actionpack (4.2.9)
30
+ actionview (= 4.2.9)
31
+ activesupport (= 4.2.9)
32
32
  rack (~> 1.6)
33
33
  rack-test (~> 0.6.2)
34
34
  rails-dom-testing (~> 1.0, >= 1.0.5)
35
35
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
36
- actionview (4.2.8)
37
- activesupport (= 4.2.8)
36
+ actionview (4.2.9)
37
+ activesupport (= 4.2.9)
38
38
  builder (~> 3.1)
39
39
  erubis (~> 2.7.0)
40
40
  rails-dom-testing (~> 1.0, >= 1.0.5)
41
41
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
42
- activejob (4.2.8)
43
- activesupport (= 4.2.8)
42
+ activejob (4.2.9)
43
+ activesupport (= 4.2.9)
44
44
  globalid (>= 0.3.0)
45
- activemodel (4.2.8)
46
- activesupport (= 4.2.8)
45
+ activemodel (4.2.9)
46
+ activesupport (= 4.2.9)
47
47
  builder (~> 3.1)
48
- activerecord (4.2.8)
49
- activemodel (= 4.2.8)
50
- activesupport (= 4.2.8)
48
+ activerecord (4.2.9)
49
+ activemodel (= 4.2.9)
50
+ activesupport (= 4.2.9)
51
51
  arel (~> 6.0)
52
52
  activerecord-sqlserver-adapter (4.2.18)
53
53
  activerecord (~> 4.2.1)
54
- activesupport (4.2.8)
54
+ activesupport (4.2.9)
55
55
  i18n (~> 0.7)
56
56
  minitest (~> 5.1)
57
57
  thread_safe (~> 0.3, >= 0.3.4)
58
58
  tzinfo (~> 1.1)
59
59
  ansi (1.5.0)
60
60
  arel (6.0.4)
61
- autoprefixer-rails (7.1.2.3)
61
+ autoprefixer-rails (7.1.2.4)
62
62
  execjs
63
63
  bcrypt (3.1.11)
64
64
  bootstrap-sass (3.3.7)
@@ -85,7 +85,7 @@ GEM
85
85
  ffi (1.9.18)
86
86
  globalid (0.4.0)
87
87
  activesupport (>= 4.2.0)
88
- i18n (0.8.1)
88
+ i18n (0.8.6)
89
89
  jbuilder (2.7.0)
90
90
  activesupport (>= 4.2.0)
91
91
  multi_json (>= 1.2)
@@ -100,25 +100,25 @@ GEM
100
100
  mime-types (3.1)
101
101
  mime-types-data (~> 3.2015)
102
102
  mime-types-data (3.2016.0521)
103
- mini_portile2 (2.1.0)
104
- minitest (5.10.2)
103
+ mini_portile2 (2.2.0)
104
+ minitest (5.10.3)
105
105
  multi_json (1.12.1)
106
- net-ssh (3.0.2)
107
- nokogiri (1.7.2)
108
- mini_portile2 (~> 2.1.0)
106
+ net-ssh (4.1.0)
107
+ nokogiri (1.8.0)
108
+ mini_portile2 (~> 2.2.0)
109
109
  rack (1.6.8)
110
110
  rack-test (0.6.3)
111
111
  rack (>= 1.0)
112
- rails (4.2.8)
113
- actionmailer (= 4.2.8)
114
- actionpack (= 4.2.8)
115
- actionview (= 4.2.8)
116
- activejob (= 4.2.8)
117
- activemodel (= 4.2.8)
118
- activerecord (= 4.2.8)
119
- activesupport (= 4.2.8)
112
+ rails (4.2.9)
113
+ actionmailer (= 4.2.9)
114
+ actionpack (= 4.2.9)
115
+ actionview (= 4.2.9)
116
+ activejob (= 4.2.9)
117
+ activemodel (= 4.2.9)
118
+ activerecord (= 4.2.9)
119
+ activesupport (= 4.2.9)
120
120
  bundler (>= 1.3.0, < 2.0)
121
- railties (= 4.2.8)
121
+ railties (= 4.2.9)
122
122
  sprockets-rails
123
123
  rails-deprecated_sanitizer (1.0.3)
124
124
  activesupport (>= 4.2.0.alpha)
@@ -128,9 +128,9 @@ GEM
128
128
  rails-deprecated_sanitizer (>= 1.0.1)
129
129
  rails-html-sanitizer (1.0.3)
130
130
  loofah (~> 2.0)
131
- railties (4.2.8)
132
- actionpack (= 4.2.8)
133
- activesupport (= 4.2.8)
131
+ railties (4.2.9)
132
+ actionpack (= 4.2.9)
133
+ activesupport (= 4.2.9)
134
134
  rake (>= 0.8.7)
135
135
  thor (>= 0.18.1, < 2.0)
136
136
  rake (12.0.0)
@@ -151,8 +151,8 @@ GEM
151
151
  sprockets (>= 2.8, < 4.0)
152
152
  sprockets-rails (>= 2.0, < 4.0)
153
153
  tilt (>= 1.1, < 3)
154
- shells (0.1.10)
155
- net-ssh (~> 3.0.2)
154
+ shells (0.1.15)
155
+ net-ssh (~> 4.1.0)
156
156
  rubyserial (~> 0.4.0)
157
157
  spawnling (2.1.6)
158
158
  sprockets (3.7.1)
@@ -45,7 +45,7 @@ module Incline
45
45
  private
46
46
 
47
47
  def purge_old_history_for(user, max_months = 2)
48
- user.login_histories.where('"incline_user_login_histories"."created_at" <= ?', Time.now - max_months.months).delete_all
48
+ user.login_histories.where('incline_user_login_histories.created_at <= ?', Time.now - max_months.months).delete_all
49
49
  end
50
50
 
51
51
  end
@@ -2,7 +2,14 @@ module Incline
2
2
  class CLI
3
3
 
4
4
  class CliError < ::RuntimeError; end
5
- class UsageError < CliError; end
5
+ class UsageError < CliError
6
+ attr_accessor :command
7
+ def initialize(msg, command = nil)
8
+ super msg
9
+ self.command = command
10
+ end
11
+
12
+ end
6
13
 
7
14
  end
8
15
  end
@@ -362,7 +362,7 @@ module Incline
362
362
  base_key << line[:key]
363
363
 
364
364
  last_line = {
365
- key: base_key,
365
+ key: base_key.dup,
366
366
  value: line[:value].to_s,
367
367
  comment: line[:comment],
368
368
  safe: true
@@ -518,6 +518,23 @@ module Incline
518
518
  def =~(regexp)
519
519
  @content =~ regexp
520
520
  end
521
+
522
+ ##
523
+ # Inserts a comment to the beginning of the contents.
524
+ def insert_comment(text)
525
+ text = '# ' + text.gsub("\r\n", "\n").gsub("\n", "\n# ") + "\n"
526
+ @content = @content.insert(0, text)
527
+ end
528
+
529
+ ##
530
+ # Appends a comment to the end of the contents.
531
+ def append_comment(text)
532
+ text = '# ' + text.gsub("\r\n", "\n").gsub("\n", "\n# ") + "\n"
533
+ unless @content[-1] == "\n"
534
+ @content += "\n"
535
+ end
536
+ @content += text
537
+ end
521
538
 
522
539
  private
523
540
 
@@ -0,0 +1,63 @@
1
+
2
+ module Incline
3
+ class CLI
4
+ class Prepare
5
+
6
+ private
7
+
8
+ def user_process_list(shell, user)
9
+ shell.sudo_exec_ignore_code("pgrep -u #{@options[:deploy_user]}").to_s.split("\n").map(&:strip).reject{|s| s == ''}
10
+ end
11
+
12
+ def kill_processes(shell, user, signal)
13
+ if user_process_list(shell, user).any?
14
+ shell.sudo_exec_ignore_code "pkill -#{signal} -u #{user}"
15
+ et = Time.now + 5
16
+ while Time.now < et
17
+ return true if user_process_list(shell, user).empty?
18
+ sleep 1
19
+ end
20
+ user_process_list(shell,user).any?
21
+ else
22
+ true
23
+ end
24
+ end
25
+
26
+ def add_deploy_user(shell)
27
+ # clean up first
28
+ unless shell.get_user_id(@options[:deploy_user]) == 0
29
+ shell.with_stat('Removing previous deploy user') do
30
+ unless kill_processes(shell, @options[:deploy_user], 'TERM')
31
+ unless kill_processes(shell, @options[:deploy_user], 'KILL')
32
+ raise CliError, "Failed to kill all processes owned by #{@options[:deploy_user]}."
33
+ end
34
+ end
35
+ # remove crontab for user.
36
+ shell.sudo_exec_ignore_code "crontab -u #{@options[:deploy_user]} -r"
37
+ # remove at jobs for user.
38
+ shell.sudo_exec_ignore_code "find /var/spool/cron/atjobs -name \"[^.]*\" -type f -user #{@options[:deploy_user]} -delete"
39
+ # remove the user.
40
+ shell.sudo_exec "userdel -r #{@options[:deploy_user]}"
41
+ # remove the main user group.
42
+ shell.sudo_exec_ignore_code "groupdel #{@options[:deploy_user]}"
43
+ end
44
+ end
45
+
46
+ shell.with_stat('Adding deploy user') do
47
+ # create the user.
48
+ shell.sudo_exec "useradd -mU -s /bin/bash #{@options[:deploy_user]}"
49
+ shell.sudo_exec "printf \"#{@options[:deploy_password]}\\n#{@options[:deploy_password]}\\n\" | passwd #{@options[:deploy_user]}"
50
+
51
+ # add the user's group to the admin user.
52
+ shell.sudo_exec "usermod -G #{@options[:deploy_user]} -a #{@options[:admin_user]}"
53
+
54
+ # set the permissions on the user's home directory.
55
+ # it should be /home/deploy or some such, but let's not assume so.
56
+ @options[:deploy_home] = shell.exec("eval echo \"~#{@options[:deploy_user]}\"").split("\n").first.strip
57
+ shell.sudo_exec "chown #{@options[:deploy_user]}:#{@options[:deploy_user]} #{@options[:deploy_home]} && chmod 755 #{@options[:deploy_home]}"
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,178 @@
1
+
2
+ module Incline
3
+ class CLI
4
+ class Prepare
5
+
6
+ PASSENGER_ROOT_PLACEHOLDER = /\?\?PR/
7
+ DEPLOY_HOME_PLACEHOLDER = /\?\?DH/
8
+ INST_REG_COMMENT_PLACEHOLDER = /\?\?IR/
9
+
10
+ PASSENGER_ROOT_PATH = 'ruby/vendor_ruby/phusion_passenger/locations.ini'
11
+ PASSENGER_ROOT_SEARCH = %w(/usr/share /usr/lib)
12
+
13
+ NGINX_CONFIG = <<-EOCFG
14
+ # General nginx configuration from Incline prepare script.
15
+ # Configuration file generated #{Time.now}.
16
+
17
+ user ruby-apps;
18
+ worker_processes 1;
19
+ error_log /var/log/nginx/error.log;
20
+ pid /run/nginx.pid;
21
+
22
+ events {
23
+ worker_connections 1024;
24
+ }
25
+
26
+ http {
27
+ ##
28
+ # Basic Settings
29
+ ##
30
+
31
+ include /etc/nginx/mime.types;
32
+ default_type application/octet-stream;
33
+
34
+ log_format main '$remote_addr - $remote_user [$time_local] "$request" '
35
+ '$status $body_bytes_sent "$http_referer" '
36
+ '"$http_user_agent" "$http_x_forwarded_for"';
37
+ access_log /var/log/nginx/access.log main;
38
+
39
+ sendfile on;
40
+ index index.html index.htm;
41
+
42
+ tcp_nopush on;
43
+ tcp_nodelay on;
44
+ keepalive_timeout 65;
45
+ types_hash_max_size 2048;
46
+
47
+ ##
48
+ # SSL Settings
49
+ ##
50
+
51
+ ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
52
+ ssl_ciphers DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:kEDH+AESGCM:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA;
53
+ ssl_prefer_server_ciphers on;
54
+ ssl_session_cache shared:SSL:10m;
55
+ ssl_session_timeout 5m;
56
+ ssl_dhparam /var/ssl/dhparams.pem;
57
+
58
+ ##
59
+ # Phusion Passenger settings
60
+ ##
61
+
62
+ passenger_root ??PR;
63
+ passenger_ruby ??DH/.rbenv/shims/ruby;
64
+ passenger_log_level 1;
65
+ ??IRpassenger_instance_registry_dir /var/run/passenger-instreg;
66
+
67
+ ##
68
+ # Default server settings
69
+ ##
70
+
71
+ server {
72
+ listen 80 default_server;
73
+ listen [::]:80 default_server ipv6only=on;
74
+ listen 443 ssl;
75
+ listen [::]:443 ssl;
76
+
77
+ ssl_certificate /var/ssl/ssl.crt;
78
+ ssl_certificate_key /var/ssl/ssl.key;
79
+
80
+ # Set this as appropriate.
81
+ server_name localhost;
82
+
83
+ proxy_set_header X-Forwarded-Proto $scheme;
84
+
85
+ keepalive_timeout 70;
86
+
87
+ include /etc/nginx/locations-enabled/*;
88
+ }
89
+ }
90
+ EOCFG
91
+
92
+ DEFAULT_LOC = <<-EOCFG
93
+ # This is a sample rails app configuration that also happens to take care of ignoring zombie requests.
94
+ # See the fly_trap app's README for more information about it.
95
+ location / {
96
+ # path to the public folder in your app.
97
+ root ??DH/apps/fly_trap/public;
98
+
99
+ # path rails will use as the root path, should match the path provided to location above.
100
+ passenger_base_uri /;
101
+
102
+ rails_env production;
103
+ passenger_enabled on;
104
+ }
105
+ EOCFG
106
+
107
+
108
+
109
+ private_constant :NGINX_CONFIG, :DEFAULT_LOC, :PASSENGER_ROOT_PLACEHOLDER, :DEPLOY_HOME_PLACEHOLDER, :PASSENGER_ROOT_PATH, :PASSENGER_ROOT_SEARCH, :INST_REG_COMMENT_PLACEHOLDER
110
+
111
+
112
+ private
113
+
114
+ def config_passenger(shell)
115
+ shell.with_stat('Configuring Passenger') do
116
+
117
+ # add the ruby-apps user.
118
+ if shell.get_user_id('ruby-apps') == 0
119
+ shell.sudo_exec "useradd -mU ruby-apps"
120
+ end
121
+
122
+ # add ruby-apps and deploy groups to each other.
123
+ shell.sudo_exec_ignore_code "usermod -G ruby-apps -a #{@options[:deploy_user]}"
124
+ shell.sudo_exec_ignore_code "usermod -G #{@options[:deploy_user]} -a ruby-apps"
125
+
126
+ # backup and remove the original configuration.
127
+ shell.sudo_exec 'if [ ! -f /etc/nginx/nginx.conf.original ]; then mv -f /etc/nginx/nginx.conf /etc/nginx/nginx.conf.original; fi'
128
+
129
+ # get the passenger_root path.
130
+ pr_path = shell.sudo_exec_ignore_code "ls {#{PASSENGER_ROOT_SEARCH.join(',')}}/#{PASSENGER_ROOT_PATH} 2>/dev/null"
131
+ pr_path = pr_path.to_s.split("\n").first.to_s.strip
132
+ raise CliError, 'Failed to locate passenger_root path' if pr_path == ''
133
+
134
+ # write the new configuration to a temporary file.
135
+ shell.write_file(
136
+ "#{shell.home_path}/nginx.conf",
137
+ NGINX_CONFIG
138
+ .gsub(PASSENGER_ROOT_PLACEHOLDER, pr_path)
139
+ .gsub(DEPLOY_HOME_PLACEHOLDER, @options[:deploy_home])
140
+ .gsub(INST_REG_COMMENT_PLACEHOLDER, host_id == :centos ? '' : '# ')
141
+ )
142
+
143
+ # move it where it belongs.
144
+ shell.sudo_exec "mv -f #{shell.home_path}/nginx.conf /etc/nginx/nginx.conf"
145
+ shell.sudo_exec 'chown root:root /etc/nginx/nginx.conf && chmod 644 /etc/nginx/nginx.conf'
146
+
147
+ # create the location folders.
148
+ %w(locations-available locations-enabled).each do |loc|
149
+ loc = "/etc/nginx/#{loc}"
150
+ shell.sudo_exec "if [ ! -d #{loc} ]; then mkdir #{loc}; fi"
151
+ shell.sudo_exec "chown #{@options[:deploy_user]}:root #{loc} && chmod 6755 #{loc}"
152
+ end
153
+
154
+ # create the default location.
155
+ shell.write_file(
156
+ "#{shell.home_path}/default.loc",
157
+ DEFAULT_LOC
158
+ .gsub(PASSENGER_ROOT_PLACEHOLDER, pr_path)
159
+ .gsub(DEPLOY_HOME_PLACEHOLDER, @options[:deploy_home])
160
+ )
161
+ shell.sudo_exec "mv -f #{shell.home_path}/default.loc /etc/nginx/locations-available/default"
162
+ shell.sudo_exec "chown #{@options[:deploy_user]}:root /etc/nginx/locations-available/default && chmod 644 /etc/nginx/locations-available/default"
163
+ shell.sudo_exec "ln -s /etc/nginx/locations-available/default /etc/nginx/locations-enabled/default"
164
+
165
+ # create the SSL files.
166
+ shell.sudo_exec 'if [ ! -d /var/ssl ]; then mkdir /var/ssl; fi'
167
+ shell.sudo_exec 'chown ruby-apps:root /var/ssl && chmod 700 /var/ssl'
168
+ # strengthen SSL by using unique dhparams
169
+ shell.sudo_exec 'openssl dhparam -out /var/ssl/dhparams.pem 2048'
170
+ # generate a generic self-signed certificate to get started with.
171
+ shell.sudo_exec "openssl req -x509 -nodes -days 365 -newkey rsa:4096 -subj \"/C=#{@options[:ssl_country]}/ST=#{@options[:ssl_state]}/L=#{@options[:ssl_location]}/O=#{@options[:ssl_org]}/CN=$(hostname -f)\" -keyout /var/ssl/ssl.key -out /var/ssl/ssl.crt"
172
+ shell.sudo_exec 'chown ruby-apps:root /var/ssl/* -R && chmod 600 /var/ssl/*'
173
+ end
174
+ end
175
+
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,33 @@
1
+
2
+ module Incline
3
+ class CLI
4
+ class Prepare
5
+
6
+ private
7
+
8
+ def config_ssh(shell)
9
+ pa_rex = /#\s*PubkeyAuthentication\s+[^\n]*\n/
10
+ rl_rex = /#\s*PermitRootLogin\s+[^\n]*\n/
11
+
12
+ shell.with_stat('Configuring SSH') do
13
+ shell.sudo_exec "cp -f /etc/ssh/sshd_config #{shell.home_path}/tmp_sshd_conf"
14
+ contents = shell.read_file("#{shell.home_path}/tmp_sshd_conf")
15
+ new_contents = contents.gsub(pa_rex, "PubkeyAuthentication yes\n").gsub(rl_rex, "PermitRootLogin no\n")
16
+ if new_contents != contents
17
+ shell.write_file "#{shell.home_path}/tmp_sshd_conf", new_contents
18
+ shell.sudo_exec "chown root:root #{shell.home_path}/tmp_sshd_conf"
19
+ shell.sudo_exec "chmod 600 #{shell.home_path}/tmp_sshd_conf"
20
+ shell.sudo_exec "mv -f #{shell.home_path}/tmp_sshd_conf /etc/ssh/sshd_config"
21
+
22
+ begin
23
+ shell.sudo_exec_ignore_code 'systemctl restart sshd.service'
24
+ rescue
25
+ # ignore any errors from the SSH restart since we should be exiting the SSH session immediately after this gets executed anyway.
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,52 @@
1
+
2
+ module Incline
3
+ class CLI
4
+ class Prepare
5
+
6
+ # create setuid utilities to test the config and reload the config.
7
+
8
+ UTIL_NGINX_RELOAD = <<-EOU
9
+ #include <unistd.h>
10
+
11
+ int main(int argc, char **argv)
12
+ {
13
+ const char *args[] = { "??NG", "-s", "reload", NULL };
14
+ setuid(0);
15
+ execv(args[0], (char **)args);
16
+ return 0;
17
+ }
18
+ EOU
19
+
20
+ UTIL_NGINX_TEST = <<-EOU
21
+ #include <unistd.h>
22
+
23
+ int main(int argc, char **argv)
24
+ {
25
+ const char *args[] = { "??NG", "-t", "-q", NULL };
26
+ setuid(0);
27
+ execv(args[0], (char **)args);
28
+ return 0;
29
+ }
30
+ EOU
31
+
32
+
33
+ private_constant :UTIL_NGINX_RELOAD, :UTIL_NGINX_TEST
34
+
35
+ private
36
+
37
+ def create_nginx_utils(shell)
38
+ shell.with_status('Creating utilities') do
39
+ nginx_path = shell.exec("which nginx").split("\n").first.to_s.strip
40
+
41
+ { 'nginx-reload' => UTIL_NGINX_RELOAD, 'nginx-test' => UTIL_NGINX_TEST }.each do |util,src|
42
+ shell.write_file "#{shell.home_path}/temp-util.c", src.gsub("??NG", nginx_path)
43
+ shell.exec "gcc -o #{shell.home_path}/#{util} #{shell.home_path}/temp-util.c"
44
+ shell.sudo_exec "chown root:root #{shell.home_path}/#{util} && chmod 4755 #{shell.home_path}/#{util}"
45
+ shell.sudo_exec "mv -f #{shell.home_path}/#{util} /usr/local/bin/#{util}"
46
+ shell.exec "rm #{shell.home_path}/temp-util.c"
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,102 @@
1
+ module Incline
2
+ class CLI
3
+ class Prepare
4
+
5
+ private
6
+
7
+ # Add full logging to the shell along with a few helper methods.
8
+ # The prefix is used to identify the shell creating the messages and will be prefixed to each line in the log.
9
+ def extend_shell(sh, prefix)
10
+ logfile.write "\n" + prefix
11
+ sh.instance_variable_set :@prep_log, logfile
12
+ sh.instance_variable_set :@prep_prefix, "\n#{prefix}"
13
+ sh.instance_variable_set :@stat_count, -1
14
+ sh.instance_variable_set :@stat_every, 128
15
+ sh.instance_variable_set :@home_path, nil
16
+
17
+ def sh.home_path
18
+ @home_path ||= exec_ignore_code("eval echo \"~#{@options[:user]}\"").to_s.split("\n").first.to_s.strip
19
+ end
20
+
21
+ def sh.with_stat(status, stat_every = 128)
22
+ if @stat_count > -1
23
+ yield
24
+ else
25
+ @stat_count = 0
26
+ @stat_every = stat_every < 1 ? 128 : stat_every
27
+ print status
28
+ yield
29
+ print "\n"
30
+ @stat_count = -1
31
+ @stat_every = 128
32
+ end
33
+ end
34
+
35
+ def sh.exec(cmd, options = {}, &block)
36
+ super(cmd, options) do |data, type|
37
+ @prep_log.write data.gsub("\n", @prep_prefix)
38
+ @prep_log.flush
39
+ if @stat_count > -1
40
+ @stat_count += data.length
41
+ while @stat_count >= @stat_every
42
+ @stat_count -= @stat_every
43
+ print '.'
44
+ end
45
+ end
46
+ if block
47
+ block.call data, type
48
+ else
49
+ nil
50
+ end
51
+ end
52
+ end
53
+
54
+ def sh.stat_exec(status, cmd, options = {}, &block)
55
+ with_stat(status) { exec(cmd, options, &block) }
56
+ end
57
+
58
+ def sh.sudo_stat_exec(status, cmd, options = {}, &block)
59
+ with_stat(status) { sudo_exec(cmd, options, &block) }
60
+ end
61
+
62
+ def sh.apt_get(command)
63
+ sudo_exec "DEBIAN_FRONTEND=noninteractive apt-get -y -q #{command}"
64
+ end
65
+
66
+ def sh.get_user_id(user)
67
+ result = exec_ignore_code("id -u #{user} 2>/dev/null").to_s.split("\n")
68
+ result.any? ? result.first.strip.to_i : 0
69
+ end
70
+
71
+ def sh.host_info
72
+ @host_info ||=
73
+ begin
74
+ results = exec('cat /etc/*-release').split("\n").map{|s| s.strip}.reject{|s| s == ''}
75
+ info = {}
76
+
77
+ results.each do |line|
78
+ if line.include?('=')
79
+ var,_,val = line.partition('=').map{|s| s.strip}
80
+ val = val[1...-1] if val[0] == '"' && val[-1] == '"'
81
+ var.upcase!
82
+ info[var] = val
83
+ end
84
+ end
85
+
86
+ info['ID'] = (info['ID'] || 'unknown').downcase.to_sym
87
+ info['NAME'] ||= info['ID'].to_s
88
+ info['VERSION'] ||= '??'
89
+ info['PRETTY_NAME'] ||= "#{info['NAME']} #{info['VERSION']}"
90
+
91
+ puts info['PRETTY_NAME']
92
+
93
+ info
94
+ end
95
+ end
96
+
97
+ sh
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,21 @@
1
+
2
+ module Incline
3
+ class CLI
4
+ class Prepare
5
+
6
+ private
7
+
8
+ def install_db(shell)
9
+ shell.with_stat('Installing MariaDB') do
10
+ shell.sudo_exec 'debconf-set-selections <<< \'mariadb-server mysql-server/root_password password \''
11
+ shell.sudo_exec 'debconf-set-selections <<< \'mariadb-server mysql-server/root_password_again password \''
12
+ shell.apt_get 'install mariadb-server mariadb-client libmysqlclient-dev'
13
+ shell.sudo_exec_ignore_code 'systemctl stop mysql.service'
14
+ shell.sudo_exec 'systemctl start mysql.service'
15
+ shell.sudo_exec 'systemctl enable mysql.service'
16
+ end
17
+ end
18
+
19
+ end
20
+ end
21
+ end