1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
#!/bin/sh
set -e
#set -x
. /root/oci-openrc
####################################
### INSTANCE UP AND DOWN PRICING ###
####################################
FLAVOR_PRICES="cpu1-ram2-disk5,0.194055322,0.104055322
cpu2-ram6-disk20,0.50701171,0.30701171
cpu4-ram12-disk40,1.251825552,0.851825552"
HDD_STORAGE_PRICE="0.0007400"
SSD_STORAGE_PRICE="0.005945050"
PUBLIC_IP_PRICE="0.22831050"
LOADBALANCER_PRICE="0.68493150"
setup_group_id_for_instances () {
echo "---> Retryving (or creating) hashmap group ID for instance billing"
HASHMAP_GROUP_ID=$(cloudkitty hashmap group list --format csv | q -H -d, "SELECT \`Group ID\` from - WHERE Name='instance_flavor_names'")
if [ -z "${HASHMAP_GROUP_ID}" ] ; then
cloudkitty hashmap group create instance_flavor_names
HASHMAP_GROUP_ID=$(cloudkitty hashmap group list --format csv | q -H -d, "SELECT \`Group ID\` from - WHERE Name='instance_flavor_names'")
else
echo "Found: ${HASHMAP_GROUP_ID}"
fi
}
setup_instance_up_prices () {
echo "---> Retryving (or creating) hashmap service ID for instance up..."
HASHMAP_SERVICE_ID=$(cloudkitty hashmap service list --format csv | q -H -d, "SELECT \`Service ID\` from - WHERE Name='instance_up'")
if [ -z "${HASHMAP_SERVICE_ID}" ] ; then
cloudkitty hashmap service create instance_up
HASHMAP_SERVICE_ID=$(cloudkitty hashmap service list --format csv | q -H -d, "SELECT \`Service ID\` from - WHERE Name='instance_up'")
else
echo "Found: ${HASHMAP_SERVICE_ID}"
fi
echo "---> Retryving (or creating) hashmap field ID for instance up..."
HASHMAP_FIELD_ID=$(cloudkitty hashmap field list --format csv ${HASHMAP_SERVICE_ID} | q -H -d, "SELECT \`Field ID\` from - WHERE Name='flavor_name'")
if [ -z "${HASHMAP_FIELD_ID}" ] ; then
cloudkitty hashmap field create ${HASHMAP_SERVICE_ID} flavor_name
HASHMAP_FIELD_ID=$(cloudkitty hashmap field list --format csv ${HASHMAP_SERVICE_ID} | q -H -d, "SELECT \`Field ID\` from - WHERE Name='flavor_name'")
else
echo "Found: ${HASHMAP_FIELD_ID}"
fi
echo "---> Setting-up flavor price listing for instance_up..."
for FLAVOR_NAME_AND_PRICE in $FLAVOR_PRICES ; do
FLAVOR_NAME=$(echo ${FLAVOR_NAME_AND_PRICE} | cut -d, -f1)
FLAVOR_PRICE=$(echo ${FLAVOR_NAME_AND_PRICE} | cut -d, -f2)
echo "-> Searching for flavor ID of ${FLAVOR_NAME}"
FLAVOR_ID=$(openstack flavor show ${FLAVOR_NAME} --format value -c id)
echo "-> Check price in db for ${FLAVOR_NAME}"
FOUND_COST=$(cloudkitty hashmap mapping list --field-id ${HASHMAP_FIELD_ID} --group-id ${HASHMAP_GROUP_ID} --format csv | q -H -d, "SELECT Cost FROM - WHERE Value='${FLAVOR_NAME}'")
if [ -z "${FOUND_COST}" ] ; then
echo "-> Price not found: creating..."
cloudkitty hashmap mapping create --field-id ${HASHMAP_FIELD_ID} --value ${FLAVOR_NAME} -t flat -g ${HASHMAP_GROUP_ID} ${FLAVOR_PRICE}
elif [ "${FOUND_COST}" != "${FLAVOR_PRICE}" ] ; then
echo "-> Non-matching price found: deleting mapping..."
MAPPING_ID=$(cloudkitty hashmap mapping list --field-id ${HASHMAP_FIELD_ID} --group-id ${HASHMAP_GROUP_ID} --format csv | q -H -d, "SELECT \`Mapping ID\` FROM - WHERE Value='${FLAVOR_NAME}'")
cloudkitty hashmap mapping delete ${MAPPING_ID}
echo "-> Re-creating..."
cloudkitty hashmap mapping create --field-id ${HASHMAP_FIELD_ID} --value ${FLAVOR_NAME} -t flat -g ${HASHMAP_GROUP_ID} ${FLAVOR_PRICE}
else
echo "-> Exact price found: skipping..."
fi
done
}
setup_instance_down_prices () {
echo "---> Retryving (or creating) hashmap service ID for instance down..."
HASHMAP_SERVICE_ID=$(cloudkitty hashmap service list --format csv | q -H -d, "SELECT \`Service ID\` from - WHERE Name='instance_down'")
if [ -z "${HASHMAP_SERVICE_ID}" ] ; then
cloudkitty hashmap service create instance_down
HASHMAP_SERVICE_ID=$(cloudkitty hashmap service list --format csv | q -H -d, "SELECT \`Service ID\` from - WHERE Name='instance_down'")
else
echo "Found: ${HASHMAP_SERVICE_ID}"
fi
echo "---> Retryving (or creating) hashmap field ID for instance down..."
HASHMAP_FIELD_ID=$(cloudkitty hashmap field list --format csv ${HASHMAP_SERVICE_ID} | q -H -d, "SELECT \`Field ID\` from - WHERE Name='flavor_name'")
if [ -z "${HASHMAP_FIELD_ID}" ] ; then
cloudkitty hashmap field create ${HASHMAP_SERVICE_ID} flavor_name
HASHMAP_FIELD_ID=$(cloudkitty hashmap field list --format csv ${HASHMAP_SERVICE_ID} | q -H -d, "SELECT \`Field ID\` from - WHERE Name='flavor_name'")
else
echo "Found: ${HASHMAP_FIELD_ID}"
fi
echo "---> Setting-up flavor price listing for instance_down..."
for FLAVOR_NAME_AND_PRICE in $FLAVOR_PRICES ; do
FLAVOR_NAME=$(echo ${FLAVOR_NAME_AND_PRICE} | cut -d, -f1)
FLAVOR_PRICE=$(echo ${FLAVOR_NAME_AND_PRICE} | cut -d, -f3)
echo "-> Searching for flavor ID of ${FLAVOR_NAME}"
FLAVOR_ID=$(openstack flavor show ${FLAVOR_NAME} --format value -c id)
echo "-> Check price in db for ${FLAVOR_NAME}"
FOUND_COST=$(cloudkitty hashmap mapping list --field-id ${HASHMAP_FIELD_ID} --group-id ${HASHMAP_GROUP_ID} --format csv | q -H -d, "SELECT Cost FROM - WHERE Value='${FLAVOR_NAME}'")
if [ -z "${FOUND_COST}" ] ; then
echo "-> Price not found: creating..."
cloudkitty hashmap mapping create --field-id ${HASHMAP_FIELD_ID} --value ${FLAVOR_NAME} -t flat -g ${HASHMAP_GROUP_ID} ${FLAVOR_PRICE}
elif [ "${FOUND_COST}" != "${FLAVOR_PRICE}" ] ; then
echo "-> Non-matching price found: deleting mapping..."
MAPPING_ID=$(cloudkitty hashmap mapping list --field-id ${HASHMAP_FIELD_ID} --group-id ${HASHMAP_GROUP_ID} --format csv | q -H -d, "SELECT \`Mapping ID\` FROM - WHERE Value='${FLAVOR_NAME}'")
cloudkitty hashmap mapping delete ${MAPPING_ID}
echo "-> Re-creating..."
cloudkitty hashmap mapping create --field-id ${HASHMAP_FIELD_ID} --value ${FLAVOR_NAME} -t flat -g ${HASHMAP_GROUP_ID} ${FLAVOR_PRICE}
else
echo "-> Exact price found: skipping..."
fi
done
}
#########################
### GENERIC FUNCTIONS ###
#########################
# Param: $1 gropu name
get_or_create_hashmap_group () {
GROUP_NAME=$1
# Create group:
echo "---> Searching for hashmap group ${GROUP_NAME}"
if ! cloudkitty hashmap group list --format value -c Name | grep -E '^'${GROUP_NAME}'$' ; then
echo "-> Didn't find: creating..."
cloudkitty hashmap group create ${GROUP_NAME}
fi
echo -n "-> Getting ID: "
HASHMAP_GROUP=$(cloudkitty hashmap group list --format csv -c Name -c 'Group ID' | q -H -d, "SELECT \`Group ID\` FROM - WHERE Name='${GROUP_NAME}'")
echo ${HASHMAP_GROUP}
}
get_or_create_hashmap_service () {
SERVICE_NAME=$1
echo "---> Searching for hashmap service ${SERVICE_NAME}"
if ! cloudkitty hashmap service list --format value -c Name | grep -E '^'${SERVICE_NAME}'$' ; then
cloudkitty hashmap service create ${SERVICE_NAME}
fi
echo -n "-> Getting ID: "
HASHMAP_SERVICE=$(cloudkitty hashmap service list --format csv -c Name -c 'Service ID' | q -H -d, "SELECT \`Service ID\` FROM - WHERE Name='${SERVICE_NAME}'")
echo ${HASHMAP_SERVICE}
}
set_hashmap_mapping_price () {
local PRICE
PRICE=${1}
echo "---> Searching for set price for group id ${HASHMAP_GROUP} and service id ${HASHMAP_SERVICE}"
VAL=$(cloudkitty hashmap mapping list --group-id ${HASHMAP_GROUP} --service-id ${HASHMAP_SERVICE} --format value -c Cost -c 'Mapping ID')
if [ -z "${VAL}" ] ; then
echo "-> Price not set: setting-up"
cloudkitty hashmap mapping create --group-id ${HASHMAP_GROUP} --service-id ${HASHMAP_SERVICE} -t flat ${PRICE}
else
COST=$(echo ${VAL} | cut -d' ' -f2)
ID=$(echo ${VAL} | cut -d' ' -f1)
echo "-> Price already set"
if [ "${COST}" = "${PRICE}" ] ; then
echo "-> Price is correct"
else
echo "-> Price is NOT correct: updating"
cloudkitty hashmap mapping update --group-id ${HASHMAP_GROUP} --service-id ${HASHMAP_SERVICE} --cost ${PRICE} ${ID}
fi
fi
}
########################
### VOLUME FUNCTIONS ###
########################
get_or_create_volume_type_field () {
echo "---> Retryving (or creating) hashmap field ID"
HASHMAP_FIELD_ID=$(cloudkitty hashmap field list --format csv ${HASHMAP_SERVICE} | q -H -d, "SELECT \`Field ID\` from - WHERE Name='volume_type'")
if [ -z "${HASHMAP_FIELD_ID}" ] ; then
cloudkitty hashmap field create ${HASHMAP_SERVICE} volume_type
HASHMAP_FIELD_ID=$(cloudkitty hashmap field list --format csv ${HASHMAP_SERVICE} | q -H -d, "SELECT \`Field ID\` from - WHERE Name='volume_type'")
else
echo "Found: ${HASHMAP_FIELD_ID}"
fi
}
# Params: $1 = volume type name
# $2 = volume price for that type
get_or_create_volume_mapping (){
local VOLUME_TYPE_NAME VOLUME_PRICE
VOLUME_TYPE_NAME=$1
VOLUME_PRICE=$2
echo "-> Check price in db for ${VOLUME_TYPE_NAME}"
FOUND_COST=$(cloudkitty hashmap mapping list --field-id ${HASHMAP_FIELD_ID} --format csv | q -H -d, "SELECT Cost FROM - WHERE Value='${VOLUME_TYPE_NAME}'")
if [ -z "${FOUND_COST}" ] ; then
echo "-> Price not found: creating..."
cloudkitty hashmap mapping create --field-id ${HASHMAP_FIELD_ID} --value ${VOLUME_TYPE_NAME} -t flat ${VOLUME_PRICE}
elif [ "${FOUND_COST}" != "${VOLUME_PRICE}" ] ; then
echo "-> Non-matching price found: deleting mapping..."
MAPPING_ID=$(cloudkitty hashmap mapping list --field-id ${HASHMAP_FIELD_ID} --format csv | q -H -d, "SELECT \`Mapping ID\` FROM - WHERE Value='${VOLUME_TYPE_NAME}'")
cloudkitty hashmap mapping delete ${MAPPING_ID}
echo "-> Re-creating..."
cloudkitty hashmap mapping create --field-id ${HASHMAP_FIELD_ID} --value ${VOLUME_TYPE_NAME} -t flat -g ${HASHMAP_GROUP} ${VOLUME_PRICE}
else
echo "-> Exact price found: skipping..."
fi
}
# Warning: this function does NOT handle the case with multiple threshold.
set_hashmap_mapping_threshold_price (){
local PRICE THRESHOLD
THRESHOLD=${1}
PRICE=${2}
echo "---> Searching for set price for group id ${HASHMAP_GROUP} and service id ${HASHMAP_SERVICE}"
VAL=$(cloudkitty hashmap threshold list --group-id ${HASHMAP_GROUP} --service-id ${HASHMAP_SERVICE} --format value -c 'Threshold ID' -c Level -c Cost -c Level)
if [ -z "${VAL}" ] ; then
echo "-> Price not set for threshold ${THRESHOLD}: setting-up"
cloudkitty hashmap threshold create --group-id ${HASHMAP_GROUP} --service-id ${HASHMAP_SERVICE} -t flat ${THRESHOLD} ${PRICE}
else
ID=$(echo ${VAL} | cut -d' ' -f1)
LEVEL=$(echo ${VAL} | cut -d' ' -f2)
COST=$(echo ${VAL} | cut -d' ' -f3)
echo "-> Price already set"
if [ "${COST}" = "${PRICE}" ] && [ "${LEVEL}" = "${THRESHOLD}" ] ; then
echo "-> Price ${PRICE} is correct for threshold ${THRESHOLD}"
else
echo "-> Price is NOT correct: updating"
cloudkitty hashmap threshold update -l ${THRESHOLD} --cost ${PRICE} ${ID}
fi
fi
}
# Instance up/down pricing #
#setup_group_id_for_instances
#setup_instance_up_prices
#setup_instance_down_prices
# Cinder backup
get_or_create_hashmap_group volume_backup
get_or_create_hashmap_service volume.backup.size
set_hashmap_mapping_price ${HDD_STORAGE_PRICE}
# Glance image
get_or_create_hashmap_group image_size
get_or_create_hashmap_service image.size
set_hashmap_mapping_price ${HDD_STORAGE_PRICE}
# LB
get_or_create_hashmap_group loadbalancer
get_or_create_hashmap_service network.services.lb.loadbalancer
set_hashmap_mapping_price ${LOADBALANCER_PRICE}
# Volume
get_or_create_hashmap_group volume_size
get_or_create_hashmap_service volume.size
get_or_create_volume_type_field
get_or_create_volume_mapping CEPH_1 ${SSD_STORAGE_PRICE}
# Snapshots
get_or_create_hashmap_group snapshot_size
get_or_create_hashmap_service snapshot.size
set_hashmap_mapping_price ${SSD_STORAGE_PRICE}
# Public IPs
get_or_create_hashmap_group public_ip
get_or_create_hashmap_service network.ports.router-gw-ext-floating1
set_hashmap_mapping_price ${PUBLIC_IP_PRICE}
get_or_create_hashmap_service ip.floating
set_hashmap_mapping_price ${PUBLIC_IP_PRICE}
get_or_create_hashmap_service network.ports.ext-net1
set_hashmap_mapping_price ${PUBLIC_IP_PRICE}
|