Last active
June 13, 2023 02:36
-
-
Save viniciusgonmelo/a3bea847d194cd3c18e6d8a13db38557 to your computer and use it in GitHub Desktop.
Inicia/para serviços com o Docker.
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use 5.010; | |
use autodie; | |
use Cwd qw(cwd); | |
# Script: docker-services.pl | |
# Descrição: Inicia/para serviços do Docker | |
# Exemplo de 'docker-services.conf': | |
# tinyproxy:tinyproxy | |
# nginx:nginx-frontend | |
# joplin:nginx-backend | |
# Exemplo de 'docker-services-networks.conf': | |
# tinyproxy:192.168.10.0/24 | |
# nginx-frontend:192.168.20.0/24 | |
# nginx-backend:192.168.30.0/24 | |
# Exemplo de uso: | |
# ./docker-services start - inicia todos os serviços | |
# ./docker-services stop - para todos os serviços | |
# ./docker-services start nginx | |
# Diretório dos scripts em Bash para iniciar e interromper os serviços; | |
# o nome dos scripts deve seguir o formato: | |
# "[SERVICO]_[COMANDO]" - "nginx_start" | |
my $docker_cmds_dir = cwd . '/docker-cmds'; | |
my (%docker_cmds, %docker_networks, %docker_services); | |
# Lê o arquivo de configuração de redes | |
eval { | |
open my $networks_fh, '<', 'docker-services-networks.conf'; | |
while (<$networks_fh>) { | |
s/#.*//; # ignora comentários | |
next if /^(\s)*$/; # pula linhas em branco | |
chomp; | |
my ($network_name, $network_ip) = split /:/; | |
$docker_networks{$network_name} = $network_ip; | |
} | |
close $networks_fh; | |
} or do { | |
say STDERR "Não foi possível abrir networks: $!"; | |
exit 1; | |
}; | |
# Lê o arquivo de configuração de serviços | |
eval { | |
open my $services_fh, '<', 'docker-services.conf'; | |
while (<$services_fh>) { | |
s/#.*//; # ignora comentários | |
next if /^(\s)*$/; # pula linhas em branco | |
chomp; | |
my ($service_name, $network_name) = split /:/; | |
$docker_services{$service_name} = $network_name; | |
$docker_cmds{"$service_name\_start"} = "$docker_cmds_dir/$service_name\_start"; | |
$docker_cmds{"$service_name\_stop"} = "$docker_cmds_dir/$service_name\_stop"; | |
} | |
close $services_fh; | |
} or do { | |
say STDERR "Não foi possível abrir services: $!"; | |
exit 1; | |
}; | |
my ($cmd, $service_name) = @ARGV; | |
if (not defined $cmd) { | |
say STDERR "$0 CMD [SERVICE]"; | |
exit 1; | |
} | |
if (not defined $service_name) { | |
if ($cmd eq "start") { | |
# Cria todas as redes do Docker | |
for my $network_name (keys %docker_networks) { | |
system("docker network create --subnet=$docker_networks{$network_name} $network_name") == 0 | |
or say STDERR "Erro ao criar a rede $network_name"; | |
} | |
} | |
for (keys %docker_cmds){ | |
system($docker_cmds{$_}) == 0 or say STDERR "Erro ao executar $docker_cmds{$_}" if $_ =~ /$cmd$/; | |
} | |
if ($cmd eq "stop") { | |
# Remove todas as redes do Docker | |
for my $network_name (keys %docker_networks) { | |
system("docker network rm $network_name") == 0 | |
or say STDERR "Erro ao remover a rede $network_name"; | |
} | |
} | |
} else { | |
if ($cmd eq "start") { | |
# Cria a rede do Docker para o serviço especificado | |
system("docker network create --subnet=$docker_networks{$docker_services{$service_name}} $docker_services{$service_name}") == 0 | |
or say STDERR "Erro ao criar a rede $docker_services{$service_name}"; | |
} | |
system($docker_cmds{"$service_name\_$cmd"}) == 0 | |
or say STDERR "Erro ao executar " . $docker_cmds{"$service_name\_$cmd"}; | |
if ($cmd eq "stop") { | |
# Remove a rede do Docker para o serviço especificado | |
system("docker network rm $docker_services{$service_name}") == 0 | |
or say STDERR "Erro ao remover a rede $docker_services{$service_name}"; | |
} | |
} | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment