Skip to content

Commit 3c75ba4

Browse files
authored
Merge pull request #51 from supermeng/master
Expanded fetch commits info from git api
2 parents 6f73097 + a8f47e3 commit 3c75ba4

File tree

13 files changed

+178
-16
lines changed

13 files changed

+178
-16
lines changed

apis/base_app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,17 @@ def _get_lain_config(self):
177177
return None
178178
try:
179179
config = LainConf()
180-
image_config = get_image_config_from_registry(self.appname, self.meta_version)
180+
image_config = get_image_config_from_registry(
181+
self.appname, self.meta_version)
181182
config.load(self.meta, self.meta_version, self.default_image,
182-
image_config=image_config, registry=PRIVATE_REGISTRY,
183-
domains=get_domains())
183+
image_config=image_config, registry=PRIVATE_REGISTRY,
184+
domains=get_domains())
184185
return config
185186
except Exception as e:
186187
logger.error('_get_lain_config() failed, error: %s' % e)
187188
config = LainConf()
188189
config.load(self.meta, self.meta_version, self.default_image,
189-
registry=PRIVATE_REGISTRY, domains=get_domains())
190+
registry=PRIVATE_REGISTRY, domains=get_domains())
190191
return config
191192

192193
@property
@@ -350,6 +351,8 @@ def check_giturl(self, meta, update=False):
350351
giturl = "http://" + giturl
351352
if update and self.update_git_url(giturl):
352353
return
354+
if self.giturl == '':
355+
raise InvalidLainYaml("No Giturl bound")
353356
if giturl != self.giturl:
354357
raise InvalidLainYaml(
355358
"app giturl '%s' doesn't match with bound url '%s'" % (giturl, self.giturl))

apis/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,19 @@ def get_image_config_from_registry(app, meta_version, registry=None):
180180

181181
headers = _get_registry_access_header(app, registry)
182182
headers['Accept'] = 'application/vnd.docker.distribution.manifest.v2+json'
183-
url = 'http://%s/v2/%s/manifests/release-%s' % (registry, app, meta_version)
183+
url = 'http://%s/v2/%s/manifests/release-%s' % (
184+
registry, app, meta_version)
184185
resp = requests.get(url, headers=headers)
185186
if resp.status_code != 200:
186-
raise Exception("requests.get(%s, %s) failed, resp: %s" % (url, headers, resp))
187+
raise Exception("requests.get(%s, %s) failed, resp: %s" %
188+
(url, headers, resp))
187189
config_digest = resp.json()['config']['digest']
188190
headers = _get_registry_access_header(app, registry)
189191
url = 'http://%s/v2/%s/blobs/%s' % (registry, app, config_digest)
190192
resp = requests.get(url, headers=headers)
191193
if resp.status_code != 200:
192-
raise Exception("requests.get(%s, %s) failed, resp: %s" % (url, headers, resp))
194+
raise Exception("requests.get(%s, %s) failed, resp: %s" %
195+
(url, headers, resp))
193196
return resp.json()['config']
194197

195198

@@ -215,7 +218,8 @@ def docker_network_exists(name):
215218
cli.inspect_network(name)
216219
except docker.errors.APIError as e:
217220
# Forward compatibility for some exceptions raise.
218-
if e.status_code == 404:
221+
# Fixed bug for docker 2.1.0 e.status_code (ugly)
222+
if e.response is not None and e.response.status_code == 404:
219223
return False
220224
raise e
221225
return True

apis/views.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from raven.contrib.django.raven_compat.models import client
1717
from log import logger, op_logger
1818
from oplog.models import add_oplog
19+
from git.client import fetch_project_commits
1920

2021

