2014-08-29 21:05:56 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# __version__ is a human-readable version number.
|
|
|
|
|
|
|
|
# __version_info__ is a four-tuple for programmatic comparison. The first
|
|
|
|
# three numbers are the components of the version number. The fourth
|
|
|
|
# is zero for an official release, positive for a development branch,
|
|
|
|
# or negative for a release candidate or beta (after the base version
|
|
|
|
# number has been incremented)
|
|
|
|
|
|
|
|
"""
|
2014-09-07 05:46:06 +03:00
|
|
|
Monitors communication with the GNS3 client via tmp file. Will terminate the instance if
|
2014-08-29 21:05:56 +03:00
|
|
|
communication is lost.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import getopt
|
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
import signal
|
|
|
|
import configparser
|
|
|
|
from logging.handlers import *
|
|
|
|
from os.path import expanduser
|
|
|
|
|
|
|
|
SCRIPT_NAME = os.path.basename(__file__)
|
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
# Is the full path when used as an import
|
2014-08-29 21:05:56 +03:00
|
|
|
SCRIPT_PATH = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
if not SCRIPT_PATH:
|
|
|
|
SCRIPT_PATH = os.path.join(os.path.dirname(os.path.abspath(
|
|
|
|
sys.argv[0])))
|
|
|
|
|
|
|
|
|
|
|
|
EXTRA_LIB = "%s/modules" % (SCRIPT_PATH)
|
|
|
|
sys.path.append(EXTRA_LIB)
|
|
|
|
|
|
|
|
from . import cloud
|
|
|
|
from rackspace_cloud import Rackspace
|
|
|
|
|
|
|
|
LOG_NAME = "gns3dms"
|
|
|
|
log = None
|
|
|
|
|
|
|
|
sys.path.append(EXTRA_LIB)
|
|
|
|
|
|
|
|
import daemon
|
|
|
|
|
2014-09-07 05:46:06 +03:00
|
|
|
my_daemon = None
|
2014-08-29 21:05:56 +03:00
|
|
|
|
|
|
|
usage = """
|
|
|
|
USAGE: %s
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-d, --debug Enable debugging
|
|
|
|
-v, --verbose Enable verbose logging
|
|
|
|
-h, --help Display this menu :)
|
|
|
|
|
2014-09-07 05:46:06 +03:00
|
|
|
--cloud_api_key <api_key> Rackspace API key
|
2014-08-29 21:05:56 +03:00
|
|
|
--cloud_user_name
|
|
|
|
|
|
|
|
--instance_id ID of the Rackspace instance to terminate
|
2014-10-28 02:12:56 +02:00
|
|
|
--cloud_region Region of instance
|
2014-09-07 05:46:06 +03:00
|
|
|
|
2014-11-25 00:08:58 +02:00
|
|
|
--dead_time How long in seconds can the communication lose exist before we
|
2014-09-07 05:46:06 +03:00
|
|
|
shutdown this instance.
|
|
|
|
Default:
|
2014-11-25 00:08:58 +02:00
|
|
|
Example --dead_time=3600 (60 minutes)
|
2014-08-29 21:05:56 +03:00
|
|
|
|
2014-11-25 00:08:58 +02:00
|
|
|
--check-interval Defaults to --dead_time, used for debugging
|
2014-08-29 21:05:56 +03:00
|
|
|
|
|
|
|
--init-wait Inital wait time, how long before we start pulling the file.
|
|
|
|
Default: 300 (5 min)
|
|
|
|
Example --init-wait=300
|
|
|
|
|
|
|
|
--file The file we monitor for updates
|
|
|
|
|
|
|
|
-k Kill previous instance running in background
|
|
|
|
--background Run in background
|
|
|
|
|
|
|
|
""" % (SCRIPT_NAME)
|
|
|
|
|
|
|
|
# Parse cmd line options
|
2015-01-20 14:24:00 +02:00
|
|
|
|
|
|
|
|
2014-08-29 21:05:56 +03:00
|
|
|
def parse_cmd_line(argv):
|
|
|
|
"""
|
|
|
|
Parse command line arguments
|
|
|
|
|
|
|
|
argv: Pass in cmd line arguments
|
|
|
|
"""
|
|
|
|
|
|
|
|
short_args = "dvhk"
|
|
|
|
long_args = ("debug",
|
2015-01-20 14:24:00 +02:00
|
|
|
"verbose",
|
|
|
|
"help",
|
|
|
|
"cloud_user_name=",
|
|
|
|
"cloud_api_key=",
|
|
|
|
"instance_id=",
|
|
|
|
"region=",
|
|
|
|
"dead_time=",
|
|
|
|
"init-wait=",
|
|
|
|
"check-interval=",
|
|
|
|
"file=",
|
|
|
|
"background",
|
|
|
|
)
|
2014-08-29 21:05:56 +03:00
|
|
|
try:
|
|
|
|
opts, extra_opts = getopt.getopt(argv[1:], short_args, long_args)
|
|
|
|
except getopt.GetoptError as e:
|
2015-01-20 14:24:00 +02:00
|
|
|
print("Unrecognized command line option or missing required argument: %s" % (e))
|
2014-08-29 21:05:56 +03:00
|
|
|
print(usage)
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
cmd_line_option_list = {}
|
|
|
|
cmd_line_option_list["debug"] = False
|
|
|
|
cmd_line_option_list["verbose"] = True
|
|
|
|
cmd_line_option_list["cloud_user_name"] = None
|
|
|
|
cmd_line_option_list["cloud_api_key"] = None
|
|
|
|
cmd_line_option_list["instance_id"] = None
|
2014-09-08 18:35:22 +03:00
|
|
|
cmd_line_option_list["region"] = None
|
2015-01-20 14:24:00 +02:00
|
|
|
cmd_line_option_list["dead_time"] = 60 * 60 # minutes
|
2014-08-29 21:05:56 +03:00
|
|
|
cmd_line_option_list["check-interval"] = None
|
|
|
|
cmd_line_option_list["init-wait"] = 5 * 60
|
|
|
|
cmd_line_option_list["file"] = None
|
|
|
|
cmd_line_option_list["shutdown"] = False
|
|
|
|
cmd_line_option_list["daemon"] = False
|
|
|
|
cmd_line_option_list['starttime'] = datetime.datetime.now()
|
|
|
|
|
|
|
|
if sys.platform == "linux":
|
|
|
|
cmd_line_option_list['syslog'] = "/dev/log"
|
|
|
|
elif sys.platform == "osx":
|
|
|
|
cmd_line_option_list['syslog'] = "/var/run/syslog"
|
|
|
|
else:
|
2015-01-20 14:24:00 +02:00
|
|
|
cmd_line_option_list['syslog'] = ('localhost', 514)
|
2014-09-08 18:35:22 +03:00
|
|
|
|
|
|
|
get_gns3secrets(cmd_line_option_list)
|
2014-11-25 00:35:09 +02:00
|
|
|
cmd_line_option_list["dead_time"] = int(cmd_line_option_list["dead_time"])
|
2014-08-29 21:05:56 +03:00
|
|
|
|
|
|
|
for opt, val in opts:
|
|
|
|
if (opt in ("-h", "--help")):
|
|
|
|
print(usage)
|
|
|
|
sys.exit(0)
|
|
|
|
elif (opt in ("-d", "--debug")):
|
|
|
|
cmd_line_option_list["debug"] = True
|
|
|
|
elif (opt in ("-v", "--verbose")):
|
|
|
|
cmd_line_option_list["verbose"] = True
|
|
|
|
elif (opt in ("--cloud_user_name")):
|
|
|
|
cmd_line_option_list["cloud_user_name"] = val
|
|
|
|
elif (opt in ("--cloud_api_key")):
|
|
|
|
cmd_line_option_list["cloud_api_key"] = val
|
|
|
|
elif (opt in ("--instance_id")):
|
|
|
|
cmd_line_option_list["instance_id"] = val
|
2014-09-08 18:35:22 +03:00
|
|
|
elif (opt in ("--region")):
|
|
|
|
cmd_line_option_list["region"] = val
|
2014-11-25 00:08:58 +02:00
|
|
|
elif (opt in ("--dead_time")):
|
|
|
|
cmd_line_option_list["dead_time"] = int(val)
|
2014-08-29 21:05:56 +03:00
|
|
|
elif (opt in ("--check-interval")):
|
|
|
|
cmd_line_option_list["check-interval"] = int(val)
|
|
|
|
elif (opt in ("--init-wait")):
|
|
|
|
cmd_line_option_list["init-wait"] = int(val)
|
|
|
|
elif (opt in ("--file")):
|
|
|
|
cmd_line_option_list["file"] = val
|
|
|
|
elif (opt in ("-k")):
|
|
|
|
cmd_line_option_list["shutdown"] = True
|
|
|
|
elif (opt in ("--background")):
|
|
|
|
cmd_line_option_list["daemon"] = True
|
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
if cmd_line_option_list["shutdown"] is False:
|
2014-08-29 21:05:56 +03:00
|
|
|
|
|
|
|
if cmd_line_option_list["check-interval"] is None:
|
2014-11-25 00:08:58 +02:00
|
|
|
cmd_line_option_list["check-interval"] = cmd_line_option_list["dead_time"] + 120
|
2014-08-29 21:05:56 +03:00
|
|
|
|
|
|
|
if cmd_line_option_list["cloud_user_name"] is None:
|
|
|
|
print("You need to specify a username!!!!")
|
|
|
|
print(usage)
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if cmd_line_option_list["cloud_api_key"] is None:
|
|
|
|
print("You need to specify an apikey!!!!")
|
|
|
|
print(usage)
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if cmd_line_option_list["file"] is None:
|
|
|
|
print("You need to specify a file to watch!!!!")
|
|
|
|
print(usage)
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if cmd_line_option_list["instance_id"] is None:
|
|
|
|
print("You need to specify an instance_id")
|
|
|
|
print(usage)
|
|
|
|
sys.exit(2)
|
|
|
|
|
2014-10-28 02:12:56 +02:00
|
|
|
if cmd_line_option_list["cloud_region"] is None:
|
|
|
|
print("You need to specify a cloud_region")
|
2014-09-08 18:35:22 +03:00
|
|
|
print(usage)
|
|
|
|
sys.exit(2)
|
|
|
|
|
2014-08-29 21:05:56 +03:00
|
|
|
return cmd_line_option_list
|
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
|
2014-09-08 18:35:22 +03:00
|
|
|
def get_gns3secrets(cmd_line_option_list):
|
2014-08-29 21:05:56 +03:00
|
|
|
"""
|
|
|
|
Load cloud credentials from .gns3secrets
|
|
|
|
"""
|
|
|
|
|
|
|
|
gns3secret_paths = [
|
2014-09-22 06:41:51 +03:00
|
|
|
os.path.join(os.path.expanduser("~"), '.config', 'GNS3'),
|
2014-08-29 21:05:56 +03:00
|
|
|
SCRIPT_PATH,
|
|
|
|
]
|
|
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
|
|
|
|
for gns3secret_path in gns3secret_paths:
|
2014-09-22 06:41:51 +03:00
|
|
|
gns3secret_file = "%s/cloud.conf" % (gns3secret_path)
|
2014-08-29 21:05:56 +03:00
|
|
|
if os.path.isfile(gns3secret_file):
|
|
|
|
config.read(gns3secret_file)
|
|
|
|
|
|
|
|
try:
|
2014-09-22 06:41:51 +03:00
|
|
|
for key, value in config.items("CLOUD_SERVER"):
|
2014-08-29 21:05:56 +03:00
|
|
|
cmd_line_option_list[key] = value.strip()
|
|
|
|
except configparser.NoSectionError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def set_logging(cmd_options):
|
|
|
|
"""
|
|
|
|
Setup logging and format output for console and syslog
|
|
|
|
|
|
|
|
Syslog is using the KERN facility
|
|
|
|
"""
|
|
|
|
log = logging.getLogger("%s" % (LOG_NAME))
|
|
|
|
log_level = logging.INFO
|
|
|
|
log_level_console = logging.WARNING
|
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
if cmd_options['verbose']:
|
2014-08-29 21:05:56 +03:00
|
|
|
log_level_console = logging.INFO
|
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
if cmd_options['debug']:
|
2014-08-29 21:05:56 +03:00
|
|
|
log_level_console = logging.DEBUG
|
|
|
|
log_level = logging.DEBUG
|
|
|
|
|
|
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
sys_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
|
console_log = logging.StreamHandler()
|
|
|
|
console_log.setLevel(log_level_console)
|
|
|
|
console_log.setFormatter(formatter)
|
|
|
|
|
|
|
|
syslog_hndlr = SysLogHandler(
|
|
|
|
address=cmd_options['syslog'],
|
|
|
|
facility=SysLogHandler.LOG_KERN
|
|
|
|
)
|
|
|
|
|
|
|
|
syslog_hndlr.setFormatter(sys_formatter)
|
2014-09-07 05:46:06 +03:00
|
|
|
|
2014-08-29 21:05:56 +03:00
|
|
|
log.setLevel(log_level)
|
|
|
|
log.addHandler(console_log)
|
|
|
|
log.addHandler(syslog_hndlr)
|
|
|
|
|
|
|
|
return log
|
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
|
2014-08-29 21:05:56 +03:00
|
|
|
def send_shutdown(pid_file):
|
|
|
|
"""
|
|
|
|
Sends the daemon process a kill signal
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with open(pid_file, 'r') as pidf:
|
|
|
|
pid = int(pidf.readline().strip())
|
|
|
|
pidf.close()
|
|
|
|
os.kill(pid, 15)
|
|
|
|
except:
|
|
|
|
log.info("No running instance found!!!")
|
|
|
|
log.info("Missing PID file: %s" % (pid_file))
|
|
|
|
|
|
|
|
|
|
|
|
def _get_file_age(filename):
|
|
|
|
return datetime.datetime.fromtimestamp(
|
2015-01-20 14:24:00 +02:00
|
|
|
os.path.getmtime(filename)
|
|
|
|
)
|
|
|
|
|
2014-08-29 21:05:56 +03:00
|
|
|
|
|
|
|
def monitor_loop(options):
|
|
|
|
"""
|
|
|
|
Checks the options["file"] modification time against an interval. If the
|
|
|
|
modification time is too old we terminate the instance.
|
|
|
|
"""
|
|
|
|
|
|
|
|
log.debug("Waiting for init-wait to pass: %s" % (options["init-wait"]))
|
|
|
|
time.sleep(options["init-wait"])
|
|
|
|
|
|
|
|
log.info("Starting monitor_loop")
|
|
|
|
|
|
|
|
terminate_attempts = 0
|
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
while options['shutdown'] is False:
|
2014-08-29 21:05:56 +03:00
|
|
|
log.debug("In monitor_loop for : %s" % (
|
|
|
|
datetime.datetime.now() - options['starttime'])
|
|
|
|
)
|
|
|
|
|
|
|
|
file_last_modified = _get_file_age(options["file"])
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
|
|
|
delta = now - file_last_modified
|
|
|
|
log.debug("File last updated: %s seconds ago" % (delta.seconds))
|
|
|
|
|
2014-11-25 00:08:58 +02:00
|
|
|
if delta.seconds > options["dead_time"]:
|
|
|
|
log.warning("Dead time exceeded, terminating instance ...")
|
2015-01-20 14:24:00 +02:00
|
|
|
# Terminate involves many layers of HTTP / API calls, lots of
|
|
|
|
# different errors types could occur here.
|
2014-08-29 21:05:56 +03:00
|
|
|
try:
|
|
|
|
rksp = Rackspace(options)
|
|
|
|
rksp.terminate()
|
|
|
|
except Exception as e:
|
|
|
|
log.critical("Exception during terminate: %s" % (e))
|
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
terminate_attempts += 1
|
2014-08-29 21:05:56 +03:00
|
|
|
log.warning("Termination sent, attempt: %s" % (terminate_attempts))
|
|
|
|
time.sleep(600)
|
|
|
|
else:
|
|
|
|
time.sleep(options["check-interval"])
|
|
|
|
|
|
|
|
log.info("Leaving monitor_loop")
|
|
|
|
log.info("Shutting down")
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
global log
|
|
|
|
global my_daemon
|
|
|
|
options = parse_cmd_line(sys.argv)
|
|
|
|
log = set_logging(options)
|
|
|
|
|
|
|
|
def _shutdown(signalnum=None, frame=None):
|
|
|
|
"""
|
|
|
|
Handles the SIGINT and SIGTERM event, inside of main so it has access to
|
|
|
|
the log vars.
|
|
|
|
"""
|
|
|
|
|
|
|
|
log.info("Received shutdown signal")
|
|
|
|
options["shutdown"] = True
|
2014-09-07 05:46:06 +03:00
|
|
|
|
2014-09-08 18:35:22 +03:00
|
|
|
pid_file = "%s/.gns3dms.pid" % (expanduser("~"))
|
2014-08-29 21:05:56 +03:00
|
|
|
|
|
|
|
if options["shutdown"]:
|
|
|
|
send_shutdown(pid_file)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if options["daemon"]:
|
|
|
|
my_daemon = MyDaemon(pid_file, options)
|
|
|
|
|
|
|
|
# Setup signal to catch Control-C / SIGINT and SIGTERM
|
|
|
|
signal.signal(signal.SIGINT, _shutdown)
|
|
|
|
signal.signal(signal.SIGTERM, _shutdown)
|
|
|
|
|
|
|
|
log.info("Starting ...")
|
|
|
|
log.debug("Using settings:")
|
|
|
|
for key, value in iter(sorted(options.items())):
|
|
|
|
log.debug("%s : %s" % (key, value))
|
|
|
|
|
|
|
|
log.debug("Checking file ....")
|
2015-01-20 14:24:00 +02:00
|
|
|
if os.path.isfile(options["file"]) is False:
|
2014-08-29 21:05:56 +03:00
|
|
|
log.critical("File does not exist!!!")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
test_acess = _get_file_age(options["file"])
|
2015-01-20 14:24:00 +02:00
|
|
|
if not isinstance(test_acess, datetime.datetime):
|
2014-08-29 21:05:56 +03:00
|
|
|
log.critical("Can't get file modification time!!!")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if my_daemon:
|
|
|
|
my_daemon.start()
|
|
|
|
else:
|
|
|
|
monitor_loop(options)
|
|
|
|
|
|
|
|
|
|
|
|
class MyDaemon(daemon.daemon):
|
2015-01-20 14:24:00 +02:00
|
|
|
|
2014-08-29 21:05:56 +03:00
|
|
|
def run(self):
|
|
|
|
monitor_loop(self.options)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
result = main()
|
|
|
|
sys.exit(result)
|