dokku-ruby 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2c1c3ba24f4e2e7cb273efb5f0da3be07790e636aa0f0c7494f0947d1797688
4
- data.tar.gz: df3ea07a87442ed43a0eb3aa91f3dd679a4d757431055f7392a8e9087e525f54
3
+ metadata.gz: 2e4326cf5810a609f4d73b786316dee42a0d5f57fb169c8158d78fc78e50e3af
4
+ data.tar.gz: db2664bf2c55e26c77467c326a2578c2677a8c16a5cf2fb885b4130fc41f3e15
5
5
  SHA512:
6
- metadata.gz: 26f4956de70aba19cc7527535065d8eb3012f20f3640ed9fdfc148f1a625d3be291c7b646ae4f497abbc5c1dab3cd3f89c976772a98fb3b7e94584993ec73c69
7
- data.tar.gz: d5829309b50407a0590b0d704e9227c1009e66a145b207fa543f1039e10fc61358da95a73750716578dc4607509c0aed6544d6a036aa06f12f63dfb6061e1153
6
+ metadata.gz: d2c497d00df8ae74350a839f2d49d9482cf3a5f8442e12ba69d87df851d8dc47981e89acdb1c5a98e2793d14e20bca97f885d171f76c4bad869de4c44ed56b1e
7
+ data.tar.gz: ad1b909a7a02c864d2804110c40aa9e1a6826158f32006e59ce3734d09796e5b353bb662227725ea0918114fe4c88a94d326963806a0a7131c017bce75468bf1
data/Gemfile CHANGED
@@ -5,10 +5,12 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in dokku-ruby.gemspec
6
6
  gemspec
7
7
 
8
- gem 'rake', '~> 12.0'
9
- gem 'rspec', '~> 3.9'
10
-
11
- # net-ssh
12
8
  gem 'bcrypt_pbkdf', '~> 1.0.1'
13
9
  gem 'ed25519', '~> 1.2.4'
14
- gem 'net-ssh', '~> 5.2.0'
10
+ gem 'sshkit', '~>1.21.0'
11
+
12
+ group :development, :test do
13
+ gem 'pry'
14
+ gem 'rake', '~> 12.0'
15
+ gem 'rspec', '~> 3.9'
16
+ end
@@ -1,15 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dokku-ruby (0.1.0)
4
+ dokku-ruby (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  bcrypt_pbkdf (1.0.1)
10
+ coderay (1.1.3)
10
11
  diff-lcs (1.4.4)
11
12
  ed25519 (1.2.4)
12
- net-ssh (5.2.0)
13
+ method_source (1.0.0)
14
+ net-scp (3.0.0)
15
+ net-ssh (>= 2.6.5, < 7.0.0)
16
+ net-ssh (6.1.0)
17
+ pry (0.13.1)
18
+ coderay (~> 1.1)
19
+ method_source (~> 1.0)
13
20
  rake (12.3.3)
14
21
  rspec (3.9.0)
15
22
  rspec-core (~> 3.9.0)
@@ -24,6 +31,9 @@ GEM
24
31
  diff-lcs (>= 1.2.0, < 2.0)
25
32
  rspec-support (~> 3.9.0)
26
33
  rspec-support (3.9.3)
34
+ sshkit (1.21.0)
35
+ net-scp (>= 1.1.2)
36
+ net-ssh (>= 2.8.0)
27
37
 
28
38
  PLATFORMS
29
39
  ruby
@@ -32,9 +42,10 @@ DEPENDENCIES
32
42
  bcrypt_pbkdf (~> 1.0.1)
33
43
  dokku-ruby!
34
44
  ed25519 (~> 1.2.4)
35
- net-ssh (~> 5.2.0)
45
+ pry
36
46
  rake (~> 12.0)
37
47
  rspec (~> 3.9)
48
+ sshkit (~> 1.21.0)
38
49
 
39
50
  BUNDLED WITH
40
51
  2.1.2
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Dokku::Ruby
2
2
 
3
- Small [Dokku](https://github.com/dokku/dokku) API for Ruby over SSH
3
+ Small [Dokku](https://github.com/dokku/dokku) API for Ruby over SSHKit.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,26 +20,20 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Start a Dokku Session:
23
+ Configure the Dokku client with desired set user, host and ssh options.
24
+
24
25
  ```
25
- ssh = Dokku::SSH.new(
26
- host: 'http://example.com',
27
- user: 'deploy-user',
28
- key_file: '~/.ssh/id_dokku'
29
- )
26
+ Dokku.config do |c|
27
+ c.user = 'root'
28
+ c.host = 'tradingbotplatformbook.com'
29
+ c.ssh_options = { keys: '~/.ssh/id_trading_platform' }
30
+ end
30
31
  ```
31
32
 
32
- Use that session to create apps on server:
33
-
33
+ Start a session, use the yielded block argument's DSL to create, delete and set environments for Dokku apps
34
34
  ```
35
- ssh.start do |session|
36
- env = { env_var_one: '123', env_var_two: '456' }
37
-
38
- # Create a new Dokku application on the server
39
- session.create(app_name: 'my-dokku-app')
40
-
41
- # Set the environment config for that application
42
- session.set_env_vars(app_name: app_name, env_vars: env)
35
+ Dokku.start_session! do |d|
36
+ d.create!('app')
43
37
  end
44
38
  ```
45
39
 
@@ -1,10 +1,55 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'sshkit'
4
+ require 'sshkit/dsl'
3
5
  require 'dokku/version'
4
- require 'dokku/session'
5
- require 'dokku/ssh'
6
+ require 'dokku/configuration'
7
+ require 'dokku/dsl'
6
8
 
7
9
  module Dokku
8
- class Error < StandardError; end
9
- # Your code goes here...
10
+ extend SSHKit::DSL
11
+
12
+ class << self
13
+ attr_writer :configuration
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def self.config
21
+ yield(configuration)
22
+
23
+ SSHKit::Backend::Netssh.configure do |ssh|
24
+ unless configuration.ssh_options.nil?
25
+ ssh.ssh_options = configuration.ssh_options
26
+ end
27
+ end
28
+ end
29
+
30
+ class DokkuError < StandardError; end
31
+ class MissingUserError < DokkuError; end
32
+ class MissingHostError < DokkuError; end
33
+
34
+ def self.start_session!
35
+ on ssh_target do
36
+ # self is an SSH context
37
+ yield Dokku::DSL.new(self)
38
+ end
39
+ end
40
+
41
+ def self.ssh_target
42
+ user = configuration.user
43
+ host = configuration.host
44
+
45
+ if user.nil? || user == ''
46
+ raise MissingUserError, 'User not set. Did you add it to the configuration?'
47
+ end
48
+
49
+ if host.nil? || host == ''
50
+ raise MissingHostError, 'Host is not set. Did you add it to the configuration?'
51
+ end
52
+
53
+ "#{user}@#{host}"
54
+ end
10
55
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dokku
4
+ class Configuration
5
+ attr_accessor :ssh_options, :host, :user
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sshkit/dsl'
4
+
5
+ module Dokku
6
+ class DSL
7
+ include SSHKit::DSL
8
+
9
+ CREATE_APP_CMD = 'apps:create'
10
+ CONFIG_SET_CMD = 'config:set'
11
+ FORCE_DELETE_APP_CMD = '--force apps:destroy'
12
+
13
+ def initialize(runner, logger = Logger.new(STDOUT))
14
+ @runner = runner
15
+ @logger = logger
16
+ end
17
+
18
+ def create!(app_name)
19
+ @logger.info("dokku #{CREATE_APP_CMD} #{app_name}")
20
+ @runner.execute :dokku, CREATE_APP_CMD, app_name
21
+ end
22
+
23
+ def destroy!(app_name)
24
+ @logger.info("dokku #{FORCE_DELETE_APP_CMD} #{app_name}")
25
+ @runner.execute :dokku, FORCE_DELETE_APP_CMD, app_name
26
+ end
27
+
28
+ def set_config(app_name:, **config)
29
+ @logger.info("dokku #{CONFIG_SET_CMD} #{app_name} REDACTED")
30
+ @runner.execute :dokku, CONFIG_SET_CMD, app_name, redact(hash_to_env_vars(config))
31
+ end
32
+
33
+ private
34
+
35
+ def hash_to_env_vars(hash)
36
+ hash.map do |k, v|
37
+ "#{k.upcase}=#{v}"
38
+ end.join(' ')
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dokku
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dokku-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - robertsimoes
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-12 00:00:00.000000000 Z
11
+ date: 2020-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -42,8 +42,8 @@ files:
42
42
  - bin/setup
43
43
  - dokku-ruby.gemspec
44
44
  - lib/dokku.rb
45
- - lib/dokku/session.rb
46
- - lib/dokku/ssh.rb
45
+ - lib/dokku/configuration.rb
46
+ - lib/dokku/dsl.rb
47
47
  - lib/dokku/version.rb
48
48
  homepage:
49
49
  licenses:
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'logger'
4
-
5
- module Dokku
6
- class Session
7
- def initialize(session, logger = Logger.new(STDOUT))
8
- @session = session
9
- @logger = logger
10
- end
11
-
12
- def create(app_name:)
13
- command = "dokku apps:create #{app_name}"
14
- @logger.info command
15
- @logger.info @session.exec!(command)
16
- end
17
-
18
- def set_env_vars(app_name:, env_vars:)
19
- env = hash_to_env_vars(env_vars)
20
-
21
- command = "dokku config:set #{app_name} #{env}"
22
-
23
- stdout = ''
24
- @logger.info "dokku config:set #{app_name}"
25
- @session.exec!(command) do |_channel, stream, data|
26
- stdout << redact_sensitive(data) if stream == :stdout
27
- end
28
-
29
- @logger.info stdout
30
- end
31
-
32
- def destroy(app_name:)
33
- command = "dokku --force apps:destroy #{app_name}"
34
- @logger.info command
35
- @logger.info @session.exec!(command)
36
- end
37
-
38
- private
39
-
40
- def hash_to_env_vars(hash)
41
- hash.map do |k, v|
42
- "#{k.upcase}=#{v}"
43
- end.join(' ')
44
- end
45
-
46
- def redact_sensitive(s)
47
- redactable = s.scan(/KEY|SECRET|TOKEN|PASSPHRASE/)
48
-
49
- if redactable&.empty?
50
- s
51
- else
52
- delimiter = '=' * 25
53
- "\n#{delimiter}\n\nOUTPUT REDACTED\n\n#{delimiter}\n"
54
- end
55
- end
56
- end
57
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'net/ssh'
4
-
5
- module Dokku
6
- class SSH
7
- def initialize(**params)
8
- @host = params[:host]
9
- @user = params[:user]
10
- @key_file = params[:key_file]
11
- @keys_only = params[:keys_only] ||= true
12
- @key_type = params[:key_type] ||= 'ssh-rsa'
13
- end
14
-
15
- def start
16
- Net::SSH.start(
17
- @host,
18
- @user,
19
- host_key: @key_type,
20
- keys_only: @keys_only,
21
- keys: @key_file,
22
- verbose: :error
23
- ) do |s|
24
- yield Dokku::Session.new(s)
25
- end
26
- end
27
- end
28
- end