2122
def render_op_result(op_result):
@@ -735,16 +736,30 @@ def handle(app):
735736
app.check_latest_giturl()
736737
except InvalidLainYaml, e:
737738
return (400, None, '%s' % e, reverse('api_image_push', kwargs={'appname': appname}))
739+
740+
uniq_authors = authors
741+
total_commits = commits
742+
if authors is None:
743+
timestamp_len = 10
744+
commits_info = fetch_project_commits(
745+
app.giturl, int(app.meta_version[:timestamp_len]), int(app.latest_meta_version[:timestamp_len]))
746+
if commits_info is not None:
747+
uniq_authors, total_commits = commits_info
748+
else:
749+
return (400, None, 'Fetch commmits information failed', reverse('api_docs'))
750+
if len(total_commits) == 0 or len(uniq_authors) == 0:
751+
return (400, None, 'Nothing Changed', reverse('api_docs'))
738752
commitid_len = 40
739753
datas = {
740754
"appname": appname,
741-
"commits": commits,
755+
"commits": total_commits,
742756
"operator": AuthApi.operater,
743757
"lastid": app.meta_version[-commitid_len:],
744758
"nextid": app.latest_meta_version[-commitid_len:],
745759
"giturl": app.giturl,
746-
"authors": authors,
760+
"authors": uniq_authors,
747761
}
762+
logger.info('notify datas:%s', str(datas))
748763
image_push_notify(datas)
749764
return (200, None, 'ok', reverse('api_image_push', kwargs={'appname': appname}))
750765
return cls.deal_with_appname(appname, handle)

commons/miscs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ class DoesNotExist(Exception):
1919

2020
class NoAvailableImages(Exception):
2121
pass
22+

commons/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,6 @@
7575
IMAGE_PUSH_KEY = environ.get(
7676
"IMAGE_PUSH_KEY", "image_push")
7777
NOTIFIES_TYPES = {'imagepush': IMAGE_PUSH_KEY}
78+
79+
# git configs
80+
GITLAB_TOKEN = environ.get("GITLAB_TOKEN", "1234567890")

config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ mysql_dbname="console"
99
mysql_port="3306"
1010
mysql_user="console"
1111
mysql_passwd="console"
12+
gitlab_token="1234567890"
1213
console_sentry_dsn="http://da:[email protected]/8"

console/views.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from apis.views import is_deployable
99
from commons.settings import SERVER_NAME, AUTH_TYPES
1010
from functools import wraps
11+
from log import logger
1112

1213

1314
def permission_required(permission=None):
@@ -397,12 +398,17 @@ def api_detail_get(request, appname):
397398

398399
@permission_required('maintain')
399400
def api_image_push_post(request, appname):
400-
try:
401-
options = json.loads(request.body)
402-
authors = options['authors']
403-
commits = options['commits']
404-
except Exception:
405-
return render_json_response(400, 'proc', None, 'invalid request: should be json body as {"authors": [string], "commits": [{"id": string, "message": string}]}', reverse('api_docs'))
401+
authors = None
402+
commits = None
403+
if request.body != '':
404+
try:
405+
options = json.loads(request.body)
406+
if options.get('authors') is not None and options.get('commits') is not None:
407+
authors = options['authors']
408+
commits = options['commits']
409+
except Exception as e:
410+
logger.error(
411+
'loads api_image_push_post body failed with error %s' % str(e))
406412
status_code, view_object, msg, url = AppApi.post_image_push(
407413
appname, authors, commits)
408414
return render_json_response(status_code, 'image_push', view_object, msg, url)

docs/API.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,23 @@ curl -X DELETE /api/v1/apps/:appName/procs/:procName/
162162
```sh
163163
curl /api/v1/resources/:resourceName/instances/
164164
```
165+
166+
### `/api/v1/notify/:notify_type`
167+
168+
1. `GET`
169+
>获取 notify_type 所有 notifies 列表
170+
```sh
171+
curl /api/v1/notify/imagepush
172+
```
173+
174+
2. `POST`
175+
>向 notify_type 注册 notifier
176+
```sh
177+
curl -XPOST -H 'Content-type: application/json' /api/v1/notify/imagepush/ -d '{"notify_url": "http://lxcmond.lain:3001/api/v1/imagepush"}'
178+
```
179+
180+
2. `DELETE`
181+
>向 notify_type 注销 notifier
182+
```sh
183+
curl -XDELETE -H 'Content-type: application/json' /api/v1/notify/imagepush/ -d '{"notify_url": "http://lxcmond.lain:3001/api/v1/imagepush"}'
184+
```

entry.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export CONSOLE_DB_PORT=$mysql_port
1515
export CONSOLE_DB_USER=$mysql_user
1616
export CONSOLE_DB_PASSWORD=$mysql_passwd
1717
export CONSOLE_SENTRY_DSN=$console_sentry_dsn
18+
export GITLAB_TOKEN=$gitlab_token
1819

1920
mkdir -p /lain/logs
2021
exec gunicorn -w 3 -b 0.0.0.0:8000 --preload --error-logfile /lain/logs/error.log --access-logfile /lain/logs/access.log console.wsgi

git/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)