From 19e4f887c487d76421e9ae5aa206ed8c7ffde93f Mon Sep 17 00:00:00 2001 From: Misha Merkushin Date: Thu, 7 Mar 2024 17:58:57 +0300 Subject: [PATCH] feat: drop support dns and nginx in favor of infra --- README.md | 157 +++++++--------------------- docs/docker-for-mac-install.md | 28 ----- docs/docker-ubuntu-install.md | 17 --- lib/dip/cli.rb | 10 +- lib/dip/cli/nginx.rb | 72 ------------- lib/dip/commands/nginx.rb | 52 --------- spec/lib/dip/commands/nginx_spec.rb | 114 -------------------- 7 files changed, 39 insertions(+), 411 deletions(-) delete mode 100644 docs/docker-for-mac-install.md delete mode 100644 docs/docker-ubuntu-install.md delete mode 100644 lib/dip/cli/nginx.rb delete mode 100644 lib/dip/commands/nginx.rb delete mode 100644 spec/lib/dip/commands/nginx_spec.rb diff --git a/README.md b/README.md index f52385f..b982e2f 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Development-environment CLI program providing the native-like interaction with a ## Presentations and examples - [Local development with Docker containers](https://slides.com/bibendi/dip) -- Dockerized Ruby on Rails applications: [one](https://github.com/lewagon/rails-k8s-demo), [two](https://github.com/bibendi/dip-example-rails), [three](https://github.com/evilmartians/evil_chat) +- [Dockerized Ruby on Rails application](https://SberMarket-Tech/outbox-example-apps) - Dockerized Node.js application: [one](https://github.com/bibendi/twinkle.js), [two](https://github.com/bibendi/yt-graphql-react-event-booking-api) - [Dockerized Ruby gem](https://github.com/bibendi/schked) - [Dockerizing Ruby and Rails development](https://evilmartians.com/chronicles/ruby-on-whales-docker-for-ruby-rails-development) @@ -22,7 +22,13 @@ Development-environment CLI program providing the native-like interaction with a [![asciicast](https://asciinema.org/a/210236.svg)](https://asciinema.org/a/210236) -## Integration with shell +## Installation + +```sh +gem install dip +``` + +### Integration with shell Dip can be injected into the current shell (ZSH or Bash). @@ -53,14 +59,6 @@ VERSION=20180515103400 rails db:migrate:down You could add this `eval` at the end of your `~/.zshrc`, or `~/.bashrc`, or `~/.bash_profile`. After that, it will be automatically applied when you open your preferred terminal. -## Installation - -```sh -gem install dip -``` - -The compiled binary is no more provided since version 7, because of new version of [Ruby Packer](https://github.com/pmq20/ruby-packer) not released for a long time with recent Ruby version. Also there was a lot of work to prepare each release of Dip for MacOS version. - ## Usage ```sh @@ -242,7 +240,6 @@ services: The container will run using the same user ID as your host machine. - ### dip run Run commands defined within the `interaction` section of dip.yml @@ -312,6 +309,35 @@ dip compose COMMAND [OPTIONS] dip compose up -d redis ``` +### dip infra + +Runs shared Docker Compose services that are used by the current application. Useful for microservices. + +There are several official infrastructure services available: +- [dip-postgres](https://github.com/bibendi/dip-postgres) +- [dip-kafka](https://github.com/bibendi/dip-kafka) +- [dip-nginx](https://github.com/bibendi/dip-nginx) + +```yaml +# dip.yml +infra: + foo: + git: https://github.com/owner/foo.git + ref: latest # default, optional + bar: + path: ~/path/to/bar +``` + +Repositories will be pulled to a `~/.dip/infra` folder. For example, for the `foo` service it would be like this: `~/.dip/infra/foo/latest` and clonned with the following command: `git clone -b --single-branch --depth 1`. + +Available CLI commands: + +- `dip infra update` pulls updates from sources +- `dip infra up` starts all infra services +- `dip infra up -n kafka` starts a specific infra service +- `dip infra down` stops all infra services +- `dip infra down -n kafka` stops a specific infra service + ### dip ktl Run kubectl commands that are configured according to the application's dip.yml: @@ -356,119 +382,12 @@ dip ssh up -u 1000 This especially helpful if you have something like this in your docker-compose.yml: -``` +```yml services: web: user: "1000:1000" - -``` - -### dip nginx - -Runs Nginx server container based on [nginxproxy/nginx-proxy](https://github.com/nginx-proxy/nginx-proxy) image. An application's docker-compose.yml should contain environment variable `VIRTUAL_HOST` and `VIRTUAL_PATH` and connects to external network `frontend`. - -foo-project/docker-compose.yml - -```yml -services: - foo-web: - image: company/foo_image - environment: - - VIRTUAL_HOST=*.bar-app.docker - - VIRTUAL_PATH=/ - networks: - - default - - frontend - dns: $DIP_DNS - -networks: - frontend: - external: - name: frontend ``` -baz-project/docker-compose.yml - -```yml -services: - baz-web: - image: company/baz_image - environment: - - VIRTUAL_HOST=*.bar-app.docker - - VIRTUAL_PATH=/api/v1/baz_service,/api/v2/baz_service - networks: - - default - - frontend - dns: $DIP_DNS - -networks: - frontend: - external: - name: frontend -``` - -```sh -dip nginx up -cd foo-project && dip compose up -cd baz-project && dip compose up -curl www.bar-app.docker/api/v1/quz -curl www.bar-app.docker/api/v1/baz_service/qzz -``` - -#### Pass SSL certificates - -```sh -dip nginx up -c $HOME/ssl_certificates -``` - -#### Publish more than one port to localhost - -Just pass a list, separated by a space: - -```sh -dip nginx up -p 80:80 443:443 -``` - -### dip dns - -Runs a DNS server container based on https://github.com/aacebedo/dnsdock. It is used for container to container requests through Nginx. An application's docker-compose.yml should define `dns` configuration with environment variable `$DIP_DNS` and connect to external network `frontend`. `$DIP_DNS` will be automatically assigned by dip. - -```sh -dip dns up - -cd foo-project -dip compose exec foo-web curl http://www.bar-app.docker/api/v1/baz_service -``` - -### dip infra - -Runs shared Docker Compose services that are used by the current application. Useful for microservices. - -There are several official infrastructure services available: -- [dip-postgres](https://github.com/bibendi/dip-postgres) -- [dip-kafka](https://github.com/bibendi/dip-kafka) -- [dip-nginx](https://github.com/bibendi/dip-nginx) - -```yaml -# dip.yml -infra: - foo: - git: https://github.com/owner/foo.git - ref: latest # default, optional - bar: - path: ~/path/to/bar -``` - -Repositories will be pulled to a `~/.dip/infra` folder. For example, for the `foo` service it would be like this: `~/.dip/infra/foo/latest` and clonned with the following command: `git clone -b --single-branch --depth 1`. - -Available CLI commands: - -- `dip infra update` pulls updates from sources -- `dip infra up` starts all infra services -- `dip infra up -n kafka` starts a specific infra service -- `dip infra down` stops all infra services -- `dip infra down -n kafka` stops a specific infra service - ## Changelog https://github.com/bibendi/dip/releases diff --git a/docs/docker-for-mac-install.md b/docs/docker-for-mac-install.md deleted file mode 100644 index a871835..0000000 --- a/docs/docker-for-mac-install.md +++ /dev/null @@ -1,28 +0,0 @@ -# Docker for Mac - -Download and install [Docker for Mac](https://www.docker.com/docker-mac). - -**WARNING**: Latest Docker for Mac 17.12.0-ce-mac46 seems to [break d4m-nfs](https://github.com/IFSight/d4m-nfs/issues/55). - -# d4m-nfs - -For the best i/o performance git clone latest [IFSight/d4m-nfs](https://github.com/IFSight/d4m-nfs). - -- Remove all shared paths from Docker for Mac Preferences except `/tmp`. -- Run `echo '/Users:/Users:0:0' > ./etc/d4m-nfs-mounts.txt` -- Run `./d4m-nfs.sh` after each reboot. - -# Create resolver - -```sh - sudo touch /etc/resolver/docker - echo "nameserver 127.0.0.1" | sudo tee -a /etc/resolver/docker -``` - -# Dnsmasq - -```sh - brew install dnsmasq - echo 'address=/docker/127.0.0.1' >> $(brew --prefix)/etc/dnsmasq.conf - brew services restart dnsmasq -``` diff --git a/docs/docker-ubuntu-install.md b/docs/docker-ubuntu-install.md deleted file mode 100644 index 3bfc03d..0000000 --- a/docs/docker-ubuntu-install.md +++ /dev/null @@ -1,17 +0,0 @@ -# Docker - -https://docs.docker.com/install/linux/docker-ce/ubuntu/ - -# Docker Compose - -https://docs.docker.com/compose/install/ - -# Dnsmasq - -**WARNING**: Latest Ubuntu 18.04 already runs own local dns resolver at *.localhost. Dnsmasq is not needed. In that case you should run `dip dns` and `dip nginx` with option `--domain localhost`. - -```sh -sudo apt-get install dnsmasq -echo "address=/docker/127.0.0.1" | sudo tee -a /etc/dnsmasq.conf -sudo service dnsmasq restart -``` diff --git a/lib/dip/cli.rb b/lib/dip/cli.rb index eb5e735..15e66df 100644 --- a/lib/dip/cli.rb +++ b/lib/dip/cli.rb @@ -5,7 +5,7 @@ module Dip class CLI < Thor - TOP_LEVEL_COMMANDS = %w[help version ls compose up stop down run provision ssh dns nginx console].freeze + TOP_LEVEL_COMMANDS = %w[help version ls compose up stop down run provision ssh infra console].freeze class << self # Hackery. Take the run method away from Thor so that we can redefine it. @@ -121,14 +121,6 @@ def provision desc "ssh", "ssh-agent container commands" subcommand :ssh, Dip::CLI::SSH - require_relative "cli/dns" - desc "dns", "DNS server for automatic docker container discovery" - subcommand :dns, Dip::CLI::DNS - - require_relative "cli/nginx" - desc "nginx", "Nginx reverse proxy server" - subcommand :nginx, Dip::CLI::Nginx - require_relative "cli/infra" desc "infra", "Infrastructure services" subcommand :infra, Dip::CLI::Infra diff --git a/lib/dip/cli/nginx.rb b/lib/dip/cli/nginx.rb deleted file mode 100644 index 1876206..0000000 --- a/lib/dip/cli/nginx.rb +++ /dev/null @@ -1,72 +0,0 @@ -# frozen_string_literal: true - -require "thor" -require_relative "base" -require_relative "../commands/nginx" - -module Dip - class CLI - # See more https://github.com/nginx-proxy/nginx-proxy - class Nginx < Base - desc "up", "Run nginx container" - method_option :help, aliases: "-h", type: :boolean, - desc: "Display usage information" - method_option :name, aliases: "-n", type: :string, default: "nginx", - desc: "Container name" - method_option :socket, aliases: "-s", type: :string, default: "/var/run/docker.sock", - desc: "Path to docker socket" - method_option :net, aliases: "-t", type: :string, default: "frontend", - desc: "Container network name" - method_option :publish, aliases: "-p", type: :array, default: ["80:80"], - desc: "Container port(s). For more than one port, separate them by a space" - method_option :image, aliases: "-i", type: :string, default: "nginxproxy/nginx-proxy:latest", - desc: "Docker image name" - method_option :domain, aliases: "-d", type: :string, default: "docker", - desc: "Top level domain" - method_option :certs, aliases: "-c", type: :string, desc: "Path to ssl certificates" - def up - if options[:help] - invoke :help, ["up"] - else - Dip::Commands::Nginx::Up.new( - name: options.fetch(:name), - socket: options.fetch(:socket), - net: options.fetch(:net), - publish: options.fetch(:publish), - image: options.fetch(:image), - domain: options.fetch(:domain), - certs: options[:certs] - ).execute - end - end - - desc "down", "Stop nginx container" - method_option :help, aliases: "-h", type: :boolean, - desc: "Display usage information" - method_option :name, aliases: "-n", type: :string, default: "nginx", - desc: "Container name" - def down - if options[:help] - invoke :help, ["down"] - else - Dip::Commands::Nginx::Down.new( - name: options.fetch(:name) - ).execute - end - end - - desc "restart", "Stop and start nginx container" - method_option :help, aliases: "-h", type: :boolean, - desc: "Display usage information" - def restart(*args) - if options[:help] - invoke :help, ["restart"] - else - Dip::CLI::Nginx.start(["down"] + args) - sleep 1 - Dip::CLI::Nginx.start(["up"] + args) - end - end - end - end -end diff --git a/lib/dip/commands/nginx.rb b/lib/dip/commands/nginx.rb deleted file mode 100644 index 6078e5f..0000000 --- a/lib/dip/commands/nginx.rb +++ /dev/null @@ -1,52 +0,0 @@ -# frozen_string_literal: true - -require "shellwords" -require_relative "../command" - -module Dip - module Commands - module Nginx - class Up < Dip::Command - def initialize(name:, socket:, net:, publish:, image:, domain:, certs:) - @name = name - @socket = socket - @net = net - @publish = publish - @image = image - @domain = domain - @certs = certs - end - - def execute - exec_subprocess("docker", "network create #{@net}", panic: false, err: File::NULL) - exec_subprocess("docker", "run #{container_args} #{@image}") - end - - private - - def container_args - result = %w[--detach] - result << "--volume #{@socket}:/tmp/docker.sock:ro" - result << "--volume #{@certs}:/etc/nginx/certs" unless @certs.to_s.empty? - result << "--restart always" - result << Array(@publish).map { |p| "--publish #{p}" }.join(" ") - result << "--net #{@net}" - result << "--name #{@name}" - result << "--label com.dnsdock.alias=#{@domain}" - result.join(" ") - end - end - - class Down < Dip::Command - def initialize(name:) - @name = name - end - - def execute - exec_subprocess("docker", "stop #{@name}", panic: false, out: File::NULL, err: File::NULL) - exec_subprocess("docker", "rm -v #{@name}", panic: false, out: File::NULL, err: File::NULL) - end - end - end - end -end diff --git a/spec/lib/dip/commands/nginx_spec.rb b/spec/lib/dip/commands/nginx_spec.rb deleted file mode 100644 index c853d76..0000000 --- a/spec/lib/dip/commands/nginx_spec.rb +++ /dev/null @@ -1,114 +0,0 @@ -# frozen_string_literal: true - -require "shellwords" -require "dip/cli/nginx" -require "dip/commands/nginx" - -describe Dip::Commands::Nginx do - let(:cli) { Dip::CLI::Nginx } - - describe Dip::Commands::Nginx::Up do - context "when without arguments" do - before { cli.start "up".shellsplit } - - it { expected_subprocess("docker", ["network", "create", "frontend"]) } - - it do - expected_subprocess( - "docker", - "run --detach --volume /var/run/docker.sock:/tmp/docker.sock:ro --restart always --publish 80:80 --net frontend --name nginx --label com.dnsdock.alias=docker nginxproxy/nginx-proxy:latest" - ) - end - end - - context "when option `name` is present" do - before { cli.start "up --name foo".shellsplit } - - it { - expected_subprocess("docker", - "run --detach --volume /var/run/docker.sock:/tmp/docker.sock:ro --restart always --publish 80:80 --net frontend --name foo --label com.dnsdock.alias=docker nginxproxy/nginx-proxy:latest") - } - end - - context "when option `socket` is present" do - before { cli.start "up --socket foo".shellsplit } - - it { - expected_subprocess("docker", - "run --detach --volume foo:/tmp/docker.sock:ro --restart always --publish 80:80 --net frontend --name nginx --label com.dnsdock.alias=docker nginxproxy/nginx-proxy:latest") - } - end - - context "when option `net` is present" do - before { cli.start "up --net foo".shellsplit } - - it { expected_subprocess("docker", ["network", "create", "foo"]) } - - it { - expected_subprocess("docker", - "run --detach --volume /var/run/docker.sock:/tmp/docker.sock:ro --restart always --publish 80:80 --net foo --name nginx --label com.dnsdock.alias=docker nginxproxy/nginx-proxy:latest") - } - end - - context "when option `publish` is present" do - before { cli.start "up --publish 80:80".shellsplit } - - it { - expected_subprocess("docker", - "run --detach --volume /var/run/docker.sock:/tmp/docker.sock:ro --restart always --publish 80:80 --net frontend --name nginx --label com.dnsdock.alias=docker nginxproxy/nginx-proxy:latest") - } - - context "when more than one port given" do - before { cli.start "up --publish 80:80 443:443".shellsplit } - - it { - expected_subprocess("docker", - "run --detach --volume /var/run/docker.sock:/tmp/docker.sock:ro --restart always --publish 80:80 --net frontend --name nginx --label com.dnsdock.alias=docker nginxproxy/nginx-proxy:latest") - } - end - end - - context "when option `image` is present" do - before { cli.start "up --image foo".shellsplit } - - it { - expected_subprocess("docker", - "run --detach --volume /var/run/docker.sock:/tmp/docker.sock:ro --restart always --publish 80:80 --net frontend --name nginx --label com.dnsdock.alias=docker foo") - } - end - - context "when option `domain` is present" do - before { cli.start "up --domain foo".shellsplit } - - it { - expected_subprocess("docker", - "run --detach --volume /var/run/docker.sock:/tmp/docker.sock:ro --restart always --publish 80:80 --net frontend --name nginx --label com.dnsdock.alias=foo nginxproxy/nginx-proxy:latest") - } - end - - context "when option `certs` is present" do - before { cli.start "up --certs /home/whoami/certs_storage".shellsplit } - - it { - expected_subprocess("docker", - "run --detach --volume /var/run/docker.sock:/tmp/docker.sock:ro --volume /home/whoami/certs_storage:/etc/nginx/certs --restart always --publish 80:80 --net frontend --name nginx --label com.dnsdock.alias=docker nginxproxy/nginx-proxy:latest") - } - end - end - - describe Dip::Commands::Nginx::Down do - context "when without arguments" do - before { cli.start "down".shellsplit } - - it { expected_subprocess("docker", ["stop", "nginx"]) } - it { expected_subprocess("docker", ["rm", "-v", "nginx"]) } - end - - context "when option `name` is present" do - before { cli.start "down --name foo".shellsplit } - - it { expected_subprocess("docker", ["stop", "foo"]) } - it { expected_subprocess("docker", ["rm", "-v", "foo"]) } - end - end -end