PyXMake Developer Guide 1.0
PyXMake
Loading...
Searching...
No Matches
portainer.py
1# -*- coding: utf-8 -*-
2# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3# % PyXMake - Build environment for PyXMake %
4# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5"""
6Triple-use minimum working example for PyXMake.
7Technically, this script runs w/o PyXMake, but the default pipeline refers to the project.
8
9@note: Execute a docker command via the Portainer API remotely from any system.
10
11@version: 1.0
12----------------------------------------------------------------------------------------------
13@requires:
14 - Portainer X-API-Token
15
16@date:
17 - 16.01.2021
18
19@author: garb_ma [DLR-FA,STM Braunschweig]
20----------------------------------------------------------------------------------------------
21"""
22import os
23import sys
24import posixpath
25
26def main(token=None, **kwargs):
27 """
28 Main function to execute the script.
29 """
30 result = []
31 ## Add additional path to environment variable
32 if os.path.exists(os.path.join(sys.prefix,"conda-meta")) and not os.path.join(sys.prefix,"conda-meta") in os.getenv("PATH",""):
33 os.environ["PATH"] = os.pathsep.join([os.path.join(sys.prefix,"Library","bin"),os.getenv("PATH","")])
34 # Now the requests module can be load w/o errors.
35 import requests
36
37 api_portainer_url = posixpath.join(kwargs.get("base_url","https://portainer.fa-services.intra.dlr.de/api"))
38 api_header = {'X-API-KEY': token}
39
40 # Raise an error if no token was given.
41 if not token: raise ValueError
42
43 # Return all default values.
44 if kwargs.get("datacheck",False): return [api_portainer_url, api_header]
45
46 try:
47 # These are all predefined calls
48 result = [x for x in requests.get(posixpath.join(api_portainer_url,"users"), headers=api_header).json()
49 if requests.get(posixpath.join(api_portainer_url,"users",str(x["Id"]),"tokens"), headers=api_header).status_code == 200]
50 except TypeError: result = "The token is invalid."
51 except: pass
52
53 ## Return the result. This should be always a list or an empty list
54 # if something went wrong.
55 return result
56
57if __name__ == "__main__":
58 main(); sys.exit()
main(token=None, **kwargs)
Definition portainer.py:26