GithubHelp home page GithubHelp logo

harbor-delete-tags's Introduction

Shell script to remove docker container files from Harbor


Our team has started using Harbor- which is an open source cloud native registry that stores, container images . It also signs, and scans container images for vulnerabilities.

As we are dealing with repositories and container images and there are many images for different stacks like development, staging and production.  Storing them in Harbor without any deletion strategy results in no space left on harbor hosted machine. 

Harbor captures the documentation of deleting repositories and container images. 

It is a 2 step process First you need to delete a container images from Harbor's UI. This is soft deletion. And then you need to run Garbage Collector which actually deletes the files from repository.

Now the problem statement is removing the repository files or container images from Harbor's UI is a cumbersome process and requires manual intervention. Lets see how can we automate the process of deletion of repository container images using Harbor's REST API and Shell scripting without using UI

You can also start reading my blog here Shahid's medium blog.

#!/bin/bash

## ##############################################################################################################################
##  Author : Sahid Shaik                                                                                                        #                            
## Usage: ./tags_delete.sh                                                                                                      #
## Harbor images deletion is 2 step process                                                                                     #
## 1. Soft delete image using this script                                                                                       #
## 2. Run garbage collector                                                                                                     #                            
## ##############################################################################################################################

source config.ini
folder=$(date +"%m-%d-%Y_%H.%M.%S")
mkdir ${folder}
staging_tag=stage
prod_tag=prod


TagsDelete() {
        for repo in `cat repos.txt`;
          do
                echo $repo;
                curl -u $harbor_user:$harbor_pswd -X GET http://$harbor_host/api/repositories/myrepo/$repo/tags > $folder/$repo.json;
                awk '/name/ {print $2}' $folder/$repo.json | tr -d '",' | sort > $folder/$repo.txt;
                if [ $1 == "stage" ]
                then
                     awk '/Staging/{print}'  $folder/$repo.txt | sort -Vr | sed -n '3,$p' > $folder/$1_$repo.ini;
                else
                     awk '!/Staging/{print}'  $folder/$repo.txt | sort -Vr | sed -n '2,$p' > $folder/$1_$repo.ini;
                fi
                for buildnum in `cat $folder/$1_$repo.ini`;
                        do
                                echo $buildnum;
                                curl -v -u $harbor_user:$harbor_pswd -X DELETE  http://$harbor_host/api/myrepo/tsom-ecc/$repo/tags/$buildnum;
                        done
        done
}

TagsDelete $staging_tag
TagsDelete $prod_tag

harbor-delete-tags's People

Contributors

shahidv3 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.