smartmachine 0.8.0 → 1.0.0

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.
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,144 +1,145 @@
1
- # The main SmartMachine Docker driver
2
1
  module SmartMachine
3
- class Docker < SmartMachine::Base
4
- def initialize
5
- end
6
-
7
- # Installing Docker!
8
- #
9
- # Example:
10
- # => Installation Complete
11
- #
12
- # Arguments:
13
- # none
14
- def install
15
- puts "-----> Installing Docker"
16
-
17
- ssh = SmartMachine::SSH.new
18
-
19
- print "-----> Installing Docker Engine ... "
20
- commands = [
21
- "sudo apt-get -y update",
22
- "sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common",
23
- "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -",
24
- "sudo apt-key fingerprint 0EBFCD88",
25
- "sudo add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable\"",
26
- "sudo apt-get -y update",
27
- "sudo apt-get -y install docker-ce",
28
- "sudo usermod -aG docker $USER",
29
- "docker run --rm hello-world",
30
- "docker rmi hello-world"
31
- ]
32
- ssh.run commands
33
- puts "done"
34
-
35
- print "-----> Installing Docker Compose ... "
36
- commands = [
37
- "sudo curl -L --fail https://github.com/docker/compose/releases/download/1.24.0/run.sh -o /usr/local/bin/docker-compose",
38
- "sudo chmod +x /usr/local/bin/docker-compose",
39
- "docker-compose --version",
40
- "sudo curl -L https://raw.githubusercontent.com/docker/compose/1.24.0/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose"
41
- ]
42
- ssh.run commands
43
- puts "done"
44
-
45
- self.add_ufw_rules
46
-
47
- puts "-----> Docker Installation Complete"
48
- end
49
-
50
- # Uninstalling Docker!
51
- #
52
- # Example:
53
- # => Uninstallation Complete
54
- #
55
- # Arguments:
56
- # none
57
- def uninstall
58
- puts "-----> Uninstalling Docker"
59
-
60
- ssh = SmartMachine::SSH.new
61
-
62
- print "-----> Uninstalling Docker Compose ... "
63
- commands = ["sudo rm /usr/local/bin/docker-compose"]
64
- ssh.run commands
65
- puts "done"
66
-
67
- print "-----> Uninstalling Docker Engine ... "
68
- commands = ["sudo apt-get purge docker-ce", "sudo rm -rf /var/lib/docker"]
69
- ssh.run commands
70
- puts "done"
71
-
72
- self.remove_ufw_rules
73
-
74
- puts "-----> You must delete any edited configuration files manually."
75
-
76
- puts "-----> Docker Uninstallation Complete"
77
- end
78
-
79
- def update
80
- end
81
-
82
- def add_ufw_rules
83
- puts '-----> Add the following rules to the end of the file /etc/ufw/after.rules and reload ufw using - sudo ufw reload'
84
- puts '# BEGIN UFW AND DOCKER
85
- *filter
86
- :ufw-user-forward - [0:0]
87
- :DOCKER-USER - [0:0]
88
- -A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
89
- -A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
90
- -A DOCKER-USER -i eth0 -j ufw-user-forward
91
- -A DOCKER-USER -i eth0 -j DROP
92
- COMMIT
93
- # END UFW AND DOCKER'
94
-
95
- # puts "-----> Adding UFW rules for Docker"
96
- # interface_name = system("ip route show | sed -e 's/^default via [0-9.]* dev \(\w\+\).*/\1/'")
97
- # puts interface_name
98
-
99
- # system("sed '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' '/etc/ufw/after.rules'")
100
- # system("sudo tee -a '/etc/ufw/after.rules' > /dev/null <<EOT
101
- # # BEGIN UFW AND DOCKER
102
- # *filter
103
- # :ufw-user-forward - [0:0]
104
- # :DOCKER-USER - [0:0]
105
- # -A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
106
- # -A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
107
- # -A DOCKER-USER -i eth0 -j ufw-user-forward
108
- # -A DOCKER-USER -i eth0 -j DROP
109
- # COMMIT
110
- # # END UFW AND DOCKER
111
- # EOT")
112
- # system("sudo ufw reload")
113
- end
114
-
115
- def remove_ufw_rules
116
- puts '-----> Remove the following rules at the end of the file /etc/ufw/after.rules and reload ufw using - sudo ufw reload'
117
- puts '# BEGIN UFW AND DOCKER
118
- *filter
119
- :ufw-user-forward - [0:0]
120
- :DOCKER-USER - [0:0]
121
- -A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
122
- -A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
123
- -A DOCKER-USER -i eth0 -j ufw-user-forward
124
- -A DOCKER-USER -i eth0 -j DROP
125
- COMMIT
126
- # END UFW AND DOCKER'
127
-
128
- # puts "-----> Removing UFW rules for Docker"
129
- # system("sed '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' '/etc/ufw/after.rules'")
130
- # system("sudo ufw reload")
131
- end
132
-
133
- # Below methods are non ssh methods and should be executed on the server only.
134
-
135
- def self.running?
136
- if system("docker info", [:out, :err] => File::NULL)
137
- true
138
- else
139
- puts "Error: Docker daemon is not running. Have you installed docker? Please ensure docker daemon is running and try again."
140
- false
141
- end
142
- end
143
- end
144
- end
2
+ class Docker < SmartMachine::Base
3
+ def initialize
4
+ @machine = SmartMachine::Machine.new
5
+ end
6
+
7
+ # Installing Docker!
8
+ #
9
+ # Example:
10
+ # => Installation Complete
11
+ #
12
+ # Arguments:
13
+ # none
14
+ def install
15
+ puts "-----> Installing Docker"
16
+ if platform_on_machine?(os: "linux", distro_name: "debian")
17
+ install_on_linuxos(distro_name: "debian", arch: "amd64")
18
+ # elsif platform_on_machine?(os: "mac")
19
+ # install_on_macos
20
+ else
21
+ raise "Installation of docker is currently supported on Debian GNU/Linux."
22
+ end
23
+ puts "-----> Docker Installation Complete"
24
+ end
25
+
26
+ # Uninstalling Docker!
27
+ #
28
+ # Example:
29
+ # => Uninstallation Complete
30
+ #
31
+ # Arguments:
32
+ # none
33
+ def uninstall
34
+ puts "-----> Uninstalling Docker"
35
+ if platform_on_machine?(os: "linux", distro_name: "debian")
36
+ uninstall_on_linuxos(distro_name: "debian", arch: "amd64")
37
+ # elsif platform_on_machine?(os: "mac")
38
+ # uninstall_on_macos
39
+ else
40
+ raise "Uninstallation of docker is currently supported on Debian GNU/Linux."
41
+ end
42
+ puts "-----> Docker Uninstallation Complete"
43
+ end
44
+
45
+ private
46
+
47
+ def install_on_linuxos(distro_name:, arch:)
48
+
49
+ commands = [
50
+ "sudo apt-get -y update",
51
+ "sudo apt-get -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common",
52
+ "curl -fsSL https://download.docker.com/linux/#{distro_name}/gpg | sudo apt-key add -",
53
+ "sudo apt-key fingerprint 0EBFCD88",
54
+ "sudo add-apt-repository \"deb [arch=#{arch}] https://download.docker.com/linux/#{distro_name} $(lsb_release -cs) stable\"",
55
+ "sudo apt-get -y update",
56
+ "sudo apt-get -y install docker-ce docker-ce-cli containerd.io",
57
+ "sudo usermod -aG docker $USER",
58
+ "docker run --rm hello-world",
59
+ "docker rmi hello-world"
60
+ ]
61
+ @machine.run_on_machine(commands: commands)
62
+
63
+ puts '-----> Add the following rules to the end of the file /etc/ufw/after.rules and reload ufw using - sudo ufw reload'
64
+ puts '# BEGIN UFW AND DOCKER
65
+ *filter
66
+ :ufw-user-forward - [0:0]
67
+ :DOCKER-USER - [0:0]
68
+ -A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
69
+ -A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
70
+ -A DOCKER-USER -i eth0 -j ufw-user-forward
71
+ -A DOCKER-USER -i eth0 -j DROP
72
+ COMMIT
73
+ # END UFW AND DOCKER'
74
+
75
+ # puts "-----> Adding UFW rules for Docker"
76
+ # interface_name = system("ip route show | sed -e 's/^default via [0-9.]* dev \(\w\+\).*/\1/'")
77
+ # puts interface_name
78
+
79
+ # system("sed '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' '/etc/ufw/after.rules'")
80
+ # system("sudo tee -a '/etc/ufw/after.rules' > /dev/null <<EOT
81
+ # # BEGIN UFW AND DOCKER
82
+ # *filter
83
+ # :ufw-user-forward - [0:0]
84
+ # :DOCKER-USER - [0:0]
85
+ # -A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
86
+ # -A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
87
+ # -A DOCKER-USER -i eth0 -j ufw-user-forward
88
+ # -A DOCKER-USER -i eth0 -j DROP
89
+ # COMMIT
90
+ # # END UFW AND DOCKER
91
+ # EOT")
92
+ # system("sudo ufw reload")
93
+ end
94
+
95
+ def uninstall_on_linuxos(distro_name:, arch:)
96
+
97
+ commands = [
98
+ "sudo apt-get purge docker-ce docker-ce-cli containerd.io",
99
+ "sudo apt-get autoremove",
100
+ "sudo rm -rf /var/lib/docker",
101
+ "sudo rm -rf /var/lib/containerd"
102
+ ]
103
+ @machine.run_on_machine(commands: commands)
104
+
105
+ puts '-----> Remove the following rules at the end of the file /etc/ufw/after.rules and reload ufw using - sudo ufw reload'
106
+ puts '# BEGIN UFW AND DOCKER
107
+ *filter
108
+ :ufw-user-forward - [0:0]
109
+ :DOCKER-USER - [0:0]
110
+ -A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
111
+ -A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
112
+ -A DOCKER-USER -i eth0 -j ufw-user-forward
113
+ -A DOCKER-USER -i eth0 -j DROP
114
+ COMMIT
115
+ # END UFW AND DOCKER'
116
+
117
+ # puts "-----> Removing UFW rules for Docker"
118
+ # system("sed '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' '/etc/ufw/after.rules'")
119
+ # system("sudo ufw reload")
120
+ end
121
+
122
+ # def install_on_macos
123
+ # commands = [
124
+ # "/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"",
125
+ # "brew install homebrew/cask/docker",
126
+ # "brew install bash-completion",
127
+ # "brew install docker-completion",
128
+ # "open /Applications/Docker.app",
129
+ # # The docker app asks for permission after opening gui. if that can be automated then the next two statements can be uncommented and automated. Until then can't execute automatically.
130
+ # # "docker run --rm hello-world",
131
+ # # "docker rmi hello-world"
132
+ # ]
133
+ # @machine.run_on_machine(commands: commands)
134
+ # end
135
+
136
+ # def uninstall_on_macos
137
+ # commands = [
138
+ # "brew uninstall docker-completion",
139
+ # "brew uninstall bash-completion",
140
+ # "brew uninstall --zap homebrew/cask/docker"
141
+ # ]
142
+ # @machine.run_on_machine(commands: commands)
143
+ # end
144
+ end
145
+ end
@@ -1,26 +1,28 @@
1
1
  FROM ruby:2.7.0-alpine3.11
2
- LABEL maintainer="Timeboard <hello@timeboard.me>"
2
+ LABEL maintainer="plainsource <plainsource@humanmind.me>"
3
3
 
4
4
  # User
5
- # --- Fix to change gid of 999 to 99 so that addgroup is free to create a group with 999 as gid
5
+ # --- Fix to change docker gid to 998 (if it is in use) so that addgroup is free to create a group with docker gid.
6
6
  ARG USER_NAME
7
7
  ARG USER_UID
8
8
  ARG DOCKER_GID
9
- RUN sed -i "s/999/99/" /etc/group && \
9
+ ARG DOCKER_GNAME
10
+ RUN sed -i "s/$DOCKER_GID/998/" /etc/group && \
10
11
  adduser --disabled-password --gecos "" --uid "$USER_UID" "$USER_NAME" && \
11
- addgroup --gid "$DOCKER_GID" "docker" && adduser "$USER_NAME" "docker"
12
+ addgroup --gid "$DOCKER_GID" "$DOCKER_GNAME" && adduser "$USER_NAME" "$DOCKER_GNAME"
12
13
 
13
14
  # Essentials
14
15
  RUN apk add --update build-base && \
15
16
  apk add --update xz && \
16
17
  apk add --update docker && \
17
18
  apk add --update git && \
