Created
October 4, 2018 16:30
-
-
Save groundnuty/a4f6570bc6295530026a512f182285d4 to your computer and use it in GitHub Desktop.
plcloud_utils.sh
This file contains hidden or 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
#!/usr/bin/env bash | |
export URL='https://cloud.plgrid.pl/api/v1/' | |
#export PASS=~/.certs/user.pem:userpass | |
export PASS=~/.ssh/user.pem | |
plc_api() { | |
curl --cert $PASS ${URL}"$@" 2>/dev/null | |
} | |
# 220 - ubuntu 16 | |
# 279 - k8s-template | |
configuration_template_id=270 | |
# cat <<'EOF' | curl -X POST -vv --data @- -H "Content-Type: application/json" --cert $PASS ${URL}/port_mapping_templates | |
# { | |
# "port_mapping_template": { | |
# "transport_protocol": "tcp", | |
# "application_protocol":"none", | |
# "service_name": "vpn-k8s", | |
# "target_port": 31484, | |
# "appliance_type_id": 270 | |
# } | |
# } | |
# EOF | |
delete_vm() { | |
for name in $@ ; do | |
id="$(plc_api /appliances | jq --arg vm_name "$name" '.appliances[] | select(.name==$vm_name) | .id')" | |
curl -X DELETE -vv --cert $PASS ${URL}/appliances/$id | |
done | |
} | |
create_vm() { | |
name=$1 | |
vcpu=$2 | |
memory=$3 | |
description=$4 | |
vm_id=$(cat <<EOF | curl -X POST -vv --data @- -H "Content-Type: application/json" --cert $PASS ${URL}/appliances | |
{ | |
"appliance": { | |
"appliance_set_id": 148, | |
"name": "$name", | |
"description": "$description", | |
"user_key_id": 48, | |
"configuration_template_id": 220, | |
"compute_site_ids": [15], | |
"dev_mode_property_set": { | |
"preference_memory": ${memory}, | |
"preference_cpu": ${vcpu}, | |
"preference_disk": 10 | |
} | |
} | |
} | |
EOF | |
) | |
echo "VM_ID=$vm_id" | |
# echo '{"appliance":{"id":2513,"name":"k8s-cyfronet-2-worker3","description":"K8s experimental cluster, worker 3","state":"new","state_explanation":null,"amount_billed":0,"prepaid_until":"2000-01-01T12:00:00.000+01:00","team_id":null," | |
# appliance_configuration_instance_id":834,"appliance_set_id":148,"appliance_type_id":211,"virtual_machine_ids":[],"compute_site_ids":[15]}}' | |
# plc_api /appliance_types/211 | |
} | |
update_vm() { | |
name=$1 | |
description=$2 | |
id="$(plc_api /appliances | jq --arg vm_name "$name" '.appliances[] | select(.name==$vm_name) | .id')" | |
vm_id=$(cat <<EOF | curl -X PUT --data @- -H "Content-Type: application/json" --cert $PASS ${URL}/appliances/$id 2>/dev/null | |
{ | |
"appliance": { | |
"name": "$name", | |
"description": "$description" | |
} | |
} | |
EOF | |
) | |
echo "$vm_id" | jq . | |
} | |
list_vms() { | |
plc_api /appliances 2>/dev/null | \ | |
jq -r \ | |
--argjson all_virtual_machine_flavors "$(plc_api /virtual_machine_flavors 2>/dev/null)" \ | |
--argjson all_virtual_machines "$(plc_api /virtual_machines 2>/dev/null)" \ | |
--argjson all_compute_sites "$(plc_api /compute_sites 2>/dev/null)" \ | |
--argjson all_applicance_types "$(plc_api /appliance_types 2>/dev/null)" \ | |
\ | |
'def flavor_spec($flavor_id): $all_virtual_machine_flavors.virtual_machine_flavors[] | select(.id==$flavor_id) | "\(.cpu)\t\(.memory)" ; | |
def vm_specs($vm_id): $all_virtual_machines.virtual_machines[] | select(.id==$vm_id) | "\(.ip)\t\(flavor_spec(.virtual_machine_flavor_id))" ; | |
def site_name($site_id): $all_compute_sites.compute_sites[] | select(.id==$site_id) | .name ; def os_name($appliance_type_id): $all_applicance_types.appliance_types[] | select(.id==$appliance_type_id) | .name ; | |
.appliances[] | | |
"\( | |
.id)\t\( | |
.name)\t\( | |
if ((.virtual_machine_ids | length) == 1 ) then vm_specs(.virtual_machine_ids[0]) else "[...]" end)\t\( | |
os_name(.appliance_type_id))\t\(if ((.compute_site_ids | length) == 1 ) then site_name(.compute_site_ids[0]) else "[...]" end)\t\( | |
.description)" | |
' | \ | |
cat <(echo "ID\tNAME\tIP\tVCPU\tRAM[MB]\tIMAGE\tGROUP\tDESC\n----\t----\t----\t----\t----\t----\t----\t----") - | column -s $'\t' -t | |
} | |
list_vms_p() { | |
plc_api /appliances 2>/dev/null | \ | |
jq -r \ | |
--argjson all_ports "$(plc_api /port_mappings 2>/dev/null | jq -r '.port_mappings[] | "\(.virtual_machine_id) \(.source_port) \(.port_mapping_template_id)"' | while read vm source_port l ; do echo -n "{\"virtual_machine_id\":$vm,\"source_port\":$source_port,\"port_mapping_id\":$l,\"target_port\":" ; plc_api 2>/dev/null /port_mapping_templates/$l 2>/dev/null | jq -r -j ".port_mapping_template | .target_port" ; echo -n "},"; done | rev | cut -c 2- | rev | cat <(echo [ ) - <(echo ]))" \ | |
--argjson all_virtual_machine_flavors "$(plc_api /virtual_machine_flavors 2>/dev/null)" \ | |
--argjson all_virtual_machines "$(plc_api /virtual_machines 2>/dev/null)" \ | |
--argjson all_compute_sites "$(plc_api /compute_sites 2>/dev/null)" \ | |
--argjson all_applicance_types "$(plc_api /appliance_types 2>/dev/null)" \ | |
\ | |
'def ports($vm_id): $all_ports | group_by(.virtual_machine_id)[] | select(.[0].virtual_machine_id==$vm_id) | map([.target_port,.source_port]) | sort | map("\(.[0]):\(.[1])") | join(",") ; | |
def flavor_spec($flavor_id): $all_virtual_machine_flavors.virtual_machine_flavors[] | select(.id==$flavor_id) | "\(.cpu)\t\(.memory)" ; | |
def vm_specs($vm_id): $all_virtual_machines.virtual_machines[] | select(.id==$vm_id) | "\(.ip)\t\(flavor_spec(.virtual_machine_flavor_id))" ; | |
def site_name($site_id): $all_compute_sites.compute_sites[] | select(.id==$site_id) | .name ; def os_name($appliance_type_id): $all_applicance_types.appliance_types[] | select(.id==$appliance_type_id) | .name ; | |
.appliances[] | | |
"\( | |
.id)\t\( | |
.name)\t\( | |
if ((.virtual_machine_ids | length) == 1 ) then vm_specs(.virtual_machine_ids[0]) else "[...]" end)\t\( | |
ports(.virtual_machine_ids[0]))\t\( | |
os_name(.appliance_type_id))\t\(if ((.compute_site_ids | length) == 1 ) then site_name(.compute_site_ids[0]) else "[...]" end)\t\( | |
.description)" | |
' | \ | |
cat <(echo "ID\tNAME\tIP\tVCPU\tRAM[MB]\tPORTS\tIMAGE\tGROUP\tDESC\n----\t----\t----\t----\t----\t----\t----\t----\t----") - | column -s $'\t' -t | |
} | |
create_workflow() { | |
set_id=$(cat <<'EOF' | curl -X POST -vv --data @- -H "Content-Type: application/json" --cert $PASS ${URL}/appliance_sets | jq '.appliance_set.id' | |
{ | |
"appliance_set": { | |
"priority": 50, | |
"appliance_set_type": "workflow", | |
"optimalization_policy": "manual", | |
"appliances": [ | |
{ | |
"configuration_template_id": 279, | |
"vms": [ | |
{ "cpu": 2, "mem": 2048 } | |
] | |
}, | |
{ | |
"configuration_template_id": 279, | |
"vms": [ | |
{ "cpu": 2, "mem": 2048 } | |
] | |
} | |
] | |
} | |
} | |
EOF | |
) | |
#set_id=449 | |
echo $set_id | |
# list of worflow vms | |
for vm in $(curl --cert $PASS ${URL}/appliances | jq -r --arg set_id "$set_id" '.appliances[] | select(.appliance_set_id==($set_id|tonumber)) | .virtual_machine_ids[] ' | tr '\n' ' ' ); do | |
echo | |
vm_ip=$(curl --cert $PASS ${URL}/virtual_machines/${vm} | jq -r '.virtual_machine.ip' ) | |
punlic_ip_and_ssh_port=$(curl --cert $PASS ${URL}/port_mappings | jq -r --arg vm "$vm" '.port_mappings[] | select(.virtual_machine_id==($vm|tonumber)) | "\(.public_ip) \(.source_port)"') | |
echo "$vm" "$vm_ip" "$punlic_ip_and_ssh_port" | |
done | |
} | |
plc_ssh_config() { | |
plc_api /appliances 2>/dev/null | \ | |
jq -r \ | |
--argjson all_ports "$(plc_api /port_mappings 2>/dev/null | jq -r '.port_mappings[] | "\(.virtual_machine_id) \(.source_port) \(.port_mapping_template_id)"' | while read vm source_port l ; do echo -n "{\"virtual_machine_id\":$vm,\"source_port\":$source_port,\"port_mapping_id\":$l,\"target_port\":" ; plc_api 2>/dev/null /port_mapping_templates/$l 2>/dev/null | jq -r -j ".port_mapping_template | .target_port" ; echo -n "},"; done | rev | cut -c 2- | rev | cat <(echo [ ) - <(echo ]))" \ | |
\ | |
'def ports($vm_id): $all_ports[] | select(.target_port==22) | select(.virtual_machine_id==$vm_id) | .source_port; | |
.appliances[] | select(.description | startswith("@ssh")) | "\(.description / ":" | .[1] )\t\(ports(.virtual_machine_ids[0]))" | |
' | \ | |
while read name port ; do | |
cat <<EOF | |
Host $name | |
#@onedata @cyfronet | |
hostname 149.156.11.4 | |
port $port | |
user ubuntu | |
EOF | |
done | |
} | |
plc_assh_config() { | |
plc_api /appliances 2>/dev/null | \ | |
jq -r \ | |
--argjson all_ports "$(plc_api /port_mappings 2>/dev/null | jq -r '.port_mappings[] | "\(.virtual_machine_id) \(.source_port) \(.port_mapping_template_id)"' | while read vm source_port l ; do echo -n "{\"virtual_machine_id\":$vm,\"source_port\":$source_port,\"port_mapping_id\":$l,\"target_port\":" ; plc_api 2>/dev/null /port_mapping_templates/$l 2>/dev/null | jq -r -j ".port_mapping_template | .target_port" ; echo -n "},"; done | rev | cut -c 2- | rev | cat <(echo [ ) - <(echo ]))" \ | |
\ | |
'def ports($vm_id): $all_ports[] | select(.target_port==22) | select(.virtual_machine_id==$vm_id) | .source_port; | |
.appliances[] | select(.description | startswith("@ssh")) | "\(.description / ":" | .[1] )\t\(ports(.virtual_machine_ids[0]))" | |
' | \ | |
while read name port ; do | |
cat <<EOF | |
$name: | |
Inherits: pl-cloud | |
Port: $port | |
EOF | |
done | |
} | |
# # forwarded ssh port of VM | |
# /c.sh /api/v1//port_mappings | jq '.port_mappings[] | select(.virtual_machine_id==2340) | .source_port' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment