Docker Private Registry Info and Clear Automation

Want create site? Find Free WordPress Themes and plugins.

Eğer Devops ile ilgileniyorsanız container haline getirdiğiniz imajları bir repository üzerinde tutmalısınız. Ve bir çok uygulama veye sürekli geliştirme yapıyorsanız repo üzerindeki imajları yönetmek zor olabilir. Aşağıda yazdığım kod bloğu bir private registry üzerindeki imajları görüntülemenizi gerektriği taktirde silmenize olanak tanıyacaktır.

 

#this script docker registry image clean and information.

[cmdletbinding()]

Param(

    [Parameter(ParameterSetName="info")]

    [switch]$Information,

    [Parameter(ParameterSetName="delete")]

    [switch]$Delete,

    [Parameter(Mandatory)]

    [string]$Password,

    [Parameter(Mandatory)]

    [string]$UserName,

    [Parameter(Mandatory)]

    [string]$DockerRegistry = "https://repourl:5000/v2",

    [Parameter(Mandatory,ParameterSetName="delete")]

    [int]$KeepImages = 100

)

BEGIN {

if (-not("dummy" -as [type])) {

    add-type -TypeDefinition @"

        using System;

        using System.Net;

        using System.Net.Security;

        using System.Security.Cryptography.X509Certificates;

        public static class Dummy {

            public static bool ReturnTrue(object sender,

                X509Certificate certificate,

                X509Chain chain,

                SslPolicyErrors sslPolicyErrors) { return true; }

            public static RemoteCertificateValidationCallback GetDelegate() {

                return new RemoteCertificateValidationCallback(Dummy.ReturnTrue);

            }

        }

"@

      } 

      $Header = @{"Accept" = "application/vnd.docker.distribution.manifest.v2+json"}

      [System.Net.ServicePointManager]::ServerCertificateValidationCallback = [dummy]::GetDelegate()

      $cred = New-Object -TypeName System.Management.Automation.PSCredential("$UserName",("$Password" | ConvertTo-SecureString -AsPlainText -Force))

      $PSDefaultParameterValues=@{"Invoke-RestMethod:Credential" = $cred}

      [System.Collections.Generic.List[object]]$obj = @()

}

PROCESS {

    $Repos = Invoke-RestMethod -Uri "$DockerRegistry/_catalog" -Headers $Header

    if($PSBoundParameters['Delete']){

       

            foreach($r in $Repos.repositories)

            {

                $tags = Invoke-RestMethod -Uri "$DockerRegistry/$r/tags/list"

                if($tags.tags.Count -ge $KeepImages){

                    $tags.tags | sort -Descending | Select-Object -Skip $KeepImages | % {

                        $raw = Invoke-WebRequest -Method get -Uri "$DockerRegistry/$r/manifests/$_" -Headers $Header -Credential $cred

                        $SHAValue = $raw.Headers.'Docker-Content-Digest'

                        Invoke-RestMethod -Method DELETE  -Uri "$DockerRegistry/$r/manifests/$SHAValue" -Headers $Header

                    }

                        $obj += ([pscustomobject]@{

                            RepoName = $r

                            RepoOldImagesCount = $tags.tags.Count

                            RepoNewImagesCount =  (Invoke-RestMethod -Uri "$DockerRegistry/$r/tags/list").tags.Count

                        })

                }

            }

    }

    if($PSBoundParameters['Information']){     

        foreach($r in $Repos.repositories)

        {

            $tags = Invoke-RestMethod -Uri "$DockerRegistry/$r/tags/list"

            [pscustomobject]@{

                Reponame = $r

                repoTagCount = $tags.tags.Count

                repoTag  = ($tags.tags | sort -Descending) -join ','

            }

        }

    }

}

END {

       

}

Did you find apk for android? You can find new Free Android Games and apps.

Bir Cevap Yazın

Find NULLED WordPress Themes and Plugins at NulledHub.net