1
0
mirror of https://github.com/GNS3/gns3-server synced 2025-01-13 01:20:58 +00:00

Importing changeset from gns3dms repo

This commit is contained in:
Jerry Seutter 2014-09-08 15:35:22 +00:00
parent 6ff2c654d9
commit 6421367259
3 changed files with 70 additions and 53 deletions

View File

@ -77,6 +77,7 @@ Options:
--cloud_user_name --cloud_user_name
--instance_id ID of the Rackspace instance to terminate --instance_id ID of the Rackspace instance to terminate
--region Region of instance
--deadtime How long in seconds can the communication lose exist before we --deadtime How long in seconds can the communication lose exist before we
shutdown this instance. shutdown this instance.
@ -111,6 +112,7 @@ def parse_cmd_line(argv):
"cloud_user_name=", "cloud_user_name=",
"cloud_api_key=", "cloud_api_key=",
"instance_id=", "instance_id=",
"region=",
"deadtime=", "deadtime=",
"init-wait=", "init-wait=",
"check-interval=", "check-interval=",
@ -130,6 +132,7 @@ def parse_cmd_line(argv):
cmd_line_option_list["cloud_user_name"] = None cmd_line_option_list["cloud_user_name"] = None
cmd_line_option_list["cloud_api_key"] = None cmd_line_option_list["cloud_api_key"] = None
cmd_line_option_list["instance_id"] = None cmd_line_option_list["instance_id"] = None
cmd_line_option_list["region"] = None
cmd_line_option_list["deadtime"] = 60 * 60 #minutes cmd_line_option_list["deadtime"] = 60 * 60 #minutes
cmd_line_option_list["check-interval"] = None cmd_line_option_list["check-interval"] = None
cmd_line_option_list["init-wait"] = 5 * 60 cmd_line_option_list["init-wait"] = 5 * 60
@ -145,7 +148,8 @@ def parse_cmd_line(argv):
else: else:
cmd_line_option_list['syslog'] = ('localhost',514) cmd_line_option_list['syslog'] = ('localhost',514)
get_gns3config(cmd_line_option_list)
get_gns3secrets(cmd_line_option_list)
for opt, val in opts: for opt, val in opts:
if (opt in ("-h", "--help")): if (opt in ("-h", "--help")):
@ -161,6 +165,8 @@ def parse_cmd_line(argv):
cmd_line_option_list["cloud_api_key"] = val cmd_line_option_list["cloud_api_key"] = val
elif (opt in ("--instance_id")): elif (opt in ("--instance_id")):
cmd_line_option_list["instance_id"] = val cmd_line_option_list["instance_id"] = val
elif (opt in ("--region")):
cmd_line_option_list["region"] = val
elif (opt in ("--deadtime")): elif (opt in ("--deadtime")):
cmd_line_option_list["deadtime"] = int(val) cmd_line_option_list["deadtime"] = int(val)
elif (opt in ("--check-interval")): elif (opt in ("--check-interval")):
@ -199,9 +205,15 @@ def parse_cmd_line(argv):
print(usage) print(usage)
sys.exit(2) sys.exit(2)
if cmd_line_option_list["region"] is None:
print("You need to specify a region")
print(usage)
sys.exit(2)
return cmd_line_option_list return cmd_line_option_list
def get_gns3config(cmd_line_option_list): def get_gns3secrets(cmd_line_option_list):
""" """
Load cloud credentials from .gns3secrets Load cloud credentials from .gns3secrets
""" """
@ -224,17 +236,6 @@ def get_gns3config(cmd_line_option_list):
except configparser.NoSectionError: except configparser.NoSectionError:
pass pass
cloud_config_file = "%s/.config/GNS3/cloud.conf" % (
os.path.expanduser("~/"))
if os.path.isfile(cloud_config_file):
config.read(cloud_config_file)
try:
for key, value in config.items("CLOUD_SERVER"):
cmd_line_option_list[key] = value.strip()
except configparser.NoSectionError:
pass
def set_logging(cmd_options): def set_logging(cmd_options):
""" """
@ -351,9 +352,8 @@ def main():
log.info("Received shutdown signal") log.info("Received shutdown signal")
options["shutdown"] = True options["shutdown"] = True
sys.exit(0)
pid_file = "%s/.gns3ias.pid" % (expanduser("~")) pid_file = "%s/.gns3dms.pid" % (expanduser("~"))
if options["shutdown"]: if options["shutdown"]:
send_shutdown(pid_file) send_shutdown(pid_file)

View File

