smartmachine 0.8.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +661 -0
  3. data/README.md +147 -0
  4. data/bin/console +14 -0
  5. data/bin/setup +8 -0
  6. data/exe/smartmachine +3 -0
  7. data/lib/smart_machine/apps/app.rb +165 -181
  8. data/lib/smart_machine/apps/container.rb +120 -0
  9. data/lib/smart_machine/apps/manager.rb +182 -0
  10. data/lib/smart_machine/base.rb +24 -6
  11. data/lib/smart_machine/buildpackers/buildpacker.rb +95 -0
  12. data/lib/smart_machine/{engine/buildpacks → buildpackers}/rails/Dockerfile +3 -3
  13. data/lib/smart_machine/buildpackers/rails.rb +221 -0
  14. data/lib/smart_machine/commands/app.rb +112 -0
  15. data/lib/smart_machine/commands/buildpacker.rb +53 -0
  16. data/lib/smart_machine/commands/credentials.rb +17 -0
  17. data/lib/smart_machine/commands/docker.rb +23 -0
  18. data/lib/smart_machine/commands/engine.rb +27 -0
  19. data/lib/smart_machine/commands/grid.rb +33 -0
  20. data/lib/smart_machine/commands/grid_commands/elasticsearch.rb +68 -0
  21. data/lib/smart_machine/commands/grid_commands/minio.rb +65 -0
  22. data/lib/smart_machine/commands/grid_commands/mysql.rb +71 -0
  23. data/lib/smart_machine/commands/grid_commands/nginx.rb +53 -0
  24. data/lib/smart_machine/commands/grid_commands/prereceiver.rb +96 -0
  25. data/lib/smart_machine/commands/grid_commands/redis.rb +65 -0
  26. data/lib/smart_machine/commands/grid_commands/scheduler.rb +15 -0
  27. data/lib/smart_machine/commands/grid_commands/sub_thor.rb +15 -0
  28. data/lib/smart_machine/commands/machine.rb +24 -0
  29. data/lib/smart_machine/commands/syncer.rb +23 -0
  30. data/lib/smart_machine/commands/utilities.rb +37 -0
  31. data/lib/smart_machine/commands.rb +66 -0
  32. data/lib/smart_machine/configuration.rb +79 -0
  33. data/lib/smart_machine/credentials.rb +93 -95
  34. data/lib/smart_machine/docker.rb +144 -143
  35. data/lib/smart_machine/engine/Dockerfile +7 -5
  36. data/lib/smart_machine/engine.rb +104 -79
  37. data/lib/smart_machine/grids/elasticsearch.rb +70 -89
  38. data/lib/smart_machine/grids/minio.rb +79 -68
  39. data/lib/smart_machine/grids/mysql.rb +207 -202
  40. data/lib/smart_machine/grids/nginx.rb +176 -135
  41. data/lib/smart_machine/grids/prereceiver/Dockerfile +2 -2
  42. data/lib/smart_machine/grids/prereceiver/pre-receive +17 -0
  43. data/lib/smart_machine/grids/prereceiver.rb +169 -169
  44. data/lib/smart_machine/grids/redis.rb +75 -57
  45. data/lib/smart_machine/grids/scheduler/Dockerfile +1 -1
  46. data/lib/smart_machine/logger.rb +26 -26
  47. data/lib/smart_machine/machine.rb +128 -194
  48. data/lib/smart_machine/scp.rb +15 -0
  49. data/lib/smart_machine/ssh.rb +69 -40
  50. data/lib/smart_machine/syncer.rb +133 -0
  51. data/lib/smart_machine/templates/dotsmartmachine/Gemfile +7 -0
  52. data/lib/smart_machine/templates/dotsmartmachine/config/elasticsearch.yml +5 -0
  53. data/lib/smart_machine/templates/dotsmartmachine/config/environment.rb +0 -9
  54. data/lib/smart_machine/templates/dotsmartmachine/config/minio.yml +13 -0
  55. data/lib/smart_machine/templates/dotsmartmachine/config/mysql/schedule.rb +3 -3
  56. data/lib/smart_machine/templates/dotsmartmachine/config/mysql.yml +13 -0
  57. data/lib/smart_machine/templates/dotsmartmachine/config/prereceiver.yml +7 -0
  58. data/lib/smart_machine/templates/dotsmartmachine/config/redis.yml +15 -0
  59. data/lib/smart_machine/templates/dotsmartmachine/config/users.yml +1 -1
  60. data/lib/smart_machine/templates/dotsmartmachine/gitignore-template +47 -0
  61. data/lib/smart_machine/templates/dotsmartmachine/{grids/elasticsearch/data → vendor}/.keep +0 -0
  62. data/lib/smart_machine/version.rb +30 -6
  63. data/lib/smart_machine.rb +42 -16
  64. metadata +97 -47
  65. data/CHANGELOG.rdoc +0 -0
  66. data/MIT-LICENSE +0 -21
  67. data/README.rdoc +0 -87
  68. data/bin/buildpacker +0 -8
  69. data/bin/prereceiver +0 -8
  70. data/bin/scheduler +0 -18
  71. data/bin/smartmachine +0 -81
  72. data/bin/smartrunner +0 -33
  73. data/lib/smart_machine/apps/rails.rb +0 -250
  74. data/lib/smart_machine/apps.rb +0 -14
  75. data/lib/smart_machine/boot.rb +0 -32
  76. data/lib/smart_machine/buildpacker.rb +0 -106
  77. data/lib/smart_machine/gem_version.rb +0 -17
  78. data/lib/smart_machine/grids.rb +0 -18
  79. data/lib/smart_machine/sync.rb +0 -120
  80. data/lib/smart_machine/templates/dotsmartmachine/grids/elasticsearch/logs/.keep +0 -0
  81. data/lib/smart_machine/templates/dotsmartmachine/grids/minio/data/.keep +0 -0
  82. data/lib/smart_machine/templates/dotsmartmachine/grids/mysql/backups/.keep +0 -0
  83. data/lib/smart_machine/templates/dotsmartmachine/grids/mysql/data/.keep +0 -0
  84. data/lib/smart_machine/templates/dotsmartmachine/grids/prereceiver/pre-receive +0 -17
  85. data/lib/smart_machine/templates/dotsmartmachine/grids/redis/data/.keep +0 -0
  86. data/lib/smart_machine/user.rb +0 -38
@@ -1,137 +1,178 @@
1
- # The main SmartMachine Grids Nginx driver
1
+ require 'yaml'
2
+ require "base64"
3
+ require 'bcrypt'
4
+
2
5
  module SmartMachine
3
- class Grids
4
- class Nginx < SmartMachine::Base
5
- def initialize
6
- end
7
-
8
- def up(*args)
9
- args.flatten!
10
- exposed = args.empty? ? '' : args.shift
11
-
12
- if SmartMachine::Docker.running?
13
- # Creating volumes
14
- print "-----> Creating volume nginx-confd ... "
15
- if system("docker volume create nginx-confd", out: File::NULL)
16
- puts "done"
17
- end
18
-
19
- print "-----> Creating volume nginx-vhost ... "
20
- if system("docker volume create nginx-vhost", out: File::NULL)
21
- puts "done"
22
- end
23
-
24
- print "-----> Creating volume nginx-shtml ... "
25
- if system("docker volume create nginx-shtml", out: File::NULL)
26
- puts "done"
27
- end
28
-
29
- # Creating networks
30
- unless system("docker network inspect nginx-network", [:out, :err] => File::NULL)
31
- print "-----> Creating network nginx-network ... "
32
- if system("docker network create nginx-network", out: File::NULL)
33
- puts "done"
34
- end
35
- end
36
-
37
- # Creating & Starting containers
38
- print "-----> Creating container nginx ... "
39
- if system("docker create \
40
- --name='nginx' \
41
- #{"--publish='80:80' --publish='443:443'" if exposed == '--exposed'} \
42
- --volume='nginx-confd:/etc/nginx/conf.d/' \
43
- --volume='nginx-vhost:/etc/nginx/vhost.d/' \
44
- --volume='nginx-shtml:/usr/share/nginx/html' \
45
- --volume='#{SmartMachine.config.user_home_path}/.smartmachine/grids/nginx/certificates:/etc/nginx/certs' \
46
- --volume='#{SmartMachine.config.user_home_path}/.smartmachine/grids/nginx/fastcgi.conf:/etc/nginx/fastcgi.conf:ro' \
47
- --volume='#{SmartMachine.config.user_home_path}/.smartmachine/grids/nginx/htpasswd:/etc/nginx/htpasswd:ro' \
48
- --restart='always' \
49
- --network='nginx-network' \
50
- nginx:alpine", out: File::NULL)
51
-
52
- puts "done"
53
- print "-----> Starting container nginx ... "
54
- if system("docker start nginx", out: File::NULL)
55
- puts "done"
56
- end
57
- end
58
-
59
- print "-----> Creating container nginx-gen ... "
60
- if system("docker create \
61
- --name='nginx-gen' \
62
- --volumes-from nginx \
63
- --volume='#{SmartMachine.config.user_home_path}/.smartmachine/grids/nginx/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro' \
64
- --volume='/var/run/docker.sock:/tmp/docker.sock:ro' \
65
- --restart='always' \
66
- --network='nginx-network' \
67
- jwilder/docker-gen \
68
- -notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf", out: File::NULL)
69
-
70
- puts "done"
71
- print "-----> Starting container nginx-gen ... "
72
- if system("docker start nginx-gen", out: File::NULL)
73
- puts "done"
74
- end
75
- end
76
-
77
- print "-----> Creating container nginx-letsencrypt ... "
78
- if system("docker create \
79
- --name='nginx-letsencrypt' \
80
- --env NGINX_PROXY_CONTAINER=nginx \
81
- --env NGINX_DOCKER_GEN_CONTAINER=nginx-gen \
82
- --env DEFAULT_EMAIL=#{SmartMachine.config.sysadmin_email} \
83
- --volumes-from nginx \
84
- --volume='/var/run/docker.sock:/var/run/docker.sock:ro' \
85
- --restart='always' \
86
- --network='nginx-network' \
87
- jrcs/letsencrypt-nginx-proxy-companion", out: File::NULL)
88
-
89
- puts "done"
90
- print "-----> Starting container nginx-letsencrypt ... "
91
- if system("docker start nginx-letsencrypt", out: File::NULL)
92
- puts "done"
93
- end
94
- end
95
- end
96
- end
97
-
98
- def down
99
- if SmartMachine::Docker.running?
100
- # Stopping & Removing containers - in reverse order
101
- print "-----> Stopping container nginx-letsencrypt ... "
102
- if system("docker stop 'nginx-letsencrypt'", out: File::NULL)
103
- puts "done"
104
- print "-----> Removing container nginx-letsencrypt ... "
105
- if system("docker rm 'nginx-letsencrypt'", out: File::NULL)
106
- puts "done"
107
- end
108
- end
109
-
110
- print "-----> Stopping container nginx-gen ... "
111
- if system("docker stop 'nginx-gen'", out: File::NULL)
112
- puts "done"
113
- print "-----> Removing container nginx-gen ... "
114
- if system("docker rm 'nginx-gen'", out: File::NULL)
115
- puts "done"
116
- end
117
- end
118
-
119
- print "-----> Stopping container nginx ... "
120
- if system("docker stop 'nginx'", out: File::NULL)
121
- puts "done"
122
- print "-----> Removing container nginx ... "
123
- if system("docker rm 'nginx'", out: File::NULL)
124
- puts "done"
125
- end
126
- end
127
-
128
- # Removing networks
129
- print "-----> Removing network nginx-network ... "
130
- if system("docker network rm nginx-network", out: File::NULL)
131
- puts "done"
132
- end
133
- end
134
- end
135
- end
136
- end
6
+ class Grids
7
+ class Nginx < SmartMachine::Base
8
+ def initialize
9
+ @home_dir = File.expand_path('~')
10
+ end
11
+
12
+ def uper
13
+ # Creating volumes
14
+ puts "-----> Creating volume nginx-confd ... "
15
+ if system("docker volume create nginx-confd", out: File::NULL)
16
+ puts "done"
17
+ end
18
+
19
+ puts "-----> Creating volume nginx-vhost ... "
20
+ if system("docker volume create nginx-vhost", out: File::NULL)
21
+ puts "done"
22
+ end
23
+
24
+ puts "-----> Creating volume nginx-shtml ... "
25
+ if system("docker volume create nginx-shtml", out: File::NULL)
26
+ puts "done"
27
+ end
28
+
29
+ # Creating networks
30
+ unless system("docker network inspect nginx-network", [:out, :err] => File::NULL)
31
+ puts "-----> Creating network nginx-network ... "
32
+ if system("docker network create nginx-network", out: File::NULL)
33
+ puts "done"
34
+ end
35
+ end
36
+
37
+ # Creating & Starting containers
38
+ puts "-----> Creating container nginx ... "
39
+ command = [
40
+ "docker create",
41
+ "--name='nginx'",
42
+ "--publish='80:80' --publish='443:443'",
43
+ "--volume='nginx-confd:/etc/nginx/conf.d/'",
44
+ "--volume='nginx-vhost:/etc/nginx/vhost.d/'",
45
+ "--volume='nginx-shtml:/usr/share/nginx/html'",
46
+ "--volume='#{@home_dir}/smartmachine/grids/nginx/certificates:/etc/nginx/certs'",
47
+ "--volume='#{@home_dir}/smartmachine/grids/nginx/fastcgi.conf:/etc/nginx/fastcgi.conf:ro'",
48
+ "--volume='#{@home_dir}/smartmachine/grids/nginx/htpasswd:/etc/nginx/htpasswd:ro'",
49
+ "--restart='always'",
50
+ "--network='nginx-network'",
51
+ "nginx:alpine"
52
+ ]
53
+ if system(command.compact.join(" "), out: File::NULL)
54
+ puts "done"
55
+ puts "-----> Starting container nginx ... "
56
+ if system("docker start nginx", out: File::NULL)
57
+ puts "done"
58
+ else
59
+ raise "Error: Could not start the created nginx container."
60
+ end
61
+ else
62
+ raise "Error: Could not create nginx container."
63
+ end
64
+
65
+ puts "-----> Creating container nginx-gen ... "
66
+ command = [
67
+ "docker create",
68
+ "--name='nginx-gen'",
69
+ "--volumes-from nginx",
70
+ "--volume='#{@home_dir}/smartmachine/grids/nginx/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro'",
71
+ "--volume='/var/run/docker.sock:/tmp/docker.sock:ro'",
72
+ "--restart='always'",
73
+ "--network='nginx-network'",
74
+ "jwilder/docker-gen",
75
+ "-notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf"
76
+ ]
77
+ if system(command.compact.join(" "), out: File::NULL)
78
+ puts "done"
79
+ puts "-----> Starting container nginx-gen ... "
80
+ if system("docker start nginx-gen", out: File::NULL)
81
+ puts "done"
82
+ else
83
+ raise "Error: Could not start the created nginx-gen container."
84
+ end
85
+ else
86
+ raise "Error: Could not create nginx-gen container."
87
+ end
88
+
89
+ puts "-----> Creating container nginx-letsencrypt ... "
90
+ command = [
91
+ "docker create",
92
+ "--name='nginx-letsencrypt'",
93
+ "--env NGINX_PROXY_CONTAINER=nginx",
94
+ "--env NGINX_DOCKER_GEN_CONTAINER=nginx-gen",
95
+ "--env DEFAULT_EMAIL=#{SmartMachine.config.sysadmin_email}",
96
+ "--volumes-from nginx",
97
+ "--volume='/var/run/docker.sock:/var/run/docker.sock:ro'",
98
+ "--restart='always'",
99
+ "--network='nginx-network'",
100
+ "jrcs/letsencrypt-nginx-proxy-companion"
101
+ ]
102
+ if system(command.compact.join(" "), out: File::NULL)
103
+ puts "done"
104
+ puts "-----> Starting container nginx-letsencrypt ... "
105
+ if system("docker start nginx-letsencrypt", out: File::NULL)
106
+ puts "done"
107
+ else
108
+ raise "Error: Could not start the created nginx-letsencrypt container."
109
+ end
110
+ else
111
+ raise "Error: Could not create nginx-letsencrypt container."
112
+ end
113
+ end
114
+
115
+ # Stopping & Removing containers - in reverse order
116
+ def downer
117
+ puts "-----> Stopping container nginx-letsencrypt ... "
118
+ if system("docker stop 'nginx-letsencrypt'", out: File::NULL)
119
+ puts "done"
120
+ puts "-----> Removing container nginx-letsencrypt ... "
121
+ if system("docker rm 'nginx-letsencrypt'", out: File::NULL)
122
+ puts "done"
123
+ end
124
+ end
125
+
126
+ puts "-----> Stopping container nginx-gen ... "
127
+ if system("docker stop 'nginx-gen'", out: File::NULL)
128
+ puts "done"
129
+ puts "-----> Removing container nginx-gen ... "
130
+ if system("docker rm 'nginx-gen'", out: File::NULL)
131
+ puts "done"
132
+ end
133
+ end
134
+
135
+ puts "-----> Stopping container nginx ... "
136
+ if system("docker stop 'nginx'", out: File::NULL)
137
+ puts "done"
138
+ puts "-----> Removing container nginx ... "
139
+ if system("docker rm 'nginx'", out: File::NULL)
140
+ puts "done"
141
+ end
142
+ end
143
+
144
+ # Removing networks
145
+ puts "-----> Removing network nginx-network ... "
146
+ if system("docker network rm nginx-network", out: File::NULL)
147
+ puts "done"
148
+ end
149
+ end
150
+
151
+ def create_htpasswd_files
152
+ htpasswd_dir = "#{Dir.pwd}/grids/nginx/htpasswd"
153
+
154
+ # Remove existing htpasswd_dir and recreate it.
155
+ FileUtils.rm_r htpasswd_dir if Dir.exist?(htpasswd_dir)
156
+ FileUtils.mkdir htpasswd_dir
157
+ FileUtils.touch "#{htpasswd_dir}/.keep"
158
+
159
+ # Add hostfiles to htpasswd_dir
160
+ get_users_from_file.each do |domain_name, users|
161
+ next unless users
162
+
163
+ file_data = ""
164
+ users.each do |user, password|
165
+ file_data += "#{user}:#{BCrypt::Password.create(password)}\n"
166
+ end
167
+ File.open("#{Dir.pwd}/grids/nginx/htpasswd/#{domain_name}", "w") { |file| file.write(file_data) }
168
+ end
169
+ end
170
+
171
+ private
172
+
173
+ def get_users_from_file
174
+ YAML.load_file("#{Dir.pwd}/config/users.yml") || Hash.new
175
+ end
176
+ end
177
+ end
137
178
  end
@@ -1,7 +1,7 @@
1
1
  ARG SMARTMACHINE_VERSION
2
2
 
3
- FROM smartmachine:$SMARTMACHINE_VERSION
4
- LABEL maintainer="Timeboard <hello@timeboard.me>"
3
+ FROM smartmachine/smartengine:$SMARTMACHINE_VERSION
4
+ LABEL maintainer="plainsource <plainsource@humanmind.me>"
5
5
 
6
6
  # FCGI Essentials
7
7
  # --- IMPORTANT NOTE: This is custom built fcgiwrap package for alpine linux to account for NO_BUFFERING option.
@@ -0,0 +1,17 @@
1
+ #!/bin/sh
2
+
3
+ # oldrev, newrev, refname are a feature of the way in which Git executes the pre-receive hook.
4
+ # See https://www.kernel.org/pub/software/scm/git/docs/githooks.html
5
+ while read oldrev newrev refname; do
6
+ if [ $(git rev-parse --is-bare-repository) = true ]; then
7
+ REPOSITORY_BASENAME=$(basename "$PWD")
8
+ REPOSITORY_BASENAME=${REPOSITORY_BASENAME%.git}
9
+ else
10
+ REPOSITORY_BASENAME=$(basename $(readlink -nf "$PWD"/..))
11
+ fi
12
+ script --quiet --return --flush --command "smartmachine grid prereceiver prereceive $REPOSITORY_BASENAME $REMOTE_USER $oldrev $newrev $refname" /home/$(whoami)/machine/apps/containers/$REPOSITORY_BASENAME/typescript
13
+ if [ "$?" -eq 10 ]; then
14
+ exit 0
15
+ fi
16
+ exit 1
17
+ done