Commit 9b72148c authored by Spiros Koulouzis's avatar Spiros Koulouzis

added environment

parent ed587183
import json
from datetime import datetime
import urllib3
from semaphore_client import Configuration, ApiClient, api, ProjectRequest, Login, AccessKeyRequest, InventoryRequest, \
RepositoryRequest, TemplateRequest, Task
RepositoryRequest, TemplateRequest, Task, EnvironmentRequest
class SemaphoreHelper:
......@@ -77,8 +78,12 @@ class SemaphoreHelper:
templates = self.project_api.project_project_id_templates_get(project_id, playbook_name, 'asc')
return self.find_template(templates,playbook_name).id
def execute_task(self, project_id, template_id, playbook_name):
task = Task(template_id=template_id, playbook=playbook_name)
def execute_task(self, project_id, template_id, playbook_name,environment_id=None):
environment_name = None
if environment_id:
environment = self.find_environment(project_id,environment_id=environment_id)
environment_name = environment.name
task = Task(template_id=template_id, playbook=playbook_name,environment=environment_name)
self.project_api.project_project_id_tasks_post(task,project_id)
tasks = self.project_api.project_project_id_tasks_get(project_id)
return self.find_latest_task(tasks).id
......@@ -127,3 +132,20 @@ class SemaphoreHelper:
if repo.git_url == git_url:
return repo
return None
def create_environment(self, project_id,name,env_vars):
vars = {"ENV":env_vars}
vars_str = json.dumps(vars)
environment_request = EnvironmentRequest(name=name, project_id=project_id, password=None, json=vars_str)
self.project_api.project_project_id_environment_post(environment_request,project_id)
environment = self.find_environment(project_id, name,vars_str)
return environment.id
def find_environment(self, project_id, name=None,vars_str=None,environment_id=None):
environments = self.project_api.project_project_id_environment_get(project_id,'db-deploy','desc')
for environment in environments:
if environment_id and environment.id ==environment_id:
return environment
if environment.name and environment.name == name and environment.project_id == project_id and environment.json == vars_str:
return environment
return None
\ No newline at end of file
......@@ -60,7 +60,9 @@ class TestTask(unittest.TestCase):
repository_id = self.semaphore_helper.create_repository(self.project_name, project_id, key_id, self.git_url)
template_id = self.semaphore_helper.create_template(project_id, key_id, inventory_id, repository_id, self.playbook_name)
task_id = self.semaphore_helper.execute_task(project_id, template_id, self.playbook_name)
env_vars = {"SOME_VAR": "the contents"}
environment_id = self.semaphore_helper.create_environment(project_id,self.project_name,env_vars)
task_id = self.semaphore_helper.execute_task(project_id, template_id, self.playbook_name,environment_id=environment_id)
for x in range(0, 2):
task = self.semaphore_helper.get_task(project_id, task_id)
print(task)
......
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