-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
133 lines (106 loc) · 5.66 KB
/
deploy.py
File metadata and controls
133 lines (106 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from __future__ import print_function
import os
import portainer_api
from portainer_api.rest import ApiException
from pprint import pprint
import requests
import json
import subprocess
import sys
portainer_env_variables = ["PORTAINER_HOST", "PORTAINER_USERNAME", "PORTAINER_PASSWORD", "PORTAINER_STACK_NAME"]
for env_variable in portainer_env_variables:
if env_variable not in os.environ:
print("ENV variable '" + env_variable + "' required but not SET")
print("Exit...")
exit(1)
portainer_host = os.environ.get('PORTAINER_HOST') or 'http://10.100.101.201:9000/api'
portainer_username = os.environ.get('PORTAINER_USERNAME') or 'admin'
portainer_password = os.environ.get('PORTAINER_PASSWORD') or 'Qazwsx!@#123'
portainer_stack_name = os.environ.get('PORTAINER_STACK_NAME') or 'asterisksslproxy'
def main():
portainer_config = portainer_api.Configuration()
portainer_config.host = portainer_host
api_client_credentials = portainer_api.AuthApi(portainer_api.ApiClient(portainer_config))
portainer_auth = portainer_api.AuthenticateUserRequest(username=portainer_username, password=portainer_password)
try:
# Authenticate a user
print("Retrieve portainer JWT token...")
jwt_token = api_client_credentials.authenticate_user(portainer_auth).jwt
# Create API client with JWT token auth
api_client = portainer_api.ApiClient(configuration=portainer_config, header_name="Authorization", header_value=jwt_token)
# Get stacks list
print("Retrieve current portainer stacks list...")
stacks_api = portainer_api.StacksApi(api_client=api_client)
stacks_list = stacks_api.stack_list()
current_stack = [stack for stack in stacks_list if stack['Name'] == portainer_stack_name]
if current_stack:
print("...Found stack: '" + portainer_stack_name + "'")
endpoint_id = current_stack[0]['EndpointId']
stack_id = current_stack[0]['Id']
stack_containers, containers_images = containers_list(endpoint_id=endpoint_id, stack_name=portainer_stack_name, jwt_token=jwt_token)
if stack_containers:
for container in stack_containers:
print("......Found container: '" + container + "'")
print("......Delete container: '" + container + "'")
delete_container(endpoint_id=endpoint_id, container_name=container, jwt_token=jwt_token)
if containers_images:
for image in containers_images:
print("......Found container image: '" + image + "'")
print("......Delete image: '" + image + "'")
delete_container_image(endpoint_id=endpoint_id, image_name=image, jwt_token=jwt_token)
print("...Remove stack: '" + portainer_stack_name + "'")
stacks_api.stack_delete(id=stack_id, endpoint_id=endpoint_id)
else:
print("...Stack '" + portainer_stack_name + "' not found on portainer side...")
endpoint_id=1
print("...Set EndpointId='" + str(endpoint_id) + "'")
print("...Create stack from git repo")
repo_dir = sys.path[0]
git_url = subprocess.check_output("cd " + repo_dir + " && git config --get remote.origin.url", shell=True).decode('utf-8').strip()
print("......Repo URL: '" + git_url + "'")
stack_env_variables = ["DB_HOST", "DB_NAME_CDR", "DB_NAME_USERS", "DB_USERNAME", "DB_PASSWORD",
"VIRTUAL_HOST", "VIRTUAL_PORT", "LETSENCRYPT_HOST", "LETSENCRYPT_EMAIL",
"ASTERISK_HOST", "ASTERISK_AMI_USERNAME", "ASTERISK_AMI_PASSWORD"]
stack_env_dict = []
for env_variable in stack_env_variables:
if env_variable in os.environ:
print("......Stack ENV variable '" + env_variable + "' found...")
stack_env_dict.append({ "name": env_variable, "value": os.environ.get(env_variable)})
stack_create_request = portainer_api.StackCreateRequest(
name=portainer_stack_name,
repository_url=git_url,
compose_file_path_in_repository="docker-compose.yml",
env=stack_env_dict if stack_env_dict else ""
)
stacks_api.stack_create(type=2, method='repository', body=stack_create_request, endpoint_id=endpoint_id)
except ApiException as e:
print("Exception when calling AuthApi->authenticate_user: %s\n" % e)
def containers_list(endpoint_id=None, stack_name=None, jwt_token=None):
url = portainer_host + '/endpoints/' + str(endpoint_id) + '/docker/containers/json'
headers ={'Authorization': jwt_token}
payload = {
'all': '1',
'filters': '{"label":["com.docker.compose.project=' + stack_name + '"]}'
}
r = requests.get(url, headers=headers, params=payload)
r_data = json.loads(r.text)
containers = [con['Names'][0].replace("/","") for con in r_data]
images = [con['Image'] for con in r_data]
return containers, images
def delete_container(endpoint_id=None, container_name=None, jwt_token=None):
url = portainer_host + '/endpoints/' + str(endpoint_id) + '/docker/containers/' + container_name
headers ={'Authorization': jwt_token}
payload = {
'force': True,
'v': True
}
r = requests.delete(url, headers=headers, params=payload)
def delete_container_image(endpoint_id=None, image_name=None, jwt_token=None):
url = portainer_host + '/endpoints/' + str(endpoint_id) + '/docker/images/' + image_name
headers ={'Authorization': jwt_token}
payload = {
'force': True
}
r = requests.delete(url, headers=headers, params=payload)
if __name__ == '__main__':
main()