Skip to content

Instantly share code, notes, and snippets.

@ketzacoatl
Last active August 31, 2016 05:19
Show Gist options
  • Save ketzacoatl/736a56532843afb05266 to your computer and use it in GitHub Desktop.
Save ketzacoatl/736a56532843afb05266 to your computer and use it in GitHub Desktop.
is consul online?

Is consul online?

let's find out:

wget https://gist.github.com/ketzacoatl/736a56532843afb05266/raw/3df3d362d7e77acfb68d0c6bedef3849a812c489/consul-online.py
chmod +x consul-online.py
./consul-online.py
#!/usr/bin/env python
import sys
import consul
import argparse
from time import sleep
from datetime import datetime
from requests.exceptions import ConnectionError
parser = argparse.ArgumentParser(description='block until we can connect to consul')
parser.add_argument('-host', dest='host', action='store', default='127.0.0.1',
help='consul host (default: 127.0.0.1)')
parser.add_argument('-port', dest='port', action='store', default='8500',
help='HTTP port to find consul on (default: 8500)')
parser.add_argument('-token', dest='token', action='store', default=None,
help='HTTP port to find consul on')
args = parser.parse_args()
delay = 1
started = datetime.now()
print ("is consul online? let's check!")
print ("will attempt to connect to consul on %s:%s" % (args.host, args.port))
print ("started at %s" % datetime.strftime(started, "%H:%M:%S"))
c = consul.Consul(host=args.host, port=args.port, token=args.token)
a = c.agent
def retry(d=delay):
print ("connection failed! will delay for %d second at %s" % (d, datetime.strftime(datetime.now(), "%H:%M:%S")))
sleep(d)
while True:
try:
c.session.create()
finished = datetime.now()
print ("connection succeeded at %s!" % datetime.strftime(finished, "%H:%M:%S"))
elapsed = finished - started
print ("total time before successful connection: %d" % (elapsed.total_seconds()))
sys.exit()
except ConnectionError:
retry()
except consul.base.ConsulException:
retry()
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Network.Consul (getSelf, ConsulClient(..), initializeConsulClient, listKeys)
import qualified Network.Consul.Internal as I
client = initializeConsulClient "localhost" 8500 Nothing
main :: IO ()
main = do
putStrLn "will try to connect"
_client@ConsulClient{..} <- client
--let self = getSelf
--let keys = listKeys
x3 <- I.listKeys ccManager (I.hostWithScheme _client) ccPort "/testListKeys/" Nothing Nothing Nothing
putStrLn "connected!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment