-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from bibendi/feature/k8s
Add kubectl support
- Loading branch information
Showing
14 changed files
with
435 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../command" | ||
|
||
module Dip | ||
module Commands | ||
class Kubectl < Dip::Command | ||
attr_reader :argv, :config | ||
|
||
def initialize(*argv) | ||
@argv = argv | ||
@config = ::Dip.config.kubectl || {} | ||
end | ||
|
||
def execute | ||
k_argv = cli_options + argv | ||
|
||
exec_program("kubectl", k_argv) | ||
end | ||
|
||
private | ||
|
||
def cli_options | ||
%i[namespace].flat_map do |name| | ||
next unless (value = config[name]) | ||
next unless value.is_a?(String) | ||
|
||
value = ::Dip.env.interpolate(value).delete_suffix("-") | ||
["--#{name.to_s.tr("_", "-")}", value] | ||
end.compact | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dip | ||
module Commands | ||
module Runners | ||
class Base | ||
def initialize(command, argv, **options) | ||
@command = command | ||
@argv = argv | ||
@options = options | ||
end | ||
|
||
def execute | ||
raise NotImplementedError | ||
end | ||
|
||
private | ||
|
||
attr_reader :command, :argv, :options | ||
|
||
def command_args | ||
if argv.any? | ||
if command[:shell] | ||
[argv.shelljoin] | ||
else | ||
Array(argv) | ||
end | ||
elsif !(default_args = command[:default_args]).empty? | ||
if command[:shell] | ||
default_args.shellsplit | ||
else | ||
Array(default_args) | ||
end | ||
else | ||
[] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "base" | ||
require_relative "../compose" | ||
|
||
module Dip | ||
module Commands | ||
module Runners | ||
class DockerComposeRunner < Base | ||
def execute | ||
Commands::Compose.new( | ||
command[:compose][:method], | ||
*compose_arguments, | ||
shell: command[:shell] | ||
).execute | ||
end | ||
|
||
private | ||
|
||
def compose_arguments | ||
compose_argv = command[:compose][:run_options].dup | ||
|
||
if command[:compose][:method] == "run" | ||
compose_argv.concat(run_vars) | ||
compose_argv.concat(published_ports) | ||
compose_argv << "--rm" | ||
end | ||
|
||
compose_argv << command.fetch(:service) | ||
|
||
unless (cmd = command[:command]).empty? | ||
if command[:shell] | ||
compose_argv << cmd | ||
else | ||
compose_argv.concat(cmd.shellsplit) | ||
end | ||
end | ||
|
||
compose_argv.concat(command_args) | ||
|
||
compose_argv | ||
end | ||
|
||
def run_vars | ||
run_vars = Dip::RunVars.env | ||
return [] unless run_vars | ||
|
||
run_vars.map { |k, v| ["-e", "#{k}=#{Shellwords.escape(v)}"] }.flatten | ||
end | ||
|
||
def published_ports | ||
publish = options[:publish] | ||
|
||
if publish.respond_to?(:each) | ||
publish.map { |p| "--publish=#{p}" } | ||
else | ||
[] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.