forked from racheljewell/FireDocker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecdocker2.py
More file actions
77 lines (65 loc) · 2.96 KB
/
secdocker2.py
File metadata and controls
77 lines (65 loc) · 2.96 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
#!/usr/bin/env python3
import argparse
import json
import PythonFolder.docker_list as docker_list
import PythonFolder.docker_requests as docker_requests
"""
TODO:
- rm the demo json
- rm the other untracked json files from cache
git rm --cache <filename>
"""
def jsonfile_to_data(filepath):
json_string = "{}"
error = False
try:
with open(filepath, 'r') as file:
data = json.load(file)
json_string = json.dumps(data)
except:
print(f"Error: JSON file '{filepath}' does not exist.")
error = True
return json_string,error
def list_func():
print("List function ...")
def main():
parser = argparse.ArgumentParser(description='Example script with multiple required arguments')
parser.add_argument('--create', help='Create something with JSON file', metavar='JSON_PATH') # Requires JSON path
parser.add_argument('--start', help='Start a Docker container', metavar='CONTAINER_NAME')
parser.add_argument('--stop', help='Stop a Docker container by name', metavar='CONTAINER_NAME')
parser.add_argument('--rename', help='Rename a Docker container', nargs=2, metavar=('OLD_NAME', 'NEW_NAME'))
parser.add_argument('--delete', help='Delete a Docker container by name', metavar='CONTAINER_NAME')
parser.add_argument('--list', help='List something without a JSON file', action='store_true')
# Add more arguments as needed
args = parser.parse_args()
json_string = ""
# Access the arguments using args.create, args.list, etc.
if args.create:
json_string,error = jsonfile_to_data(args.create)
if not error:
#print(json_string)
#docker_requests.json_parser(docker_requests.create_container(path='/containers/create', method='POST', data=json_string))
response = docker_requests.create_container(path='/containers/create', method='POST', data=json_string)
if response:
docker_requests.json_parser(docker_requests.create_container(path='/containers/create', method='POST', data=json_string))
elif args.start:
container_name = args.start
docker_requests.start_container(container_name=container_name)
elif args.stop:
container_name = args.stop
docker_requests.stop_container(container_name=container_name)
# Implement container stoppage
elif args.rename:
old_container_name, new_container_name = args.rename
docker_requests.rename_container(old_container_name=old_container_name, new_container_name=new_container_name)
# Implement container renamage
elif args.delete:
container_name = args.delete
docker_requests.delete_container(container_name=container_name)
# Implement container deletage
elif args.list:
docker_list.docker_list("/v1.40/containers/json?all=1")
else:
parser.error("Error: At least one action is required. Use --create or --list.")
if __name__ == '__main__':
main()