The latest insights from the load balancing experts | Loadbalancer.org
  • Support
  • Blog
  • +1 833 274 2566
  • Solutions
  • Services
  • Products
  • Resources
  • Get Started
  • Support
  • Blog
Schedule your demo
  • Solutions
  • Services
  • Products
  • Resources
  • Get Started
  • Support
  • Blog

The latest insights from the load balancing experts | Loadbalancer.org

  • Latest posts
  • By topic
    • How Tos
    • Events
    • Guest Blogs
    • Top Ten Blogs
    • HA Proxy
  • By sector
    • Healthcare
    • Storage
    • Security
    • Print
    • Microsoft
  • How-To's
  • HAProxy
  • High Availability
  • Just for Fun
  • Security
  • Events
  • News
  • Linux
  • Top 10 Blogs
  • Amazon AWS
  • Reviews and Comparisons
  • Healthcare
  • SSL
  • Web Application Firewall (WAF)
  • Case Studies
  • Microsoft Azure
  • Disaster Recovery
  • Direct Server Return (DSR)
  • Global Server Load Balancing (GSLB)
  • Microsoft
  • Microsoft Exchange
  • Print
  • Denial of Service
  • Microsoft Remote Desktop Services
  • Object Storage
  • Web Filters / Proxy
  • Broadcast Media
  • X-Forwarded-For Header (XFF)
  • Guest Blogs
  • VMware
  • Google Cloud Platform (GCP)
  • Nutanix
See more tags

How to YAML your load balancer deployment

19 June 2020 / 3 min read / How-To's

If you read my other posts relating to our v8 API interfaces (follow the links here for parts one and two), but don't want to use either JSON formats as your input format, this is just fine – because we'll be using something different in this post.

Below is the code for a new API interface to take 'YAML Ain't Markup Language' (otherwise known as YAML) and post it to an LBCLI command.

<?php
/*

(c) 2020 Loadbalancer.org - Written by Andruw Smalley

Loadbalancer lbcli YAML lbcli poster /api/v2/yaml
This will just work with any version from v7.6.3 - v8.4.2 
Yes it works with ANY loadbalancer.org appliance which 
works with lbcli, note you will know what you can do in 
each version of lbcli for the API interface to work. 
Remember pre v8 lbcli did not do much!


Example YAML API Usage place in PUT
PUT /var/www/html/api/v2/yaml/index.php 
OR anywhere under /var/www/html/ if you wish.

lbcli:
- action: hostname
function: set
hostname: mole
domain: master.lb.zerodns.co.uk

// YAML IMPLIMENTATION by Andruw Smalley

*/

date_default_timezone_set('Europe/London');

require_once("/etc/loadbalancer.org/api-credentials");
if(!isset($_SERVER["SERVER_ADDR"])) {
    die("call me from a network resource please");
}
putenv("SERVER_ADDR=" . $_SERVER["SERVER_ADDR"]);

if (!isset($username, $password, $apikey)) {
    exit;
} else if (!isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) {
    header('WWW-Authenticate: Basic realm="API"');
    header('HTTP/1.0 401 Unauthorized');
    echo "lbapi:\n- noway: noyaml\n";
    exit;
} else if ($username == $_SERVER['PHP_AUTH_USER'] && $password == $_SERVER['PHP_AUTH_PW']) {
    if ($_SERVER["HTTP_X_LB_APIKEY"]) {
        if (trim(base64_decode($_SERVER["HTTP_X_LB_APIKEY"])) == $apikey) {

            $fp = fopen('php://input', 'r') or die("  input: failed\n");
            $act = 0;
            while (!feof($fp)) {
                list($key, $value) = explode(":", trim(str_replace("\n", "", fgets($fp))));
                $key = str_replace("- ", "", $key);
                if ($key == "lbcli") {
                    $actok = true;
                    echo "Continuing:\n";
                    continue;
                }
                if ($actok !== true) {
                    echo "noact:\n";
                    return false;
                }
                if ($key == "action") {
                    if (!isset($act)) {
                        $act = -1;
                    }
                    $act++;
                    $lbcli[$act] = "/usr/local/sbin/lbcli --action " . trim($value) . " --method api";
                } else {
                    if (isset($value)) {
                        $lbcli[$act] .= " --" . $key . " " . trim($value);
                    }        
                }          
            }
            fclose($fp);
            $return = yamlout($lbcli);     
        } else {
            header('WWW-Authenticate: Basic realm="YAMLAPI"');
            header('HTTP/1.0 401 Unauthorized');
            echo "lbapi:\"- json: invalid\n";
            die();
        }
    } else {
        header('WWW-Authenticate: Basic realm="API"');
        header('HTTP/1.0 401 Unauthorized');
        echo "lbapi:\n- apikey: failed\n";
        die();
    }
}

function yamlout($lbcli) {
    echo "lbcli:\n";
    foreach ($lbcli as $call) {
        $output    = "[" . shell_exec($cmd . $call) . "]";
        $responses = (json_decode($output, true));
        foreach ($responses as $iteration => $response) {
            $yam[] = $response['itteration']['0']['lbcli']['0'];
        }
    }
    $count   = count($yam);
    $counter = 0;
    while ($counter < $count) {
        foreach ($yam[$counter] as $key => $value) {
            if ($key == "action" || $key == "core") {
                echo "- " . trim($key) . ": " . trim($value) . "\n";
            } else {
                if (!is_array($value)) {
                    echo "  " . trim($key) . ": " . trim($value) . "\n";
                } else {
                    echo "  " . trim($key) . ":\n";
                    if ($value['0']) {
                        $value = $value['0'];
                    } else {
                        $value = $value;
                    }
                    foreach ($value as $okey => $ovalue) {
                        if (!empty($ovalue)) {
                            echo "  - " . trim($okey) . ": " . trim($ovalue) . "\n";
                        }
                    }
                }
            }
        }
        $counter++;
    }
}

The simple interface above will accept YAML but we need something to post the YAML file to the API - this is below:

#!/bin/bash 
# loadbalancer.org api/v2 curl apicall
# Created by Andruw Smalley 
# really simple curl command build from the input, no real validation.
username="loadbalancer"
password="loadbalancer"
port="9443"

while true; do
  case "$1" in
    -l | --loadbalancer ) loadbalancer="$2"; shift 2 ;;
    -u | --username ) username="$2"; shift 2 ;;
    -p | --password ) password="$2"; shift 2 ;;
    -o | --port ) port="$2"; shift 2 ;;
    -y | --yaml ) yaml="$2"; shift 2 ;;
    -a | --apikey ) apikey=$(echo $2 | base64); shift 2 ;;
    * ) break ;;
  esac
done

#if [ $loadbalancer != "" ] || [ $port != "" ] || [ $username != "" ] || [ $password != "" ] || [ $yaml != "" ] || [ $apikey != "" ]; then
	curl -u ${username}:${password} -X POST  \
	     --header "X_LB_APIKEY: ${apikey}" \
	     --data-binary @${yaml} https://${loadbalancer}:${port}/api/v2/yaml/ -k
	exit $?
#else
#	echo "./apicall.sh --loadbalancer 192.168.2.21 --port 9443 --username loadbalancer --password loadbalancer --yaml /path/to/yaml.yaml --apikey eP68pvSMM8dvn051LL4d35569d438ue0"
#	echo "Defaults are set for username,password,port so you only really need --yaml --apikey and --loadbalancer if you have defaults"
#fi

With this, we now have the YAML interface and a method to post the contents! So all that's left is to share all the API syntax in YAML format.

A single YAML code block example layout is below:

lbcli:
-  action: api
   function: enable
   username:
   password:
   apikey:

And if you wish to run more than one - action: at a time simply add another action without re-declaring lbcli:

lbcli:
-  action: api
   function: enable
   username:
   password:
   apikey:
-  action: api
   function: disable   

So there we have it - how to YAML our load balancer, plus the ability to add your YAML file format to the API, should you choose to.

We've uploaded the YAML interface code to GitHub, as well as the YAML library.

Found in

How-To's

About the author

Andrew Smalley-profile-image
Andrew Smalley

Andrew first started working in Information Technology retail and support in 1989. His first experience of a modem was a 1200/75 baud, way back in 1984. He found Linux in the flavor of slackware on floppy disks and since then has used many distributions. His personal workstation choice now is Arch Linux and embedded distribution is Alpine Linux and he enjoys building and compiling for IOT and embedded devices. He contributes regularly to various mailing lists and to websites

Read More

Related posts

How-To's
How-To's
30 Sep 2020
Things to keep in mind while choosing a load balancer for your object storage system Himakshi Goswami
Need your object storage system to be highly available, reliable and scalable? We've got you covered. Check out the most important things you need to know when load balancing your storage architecture.

3 min read

Read more
Amazon AWS
Amazon AWS
4 Sep 2020
Hardware, virtual, or cloud: how we help customers migrate seamlessly across platforms Richard Halcrow
In this ever-evolving business world, flexibility is the key to long-term success. With the rise of digitalization in organizations, are you looking to migrate your IT infrastructure from the existing platform and try something new? Learn how we can help you in this, and beyond.

5 min read

Read more
Healthcare
Healthcare
3 Jul 2020
How an NHS trust keeps critical medical systems up and running at all times Rosalind Bootle
The use of digital imaging within the Healthcare industry is exploding at an unprecedented rate. Find out how we helped an NHS trust achieve zero downtime for its PACS.

2 min read

Read more

Get started

Get in touch

Start a conversation about the right solution for your business.

Get in touch

Create your quote

Transparent pricing you can see straight away.

Create your quote

Download now

Try us free for 30 days – see why our customers love us.

Download now

Schedule a virtual meeting with us

Working remotely or from home? Let’s meet on a call or online.

Let's meet

Follow Loadbalancer.org

+1 833 274 2566
  • Company
    • Solutions
    • Services
    • Load balancer
    • Why Loadbalancer.org
    • Blog
    • Professional services
    • Sitemap
  • Load balancer
    • Get a quote
    • Free trial
    • Online demo
  • Resources
    • Manuals
    • Deployment guides
    • Applications
    • White papers
    • Case studies
    • Solutions
  • Support
    • FAQ's
    • Open a ticket
    • Security news
  • Applications
    • Healthcare
    • Storage
    • Print
    • Security
    • Microsoft
The latest insights from the load balancing experts | Loadbalancer.org

The latest insights from the load balancing experts | Loadbalancer.org. All rights reserved

  • Contact Us
  • Terms & Conditions
  • Privacy Policy