-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviews.py
More file actions
176 lines (145 loc) · 5.91 KB
/
Copy pathviews.py
File metadata and controls
176 lines (145 loc) · 5.91 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import requests
import json
import docker
import os
from django.shortcuts import render, redirect
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.contrib.auth import login, logout
from django.contrib.auth.forms import AuthenticationForm
from .forms import SignupForm
from .models import User
from django.views.decorators.csrf import csrf_exempt
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
def login_user(request):
if request.method == 'POST':
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
user = form.get_user()
login(request, user)
return render(request, 'home.html')
else:
username = request.POST.get('username')
if not User.objects.filter(username=username).exists():
# Redirect to signup page
return redirect('api:signup')
else:
form = AuthenticationForm()
return render(request, 'login.html', {'form': form})
def create_user(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
form.save()
# redirect to login page
# request.session['signup_success'] = True
return redirect('api:login')
else:
form = SignupForm()
return render(request, 'signup.html', {'form': form})
@login_required
def logout_user(request):
logout(request)
return redirect('/')
@csrf_exempt
def run(request):
if request.method == "POST":
try:
if request.content_type == "application/json":
body = json.loads(request.body)
github_url = body.get("githubUrl")
user_uid = body.get("user_id")
work=body.get("work")#work=deploy/redeploy
# else:
# github_url = request.POST.get("githubUrl")
# work=request.POST.get("work")
# user = request.user
# username = user.username
# user_uid = user.uid
docker_user = os.getenv("DOCKER_USERNAME")
docker_pass = os.getenv("DOCKER_PASSWORD")
image_name = os.getenv("IMAGE_NAME")
cmd = os.getenv("CMD")
#env_content = os.getenv("ENV_CONTENT", "")
env_content="PORT=3000"
github_url="https://github.com/Sahiiil1406/test-cops"
print(image_name)
# Validate required parameters
if not all([github_url, docker_user, docker_pass, image_name]):
return JsonResponse({"status": "error", "message": "Missing required parameters."})
# try:
# user = User.objects.get(uid=user_uid)
# except User.DoesNotExist:
# return JsonResponse({'error': 'User not found'}, status=404)
# Clone repository, build, and push image
client = docker.from_env()
# build the image manually using docker build -t final_deploy . (before calling the run fn)
container = client.containers.run(
image='final_deploy',
detach=True,
environment={
"GIT_URL": github_url,
"DOCKER_USERNAME": docker_user,
"DOCKER_PASSWORD": docker_pass,
"IMAGE_NAME": image_name,
"CMD":cmd,
"ENV_CONTENT":env_content
},
volumes={"/var/run/docker.sock": {"bind": "/var/run/docker.sock", "mode": "rw"}},
privileged=True,
)
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(
'run_updates',
{
'type': 'run_data', # Matches consumer method
'data': "Sending LOGS..."
}
)
# Stream logs
logs = container.logs(stream=True)
logs_output = ""
for log in logs:
decoded_log = log.decode("utf-8")
async_to_sync(channel_layer.group_send)(
'run_updates',
{
'type': 'run_data',
'data': decoded_log
}
)
container.stop()
container.remove()
#hit a URL to show success:url:http://172.25.96.1:8001/api/webhook,request type:POST,req.body:{id,image_name,image_port}
payload = {
"id": container.id,
"image_name": image_name,
"image_port": "3000",
"work":work,
}
try:
webhook_url = "http://localhost:8001/api/webhook/"
response = requests.post(webhook_url, json=payload)
print(response.json())
except Exception as e:
print(f"Failed to hit webhook URL: {str(e)}")
return JsonResponse({
"status": "success",
"message": "Container executed successfully.",
"containerId": container.id,
"logs":logs_output
})
except Exception as e:
return JsonResponse({
"status": "error",
"message": "Failed to execute container.",
"error": str(e),
})
return JsonResponse({"status": "error", "message": "Invalid request method. Use POST."})
@login_required
def fetch_giturl(request):
return render(request, 'fetch_giturl.html')