@ -7,38 +7,38 @@ class daemon:
Usage: subclass the daemon class and override the run() method.""" Usage: subclass the daemon class and override the run() method."""
def __init__(self, pidfile, options): def __init__(self, pidfile, options):
self.pidfile = pidfile self.pidfile = pidfile
self.options = options self.options = options
def daemonize(self): def daemonize(self):
"""Deamonize class. UNIX double fork mechanism.""" """Deamonize class. UNIX double fork mechanism."""
try: try:
pid = os.fork() pid = os.fork()
if pid > 0: if pid > 0:
# exit first parent # exit first parent
sys.exit(0) sys.exit(0)
except OSError as err: except OSError as err:
sys.stderr.write('fork #1 failed: {0}\n'.format(err)) sys.stderr.write('fork #1 failed: {0}\n'.format(err))
sys.exit(1) sys.exit(1)
# decouple from parent environment # decouple from parent environment
os.chdir('/') os.chdir('/')
os.setsid() os.setsid()
os.umask(0) os.umask(0)
# do second fork # do second fork
try: try:
pid = os.fork() pid = os.fork()
if pid > 0: if pid > 0:
# exit from second parent # exit from second parent
sys.exit(0) sys.exit(0)
except OSError as err: except OSError as err:
sys.stderr.write('fork #2 failed: {0}\n'.format(err)) sys.stderr.write('fork #2 failed: {0}\n'.format(err))
sys.exit(1) sys.exit(1)
# redirect standard file descriptors # redirect standard file descriptors
sys.stdout.flush() sys.stdout.flush()
sys.stderr.flush() sys.stderr.flush()
@ -49,17 +49,26 @@ class daemon:
os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno()) os.dup2(se.fileno(), sys.stderr.fileno())
# write pidfile # write pidfile
atexit.register(self.delpid) atexit.register(self.delpid)
pid = str(os.getpid()) pid = str(os.getpid())
with open(self.pidfile,'w+') as f: with open(self.pidfile,'w+') as f:
f.write(pid + '\n') f.write(pid + '\n')
def delpid(self): def delpid(self):
os.remove(self.pidfile) os.remove(self.pidfile)
def check_pid(self, pid):
""" Check For the existence of a unix pid. """
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
def start(self): def start(self):
"""Start the daemon.""" """Start the daemon."""
@ -70,13 +79,19 @@ class daemon:
pid = int(pf.read().strip()) pid = int(pf.read().strip())
except IOError: except IOError:
pid = None pid = None
if pid: if pid:
message = "pidfile {0} already exist. " + \ pid_exist = self.check_pid(pid)
"Daemon already running?\n"
if pid_exist:
message = "Already running: %s\n" % (pid)
sys.stderr.write(message)
sys.exit(1)
else:
message = "pidfile {0} already exist. " + \
"but process is dead\n"
sys.stderr.write(message.format(self.pidfile)) sys.stderr.write(message.format(self.pidfile))
sys.exit(1)
# Start the daemon # Start the daemon
self.daemonize() self.daemonize()
self.run() self.run()
@ -90,14 +105,14 @@ class daemon:
pid = int(pf.read().strip()) pid = int(pf.read().strip())
except IOError: except IOError:
pid = None pid = None
if not pid: if not pid:
message = "pidfile {0} does not exist. " + \ message = "pidfile {0} does not exist. " + \
"Daemon not running?\n" "Daemon not running?\n"
sys.stderr.write(message.format(self.pidfile)) sys.stderr.write(message.format(self.pidfile))
return # not an error in a restart return # not an error in a restart
# Try killing the daemon process # Try killing the daemon process
try: try:
while 1: while 1:
os.kill(pid, signal.SIGTERM) os.kill(pid, signal.SIGTERM)
@ -118,6 +133,6 @@ class daemon:
def run(self): def run(self):
"""You should override this method when you subclass Daemon. """You should override this method when you subclass Daemon.
It will be called after the process has been daemonized by It will be called after the process has been daemonized by
start() or restart().""" start() or restart()."""

View File

@ -41,6 +41,7 @@ class Rackspace(object):
self.authenticated = False self.authenticated = False
self.hostname = socket.gethostname() self.hostname = socket.gethostname()
self.instance_id = options["instance_id"] self.instance_id = options["instance_id"]
self.region = options["region"]
log.debug("Authenticating with Rackspace") log.debug("Authenticating with Rackspace")
log.debug("My hostname: %s" % (self.hostname)) log.debug("My hostname: %s" % (self.hostname))
@ -51,16 +52,17 @@ class Rackspace(object):
if self.authenticated == False: if self.authenticated == False:
log.critical("Not authenticated against rackspace!!!!") log.critical("Not authenticated against rackspace!!!!")
for region_dict in self.rksp.list_regions(): for region in self.rksp.list_regions():
region_k, region_v = region_dict.popitem() log.debug("Rackspace regions: %s" % (region))
log.debug("Checking region: %s" % (region_k))
self.rksp.set_region(region_v) log.debug("Checking region: %s" % (self.region))
for server in self.rksp.list_instances(): self.rksp.set_region(self.region)
log.debug("Checking server: %s" % (server.name)) for server in self.rksp.list_instances():
if server.name.lower() == self.hostname.lower() and server.id == self.instance_id: log.debug("Checking server: %s" % (server.name))
log.info("Found matching instance: %s" % (server.id)) if server.name.lower() == self.hostname.lower() and server.id == self.instance_id:
log.info("Startup id: %s" % (self.instance_id)) log.info("Found matching instance: %s" % (server.id))
return server log.info("Startup id: %s" % (self.instance_id))
return server
def terminate(self): def terminate(self):
server = self._find_my_instance() server = self._find_my_instance()