Commit 6ce96ef2 authored by Spiros Koulouzis's avatar Spiros Koulouzis

added find_latest_task

parent ba201cb5
import os import os
import sys import sys
from datetime import datetime
import urllib3 import urllib3
from semaphore_client import Configuration, ApiClient, api, ProjectRequest, Login, Repository, Inventory, \ from semaphore_client import Configuration, ApiClient, api, ProjectRequest, Login, Repository, Inventory, \
...@@ -48,13 +50,13 @@ class SemaphoreHelper: ...@@ -48,13 +50,13 @@ class SemaphoreHelper:
project_request = ProjectRequest(name=name) project_request = ProjectRequest(name=name)
self.projects_api.projects_post(project_request) self.projects_api.projects_post(project_request)
projects = self.find_projects_by_name(name) projects = self.find_projects_by_name(name)
return projects[len(projects)-1].id return self.find_latest_project(projects).id
def create_ssh_key(self, name, project_id, private_key): def create_ssh_key(self, name, project_id, private_key):
key_request = AccessKeyRequest(name=name, type='ssh', project_id=project_id, secret=private_key) key_request = AccessKeyRequest(name=name, type='ssh', project_id=project_id, secret=private_key)
self.project_api.project_project_id_keys_post(key_request, project_id ) self.project_api.project_project_id_keys_post(key_request, project_id )
keys = self.project_api.project_project_id_keys_get(project_id, name, 'asc', key_type='ssh') keys = self.project_api.project_project_id_keys_get(project_id, name, 'asc', key_type='ssh')
return keys[len(keys) - 1].id return keys[0].id
def create_inventory(self, name, project_id,ssh_key_id, inventory_contents): def create_inventory(self, name, project_id,ssh_key_id, inventory_contents):
inventory_request = InventoryRequest( name=name, project_id=project_id, inventory=inventory_contents, inventory_request = InventoryRequest( name=name, project_id=project_id, inventory=inventory_contents,
...@@ -62,27 +64,27 @@ class SemaphoreHelper: ...@@ -62,27 +64,27 @@ class SemaphoreHelper:
self.project_api.project_project_id_inventory_post(inventory_request,project_id) self.project_api.project_project_id_inventory_post(inventory_request,project_id)
inventories = self.project_api.project_project_id_inventory_get(project_id, name, 'asc') inventories = self.project_api.project_project_id_inventory_get(project_id, name, 'asc')
return inventories[len(inventories) - 1].id return inventories[0].id
def create_repository(self, name, project_id, key_id, git_url): def create_repository(self, name, project_id, key_id, git_url):
repository_request = RepositoryRequest(name=name, project_id=project_id, git_url=git_url, ssh_key_id=key_id) repository_request = RepositoryRequest(name=name, project_id=project_id, git_url=git_url, ssh_key_id=key_id)
self.project_api.project_project_id_repositories_post(repository_request ,project_id) self.project_api.project_project_id_repositories_post(repository_request ,project_id)
repositories = self.project_api.project_project_id_repositories_get(project_id, name, 'asc') repositories = self.project_api.project_project_id_repositories_get(project_id, name, 'asc')
return repositories[len(repositories) - 1].id return repositories[0].id
def create_template(self, project_id,key_id,inventory_id,repository_id,playbook_name): def create_template(self, project_id,key_id,inventory_id,repository_id,playbook_name):
template_request = TemplateRequest(ssh_key_id=key_id, project_id=project_id, inventory_id=inventory_id, template_request = TemplateRequest(ssh_key_id=key_id, project_id=project_id, inventory_id=inventory_id,
repository_id=repository_id, alias=playbook_name, playbook=playbook_name) repository_id=repository_id, alias=playbook_name, playbook=playbook_name)
self.project_api.project_project_id_templates_post(template_request , project_id ) self.project_api.project_project_id_templates_post(template_request , project_id )
templates = self.project_api.project_project_id_templates_get(project_id, playbook_name, 'asc') templates = self.project_api.project_project_id_templates_get(project_id, playbook_name, 'asc')
return templates[len(templates) - 1].id return templates[0].id
def execute_task(self, project_id, template_id, playbook_name): def execute_task(self, project_id, template_id, playbook_name):
task = Task(template_id=template_id, playbook=playbook_name) task = Task(template_id=template_id, playbook=playbook_name)
self.project_api.project_project_id_tasks_post(task,project_id) self.project_api.project_project_id_tasks_post(task,project_id)
tasks = self.project_api.project_project_id_tasks_get(project_id) tasks = self.project_api.project_project_id_tasks_get(project_id)
return tasks[len(tasks) - 1].id return self.find_latest_task(tasks).id
def get_task(self,project_id,task_id): def get_task(self,project_id,task_id):
return self.project_api.project_project_id_tasks_task_id_get(project_id,task_id) return self.project_api.project_project_id_tasks_task_id_get(project_id,task_id)
...@@ -100,4 +102,19 @@ class SemaphoreHelper: ...@@ -100,4 +102,19 @@ class SemaphoreHelper:
return True return True
def get_task_outputs(self, project_id, task_id): def get_task_outputs(self, project_id, task_id):
return self.project_api.project_project_id_tasks_task_id_output_get(project_id,task_id) return self.project_api.project_project_id_tasks_task_id_output_get(project_id,task_id)
\ No newline at end of file
def find_latest_project(self, projects):
now = datetime.now()
earlier = datetime.strptime('1900-01-01T10:07:35Z', '%Y-%m-%dT%H:%M:%SZ')
min_time_delta = now - earlier
latest_project = None
for project in projects:
date_object = datetime.strptime(project.created, '%Y-%m-%dT%H:%M:%SZ')
time_delta = now - date_object
if time_delta < min_time_delta:
latest_project = project
return latest_project
def find_latest_task(self, tasks):
return tasks[0]
\ No newline at end of file
...@@ -33,7 +33,7 @@ class TestTask(unittest.TestCase): ...@@ -33,7 +33,7 @@ class TestTask(unittest.TestCase):
if SemaphoreHelper.service_is_up(self.semaphore_base_url): if SemaphoreHelper.service_is_up(self.semaphore_base_url):
self.username = 'admin' self.username = 'admin'
self.password = 'password' self.password = 'password'
self.project_name = names.get_first_name() self.project_name = 'test'
self.private_key = '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAg0blRNV6cm3RTiivpzE8HR4JzKZRVIBZ7bxeNoMz0' \ self.private_key = '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAg0blRNV6cm3RTiivpzE8HR4JzKZRVIBZ7bxeNoMz0' \
'-----END RSA PRIVATE KEY-----' '-----END RSA PRIVATE KEY-----'
vms = [] vms = []
...@@ -67,9 +67,26 @@ class TestTask(unittest.TestCase): ...@@ -67,9 +67,26 @@ class TestTask(unittest.TestCase):
for x in range(0, 2): for x in range(0, 2):
task = self.semaphore_helper.get_task(project_id, task_id) task = self.semaphore_helper.get_task(project_id, task_id)
print(task) print(task)
task_output = self.semaphore_helper.get_task_output(project_id, task_id) # task_output = self.semaphore_helper.get_task_output(project_id, task_id)
print(task_output) # print(task_output)
sleep(0.5) sleep(1)
task_id = self.semaphore_helper.execute_task(project_id, template_id, self.playbook_name)
for x in range(0, 2):
task = self.semaphore_helper.get_task(project_id, task_id)
print(task)
# task_output = self.semaphore_helper.get_task_output(project_id, task_id)
# print(task_output)
sleep(1)
task_id = self.semaphore_helper.execute_task(project_id, template_id, 'mount.yaml')
for x in range(0, 2):
task = self.semaphore_helper.get_task(project_id, task_id)
print(task)
# task_output = self.semaphore_helper.get_task_output(project_id, task_id)
# print(task_output)
sleep(1)
def build_yml_inventory(self, vms): def build_yml_inventory(self, vms):
inventory = {} inventory = {}
......
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 to comment