Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CONF
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UvA
CONF
Commits
fb8231a1
Commit
fb8231a1
authored
Apr 03, 2020
by
Spiros Koulouzis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
try to execute ansible tasks
parent
f6905282
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
54 additions
and
127 deletions
+54
-127
drip-deployer.iml
deployer/.idea/drip-deployer.iml
+1
-1
misc.xml
deployer/.idea/misc.xml
+1
-1
ansible_service.py
deployer/service/ansible_service.py
+35
-17
semaphore_helper.py
deployer/service/semaphore_helper.py
+0
-101
semaphore_helper.py
...hon-client-generated/semaphore_client/semaphore_helper.py
+9
-6
test_task.py
semaphore-python-client-generated/test/test_task.py
+8
-1
No files found.
deployer/.idea/drip-deployer.iml
View file @
fb8231a1
...
...
@@ -4,7 +4,7 @@
<content
url=
"file://$MODULE_DIR$"
>
<excludeFolder
url=
"file://$MODULE_DIR$/venv"
/>
</content>
<orderEntry
type=
"jdk"
jdkName=
"Python 3.
7
(deployer)"
jdkType=
"Python SDK"
/>
<orderEntry
type=
"jdk"
jdkName=
"Python 3.
8
(deployer)"
jdkType=
"Python SDK"
/>
<orderEntry
type=
"sourceFolder"
forTests=
"false"
/>
</component>
<component
name=
"PyDocumentationSettings"
>
...
...
deployer/.idea/misc.xml
View file @
fb8231a1
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<component
name=
"ProjectRootManager"
version=
"2"
project-jdk-name=
"Python 3.
7
(deployer)"
project-jdk-type=
"Python SDK"
/>
<component
name=
"ProjectRootManager"
version=
"2"
project-jdk-name=
"Python 3.
8
(deployer)"
project-jdk-type=
"Python SDK"
/>
</project>
\ No newline at end of file
deployer/service/ansible_service.py
View file @
fb8231a1
...
...
@@ -3,8 +3,8 @@ import logging
from
time
import
sleep
import
yaml
from
semaphore_client.semaphore_helper
import
SemaphoreHelper
from
service.semaphore_helper
import
SemaphoreHelper
yaml
.
Dumper
.
ignore_aliases
=
lambda
*
args
:
True
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -24,25 +24,28 @@ class AnsibleService:
self
.
semaphore_username
=
semaphore_username
self
.
semaphore_password
=
semaphore_password
self
.
semaphore_helper
=
SemaphoreHelper
(
self
.
semaphore_base_url
,
self
.
semaphore_username
,
self
.
semaphore_password
)
self
.
repository_id
=
None
self
.
template_id
=
None
def
execute
(
self
,
nodes_pair
):
vms
=
nodes_pair
[
0
]
orchestartor
=
nodes_pair
[
1
]
orchestrator
=
nodes_pair
[
1
]
name
=
orchestrator
.
name
desired_state
=
None
interfaces
=
orchest
ar
tor
.
node_template
.
interfaces
if
'current_state'
in
orchest
ar
tor
.
node_template
.
attributes
:
current_state
=
orchest
ar
tor
.
node_template
.
attributes
[
'current_state'
]
if
'desired_state'
in
orchest
ar
tor
.
node_template
.
attributes
:
desired_state
=
orchest
ar
tor
.
node_template
.
attributes
[
'desired_state'
]
interfaces
=
orchest
ra
tor
.
node_template
.
interfaces
if
'current_state'
in
orchest
ra
tor
.
node_template
.
attributes
:
current_state
=
orchest
ra
tor
.
node_template
.
attributes
[
'current_state'
]
if
'desired_state'
in
orchest
ra
tor
.
node_template
.
attributes
:
desired_state
=
orchest
ra
tor
.
node_template
.
attributes
[
'desired_state'
]
if
desired_state
:
project_id
=
self
.
semaphore_helper
.
create_project
(
orchest
ar
tor
.
name
)
project_id
=
self
.
semaphore_helper
.
create_project
(
orchest
ra
tor
.
name
)
inventory_contents
=
yaml
.
dump
(
self
.
build_yml_inventory
(
vms
),
default_flow_style
=
False
)
private_key
=
self
.
get_private_key
(
vms
)
key_id
=
self
.
semaphore_helper
.
create_ssh_key
(
orchest
ar
tor
.
name
,
project_id
,
private_key
)
inventory_id
=
self
.
semaphore_helper
.
create_inventory
(
orchest
ar
tor
.
name
,
project_id
,
key_id
,
inventory_contents
)
key_id
=
self
.
semaphore_helper
.
create_ssh_key
(
orchest
ra
tor
.
name
,
project_id
,
private_key
)
inventory_id
=
self
.
semaphore_helper
.
create_inventory
(
orchest
ra
tor
.
name
,
project_id
,
key_id
,
inventory_contents
)
if
'RUNNING'
==
desired_state
:
standard
=
interfaces
[
'Standard'
]
create
=
standard
[
'create'
]
...
...
@@ -50,14 +53,14 @@ class AnsibleService:
git_url
=
inputs
[
'repository'
]
playbook_name
=
inputs
[
'playbook'
]
repository_id
=
self
.
semaphore_helper
.
create_repository
(
orchestartor
.
name
,
project_id
,
key_id
,
git_url
)
template_id
=
self
.
semaphore_helper
.
create_template
(
project_id
,
key_id
,
inventory_id
,
repository_id
,
playbook_name
)
task_id
=
self
.
semaphore_helper
.
execute_task
(
project_id
,
template_id
,
playbook_name
)
for
x
in
range
(
0
,
3
):
task
=
self
.
semaphore_helper
.
get_task
(
project_id
,
task_id
)
print
(
task
)
sleep
(
0.5
)
task_id
=
self
.
run_task
(
name
,
project_id
,
key_id
,
git_url
,
inventory_id
,
playbook_name
)
configure
=
standard
[
'configure'
]
inputs
=
configure
[
'inputs'
]
git_url
=
inputs
[
'repository'
]
playbook_name
=
inputs
[
'playbook'
]
if
self
.
semaphore_helper
.
get_task
(
project_id
,
task_id
)
.
status
==
'success'
:
task_id
=
self
.
run_task
(
name
,
project_id
,
key_id
,
git_url
,
inventory_id
,
playbook_name
)
pass
...
...
@@ -96,3 +99,18 @@ class AnsibleService:
private_key
=
vms
[
0
]
.
node_template
.
attributes
[
'user_key_pair'
][
'keys'
][
'private_key'
]
return
base64
.
b64decode
(
private_key
)
.
decode
(
'utf-8'
)
.
replace
(
r'\n'
,
'
\n
'
)
def
run_task
(
self
,
name
,
project_id
,
key_id
,
git_url
,
inventory_id
,
playbook_name
):
self
.
repository_id
=
self
.
semaphore_helper
.
create_repository
(
name
,
project_id
,
key_id
,
git_url
)
template_id
=
self
.
semaphore_helper
.
create_template
(
project_id
,
key_id
,
inventory_id
,
self
.
repository_id
,
playbook_name
)
task_id
=
self
.
semaphore_helper
.
execute_task
(
project_id
,
template_id
,
playbook_name
)
task
=
self
.
semaphore_helper
.
get_task
(
project_id
,
task_id
)
while
task
.
status
==
'waiting'
or
task
.
status
==
'running'
:
task
=
self
.
semaphore_helper
.
get_task
(
project_id
,
task_id
)
logger
.
info
(
'task status: '
+
str
(
task
.
status
))
sleep
(
1.5
)
task_outputs
=
self
.
semaphore_helper
.
get_task_outputs
(
project_id
,
task_id
)
logger
.
info
(
'task_output: '
+
str
(
task_outputs
))
return
task_id
deployer/service/semaphore_helper.py
deleted
100644 → 0
View file @
f6905282
from
semaphore_client
import
Configuration
,
ApiClient
,
api
,
ProjectRequest
,
Login
,
Repository
,
Inventory
,
\
AccessKeyRequest
,
InventoryRequest
,
RepositoryRequest
,
TemplateRequest
,
Task
class
SemaphoreHelper
:
def
__init__
(
self
,
polemarch_base_url
,
username
,
password
):
self
.
init_semaphore_client
(
polemarch_base_url
,
username
,
password
)
def
init_semaphore_client
(
self
,
polemarch_base_url
,
username
,
password
):
configuration
=
Configuration
()
configuration
.
host
=
polemarch_base_url
configuration
.
username
=
username
configuration
.
password
=
password
api_client
=
ApiClient
(
configuration
=
configuration
)
if
not
api_client
.
cookie
:
self
.
authentication_api
=
api
.
AuthenticationApi
(
api_client
=
api_client
)
login_body
=
Login
(
auth
=
username
,
password
=
password
)
self
.
authentication_api
.
auth_login_post
(
login_body
)
tokens
=
self
.
authentication_api
.
user_tokens_get
()
self
.
default_api
=
api
.
DefaultApi
(
api_client
=
api_client
)
self
.
project_api
=
api
.
ProjectApi
(
api_client
=
api_client
)
self
.
user_api
=
api
.
UserApi
(
api_client
=
api_client
)
self
.
projects_api
=
api
.
ProjectsApi
(
api_client
=
api_client
)
# def create_project_if_not_exists(self, name, git_url, private_key,inventory_contents,playbook_name):
# the_project = self.create_project(name)
# key = self.create_ssh_key(name,the_project.id,private_key)
# inventory= self.create_inventory(name,the_project.id,key.id,inventory_contents)
# repository = self.create_repository(name,the_project.id,key.id,git_url)
#
# template = self.create_template(the_project.id,key.id,inventory.id,repository.id,playbook_name)
#
# task = Task(template_id=template.id, playbook=playbook_name)
# self.project_api.project_project_id_tasks_post(the_project.id, task)
#
# tasks = self.project_api.project_project_id_tasks_get(the_project.id)
# for task in tasks:
# print(task)
#
# return the_project
def
find_projects_by_name
(
self
,
name
):
projects
=
self
.
projects_api
.
projects_get
()
projects_with_name
=
[]
for
project
in
projects
:
if
project
.
name
==
name
:
projects_with_name
.
append
(
project
)
return
projects_with_name
def
project_with_name_exists
(
self
,
name
):
projects
=
self
.
find_projects_by_name
(
name
)
if
not
projects
:
return
False
return
True
def
create_project
(
self
,
name
):
project_request
=
ProjectRequest
(
name
=
name
)
self
.
projects_api
.
projects_post
(
project_request
)
projects
=
self
.
find_projects_by_name
(
name
)
return
projects
[
len
(
projects
)
-
1
]
.
id
def
create_ssh_key
(
self
,
name
,
project_id
,
private_key
):
key_request
=
AccessKeyRequest
(
name
=
name
,
type
=
'ssh'
,
project_id
=
project_id
,
secret
=
private_key
)
self
.
project_api
.
project_project_id_keys_post
(
project_id
,
key_request
)
keys
=
self
.
project_api
.
project_project_id_keys_get
(
project_id
,
name
,
'asc'
,
key_type
=
'ssh'
)
return
keys
[
len
(
keys
)
-
1
]
.
id
def
create_inventory
(
self
,
name
,
project_id
,
ssh_key_id
,
inventory_contents
):
inventory_request
=
InventoryRequest
(
name
=
name
,
project_id
=
project_id
,
inventory
=
inventory_contents
,
ssh_key_id
=
ssh_key_id
,
type
=
'static'
)
self
.
project_api
.
project_project_id_inventory_post
(
project_id
,
inventory_request
)
inventories
=
self
.
project_api
.
project_project_id_inventory_get
(
project_id
,
name
,
'asc'
)
return
inventories
[
len
(
inventories
)
-
1
]
.
id
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
)
self
.
project_api
.
project_project_id_repositories_post
(
project_id
,
repository
=
repository_request
)
repositories
=
self
.
project_api
.
project_project_id_repositories_get
(
project_id
,
name
,
'asc'
)
return
repositories
[
len
(
repositories
)
-
1
]
.
id
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
,
repository_id
=
repository_id
,
alias
=
playbook_name
,
playbook
=
playbook_name
)
self
.
project_api
.
project_project_id_templates_post
(
project_id
,
template_request
)
templates
=
self
.
project_api
.
project_project_id_templates_get
(
project_id
,
playbook_name
,
'asc'
)
return
templates
[
len
(
templates
)
-
1
]
.
id
def
execute_task
(
self
,
project_id
,
template_id
,
playbook_name
):
task
=
Task
(
template_id
=
template_id
,
playbook
=
playbook_name
)
self
.
project_api
.
project_project_id_tasks_post
(
project_id
,
task
)
tasks
=
self
.
project_api
.
project_project_id_tasks_get
(
project_id
)
return
tasks
[
len
(
tasks
)
-
1
]
.
id
def
get_task
(
self
,
project_id
,
task_id
):
return
self
.
project_api
.
project_project_id_tasks_task_id_get
(
project_id
,
task_id
)
semaphore-python-client-generated/semaphore_client/semaphore_helper.py
View file @
fb8231a1
...
...
@@ -52,21 +52,21 @@ class SemaphoreHelper:
def
create_ssh_key
(
self
,
name
,
project_id
,
private_key
):
key_request
=
AccessKeyRequest
(
name
=
name
,
type
=
'ssh'
,
project_id
=
project_id
,
secret
=
private_key
)
self
.
project_api
.
project_project_id_keys_post
(
project_id
,
key_request
)
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'
)
return
keys
[
len
(
keys
)
-
1
]
.
id
def
create_inventory
(
self
,
name
,
project_id
,
ssh_key_id
,
inventory_contents
):
inventory_request
=
InventoryRequest
(
name
=
name
,
project_id
=
project_id
,
inventory
=
inventory_contents
,
ssh_key_id
=
ssh_key_id
,
type
=
'static'
)
self
.
project_api
.
project_project_id_inventory_post
(
project_id
,
inventory_request
)
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'
)
return
inventories
[
len
(
inventories
)
-
1
]
.
id
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
)
self
.
project_api
.
project_project_id_repositories_post
(
project_id
,
repository
=
repository_request
)
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'
)
return
repositories
[
len
(
repositories
)
-
1
]
.
id
...
...
@@ -74,13 +74,13 @@ class SemaphoreHelper:
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
,
repository_id
=
repository_id
,
alias
=
playbook_name
,
playbook
=
playbook_name
)
self
.
project_api
.
project_project_id_templates_post
(
project_id
,
template_request
)
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'
)
return
templates
[
len
(
templates
)
-
1
]
.
id
def
execute_task
(
self
,
project_id
,
template_id
,
playbook_name
):
task
=
Task
(
template_id
=
template_id
,
playbook
=
playbook_name
)
self
.
project_api
.
project_project_id_tasks_post
(
project_id
,
task
)
self
.
project_api
.
project_project_id_tasks_post
(
task
,
project_id
)
tasks
=
self
.
project_api
.
project_project_id_tasks_get
(
project_id
)
return
tasks
[
len
(
tasks
)
-
1
]
.
id
...
...
@@ -98,3 +98,6 @@ class SemaphoreHelper:
return
False
return
True
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
)
\ No newline at end of file
semaphore-python-client-generated/test/test_task.py
View file @
fb8231a1
...
...
@@ -14,6 +14,8 @@
from
__future__
import
absolute_import
import
unittest
from
time
import
sleep
import
yaml
import
semaphore_client
from
semaphore_client.models.task
import
Task
# noqa: E501
...
...
@@ -62,7 +64,12 @@ 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
)
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
(
0.5
)
def
build_yml_inventory
(
self
,
vms
):
inventory
=
{}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment