| 55 | raise Exception('build egg error, return code:{}'.format(result.returncode)) |
| 56 | |
| 57 | def deploy_egg(host, port, project_name): |
| 58 | # deploy egg |
| 59 | egg_files = find_files('*.egg', os.path.join(cur_dir, 'dist')) |
| 60 | if len(egg_files) > 1: |
| 61 | raise Exception('ERROR: there are more than one egg files') |
| 62 | elif len(egg_files) == 0: |
| 63 | raise Exception('ERROR: egg file not found') |
| 64 | |
| 65 | # deploy to spiderkeeper |
| 66 | session = requests.session() |
| 67 | base_url = 'http://{host}:{port}'.format(host=host, port=port) |
| 68 | resp = session.get(os.path.join(base_url, 'project/manage')) |
| 69 | if not resp.ok: |
| 70 | raise Exception('visit {} failed'.format(base_url)) |
| 71 | resp = session.get(os.path.join(base_url, 'api/projects')) |
| 72 | projects = json.loads(resp.text) |
| 73 | project_id = None |
| 74 | for project in projects: |
| 75 | if project['project_name'] == project_name: |
| 76 | project_id = project['project_id'] |
| 77 | break |
| 78 | if project_id is None: |
| 79 | raise Exception('ERROR: project {} not found'.format(project_name)) |
| 80 | |
| 81 | files = {'file': (open(egg_files[0], 'rb'))} |
| 82 | headers = {} |
| 83 | headers['Origin'] = base_url |
| 84 | headers['Referer'] = os.path.join(base_url, 'project/{}/spider/deploy'.format(project_id)) |
| 85 | upload_url = os.path.join(base_url, 'project/{}/spider/upload'.format(project_id)) |
| 86 | resp = session.post(upload_url, files=files, stream=True, allow_redirects=True, headers=headers) |
| 87 | if not resp.ok: |
| 88 | raise Exception('ERROR: deploy failed. status code: {}, reason: {}\n'.format(resp.status_code, resp.reason)) |
| 89 | |
| 90 | def clean(): |
| 91 | # 删除中间文件 |