Commit 848ff728 by Your Name

new agend update scheme

parent 2a3a6018
......@@ -333,7 +333,7 @@ DJANGO_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
# 'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
......@@ -531,6 +531,8 @@ LOGIN_REDIRECT_URL = "/"
AGENT_DIR = get_env_variable(
'DJANGO_AGENT_DIR', join(unicode(expanduser("~")), 'agent'))
# AGENT_DIR is the root directory for the agent.
# The directory structure SHOULD be:
# /home/username/agent
......@@ -546,9 +548,40 @@ try:
AGENT_VERSION = check_output(
('git', 'log', '-1', r'--pretty=format:%h', 'HEAD'), env=git_env)
except:
AGENT_VERSION = None
AGENT_VERSION = Non = (join(SITE_ROOT, 'locale'), )
#### NEW ####
####
# AGENT_VERSIONS is a dict eg: { "Linux" : "vers1" , "Windows" : "vers2", .... }
# Normally it is fetched from Jason fromatted AGENT_DIR/wersions.txt file
#
# The dir namings ha changed:
# The same, but the dir names are lowercase and generated like this:
# agent-(agent_system)-(version). Eg: agent-linux-vers1, agent-window-vers2
try:
with open("%s/versions.txt" % AGENT_DIR, "r") as f:
AGENT_VERSIONS = loads(f.read())
print("-----KONWN VERSIONS-----")
print(AGENT_VERSIONS)
except:
print("Format ERROR in versions.txt !!!! ") # TODO more error reposrting
AGENT_VERSIONS = None
## PUBLIC function for getting the latest version ad the DIR
def GET_AGENT_VERSION_BY_SYSTEM(agent_system):
if agent_system is None:
return None, None
if type(AGENT_VERSIONS) is not dict:
# legacy naming
return AGENT_VERSION, AGENT_DIR + "/agent-" + agent_system.lower()
ret = AGENT_VERSIONS.get(agent_system)
if ret is None:
return None, None
return ret, AGENT_DIR + "/agent-" + agent_system.lower() + "-" + ret
LOCALE_PATHS = (join(SITE_ROOT, 'locale'), )
COMPANY_NAME = get_env_variable("COMPANY_NAME", "BME IK 2015")
first, last = get_env_variable(
......
......@@ -1575,19 +1575,6 @@ class AgentStartedOperation(InstanceOperation):
self.instance._change_ip(parent_activity=activity)
self.instance._restart_networking(parent_activity=activity)
new_version = settings.AGENT_VERSION
if new_version and old_version and new_version != old_version:
try:
self.instance.update_agent(
parent_activity=activity, agent_system=agent_system)
except TimeoutError:
pass
else:
activity.sub_activity(
'agent_wait', readable_name=ugettext_noop(
"wait agent restarting"), interruptible=True)
return # agent is going to restart
if not self.initialized:
try:
self.measure_boot_time()
......@@ -1600,6 +1587,19 @@ class AgentStartedOperation(InstanceOperation):
self.instance._set_time(parent_activity=activity)
self.instance._set_hostname(parent_activity=activity)
new_version = settings.GET_AGENT_VERSION_BY_SYSTEM(agent_system)[0]
if agent_system and new_version and (old_version is None or old_version and new_version != old_version):
try:
self.instance.update_agent(
parent_activity=activity, agent_system=agent_system)
except TimeoutError:
pass
else:
activity.sub_activity(
'agent_wait', readable_name=ugettext_noop(
"wait agent restarting"), interruptible=True)
return # agent is going to restart
@register_operation
class CleanupOperation(SubOperationMixin, RemoteAgentOperation):
id = '_cleanup'
......@@ -1658,11 +1658,11 @@ class UpdateAgentOperation(RemoteAgentOperation):
def get_activity_name(self, kwargs):
return create_readable(
ugettext_noop('update agent to %(version)s'),
version=settings.AGENT_VERSION)
ugettext_noop('update agent'))
# version=settings.GET_AGENT_VERSION_BY_SYSTEM(agent_system)[0])
@staticmethod
def create_linux_tar():
def create_linux_tar(agent_system):
def exclude(tarinfo):
ignored = ('./.', './misc', './windows')
if any(tarinfo.name.startswith(x) for x in ignored):
......@@ -1670,13 +1670,14 @@ class UpdateAgentOperation(RemoteAgentOperation):
else:
return tarinfo
_vers, _dir = settings.GET_AGENT_VERSION_BY_SYSTEM(agent_system)
f = StringIO()
with TarFile.open(fileobj=f, mode='w:gz') as tar:
agent_path = os.path.join(settings.AGENT_DIR, "agent-linux")
agent_path = _dir
tar.add(agent_path, arcname='.', filter=exclude)
version_fileobj = StringIO(settings.AGENT_VERSION)
version_fileobj = StringIO(_vers)
version_info = TarInfo(name='version.txt')
version_info.size = len(version_fileobj.buf)
tar.addfile(version_info, version_fileobj)
......@@ -1684,14 +1685,15 @@ class UpdateAgentOperation(RemoteAgentOperation):
return encodestring(f.getvalue()).replace('\n', '')
@staticmethod
def create_windows_tar():
def create_windows_tar(agent_system):
_vers, _dir = settings.GET_AGENT_VERSION_BY_SYSTEM(agent_system)
f = StringIO()
agent_path = os.path.join(settings.AGENT_DIR, "agent-win")
agent_path = _dir
with TarFile.open(fileobj=f, mode='w|gz') as tar:
tar.add(agent_path, arcname='.')
version_fileobj = StringIO(settings.AGENT_VERSION)
version_fileobj = StringIO(_vers)
version_info = TarInfo(name='version.txt')
version_info.size = len(version_fileobj.buf)
tar.addfile(version_info, version_fileobj)
......@@ -1699,15 +1701,15 @@ class UpdateAgentOperation(RemoteAgentOperation):
return encodestring(f.getvalue()).replace('\n', '')
def _operation(self, user, activity, agent_system):
_vers, _dir = settings.GET_AGENT_VERSION_BY_SYSTEM(agent_system)
queue = self._get_remote_queue()
instance = self.instance
if agent_system == "Windows":
executable = os.listdir(
os.path.join(settings.AGENT_DIR, "agent-win"))[0]
data = self.create_windows_tar()
executable = sorted(os.listdir(_dir))[0]
data = self.create_windows_tar(agent_system)
elif agent_system == "Linux":
executable = ""
data = self.create_linux_tar()
data = self.create_linux_tar(agent_system)
else:
# Legacy update method
executable = ""
......@@ -1720,7 +1722,7 @@ class UpdateAgentOperation(RemoteAgentOperation):
chunk_size = 1024 * 1024
chunk_number = 0
index = 0
filename = settings.AGENT_VERSION + ".tar"
filename = _vers + ".tar"
while True:
chunk = data[index:index + chunk_size]
if chunk:
......@@ -1734,7 +1736,7 @@ class UpdateAgentOperation(RemoteAgentOperation):
agent_tasks.update.apply_async(
queue=queue,
args=(instance.vm_name, filename, executable, checksum)
).get(timeout=60)
).get(timeout=120)
break
......
......@@ -50,17 +50,19 @@ def garbage_collector(offset=timezone.timedelta(seconds=20)):
for i in Instance.objects.filter(destroyed_at=None).all():
logger.debug("Garbage_collector work_package:%d %s: %s:", work_package, i.pk, now > i.time_of_delete)
if i.time_of_delete and now > i.time_of_delete + grace_period and work_package > 0:
logger.debug("Garbage_collector delete")
work_package -= 1
i.destroy.async(system=True)
logger.debug("Garbage_collector delete")
logger.info("Expired instance %d destroyed.", i.pk)
try:
i.destroy.async(system=True)
i.owner.profile.notify(
ugettext_noop('%(instance)s destroyed'),
ugettext_noop(
'Your instance <a href="%(url)s">%(instance)s</a> '
'has been destroyed due to expiration.'),
instance=i.name, url=i.get_absolute_url())
except ActivityInProgressError:
logger.error("Expired instance %d can't be destroyed due the AtctivityInPorgressError.", i.pk)
except Exception as e:
logger.debug('Could not notify owner of instance %d .%s',
i.pk, unicode(e))
......@@ -78,7 +80,7 @@ def garbage_collector(offset=timezone.timedelta(seconds=20)):
'You can resume or destroy it.'),
instance=i.name, url=i.get_absolute_url())
except ActivityInProgressError:
logger.error("Expired instance %d can't be destroyed due the AtctivityInPorgressError.", i.pk)
logger.error("Expired instance %d can't be suspended due the AtctivityInPorgressError.", i.pk)
except Exception as e:
logger.info('Could not notify owner of instance %d .%s',
i.pk, unicode(e))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment