Skip to content

Commit f428cd7

Browse files
committed
handling git-repo connection-issues (fix #83)
1 parent 1237920 commit f428cd7

File tree

7 files changed

+45
-19
lines changed

7 files changed

+45
-19
lines changed

src/ansibleguy-webui/aw/execute/play.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from aw.execute.repository import ExecuteRepository
1010
from aw.execute.alert import Alert
1111
from aw.utils.util import datetime_w_tz, is_null, timed_lru_cache # get_ansible_versions
12-
from aw.utils.handlers import AnsibleConfigError
12+
from aw.utils.handlers import AnsibleConfigError, AnsibleRepositoryError
1313
from aw.utils.debug import log
1414

1515

@@ -63,7 +63,10 @@ def _cancel_job() -> bool:
6363
runner_cleanup(execution=execution, path_run=path_run, exec_repo=exec_repo)
6464
Alert(job=job, execution=execution).go()
6565

66-
except (OSError, AnsibleConfigError, ValueError, AttributeError, IndexError, KeyError) as err:
66+
except (
67+
AnsibleConfigError, AnsibleRepositoryError,
68+
OSError, ValueError, AttributeError, IndexError, KeyError,
69+
) as err:
6770
tb = traceback.format_exc(limit=1024)
6871
failure(
6972
execution=execution, exec_repo=exec_repo, path_run=path_run, result=result,

src/ansibleguy-webui/aw/execute/repository.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ def get_path_run_repo(self) -> Path:
121121
return self.path_run / '.repository'
122122

123123
def _git_env(self) -> dict:
124-
env = {'GIT_SSH_COMMAND': 'ssh'}
124+
env = {
125+
'GIT_SSH_COMMAND': f'ssh -o ConnectTimeout=10',
126+
'GIT_HTTP_CONNECT_TIMEOUT': '10',
127+
}
125128
if self.repository.git_origin.find(' -p') != -1:
126129
try:
127130
self.repository.git_origin, port = self.repository.git_origin.split(' -p')
@@ -182,6 +185,7 @@ def _repo_process(self, cmd: str, env: dict):
182185
if self.path_repo is None:
183186
self.path_repo = self.get_path_repo()
184187

188+
cmd = f'timeout {self.repository.git_timeout} {cmd}'
185189
result = process(cmd=cmd, cwd=self.path_repo, env=env, shell=True, timeout_sec=REPO_CLONE_TIMEOUT)
186190
self._log_file_write(f"COMMAND: {cmd}\n{result['stdout']}")
187191
if result['rc'] != 0:

src/ansibleguy-webui/aw/execute/threader.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from aw.utils.handlers import AnsibleConfigError, AnsibleRepositoryError
1313
from aw.utils.util import get_next_cron_execution_sec, get_next_cron_execution_str, is_set
1414
from aw.execute.util import update_status
15-
from aw.model.base import JOB_EXEC_STATUS_ACTIVE, JOB_EXEC_STATUS_FAILED
15+
from aw.model.base import JOB_EXEC_STATI_ACTIVE, JOB_EXEC_STATUS_FAILED, JOB_EXEC_STATUS_RETRY
1616

1717

1818
class Workload(Thread):
@@ -49,7 +49,7 @@ def stop(self) -> bool:
4949
# 'cannot join current thread'
5050
pass
5151

52-
if self.execution is not None and self.execution.status in JOB_EXEC_STATUS_ACTIVE:
52+
if self.execution is not None and self.execution.status in JOB_EXEC_STATI_ACTIVE:
5353
update_status(self.execution, status=JOB_EXEC_STATUS_FAILED)
5454

5555
log(f"Stopped thread {self.log_name_debug}", level=4)
@@ -61,6 +61,9 @@ def run_playbook(self):
6161
ansible_playbook(job=self.job, execution=self.execution)
6262

6363
def run(self, error: bool = False) -> None:
64+
if error and is_set(self.execution):
65+
update_status(self.execution, status=JOB_EXEC_STATUS_RETRY)
66+
6467
if self.once and self.started:
6568
self.stop()
6669
return
@@ -116,9 +119,12 @@ def run(self, error: bool = False) -> None:
116119

117120
except (AnsibleConfigError, AnsibleRepositoryError, OSError) as err:
118121
self.config_invalid += 1
122+
retry_cnt = ''
123+
if not self.once:
124+
retry_cnt = f" ({self.config_invalid}/{self.MAX_CONFIG_INVALID})"
125+
119126
log(
120-
msg=f"Got invalid config/environment for job {self.log_name} "
121-
f"({self.config_invalid}/{self.MAX_CONFIG_INVALID}): \"{err}\"",
127+
msg=f"Got invalid config/environment for job {self.log_name}{retry_cnt}: \"{err}\"",
122128
level=2,
123129
)
124130
self.run(error=True)

src/ansibleguy-webui/aw/model/base.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@
55
(False, 'No')
66
)
77
DEFAULT_NONE = {'null': True, 'default': None, 'blank': True}
8-
JOB_EXEC_STATUS_SUCCESS = 4
8+
JOB_EXEC_STATUS_WAIT = 0
9+
JOB_EXEC_STATUS_START = 1
10+
JOB_EXEC_STATUS_RUN = 2
911
JOB_EXEC_STATUS_FAILED = 3
12+
JOB_EXEC_STATUS_SUCCESS = 4
13+
JOB_EXEC_STATUS_STOPPING = 5
14+
JOB_EXEC_STATUS_RETRY = 7
1015
CHOICES_JOB_EXEC_STATUS = [
11-
(0, 'Waiting'),
12-
(1, 'Starting'),
13-
(2, 'Running'),
16+
(JOB_EXEC_STATUS_WAIT, 'Waiting'),
17+
(JOB_EXEC_STATUS_START, 'Starting'),
18+
(JOB_EXEC_STATUS_RUN, 'Running'),
1419
(JOB_EXEC_STATUS_FAILED, 'Failed'),
1520
(JOB_EXEC_STATUS_SUCCESS, 'Finished'),
16-
(5, 'Stopping'),
21+
(JOB_EXEC_STATUS_STOPPING, 'Stopping'),
1722
(6, 'Stopped'),
23+
(JOB_EXEC_STATUS_RETRY, 'Retry'),
24+
]
25+
JOB_EXEC_STATI_ACTIVE = [
26+
JOB_EXEC_STATUS_WAIT,
27+
JOB_EXEC_STATUS_START,
28+
JOB_EXEC_STATUS_RUN,
29+
JOB_EXEC_STATUS_STOPPING,
30+
JOB_EXEC_STATUS_RETRY,
1831
]
19-
JOB_EXEC_STATUS_ACTIVE = [0, 1, 2, 5]
2032

2133

2234
class BareModel(models.Model):

src/ansibleguy-webui/aw/model/repository.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ class Repository(BaseModel):
1717
'name', 'git_origin', 'git_credentials', 'git_branch', 'git_isolate', 'git_lfs', 'git_limit_depth',
1818
'git_playbook_base',
1919
'git_hook_pre', 'git_hook_post', 'git_hook_cleanup', 'git_override_initialize', 'git_override_update',
20+
'git_timeout',
2021
]
2122
form_fields_static = ['name', 'static_path']
2223
form_fields = [
2324
'name', 'rtype', 'static_path', 'git_origin', 'git_credentials', 'git_branch', 'git_isolate', 'git_lfs',
2425
'git_limit_depth', 'git_hook_pre', 'git_hook_post', 'git_hook_cleanup',
2526
'git_override_initialize', 'git_override_update',
26-
'git_playbook_base',
27+
'git_playbook_base', 'git_timeout',
2728
]
2829
api_fields_read = form_fields.copy()
2930
api_fields_read.extend([
3031
'id', 'rtype_name', 'time_update', 'status', 'status_name', 'log_stdout', 'log_stdout_url',
3132
'log_stderr', 'log_stderr_url',
32-
3333
])
3434
api_fields_write = form_fields
3535
fields_shell_cmds = [
@@ -59,6 +59,7 @@ class Repository(BaseModel):
5959
git_credentials = models.ForeignKey(
6060
JobGlobalCredentials, on_delete=models.SET_NULL, related_name='repo_fk_cred', null=True, blank=True,
6161
)
62+
git_timeout = models.PositiveSmallIntegerField(default=30)
6263

6364
@property
6465
def rtype_name(self) -> str:

src/ansibleguy-webui/aw/static/css/aw.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,8 @@ input:invalid {
578578
color: red;
579579
}
580580

581-
.aw-job-status-finished {
582-
color: green;
581+
.aw-job-status-retry {
582+
color: orange;
583583
}
584584

585585
.aw-job-status-finished {

src/ansibleguy-webui/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def create_manager_groups():
223223

224224

225225
def cleanup_job_stati():
226-
from aw.model.base import JOB_EXEC_STATUS_ACTIVE, JOB_EXEC_STATUS_FAILED
226+
from aw.model.base import JOB_EXEC_STATI_ACTIVE, JOB_EXEC_STATUS_FAILED
227227
from aw.model.job import JobExecution
228228

229-
JobExecution.objects.filter(status__in=JOB_EXEC_STATUS_ACTIVE).update(status=JOB_EXEC_STATUS_FAILED)
229+
JobExecution.objects.filter(status__in=JOB_EXEC_STATI_ACTIVE).update(status=JOB_EXEC_STATUS_FAILED)

0 commit comments

Comments
 (0)