deployless 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bfc7b104a8c5204bd2bcc0bc4cb003342696d899e734a1d5cfffa3193f78b36
4
- data.tar.gz: c08743ddc2790ef4e72400421ed0fa2b825069078b5ef097a2e66613bc8e00f5
3
+ metadata.gz: bcace00a79090e96ceb2880e81bd88b47135f8bc24a7df05ade8c8d74c80eeff
4
+ data.tar.gz: b45efffec748c79fb102ca229eb920fccc0fa3acc1ef92f89a902d5ad16f43cc
5
5
  SHA512:
6
- metadata.gz: 9f4461a61ccd5663588e4211592071fbcfb1be989b248a2b8fb9ccf4e1e308ffc608f02b14ea6b84159ac2b3c311df1d8304530b859e033ece5c4584bbfb4601
7
- data.tar.gz: c8077628d33494a0ac02deeccc6ea992e9fde3d87d7f28ab152c4a4884ccbd1f0b7d2f04c0fd7cf43b1d483df1a9e3a53cf38d4d4c8b800aae758809863918fa
6
+ metadata.gz: 34f41ca5148c5e350a054900788f8ec2530bd1fdca6d8df4086d19b465e0d34d096d4c8ac07f1de06859d211baa0466640365d03bedbab7c3329ce80ebbb8b9f
7
+ data.tar.gz: 86fd43372c7603a8695dafa02545e435b5cc3396720998c2ecd90fe80bac8e9792aee6ce8fc063fa0751d8f7158a3ec1952328eaec10e2764eacc3d989204044
data/bin/dpls CHANGED
@@ -1,9 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'tty-option'
4
- require 'tty-prompt'
5
- require 'tty-file'
6
- require 'yaml'
3
+ require_relative '../lib/deployless'
7
4
 
8
5
  class Command
9
6
  include TTY::Option
@@ -11,6 +8,10 @@ class Command
11
8
  argument :command do
12
9
  desc "The command to run"
13
10
  end
11
+
12
+ argument :task do
13
+ desc "The task to run"
14
+ end
14
15
  end
15
16
 
16
17
  command = Command.new
@@ -19,21 +20,52 @@ command.parse(ARGV)
19
20
  case command.params['command']
20
21
  when 'init'
21
22
  prompt = TTY::Prompt.new
22
- result = prompt.collect do
23
+ configuration = prompt.collect do
23
24
  key(:deployment_setup).enum_select("Deployment setup:", ["Dokku"], required: true)
24
25
  key(:background_job_processor).enum_select("Background job processor:", ["None", "Sidekiq"], required: true)
25
- key(:production_server_ip).ask("Production server IP:", required: true)
26
- key(:production_server_username).ask("Production server username:", required: true, default: 'ubuntu')
27
- key(:ssh_key_path).ask("SSH key path:", required: true, default: '~/.ssh/id_rsa')
28
- key(:email).ask("Email:", required: true)
29
26
  key(:app_name).ask("Application name:", required: true)
27
+ key(:support_for_staging_environment).enum_select("Support for staging environment?:", ["Yes", "No"], required: true)
28
+ end
29
+
30
+ environments = %i[production]
31
+ environments << :staging if configuration.fetch(:support_for_staging_environment) == 'Yes'
32
+
33
+ environments.each do |environment|
34
+ puts "Configuring #{environment} environment"
35
+ configuration[environment] = prompt.collect do
36
+ key(:server_ip).ask("Server IP:", required: true)
37
+ key(:ssh_key_path).ask("SSH key path:", required: true, default: '~/.ssh/id_rsa')
38
+ key(:server_username).ask("Server username:", required: true, default: 'ubuntu')
39
+ key(:email).ask("Email:", required: true)
40
+ key(:domain).ask("Domain:", required: true)
41
+ end
30
42
  end
31
43
 
32
- TTY::File.create_file('.deployless.yml', result.to_yaml)
44
+ TTY::File.create_file('.deployless.yml', configuration.to_yaml)
33
45
  TTY::File.safe_append_to_file(".gitignore", ".deployless.yml")
