improve coolify-helper + add test new functionalities

This commit is contained in:
Andras Bacsai 2023-09-04 16:03:11 +02:00
parent 759517316a
commit 3966abaf80
8 changed files with 65 additions and 39 deletions

View File

@ -25,6 +25,15 @@ public function mount()
} }
$this->projects = $projects->count(); $this->projects = $projects->count();
} }
// public function getIptables()
// {
// $servers = Server::ownedByCurrentTeam()->get();
// foreach ($servers as $server) {
// checkRequiredCommands($server);
// $iptables = instant_remote_process(['docker run --privileged --net=host --pid=host --ipc=host --volume /:/host busybox chroot /host bash -c "iptables -L -n | jc --iptables"'], $server);
// ray($iptables);
// }
// }
public function render() public function render()
{ {
return view('livewire.dashboard'); return view('livewire.dashboard');

View File

@ -66,6 +66,7 @@ class ApplicationDeploymentJob implements ShouldQueue
private $log_model; private $log_model;
private Collection $saved_outputs; private Collection $saved_outputs;
public $tries = 1;
public function middleware(): array public function middleware(): array
{ {
return [ return [
@ -242,7 +243,7 @@ private function rolling_update()
} }
private function health_check() private function health_check()
{ {
ray('New container name: ',$this->container_name); ray('New container name: ', $this->container_name);
if ($this->container_name) { if ($this->container_name) {
$counter = 0; $counter = 0;
$this->execute_remote_command( $this->execute_remote_command(
@ -264,7 +265,7 @@ private function health_check()
); );
$this->execute_remote_command( $this->execute_remote_command(
[ [
"echo 'New application version health check status: {$this->saved_outputs->get('health_check')}'" "echo 'New version health check status: {$this->saved_outputs->get('health_check')}'"
], ],
); );
if (Str::of($this->saved_outputs->get('health_check'))->contains('healthy')) { if (Str::of($this->saved_outputs->get('health_check'))->contains('healthy')) {
@ -272,6 +273,7 @@ private function health_check()
[ [
"echo 'Rolling update completed.'" "echo 'Rolling update completed.'"
], ],
["echo -n '######################'"],
); );
break; break;
} }
@ -304,17 +306,24 @@ private function deploy_pull_request()
private function prepare_builder_image() private function prepare_builder_image()
{ {
$pull = "--pull=always";
if (isDev()) {
$pull = "--pull=never";
}
$runCommand = "docker run --init {$pull} -d --network {$this->destination->network} -v /:/host --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/coollabsio/coolify-helper";
$this->execute_remote_command( $this->execute_remote_command(
[ [
"echo -n 'Pulling latest version of the helper image (ghcr.io/coollabsio/coolify-helper).'", "echo -n 'Pulling latest version of the helper image (ghcr.io/coollabsio/coolify-helper).'",
], ],
[ [
"docker run --pull=always -d --network {$this->destination->network} --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/coollabsio/coolify-helper", $runCommand,
"hidden" => true, "hidden" => true,
], ],
[ [
"command" => $this->execute_in_builder("mkdir -p {$this->workdir}") "command" => $this->execute_in_builder("mkdir -p {$this->workdir}")
], ],
["echo -n '######################'"],
); );
} }
@ -654,7 +663,7 @@ private function generate_healthcheck_commands()
private function build_image() private function build_image()
{ {
$this->execute_remote_command([ $this->execute_remote_command([
"echo -n 'Building docker image.'", "echo -n 'Building docker image for your application.'",
]); ]);
if ($this->application->settings->is_static) { if ($this->application->settings->is_static) {
@ -715,6 +724,7 @@ private function stop_running_container()
private function start_by_compose_file() private function start_by_compose_file()
{ {
$this->execute_remote_command( $this->execute_remote_command(
["echo -n '######################'"],
["echo -n 'Rolling update started.'"], ["echo -n 'Rolling update started.'"],
[$this->execute_in_builder("docker compose --project-directory {$this->workdir} up -d >/dev/null"), "hidden" => true], [$this->execute_in_builder("docker compose --project-directory {$this->workdir} up -d >/dev/null"), "hidden" => true],
); );

View File

@ -77,6 +77,7 @@ function generate_ssh_command(string $private_key_location, string $server_ip, s
if ($isMux && config('coolify.mux_enabled')) { if ($isMux && config('coolify.mux_enabled')) {
$ssh_command .= '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/ssh/mux/%h_%p_%r '; $ssh_command .= '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/ssh/mux/%h_%p_%r ';
} }
$command = "PATH=\$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/host/usr/local/sbin:/host/usr/local/bin:/host/usr/sbin:/host/usr/bin:/host/sbin:/host/bin && $command";
$ssh_command .= "-i {$private_key_location} " $ssh_command .= "-i {$private_key_location} "
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ' . '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
. '-o PasswordAuthentication=no ' . '-o PasswordAuthentication=no '
@ -216,3 +217,29 @@ function check_server_connection(Server $server)
$server->save(); $server->save();
} }
} }
function checkRequiredCommands(Server $server)
{
$commands = collect(["jq", "jc"]);
foreach ($commands as $command) {
$commandFound = instant_remote_process(["docker run --rm --privileged --net=host --pid=host --ipc=host --volume /:/host busybox chroot /host bash -c 'command -v {$command}'"], $server, false);
if ($commandFound) {
ray($command . ' found');
continue;
}
try {
instant_remote_process(["docker run --rm --privileged --net=host --pid=host --ipc=host --volume /:/host busybox chroot /host bash -c 'apt update && apt install -y {$command}'"], $server);
} catch (\Exception $e) {
ray('could not install ' . $command);
ray($e);
break;
}
$commandFound = instant_remote_process(["docker run --rm --privileged --net=host --pid=host --ipc=host --volume /:/host busybox chroot /host bash -c 'command -v {$command}'"], $server, false);
if ($commandFound) {
ray($command . ' found');
continue;
}
ray('could not install ' . $command);
break;
}
}

View File

@ -6,6 +6,7 @@ x-testing-host: &testing-host-base
context: ./docker/testing-host context: ./docker/testing-host
networks: networks:
- coolify - coolify
init: true
services: services:
coolify: coolify:
@ -53,6 +54,7 @@ services:
<<: *testing-host-base <<: *testing-host-base
container_name: coolify-testing-host container_name: coolify-testing-host
volumes: volumes:
- /:/host
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
- /data/coolify/:/data/coolify - /data/coolify/:/data/coolify
mailpit: mailpit:

View File

@ -2,19 +2,19 @@ FROM alpine:3.17
ARG TARGETPLATFORM ARG TARGETPLATFORM
# https://download.docker.com/linux/static/stable/ # https://download.docker.com/linux/static/stable/
ARG DOCKER_VERSION=23.0.6 ARG DOCKER_VERSION=24.0.5
# https://github.com/docker/compose/releases # https://github.com/docker/compose/releases
ARG DOCKER_COMPOSE_VERSION=2.18.1 ARG DOCKER_COMPOSE_VERSION=2.21.0
# https://github.com/docker/buildx/releases # https://github.com/docker/buildx/releases
ARG DOCKER_BUILDX_VERSION=0.10.5 ARG DOCKER_BUILDX_VERSION=0.11.2
# https://github.com/buildpacks/pack/releases # https://github.com/buildpacks/pack/releases
ARG PACK_VERSION=0.29.0 ARG PACK_VERSION=0.30.0
# https://github.com/railwayapp/nixpacks/releases # https://github.com/railwayapp/nixpacks/releases
ARG NIXPACKS_VERSION=1.12.0 ARG NIXPACKS_VERSION=1.13.0
USER root USER root
WORKDIR /artifacts WORKDIR /artifacts
RUN apk add --no-cache bash curl git git-lfs openssh-client tar tini RUN apk add --no-cache bash curl git git-lfs openssh-client tar
RUN mkdir -p ~/.docker/cli-plugins RUN mkdir -p ~/.docker/cli-plugins
RUN if [[ ${TARGETPLATFORM} == 'linux/amd64' ]]; then \ RUN if [[ ${TARGETPLATFORM} == 'linux/amd64' ]]; then \
curl -sSL https://github.com/docker/buildx/releases/download/v${DOCKER_BUILDX_VERSION}/buildx-v${DOCKER_BUILDX_VERSION}.linux-amd64 -o ~/.docker/cli-plugins/docker-buildx && \ curl -sSL https://github.com/docker/buildx/releases/download/v${DOCKER_BUILDX_VERSION}/buildx-v${DOCKER_BUILDX_VERSION}.linux-amd64 -o ~/.docker/cli-plugins/docker-buildx && \
@ -37,6 +37,4 @@ RUN if [[ ${TARGETPLATFORM} == 'linux/arm64' ]]; then \
COPY --from=minio/mc /usr/bin/mc /usr/bin/mc COPY --from=minio/mc /usr/bin/mc /usr/bin/mc
RUN chmod +x /usr/bin/mc RUN chmod +x /usr/bin/mc
ENTRYPOINT ["/sbin/tini", "--"] CMD ["tail", "-f", "/dev/null"]
CMD ["sh", "-c", "while true; do sleep 1; done"]

View File

@ -1,29 +1,11 @@
FROM alpine:3.17
ARG TARGETPLATFORM FROM debian:12-slim
# https://download.docker.com/linux/static/stable/
ARG DOCKER_VERSION=23.0.6
# https://github.com/docker/compose/releases
ARG DOCKER_COMPOSE_VERSION=2.18.1
# https://github.com/docker/buildx/releases
ARG DOCKER_BUILDX_VERSION=0.10.5
# https://github.com/buildpacks/pack/releases
ARG PACK_VERSION=0.29.0
# https://github.com/railwayapp/nixpacks/releases
ARG NIXPACKS_VERSION=1.12.0
USER root USER root
WORKDIR /root WORKDIR /root
RUN apk add --no-cache bash curl git git-lfs openssh-client openssh-server tar tini postgresql-client lsof ENV PATH "$PATH:/host/usr/local/sbin:/host/usr/local/bin:/host/usr/sbin:/host/usr/bin:/host/sbin:/host/bin"
RUN mkdir -p ~/.docker/cli-plugins
RUN if [[ ${TARGETPLATFORM} == 'linux/amd64' ]]; then \ RUN apt update && apt -y install openssh-client openssh-server curl wget git jq jc
curl -sSL https://github.com/docker/buildx/releases/download/v${DOCKER_BUILDX_VERSION}/buildx-v${DOCKER_BUILDX_VERSION}.linux-amd64 -o ~/.docker/cli-plugins/docker-buildx && \
curl -sSL https://github.com/docker/compose/releases/download/v${DOCKER_COMPOSE_VERSION}/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose && \
(curl -sSL https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_VERSION}.tgz | tar -C /usr/bin/ --no-same-owner -xzv --strip-components=1 docker/docker) && \
(curl -sSL https://github.com/buildpacks/pack/releases/download/v${PACK_VERSION}/pack-v${PACK_VERSION}-linux.tgz | tar -C /usr/local/bin/ --no-same-owner -xzv pack) && \
curl -sSL https://nixpacks.com/install.sh | bash && \
chmod +x ~/.docker/cli-plugins/docker-compose /usr/bin/docker /usr/local/bin/pack /root/.docker/cli-plugins/docker-buildx \
;fi
# Setup sshd # Setup sshd
RUN ssh-keygen -A RUN ssh-keygen -A
@ -32,6 +14,4 @@ RUN mkdir -p ~/.ssh
RUN echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFuGmoeGq/pojrsyP1pszcNVuZx9iFkCELtxrh31QJ68 coolify@coolify-instance" >> ~/.ssh/authorized_keys RUN echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFuGmoeGq/pojrsyP1pszcNVuZx9iFkCELtxrh31QJ68 coolify@coolify-instance" >> ~/.ssh/authorized_keys
EXPOSE 22 EXPOSE 22
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/usr/sbin/sshd", "-D", "-o", "ListenAddress=0.0.0.0"] CMD ["/usr/sbin/sshd", "-D", "-o", "ListenAddress=0.0.0.0"]

View File

@ -35,5 +35,5 @@
<div class="stat-value">{{ $s3s }}</div> <div class="stat-value">{{ $s3s }}</div>
</div> </div>
</div> </div>
{{-- <x-forms.button wire:click='getIptables'>Get IPTABLES</x-forms.button> --}}
</div> </div>

View File

@ -41,7 +41,7 @@ echo -e "-------------"
echo "Installing required packages..." echo "Installing required packages..."
apt update -y >/dev/null 2>&1 apt update -y >/dev/null 2>&1
apt install -y curl wget git jq >/dev/null 2>&1 apt install -y curl wget git jq jc >/dev/null 2>&1
if ! [ -x "$(command -v docker)" ]; then if ! [ -x "$(command -v docker)" ]; then
echo "Docker is not installed. Installing Docker..." echo "Docker is not installed. Installing Docker..."