dip 4.2.0 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dip/cli/dns.rb CHANGED
@@ -1,30 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'thor'
3
+ require "thor"
4
+ require_relative "./base"
4
5
  require_relative "../commands/dns"
5
6
 
6
7
  module Dip
7
8
  class CLI
8
9
  # See more https://github.com/aacebedo/dnsdock
9
- class DNS < Thor
10
+ class DNS < Base
10
11
  desc "up", "Run dnsdock container"
11
- method_option :help, aliases: '-h', type: :boolean,
12
- desc: 'Display usage information'
13
- method_option :name, aliases: '-n', type: :string, default: "dnsdock",
14
- desc: 'Container name'
15
- method_option :socket, aliases: '-s', type: :string, default: "/var/run/docker.sock",
16
- desc: 'Path to docker socket'
17
- method_option :net, aliases: '-t', type: :string, default: "frontend",
18
- desc: 'Container network name'
19
- method_option :publish, aliases: '-p', type: :string, default: "53/udp",
20
- desc: 'Container port'
21
- method_option :image, aliases: '-i', type: :string, default: "aacebedo/dnsdock:latest-amd64",
22
- desc: 'Docker image name'
23
- method_option :domain, aliases: '-d', type: :string, default: "docker",
24
- desc: 'Top level domain'
12
+ method_option :help, aliases: "-h", type: :boolean,
13
+ desc: "Display usage information"
14
+ method_option :name, aliases: "-n", type: :string, default: "dnsdock",
15
+ desc: "Container name"
16
+ method_option :socket, aliases: "-s", type: :string, default: "/var/run/docker.sock",
17
+ desc: "Path to docker socket"
18
+ method_option :net, aliases: "-t", type: :string, default: "frontend",
19
+ desc: "Container network name"
20
+ method_option :publish, aliases: "-p", type: :string, default: "53/udp",
21
+ desc: "Container port"
22
+ method_option :image, aliases: "-i", type: :string, default: "aacebedo/dnsdock:latest-amd64",
23
+ desc: "Docker image name"
24
+ method_option :domain, aliases: "-d", type: :string, default: "docker",
25
+ desc: "Top level domain"
25
26
  def up
26
27
  if options[:help]
27
- invoke :help, ['up']
28
+ invoke :help, ["up"]
28
29
  else
29
30
  Dip::Commands::DNS::Up.new(
30
31
  name: options.fetch(:name),
@@ -38,13 +39,13 @@ module Dip
38
39
  end
39
40
 
40
41
  desc "down", "Stop dnsdock container"
41
- method_option :help, aliases: '-h', type: :boolean,
42
- desc: 'Display usage information'
43
- method_option :name, aliases: '-n', type: :string, default: "dnsdock",
44
- desc: 'Container name'
42
+ method_option :help, aliases: "-h", type: :boolean,
43
+ desc: "Display usage information"
44
+ method_option :name, aliases: "-n", type: :string, default: "dnsdock",
45
+ desc: "Container name"
45
46
  def down
46
47
  if options[:help]
47
- invoke :help, ['down']
48
+ invoke :help, ["down"]
48
49
  else
49
50
  Dip::Commands::DNS::Down.new(
50
51
  name: options.fetch(:name)
@@ -53,11 +54,11 @@ module Dip
53
54
  end
54
55
 
55
56
  desc "restart", "Stop and start dnsdock container"
56
- method_option :help, aliases: '-h', type: :boolean,
57
- desc: 'Display usage information'
57
+ method_option :help, aliases: "-h", type: :boolean,
58
+ desc: "Display usage information"
58
59
  def restart(*args)
59
60
  if options[:help]
60
- invoke :help, ['restart']
61
+ invoke :help, ["restart"]
61
62
  else
62
63
  Dip::CLI::DNS.start(["down"] + args)
63
64
  sleep 1
@@ -66,15 +67,15 @@ module Dip
66
67
  end
67
68
 
68
69
  desc "ip", "Get ip address of dnsdock container"
69
- method_option :help, aliases: '-h', type: :boolean,
70
- desc: 'Display usage information'
71
- method_option :name, aliases: '-n', type: :string, default: "dnsdock",
72
- desc: 'Container name'
73
- method_option :net, aliases: '-t', type: :string, default: "frontend",
74
- desc: 'Container network name'
70
+ method_option :help, aliases: "-h", type: :boolean,
71
+ desc: "Display usage information"
72
+ method_option :name, aliases: "-n", type: :string, default: "dnsdock",
73
+ desc: "Container name"
74
+ method_option :net, aliases: "-t", type: :string, default: "frontend",
75
+ desc: "Container network name"
75
76
  def ip
76
77
  if options[:help]
77
- invoke :help, ['status']
78
+ invoke :help, ["status"]
78
79
  else
79
80
  Dip::Commands::DNS::IP.new(
80
81
  name: options.fetch(:name),
data/lib/dip/cli/nginx.rb CHANGED
@@ -1,31 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'thor'
3
+ require "thor"
4
+ require_relative "./base"
4
5
  require_relative "../commands/nginx"
5
6
 
6
7
  module Dip
7
8
  class CLI
8
9
  # See more https://github.com/bibendi/nginx-proxy
9
- class Nginx < Thor
10
+ class Nginx < Base
10
11
  desc "up", "Run nginx container"
11
- method_option :help, aliases: '-h', type: :boolean,
12
- desc: 'Display usage information'
13
- method_option :name, aliases: '-n', type: :string, default: "nginx",
14
- desc: 'Container name'
15
- method_option :socket, aliases: '-s', type: :string, default: "/var/run/docker.sock",
16
- desc: 'Path to docker socket'
17
- method_option :net, aliases: '-t', type: :string, default: "frontend",
18
- desc: 'Container network name'
19
- method_option :publish, aliases: '-p', type: :array, default: "80:80",
20
- desc: 'Container port(s). For more than one port, separate them by a space'
21
- method_option :image, aliases: '-i', type: :string, default: "bibendi/nginx-proxy:latest",
22
- desc: 'Docker image name'
23
- method_option :domain, aliases: '-d', type: :string, default: "docker",
24
- desc: 'Top level domain'
25
- method_option :certs, aliases: '-c', type: :string, desc: 'Path to ssl certificates'
12
+ method_option :help, aliases: "-h", type: :boolean,
13
+ desc: "Display usage information"
14
+ method_option :name, aliases: "-n", type: :string, default: "nginx",
15
+ desc: "Container name"
16
+ method_option :socket, aliases: "-s", type: :string, default: "/var/run/docker.sock",
17
+ desc: "Path to docker socket"
18
+ method_option :net, aliases: "-t", type: :string, default: "frontend",
19
+ desc: "Container network name"
20
+ method_option :publish, aliases: "-p", type: :array, default: ["80:80"],
21
+ desc: "Container port(s). For more than one port, separate them by a space"
22
+ method_option :image, aliases: "-i", type: :string, default: "bibendi/nginx-proxy:latest",
23
+ desc: "Docker image name"
24
+ method_option :domain, aliases: "-d", type: :string, default: "docker",
25
+ desc: "Top level domain"
26
+ method_option :certs, aliases: "-c", type: :string, desc: "Path to ssl certificates"
26
27
  def up
27
28
  if options[:help]
28
- invoke :help, ['up']
29
+ invoke :help, ["up"]
29
30
  else
30
31
  Dip::Commands::Nginx::Up.new(
31
32
  name: options.fetch(:name),
@@ -40,13 +41,13 @@ module Dip
40
41
  end
41
42
 
42
43
  desc "down", "Stop nginx container"
43
- method_option :help, aliases: '-h', type: :boolean,
44
- desc: 'Display usage information'
45
- method_option :name, aliases: '-n', type: :string, default: "nginx",
46
- desc: 'Container name'
44
+ method_option :help, aliases: "-h", type: :boolean,
45
+ desc: "Display usage information"
46
+ method_option :name, aliases: "-n", type: :string, default: "nginx",
47
+ desc: "Container name"
47
48
  def down
48
49
  if options[:help]
49
- invoke :help, ['down']
50
+ invoke :help, ["down"]
50
51
  else
51
52
  Dip::Commands::Nginx::Down.new(
52
53
  name: options.fetch(:name)
@@ -55,11 +56,11 @@ module Dip
55
56
  end
56
57
 
57
58
  desc "restart", "Stop and start nginx container"
58
- method_option :help, aliases: '-h', type: :boolean,
59
- desc: 'Display usage information'
59
+ method_option :help, aliases: "-h", type: :boolean,
60
+ desc: "Display usage information"
60
61
  def restart(*args)
61
62
  if options[:help]
62
- invoke :help, ['restart']
63
+ invoke :help, ["restart"]
63
64
  else
64
65
  Dip::CLI::Nginx.start(["down"] + args)
65
66
  sleep 1
data/lib/dip/cli/ssh.rb CHANGED
@@ -1,31 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'thor'
3
+ require "thor"
4
+ require_relative "./base"
4
5
  require_relative "../commands/ssh"
5
6
 
6
7
  module Dip
7
8
  class CLI
8
- class SSH < Thor
9
+ class SSH < Base
9
10
  desc "up", "Run ssh-agent container"
10
- method_option :help, aliases: '-h', type: :boolean,
11
- desc: 'Display usage information'
12
- method_option :key, aliases: '-k', type: :string, default: "$HOME/.ssh/id_rsa",
13
- desc: 'Path to ssh key'
14
- method_option :volume, aliases: '-v', type: :string, default: "$HOME",
15
- desc: 'Mounted docker volume'
16
- method_option :interactive, aliases: '-t', type: :boolean, default: true,
17
- desc: 'Run in interactive mode'
11
+ method_option :help, aliases: "-h", type: :boolean,
12
+ desc: "Display usage information"
13
+ method_option :key, aliases: "-k", type: :string, default: "$HOME/.ssh/id_rsa",
14
+ desc: "Path to ssh key"
15
+ method_option :volume, aliases: "-v", type: :string, default: "$HOME",
16
+ desc: "Mounted docker volume"
17
+ method_option :interactive, aliases: "-t", type: :boolean, default: true,
18
+ desc: "Run in interactive mode"
19
+ method_option :user, aliases: "-u", type: :string,
20
+ desc: "UID for ssh-agent container"
18
21
  # Backward compatibility
19
- method_option :nonteractive, aliases: '-T', type: :boolean,
20
- desc: 'Run in noninteractive mode'
22
+ method_option :nonteractive, aliases: "-T", type: :boolean,
23
+ desc: "Run in noninteractive mode"
21
24
  def up
22
25
  if options[:help]
23
- invoke :help, ['up']
26
+ invoke :help, ["up"]
24
27
  else
25
28
  Dip::Commands::SSH::Up.new(
26
29
  key: options.fetch(:key),
27
30
  volume: options.fetch(:volume),
28
- interactive: options.nonteractive? ? false : options.interactive?
31
+ interactive: options.nonteractive? ? false : options.interactive?,
32
+ user: options.user
29
33
  ).execute
30
34
  end
31
35
  end
@@ -33,22 +37,22 @@ module Dip
33
37
  map add: :up
34
38
 
35
39
  desc "down", "Stop ssh-agent container"
36
- method_option :help, aliases: '-h', type: :boolean,
37
- desc: 'Display usage information'
40
+ method_option :help, aliases: "-h", type: :boolean,
41
+ desc: "Display usage information"
38
42
  def down
39
43
  if options[:help]
40
- invoke :help, ['down']
44
+ invoke :help, ["down"]
41
45
  else
42
46
  Dip::Commands::SSH::Down.new.execute
43
47
  end
44
48
  end
45
49
 
46
50
  desc "restart", "Stop and start ssh-agent container"
47
- method_option :help, aliases: '-h', type: :boolean,
48
- desc: 'Display usage information'
51
+ method_option :help, aliases: "-h", type: :boolean,
52
+ desc: "Display usage information"
49
53
  def restart(*args)
50
54
  if options[:help]
51
- invoke :help, ['restart']
55
+ invoke :help, ["restart"]
52
56
  else
53
57
  Dip::CLI::SSH.start(["down"] + args)
54
58
  sleep 1
@@ -57,11 +61,11 @@ module Dip
57
61
  end
58
62
 
59
63
  desc "status", "Show status of ssh-agent container"
60
- method_option :help, aliases: '-h', type: :boolean,
61
- desc: 'Display usage information'
64
+ method_option :help, aliases: "-h", type: :boolean,
65
+ desc: "Display usage information"
62
66
  def status
63
67
  if options[:help]
64
- invoke :help, ['status']
68
+ invoke :help, ["status"]
65
69
  else
66
70
  Dip::Commands::SSH::Status.new.execute
67
71
  end
data/lib/dip/command.rb CHANGED
@@ -9,15 +9,15 @@ module Dip
9
9
  def_delegators self, :shell, :subshell
10
10
 
11
11
  class ExecRunner
12
- def self.call(cmd, argv, env: {}, **options)
13
- ::Process.exec(env, cmd, *argv, options)
12
+ def self.call(cmdline, env: {}, **options)
13
+ ::Process.exec(env, cmdline, options)
14
14
  end
15
15
  end
16
16
 
17
17
  class SubshellRunner
18
- def self.call(cmd, argv, env: {}, panic: true, **options)
19
- return if ::Kernel.system(env, cmd, *argv, options)
20
- raise Dip::Error, "Command '#{([cmd] + argv).join(' ')}' executed with error." if panic
18
+ def self.call(cmdline, env: {}, panic: true, **options)
19
+ return if ::Kernel.system(env, cmdline, options)
20
+ raise Dip::Error, "Command '#{cmdline}' executed with error." if panic
21
21
  end
22
22
  end
23
23
 
@@ -25,11 +25,12 @@ module Dip
25
25
  def shell(cmd, argv = [], subshell: false, **options)
26
26
  cmd = Dip.env.interpolate(cmd)
27
27
  argv = argv.map { |arg| Dip.env.interpolate(arg) }
28
+ cmdline = [cmd, *argv].compact.join(" ")
28
29
 
29
- puts [Dip.env.vars, cmd, argv].inspect if Dip.debug?
30
+ puts [Dip.env.vars, cmdline].inspect if Dip.debug?
30
31
 
31
32
  runner = subshell ? SubshellRunner : ExecRunner
32
- runner.call(cmd, argv, env: Dip.env.vars, **options)
33
+ runner.call(cmdline, env: Dip.env.vars, **options)
33
34
  end
34
35
 
35
36
  def subshell(*args, **kwargs)
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../command'
4
- require_relative 'dns'
3
+ require "pathname"
4
+
5
+ require_relative "../command"
6
+ require_relative "dns"
5
7
 
6
8
  module Dip
7
9
  module Commands
@@ -18,7 +20,7 @@ module Dip
18
20
  def execute
19
21
  Dip.env["DIP_DNS"] ||= find_dns
20
22
 
21
- compose_argv = Array(find_files) + Array(find_project_name) + argv
23
+ compose_argv = Array(find_files) + Array(cli_options) + argv
22
24
 
23
25
  shell("docker-compose", compose_argv)
24
26
  end
@@ -29,23 +31,26 @@ module Dip
29
31
  return unless (files = config[:files])
30
32
 
31
33
  if files.is_a?(Array)
32
- files.each_with_object([]) do |file_name, memo|
33
- file_name = ::Dip.env.interpolate(file_name)
34
- next unless File.exist?(file_name)
34
+ files.each_with_object([]) do |file_path, memo|
35
+ file_path = ::Dip.env.interpolate(file_path)
36
+ file_path = Pathname.new(file_path)
37
+ file_path = Dip.config.file_path.parent.join(file_path).expand_path if file_path.relative?
38
+ next unless file_path.exist?
35
39
 
36
40
  memo << "--file"
37
- memo << file_name
41
+ memo << file_path.to_s
38
42
  end
39
43
  end
40
44
  end
41
45
 
42
- def find_project_name
43
- return unless (project_name = config[:project_name])
46
+ def cli_options
47
+ %i[project_name project_directory].flat_map do |name|
48
+ next unless (value = config[name])
49
+ next unless value.is_a?(String)
44
50
 
45
- if project_name.is_a?(String)
46
- project_name = ::Dip.env.interpolate(project_name)
47
- ["--project-name", project_name]
48
- end
51
+ value = ::Dip.env.interpolate(value)
52
+ ["--#{name.to_s.tr("_", "-")}", value]
53
+ end.compact
49
54
  end
50
55
 
51
56
  def find_dns
@@ -53,9 +58,9 @@ module Dip
53
58
  net = Dip.env["FRONTEND_NETWORK"] || "frontend"
54
59
 
55
60
  IO.pipe do |r, w|
56
- Dip::Commands::DNS::IP.
57
- new(name: name, net: net).
58
- execute(out: w, err: File::NULL, panic: false)
61
+ Dip::Commands::DNS::IP
62
+ .new(name: name, net: net)
63
+ .execute(out: w, err: File::NULL, panic: false)
59
64
 
60
65
  w.close_write
61
66
  ip = r.readlines[0].to_s.strip
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../command'
3
+ require_relative "../command"
4
4
 
5
5
  module Dip
6
6
  module Commands
@@ -13,9 +13,9 @@ module Dip
13
13
  private
14
14
 
15
15
  def script
16
- <<-SH.gsub(/^[ ]{12}/, '')
16
+ <<-SH.gsub(/^ {12}/, "")
17
17
  export DIP_SHELL=1
18
- export DIP_EARLY_ENVS=#{ENV.keys.join(',')}
18
+ export DIP_EARLY_ENVS=#{ENV.keys.join(",")}
19
19
  export DIP_PROMPT_TEXT="ⅆ"
20
20
 
21
21
  function dip_clear() {
@@ -86,9 +86,9 @@ module Dip
86
86
  end
87
87
 
88
88
  def execute
89
- if Dip::Config.exist?
89
+ if Dip.config.exist?
90
90
  add_aliases(*Dip.config.interaction.keys) if Dip.config.interaction
91
- add_aliases("compose", "up", "stop", "down", "provision")
91
+ add_aliases("compose", "up", "stop", "down", "provision", "build")
92
92
  end
93
93
 
94
94
  clear_aliases
@@ -107,7 +107,7 @@ module Dip
107
107
 
108
108
  def clear_aliases
109
109
  out << "function dip_clear() { \n" \
110
- "#{aliases.any? ? aliases.map { |a| " unset -f #{a}" }.join("\n") : 'true'} " \
110
+ "#{aliases.any? ? aliases.map { |a| " unset -f #{a}" }.join("\n") : "true"} " \
111
111
  "\n}"
112
112
  end
113
113
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "shellwords"
4
- require_relative '../command'
4
+ require_relative "../command"
5
5
 
6
6
  module Dip
7
7
  module Commands
@@ -24,13 +24,13 @@ module Dip
24
24
  private
25
25
 
26
26
  def container_args
27
- result = %w(--detach)
27
+ result = %w[--detach]
28
28
  result << "--volume #{@socket}:/var/run/docker.sock:ro"
29
29
  result << "--restart always"
30
30
  result << "--publish #{@publish}"
31
31
  result << "--net #{@net}"
32
32
  result << "--name #{@name}"
33
- result.join(' ')
33
+ result.join(" ")
34
34
  end
35
35
  end
36
36
 
@@ -53,10 +53,10 @@ module Dip
53
53
 
54
54
  def execute(**options)
55
55
  subshell("docker",
56
- "inspect " \
57
- "--format '{{ .NetworkSettings.Networks.#{@net}.IPAddress }}' " \
58
- "#{@name}".shellsplit,
59
- **options)
56
+ "inspect " \
57
+ "--format '{{ .NetworkSettings.Networks.#{@net}.IPAddress }}' " \
58
+ "#{@name}".shellsplit,
59
+ **options)
60
60
  end
61
61
  end
62
62
  end