Přeskočit na hlavní obsah

Clean-up blobs in Azure storage account with Azure CLI

If you realize at some point of your bright Azure future you don't remove VM disks correctly (like me in JCLOUDS-1170) you'll probably want to do a storage account clean up. Here is, how I did it on my linux desktop using Azure CLI.

As I wanted to keep some private OS images which were located in the storage account too, I moved them first into a newly created storage container.

# Prerequisities
  • installed azure-cli (I personally use the latest docker image microsoft/azure-cli with the tool)
  • installed jq tool for parsing JSON

# Set up

STORAGE_ACCOUNT="PutYourStorageAccountNameHere"
STORAGE_KEY="PutYourStorageAccountAccessKeyHere"

# Move/backup OS images to a new container

# create new storage container for OS image blobs
azure storage container create -a $STORAGE_ACCOUNT -k "$STORAGE_KEY" os-images

# linux images to be kept
for IMG in eap-7-rhel-7.2 eap-7-rhel-7.2-service; do
ORIG_BLOB=$(azure vm image show --json $IMG | jq -Mr '.mediaLinkUri')
echo "Original blob URL: $ORIG_BLOB"
# delete image, but keep the blob in storage account
azure vm image delete $IMG
# copy the blob to a new container and create image from it
azure vm image create --blob-url https://$STORAGE_ACCOUNT.blob.core.windows.net/os-images/$IMG.vhd --os linux --source-key "$STORAGE_KEY" $IMG $ORIG_BLOB
done

# Remove containers (with blobs)

# list container names (without the newly created one) to be removed
azure storage container list -a $STORAGE_ACCOUNT -k "$STORAGE_KEY" --json | jq -rM '.[] | .name' |grep -v os-images > /tmp/containers.txt

# for each container remove all blobs in it and then remove container
for CONTAINER in `cat /tmp/containers.txt`; do
azure storage blob list -a $STORAGE_ACCOUNT -k "$STORAGE_KEY" --json $CONTAINER | jq -rM '.[] | .name' > /tmp/vhds.txt
for BLOB in `cat /tmp/vhds.txt`; do
echo "Removing $BLOB"
# let's try to breake blob lease (if there exists one), otherwise we'll not be able to remove it
azure storage blob lease break -a $STORAGE_ACCOUNT -k "$STORAGE_KEY" $CONTAINER $BLOB
# remove blob
azure storage blob delete -q -a $STORAGE_ACCOUNT -k "$STORAGE_KEY" $CONTAINER $BLOB
done
# let's try to breake container lease (if there exists one), otherwise we'll not be able to remove it
azure storage container lease break -a $STORAGE_ACCOUNT -k "$STORAGE_KEY" $CONTAINER
# remove storage container
azure storage container delete -q -a $STORAGE_ACCOUNT -k "$STORAGE_KEY" $CONTAINER
done

Komentáře

Populární příspěvky z tohoto blogu

Three ways to redirect HTTP requests to HTTPs in WildFly and JBoss EAP

WildFly application server (and JBoss EAP) supports several simple ways how to redirect the communication from plain HTTP to TLS protected HTTPs. This article presents 3 ways. Two are on the application level and the last one is on the server level valid for requests to all deployments. 1. Request confidentiality in the deployment descriptor The first way is based on the Servlet specification. You need to specify which URLs should be protected in the web.xml deployment descriptor. It's the same approach as the one used for specifying which URLs require authentication/authorization. Just instead of requesting an assigned role, you request a transport-guarantee . Sample content of the WEB-INF/web.xml <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1

Zipujeme efektivně

Jedna ze základních vlastností Javy je práce se ZIP archívy, ať už jsou to knihovny tříd a spustitelné JARy, webové aplikace (war), nebo třeba JEE bumbrlíčci (ear). Není tedy divu, že i přímo v základním API je implementována práce s těmito archívy. Slouží k tomu třídy v balíku java.util.zip a nejzajímavější z nich jsou ZipOutputStream a ZipInputStream. Příkladem budiž vytvoření zipu: //Vytvorime Zip ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("javlog.zip")); //V Zipu chceme mit jeden textovy soubor zos.putNextEntry(new ZipEntry("priznani.txt")); //naplnime obsah textoveho souboru zos.write("Máme rádi Javu!".getBytes()); //zavrem entry (priznani.txt) zos.closeEntry(); //zavrem stream zos.close(); a jeho rozbalení: ZipInputStream zis = new ZipInputStream(new FileInputStream("javlog.zip")); ZipEntry zipEntry; //budem predpokladat, ze v ZIPu mame jen textove soubory a tak je vypisem do konzole while ((zipEntry = zis.getNextE

jd-cli – Command line Java Decompiler

Kdo by neznal jd-gui Java Decompiler (z free.fr) a jeho sourozence, pluginy pro IDE – Eclipse a IntelliJ. Ale nechyběla vám také někdy možnost rychle decompilovat celý JAR nebo WAR do adresáře, případně zobrazit si decompilovanou třídu jen v konzoli bez nutnosti spouštění GUI? Jestliže ano, pak se zkuste podívat na utilitku jd-cli , která toto všechno umožňuje. Tato aplikace je jednoduchý wrapper nad nativní knihovnou pro jd-gui InelliJ plugin. Kde všude tato java aplikace běží? Windows Linux Mac OSX Kompilace Potřebujete git a Maven. git clone git@github.com:kwart/jd-cmd.git cd jd-cmd mvn clean package Rozbalte jd-cli-[version].zip (.tar.gz) někam, kam odkazuje systémová PATH proměnná ( C:\Windows nebo $HOME/bin ) - distribuce obsahuje i spouštěcí skripty (shell a batch), takže pak už jen vesele voláte jd-cli [aParametry] Příklady použití jd-cli HelloWorld.class Zobrazí dekompilovanou třídu v konzoli jd-cli --skipResources -n -g ALL app.jar Dekompiluje obsah