19
+ apk add --update rsync && \
18
20
  rm -rf /var/cache/apk/*
19
21
 
20
22
  # smartmachine gem
21
23
  ARG SMARTMACHINE_VERSION
22
24
  COPY ./smartmachine-$SMARTMACHINE_VERSION.gem ./smartmachine-$SMARTMACHINE_VERSION.gem
23
- RUN gem install --no-document smartmachine-$SMARTMACHINE_VERSION.gem && \
25
+ RUN gem install --no-document ./smartmachine-$SMARTMACHINE_VERSION.gem && \
24
26
  rm ./smartmachine-$SMARTMACHINE_VERSION.gem
25
27
 
26
28
  # smartmachine master key
@@ -1,82 +1,107 @@
1
1
  # The main SmartMachine Engine driver
2
2
  module SmartMachine
3
- class Engine < SmartMachine::Base
4
- def initialize
5
- end
6
-
7
- def install
8
- puts "-----> Installing SmartMachine Engine"
9
-
10
- SmartMachine::User.create_htpasswd_files
11
-
12
- ssh = SmartMachine::SSH.new
13
- machine = SmartMachine::Machine.new
14
- sync = SmartMachine::Sync.new
15
-
16
- system("mkdir -p ./tmp/engine")
17
- system("cp #{SmartMachine.config.root_path}/lib/smart_machine/engine/Dockerfile ./tmp/engine/Dockerfile")
18
-
19
- gem_file_path = File.expand_path("../../cache/smartmachine-#{SmartMachine.version}.gem", SmartMachine.config.root_path)
20
- system("cp #{gem_file_path} ./tmp/engine/smartmachine-#{SmartMachine.version}.gem")
21
-
22
- sync.run only: :push
23
-
24
- print "-----> Creating image for SmartMachine ... "
25
- ssh.run "docker image build --quiet --tag #{engine_image_name} \
26
- --build-arg SMARTMACHINE_MASTER_KEY=#{SmartMachine::Credentials.new.read_key} \
27
- --build-arg SMARTMACHINE_VERSION=#{SmartMachine.version} \
28
- --build-arg USER_NAME=`id -un` \
29
- --build-arg USER_UID=`id -u` \
30
- --build-arg DOCKER_GID=`getent group docker | cut -d: -f3` \
31
- ~/.smartmachine/tmp/engine"
32
- puts "done"
33
-
34
- print "-----> Adding SmartMachine to PATH ... "
35
- ssh.run "mkdir -p ~/.smartmachine/bin && touch ~/.smartmachine/bin/smartmachine.sh"
36
- ssh.run "echo '#{smartmachine_binary_template}' > ~/.smartmachine/bin/smartmachine.sh"
37
- ssh.run "chmod +x ~/.smartmachine/bin/smartmachine.sh && sudo ln -sf ~/.smartmachine/bin/smartmachine.sh /usr/local/bin/smartmachine"
38
- puts "done"
39
-
40
- system("rm ./tmp/engine/Dockerfile")
41
- system("rm ./tmp/engine/smartmachine-#{SmartMachine.version}.gem")
42
-
43
- sync.run
44
-
45
- puts "-----> SmartMachine Engine Installation Complete"
46
- end
47
-
48
- def uninstall
49
- puts "-----> Uninstalling SmartMachine Engine"
50
-
51
- ssh = SmartMachine::SSH.new
52
-
53
- ssh.run "sudo rm /usr/local/bin/smartmachine"
54
- ssh.run "docker rmi $(docker images -q smartmachine)"
55
-
56
- puts "-----> SmartMachine Engine Uninstallation Complete"
57
- end
58
-
59
- def update
60
- self.uninstall
61
- self.install
62
- end
63
-
64
- def smartmachine_binary_template
65
- <<~BASH
66
- #!/bin/bash
67
-
68
- docker run -it --rm \
69
- -v "/home/$(whoami)/.smartmachine:/home/$(whoami)/.smartmachine" \
70
- -v "/var/run/docker.sock:/var/run/docker.sock" \
71
- -w "/home/$(whoami)/.smartmachine" \
72
- -u `id -u` \
73
- --entrypoint "smartmachine" \
74
- #{engine_image_name} "$@"
75
- BASH
76
- end
77
-
78
- def engine_image_name
79
- "smartmachine:#{SmartMachine.version}"
80
- end
81
- end
3
+ class Engine < SmartMachine::Base
4
+ def initialize
5
+ @scp = SmartMachine::SCP.new
6
+ @grids_nginx = SmartMachine::Grids::Nginx.new
7
+ @syncer = SmartMachine::Syncer.new
8
+ @machine = SmartMachine::Machine.new
9
+
10
+ @gem_cache_dir = Gem::Specification.find_by_name("smartmachine").cache_dir
11
+ @gem_cache_dir = "../smartmachine/pkg"
12
+
13
+ if platform_on_machine?(os: "linux", distro_name: "debian")
14
+ @docker_gid = "getent group docker | cut -d: -f3"
15
+ @docker_gname = "docker"
16
+ @docker_socket_path = "/var/run/docker.sock"
17
+ @remote_smartmachine_dir = "/home/`whoami`/smartmachine"
18
+ # elsif platform_on_machine?(os: "mac")
19
+ # @docker_gid = "id -g"
20
+ # @docker_gname = "staff"
21
+ # @docker_socket_path = "/Users/`whoami`/Library/Containers/com.docker.docker/Data/docker.sock"
22
+ # @remote_smartmachine_dir = "/Users/`whoami`/Desktop/smartmachine"
23
+ else
24
+ raise("OS not supported to set docker_gid, docker_gname and docker_socket_path")
25
+ end
26
+ end
27
+
28
+ def install
29
+ puts "-----> Installing SmartMachine Engine"
30
+
31
+ if @machine.run_on_machine commands: "mkdir -p #{@remote_smartmachine_dir}/tmp/engine"
32
+ @scp.upload!(local_path: "#{SmartMachine.config.gem_dir}/lib/smart_machine/engine/Dockerfile", remote_path: "~/smartmachine/tmp/engine")
33
+ @scp.upload!(local_path: "#{@gem_cache_dir}/smartmachine-#{SmartMachine.version}.gem", remote_path: "~/smartmachine/tmp/engine")
34
+ end
35
+
36
+ puts "-----> Creating image for Engine ... "
37
+ command = [
38
+ "docker image build --quiet --tag #{engine_image_name_with_version}",
39
+ "--build-arg SMARTMACHINE_MASTER_KEY=#{SmartMachine::Credentials.new.read_key}",
40
+ "--build-arg SMARTMACHINE_VERSION=#{SmartMachine.version}",
41
+ "--build-arg USER_NAME=`id -un`",
42
+ "--build-arg USER_UID=`id -u`",
43
+ "--build-arg DOCKER_GID=`#{@docker_gid}`",
44
+ "--build-arg DOCKER_GNAME=#{@docker_gname}",
45
+ "#{@remote_smartmachine_dir}/tmp/engine"
46
+ ]
47
+ @machine.run_on_machine commands: command.join(" ")
48
+ puts "done"
49
+
50
+ puts "-----> Adding Engine to PATH ... "
51
+ commands = [
52
+ "mkdir -p #{@remote_smartmachine_dir}/bin && touch #{@remote_smartmachine_dir}/bin/smartengine",
53
+ "echo '#{smartengine_binary_template}' > #{@remote_smartmachine_dir}/bin/smartengine",
54
+ "chmod +x #{@remote_smartmachine_dir}/bin/smartengine",
55
+ "sudo ln -sf #{@remote_smartmachine_dir}/bin/smartengine /usr/local/bin/smartengine",
56
+ "rm -r #{@remote_smartmachine_dir}/tmp/engine",
57
+ "smartengine --version"
58
+ ]
59
+ @machine.run_on_machine(commands: commands)
60
+ puts "done"
61
+
62
+ @grids_nginx.create_htpasswd_files
63
+
64
+ @syncer.sync(initial: true)
65
+
66
+ puts "-----> SmartMachine Engine Installation Complete"
67
+ end
68
+
69
+ def uninstall
70
+ puts "-----> Uninstalling SmartMachine Engine"
71
+
72
+ commands = [
73
+ "sudo rm /usr/local/bin/smartengine",
74
+ "sudo rm #{@remote_smartmachine_dir}/bin/smartengine",
75
+ "docker rmi $(docker images -q #{engine_image_name})"
76
+ ]
77
+ @machine.run_on_machine(commands: commands)
78
+
79
+ puts "-----> SmartMachine Engine Uninstallation Complete"
80
+ end
81
+
82
+ private
83
+
84
+ def smartengine_binary_template
85
+ <<~BASH
86
+ #!/bin/bash
87
+
88
+ docker run -i --rm \
89
+ -e INSIDE_ENGINE="yes" \
90
+ -v "#{@remote_smartmachine_dir}:/home/`whoami`/machine" \
91
+ -v "#{@docker_socket_path}:/var/run/docker.sock" \
92
+ -w "/home/`whoami`/machine" \
93
+ -u `id -u` \
94
+ --entrypoint "smartmachine" \
95
+ #{engine_image_name_with_version} "$@"
96
+ BASH
97
+ end
98
+
99
+ def engine_image_name_with_version
100
+ "#{engine_image_name}:#{SmartMachine.version}"
101
+ end
102
+
103
+ def engine_image_name
104
+ "smartmachine/smartengine"
105
+ end
106
+ end
82
107
  end