34
- when 'prepare'
35
- config = YAML.load_file('.deployless.yml')
36
- puts "Config: #{config.inspect}"
46
+ when 'production', 'staging'
47
+ case command.params['task']
48
+ when 'console'
49
+ config = YAML.load_file('.deployless.yml')
50
+ raise ArgumentError, "Environment #{command.params['command']} is not configured" if config[command.params['command'].to_sym].nil?
51
+
52
+ provider = ::Deployless::Providers::DokkuProvider.new(
53
+ config: config,
54
+ environment: command.params['command']
55
+ )
56
+ provider.run_console
57
+ when 'prepare'
58
+ config = YAML.load_file('.deployless.yml')
59
+ raise ArgumentError, "Environment #{command.params['command']} is not configured" if config[command.params['command'].to_sym].nil?
60
+
61
+ provider = ::Deployless::Providers::DokkuProvider.new(
62
+ config: config,
63
+ environment: command.params['command']
64
+ )
65
+ provider.configure_environment
66
+ else
67
+ puts 'Unknown task'
68
+ end
37
69
  else
38
70
  puts 'Unknown command'
39
71
  end
@@ -0,0 +1,194 @@
1
+ require 'sshkit'
2
+ require 'sshkit/dsl'
3
+ require 'securerandom'
4
+
5
+ module Deployless
6
+ module Providers
7
+ class DokkuProvider
8
+ include SSHKit::DSL
9
+
10
+ AppAlreadyExists = Class.new(StandardError)
11
+
12
+ VERSION = '0.35.12'
13
+
14
+ def initialize(config:, environment:)
15
+ @config = config
16
+ @environment = environment
17
+ end
18
+
19
+ def run_console
20
+ server_ip = env_config.fetch(:server_ip)
21
+
22
+ puts "Connecting to #{@environment} rails console..."
23
+ system("ssh -t dokku@#{server_ip} enter #{app_name} web rails c")
24
+ end
25
+
26
+ def configure_environment
27
+ configure
28
+ install
29
+ add_ssh_key
30
+ create_app
31
+ set_initial_environment_variables
32
+ install_postgres
33
+ install_redis if @config.fetch(:background_job_processor) == 'Sidekiq'
34
+
35
+ result = "No"
36
+ prompt = TTY::Prompt.new
37
+
38
+ while result == "No"
39
+ result = prompt.enum_select("Add DNS record for the domain #{env_config.fetch(:domain)} - type: A, value: #{env_config.fetch(:server_ip)}. Is it done?", ["Yes", "No"], required: true)
40
+ if result == "No"
41
+ puts "Please add DNS record for the domain #{env_config.fetch(:domain)} - type: A, value: #{env_config.fetch(:server_ip)} and confirm when done."
42
+ end
43
+ end
44
+
45
+ configure_domain
46
+ configure_ssl
47
+ print_instructions
48
+ end
49
+
50
+ private
51
+
52
+ def configure
53
+ SSHKit::Backend::Netssh.configure do |ssh|
54
+ ssh.connection_timeout = 30
55
+ ssh.ssh_options = {
56
+ user: env_config.fetch(:server_username),
57
+ keys: %w(env_config.fetch(:ssh_key_path)),
58
+ forward_agent: false,
59
+ auth_methods: %w(publickey)
60
+ }
61
+ end
62
+ end
63
+
64
+ def install
65
+ on [env_config.fetch(:server_ip)] do |host|
66
+ unless test "dokku"
67
+ execute :wget, '-NP', '.', 'https://dokku.com/bootstrap.sh'
68
+ execute :sudo, "DOKKU_TAG=#{VERSION}", 'bash', 'bootstrap.sh'
69
+ end
70
+ end
71
+ end
72
+
73
+ def add_ssh_key
74
+ public_key = File.read(File.expand_path(env_config.fetch(:ssh_key_path) + '.pub'))
75
+
76
+ on [env_config.fetch(:server_ip)] do |host|
77
+ unless test :dokku, 'ssh-keys:list', 'admin'
78
+ execute "echo '#{public_key}' | sudo dokku ssh-keys:add admin"
79
+ end
80
+ end
81
+ end
82
+
83
+ def create_app
84
+ app = app_name
85
+ on [env_config.fetch(:server_ip)] do |host|
86
+ if test :dokku, 'apps:exists', app
87
+ raise AppAlreadyExists, "App #{app} is already created"
88
+ else
89
+ execute :dokku, 'apps:create', app
90
+ end
91
+ end
92
+ end
93
+
94
+ def set_initial_environment_variables
95
+ secret_key_base = SecureRandom.hex(64)
96
+
97
+ environment_variables = {
98
+ 'SECRET_KEY_BASE' => secret_key_base,
99
+ 'RAILS_ENV' => @environment,
100
+ 'RAKE_ENV' => @environment,
101
+ 'RAILS_LOG_TO_STDOUT' => 'enabled',
102
+ 'RAILS_SERVE_STATIC_FILES' => 'enabled'
103
+ }
104
+
105
+ app = app_name
106
+ on [env_config.fetch(:server_ip)] do |host|
107
+ execute :dokku, 'config:set', app, environment_variables.map { |key, value| "#{key}=#{value}" }.join(' ')
108
+ end
109
+ end
110
+
111
+ def install_postgres
112
+ app = app_name
113
+ on [env_config.fetch(:server_ip)] do |host|
114
+ unless test :dokku, 'postgres'
115
+ execute :sudo, 'dokku', 'plugin:install', 'https://github.com/dokku/dokku-postgres.git'
116
+ end
117
+
118
+ unless test :dokku, 'postgres:exists', "#{app}-db"
119
+ execute :dokku, 'postgres:create', "#{app}-db"
120
+ end
121
+
122
+ unless test :dokku, 'postgres:linked', "#{app}-db", app
123
+ execute :dokku, 'postgres:link', "#{app}-db", app
124
+ end
125
+ end
126
+ end
127
+
128
+ def install_redis
129
+ app = app_name
130
+ on [env_config.fetch(:server_ip)] do |host|
131
+ unless test :dokku, 'redis'
132
+ execute :sudo, 'dokku', 'plugin:install', 'https://github.com/dokku/dokku-redis.git'
133
+ end
134
+
135
+ unless test :dokku, 'redis:exists', "#{app}-redis"
136
+ execute :dokku, 'redis:create', "#{app}-redis"
137
+ end
138
+
139
+ unless test :dokku, 'redis:linked', "#{app}-redis", app
140
+ execute :dokku, 'redis:link', "#{app}-redis", app
141
+ end
142
+ end
143
+ end
144
+
145
+ def configure_domain
146
+ domain = env_config.fetch(:domain)
147
+ app = app_name
148
+
149
+ on [env_config.fetch(:server_ip)] do |host|
150
+ unless test("dokku domains:report #{app} | grep #{domain}")
151
+ execute :dokku, 'domains:clear-global'
152
+ execute :dokku, 'domains:clear', app
153
+ execute :dokku, 'domains:add', app, domain
154
+ end
155
+ end
156
+ end
157
+
158
+ def configure_ssl
159
+ email = env_config.fetch(:email)
160
+ app = app_name
161
+
162
+ on [env_config.fetch(:server_ip)] do |host|
163
+ unless test :dokku, 'letsencrypt'
164
+ execute :sudo, 'dokku', 'plugin:install', 'https://github.com/dokku/dokku-letsencrypt.git'
165
+ end
166
+
167
+ unless test("dokku letsencrypt:list | grep #{app}")
168
+ execute :dokku, 'letsencrypt:set', app, 'email', email
169
+ execute :dokku, 'letsencrypt:enable', app
170
+ end
171
+ end
172
+ end
173
+
174
+ def print_instructions
175
+ puts "Configure deployment by adding git remote:"
176
+ puts "git remote add dokku dokku@#{env_config.fetch(:server_ip)}:#{app_name}"
177
+ puts "Then you can deploy by running:"
178
+ puts "git push dokku main"
179
+ end
180
+
181
+ def env_config
182
+ @config.fetch(@environment.to_sym)
183
+ end
184
+
185
+ def app_name
186
+ if @environment == 'production'
187
+ @config.fetch(:app_name)
188
+ else
189
+ "#{@config.fetch(:app_name)}-#{@environment}"
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
@@ -3,7 +3,7 @@ module Deployless
3
3
  module_function
4
4
 
5
5
  def to_s
6
- '0.0.1'
6
+ '0.0.3'
7
7
  end
8
8
  end
9
9
  end
data/lib/deployless.rb CHANGED
@@ -0,0 +1,6 @@
1
+ require 'tty-option'
2
+ require 'tty-prompt'
3
+ require 'tty-file'
4
+ require 'yaml'
5
+
6
+ require_relative 'deployless/providers/dokku_provider'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deployless
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paweł Dąbrowski
@@ -109,6 +109,7 @@ extra_rdoc_files: []
109
109
  files:
110
110
  - bin/dpls
111
111
  - lib/deployless.rb
112
+ - lib/deployless/providers/dokku_provider.rb
112
113
  - lib/deployless/version.rb
113
114
  homepage: https://github.com/impactahead/deployless
114
115
  licenses: