Last active
September 27, 2024 05:54
-
-
Save benok/4a66dc8862a468516b8ecac0c2060450 to your computer and use it in GitHub Desktop.
List up & Filter Vultr's Compute Instance plans in tsv & json using vultr-cli and jq.
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
# | |
# List up & Filter Vultr's Compute Instance plans* in tsv & json using vultr-cli and jq. | |
# (filterd by Region & your own condition. sorted by monthly cost. * BareMetal not supprted.) | |
# | |
# (Please use "Open Raw" and use curl/wget to download & rename or symlink as Makefile) | |
# | |
# usage: | |
# * Edit REGION below with your region code (refer "vultr-cli regions list") and "make" | |
# (or "REGION=xxx make") | |
# * Change filter conditions below as you like | |
# * "make download" to reload current info from vultr. | |
# | |
# Instance prefix memo | |
# | |
# vc2: Regular Cloud Compute (SATA SSD, prev gen Intel CPUs) | |
# vhf: High Frequency (NVMe SSD, 3GHz+ Intel Xeon) | |
# vhp: High Performance (NVMe SSD, AMD..latest gen EPYC/INTEL..new gens XEON) | |
# voc: Optimized Cloud Compute Dedicated CPU (NVMe SSD. Dedicated vcpu on vhp like hardware?) | |
# -g: Genreral Purpose | |
# -c: CPU Optimized | |
# -m: Memory Optimized | |
# vcg: Cloud GPU | |
# | |
REGION?=nrt | |
############################ | |
# Output Header & columns | |
HEADERS=id,type,cpu,ram(MB),disk(GB),bandwidth(GB),monthly_cost(USD) | |
FILTER_ATTRS={id,type,vcpu_count,ram,disk,bandwidth,monthly_cost} | |
############################ | |
# Filter Condtions | |
#ALL_FILTER=select(.type!="vcg") # except GPU plan | |
ALL_FILTER=select(.) # pass all plans | |
# Your condition | |
MY_FILTER=select(.ram>=1024)|select(.vcpu_count>=2)|select(.monthly_cost<120) | |
# Cheap ones | |
CHEAP_FILTER=select(.ram<=2048)|select(.vcpu_count==1) | |
# Sort method | |
SORT_COND=sort_by(.monthly_cost) | |
# all plans json output from vultr-cli | |
ALL_PLAN_JSON=vultr-all-plans.json | |
INPUT_FILES=Makefile $(HEADER) $(ALL_PLAN_JSON) | |
OUT_JSONS=$(REGION)-all-plans.json $(REGION)-my-plans.json $(REGION)-cheap-plans.json | |
OUT_TXTS=$(subst json,txt,$(OUT_JSONS)) | |
all: $(ALL_PLAN_JSON) $(OUT_JSONS) $(OUT_TXTS) | |
.PHONY: test | |
test: | |
cat $(ALL_PLAN_JSON) | jq '.plans[]|select(.locations[] | contains("$(REGION)")) | $(ALL_FILTER) | $(FILTER_ATTRS)' #| jq --slurp '$(SORT_COND).[]' | |
.PHONY: clean | |
clean: | |
rm -f *.json *.txt .download | |
.PHONY: download | |
download: | |
@rm -f .download | |
@make .download | |
.download: | |
@echo "Downloading plans using vultr-cli..." | |
vultr-cli plan -t all -p 500 -o json list > $(ALL_PLAN_JSON) | |
@touch .download | |
$(ALL_PLAN_JSON): .download | |
$(REGION)-all-plans.json: $(INPUT_FILES) | |
@echo "Generating $@..." | |
@cat $(ALL_PLAN_JSON) | jq '.plans[]|select(.locations[] | contains("$(REGION)")) | $(ALL_FILTER) | $(FILTER_ATTRS)' | jq --slurp '$(SORT_COND)' > $@ | |
@echo done. | |
$(REGION)-my-plans.json: $(INPUT_FILES) | |
@echo "Generating $@..." | |
@cat $(ALL_PLAN_JSON) | jq '.plans[]|select(.locations[] | contains("$(REGION)")) | $(ALL_FILTER) | $(MY_FILTER) | $(FILTER_ATTRS)' | jq --slurp '$(SORT_COND)' > $@ | |
@echo done. | |
$(REGION)-cheap-plans.json: $(INPUT_FILES) | |
@echo "Generating $@..." | |
@cat $(ALL_PLAN_JSON) | jq '.plans[]|select(.locations[] | contains("$(REGION)")) | $(ALL_FILTER) | $(CHEAP_FILTER) | $(FILTER_ATTRS)' | jq --slurp '$(SORT_COND)' > $@ | |
@echo done. | |
%.txt: %.json | |
@echo "Generating $@..." | |
@(echo "$(HEADERS)"|sed 's/,/\t/g'; cat $< | jq '.[]|flatten|@tsv' | sed -e 's/\\t/\t/g' | tr -d \") > $@ | |
@echo done. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment