def eru_commands(ctx): if not os.path.exists(os.path.abspath('./.git')): click.echo(error('Must run inside git dir')) ctx.exit(-1) if not os.path.exists(os.path.abspath('./app.yaml')): click.echo(error('Need app.yaml in repository')) ctx.exit(-1) eru_url = os.getenv('ERU_URL') if not eru_url: click.echo(error('Need ERU_URL set in env')) ctx.exit(-1) with open(os.path.abspath('./app.yaml')) as f: appconfig = yaml.load(f) ctx.obj['appconfig'] = appconfig ctx.obj['appname'] = appconfig['appname'] repo = pygit2.Repository('.') ctx.obj['sha1'] = str(repo.head.target) ctx.obj['short_sha1'] = ctx.obj['sha1'][:7] remote = '' for r in repo.remotes: if r.name == 'origin': remote = r.url if not remote.startswith('http'): _, path = remote.split('@', 1) # 太丢人了... remote = 'http://' + path.replace(':', '/') ctx.obj['remote'] = remote ctx.obj['eru'] = EruClient(eru_url)
def eru_commands(ctx): if not os.path.exists(os.path.abspath('./.git')): click.echo(error('Must run inside git dir')) ctx.exit(-1) if not os.path.exists(os.path.abspath('./app.yaml')): click.echo(error('Need app.yaml in repository')) ctx.exit(-1) eru_url = os.getenv('ERU_URL') if not eru_url: click.echo(error('Need ERU_URL set in env')) ctx.exit(-1) with open(os.path.abspath('./app.yaml')) as f: appconfig = yaml.load(f) ctx.obj['appconfig'] = appconfig ctx.obj['appname'] = appconfig['appname'] repo = pygit2.Repository('.') ctx.obj['sha1'] = repo.head.target.hex ctx.obj['short_sha1'] = ctx.obj['sha1'][:7] remote = '' for r in repo.remotes: if r.name == 'origin': remote = r.url if not remote.startswith('http'): _, path = remote.split('@', 1) # 太丢人了... remote = 'http://' + path.replace(':', '/') ctx.obj['remote'] = remote ctx.obj['eru'] = EruClient(eru_url)
def deploy_public_container(ctx, group, pod, entrypoint, env, ncontainer, version, network, ip, raw, image): eru = ctx.obj['eru'] network_ids = [] for nname in network: try: n = eru.get_network(nname) except EruException as e: click.echo(error(e.message)) ctx.exit(-1) else: network_ids.append(n['id']) if not version: version = ctx.obj['short_sha1'] try: r = eru.deploy_public( group, pod, ctx.obj['appname'], ncontainer, version, entrypoint, env, network_ids, raw, image, ip ) except EruException as e: click.echo(error(e.message)) return count = 1 task_status = {i: 0 for i in r['tasks']} while not all(s != 0 for s in task_status.values()): if count < 10: click.echo('o' * count + '\r', nl=False) elif count % 2: click.echo('o' * 10 + 'o\r', nl=False) else: click.echo('o' * 10 + 'x\r', nl=False) for task_id, status in task_status.iteritems(): if status != 0: continue try: task = eru.get_task(task_id) if task['finished']: task_status[task_id] = 1 except EruException: task_status[task_id] = -1 time.sleep(0.5) count += 1 fcount = len([s for s in task_status.values() if s == -1]) scount = len([s for s in task_status.values() if s == 1]) click.echo(info('Done.' + count * ' ')) click.echo(info('%s failed, %s succeeded.' % (fcount, scount)))
def alloc_resource(ctx, env, res_name, name): if name == '': name = res_name if res_name not in ('influxdb', 'mysql'): click.echo(error('Res name must be influxdb/mysql')) ctx.exit(-1) eru = ctx.obj['eru'] r = eru.alloc_resource(ctx.obj['appname'], env, res_name, name) if r['r']: click.echo(error(r['msg'])) else: # TODO get tasks id click.echo(info('Alloc successfully'))
def set_app_env(ctx, env, vs): kv = {} for v in vs: if not '=' in v: click.echo(error('Env must be like key=value')) ctx.exit(-1) key, value = v.split('=', 1) kv[key] = value eru = ctx.obj['eru'] try: eru.set_app_env(ctx.obj['appname'], env, **kv) click.echo(info('env variables set successfully')) except EruException as e: click.echo(error(e.message))
def set_app_env(ctx, env, vs): kv = {} for v in vs: if not '=' in v: click.echo(error('Env must be like key=value')) ctx.exit(-1) key, value = v.split('=', 1) kv[key] = value eru = ctx.obj['eru'] r = eru.set_app_env(ctx.obj['appname'], env, **kv) if r['r']: click.echo(error(r['msg'])) else: click.echo(info('env variables set successfully'))
def assign_host_to_group(ctx, addr, group_name): eru = ctx.obj['eru'] try: eru.assign_host_to_group(addr, group_name) click.echo(info('Host assigned to group successfully')) except EruException as e: click.echo(error(e.message))
def create_host(ctx, addr, pod_name): eru = ctx.obj['eru'] try: eru.create_host(addr, pod_name) click.echo(info('Host created successfully')) except EruException as e: click.echo(error(e.message))
def create_pod(ctx, name, description): eru = ctx.obj['eru'] try: eru.create_pod(name, description) click.echo(info('Pod created successfully')) except EruException as e: click.echo(error(e.message))
def remove_containers(ctx, container_ids): eru = ctx.obj['eru'] r = eru.remove_containers(container_ids) if r['r']: click.echo(error(r['msg'])) return count = 1 task_status = {i: 0 for i in r['tasks']} while not all(s != 0 for s in task_status.values()): if count < 10: click.echo('o' * count + '\r', nl=False) elif count % 2: click.echo('o' * 10 + 'o\r', nl=False) else: click.echo('o' * 10 + 'x\r', nl=False) for task_id, status in task_status.iteritems(): if status != 0: continue task = eru.get_task(task_id) if 'r' in task: task_status[task_id] = -1 if task['finished']: task_status[task_id] = 1 time.sleep(0.5) count += 1 fcount = len([s for s in task_status.values() if s == -1]) scount = len([s for s in task_status.values() if s == 1]) click.echo(info('Done.' + count * ' ')) click.echo(info('%s failed, %s succeeded.' % (fcount, scount)))
def create_group(ctx, name, description): eru = ctx.obj['eru'] r = eru.create_group(name, description) if r['r']: click.echo(error(r['msg'])) else: click.echo(info('Group created successfully'))
def create_host(ctx, addr, pod_name): eru = ctx.obj['eru'] r = eru.create_host(addr, pod_name) if r['r']: click.echo(error(r['msg'])) else: click.echo(info('Host created successfully'))
def assign_host_to_group(ctx, addr, group_name): eru = ctx.obj['eru'] r = eru.assign_host_to_group(addr, group_name) if r['r']: click.echo(error(r['msg'])) else: click.echo(info('Host assigned to group successfully'))
def assign_pod_to_group(ctx, pod_name, group_name): eru = ctx.obj['eru'] r = eru.assign_pod_to_group(pod_name, group_name) if r['r']: click.echo(error(r['msg'])) else: click.echo(info('Pod assigned to group successfully'))
def assign_pod_to_group(ctx, pod_name, group_name): eru = ctx.obj['eru'] try: eru.assign_pod_to_group(pod_name, group_name) click.echo(info('Pod assigned to group successfully')) except EruException as e: click.echo(error(e.message))
def container_log(ctx, container_id, stdout, stderr, tail): eru = ctx.obj['eru'] if not stdout and not stderr: click.echo(error('Set at least one in --stdout/--stderr')) ctx.exit(-1) for line in eru.container_log(container_id, int(stdout), int(stderr), tail): click.echo(line, nl=False)
def create_network(ctx, name, netspace): eru = ctx.obj['eru'] try: eru.create_network(name, netspace) click.echo(info('Network created successfully')) except EruException as e: click.echo(error(e.message))
def offline_version(ctx, group, pod, version): eru = ctx.obj['eru'] if not version: version = ctx.obj['short_sha1'] try: r = eru.offline_version(group, pod, ctx.obj['appname'], version) except EruException as e: click.echo(error(e.message)) return count = 1 task_status = {i: 0 for i in r['tasks']} while not all(s != 0 for s in task_status.values()): if count < 10: click.echo('o' * count + '\r', nl=False) elif count % 2: click.echo('o' * 10 + 'o\r', nl=False) else: click.echo('o' * 10 + 'x\r', nl=False) for task_id, status in task_status.iteritems(): if status != 0: continue try: task = eru.get_task(task_id) if task['finished']: task_status[task_id] = 1 except EruException: task_status[task_id] = -1 time.sleep(0.5) count += 1 fcount = len([s for s in task_status.values() if s == -1]) scount = len([s for s in task_status.values() if s == 1]) click.echo(info('Done.' + count * ' ')) click.echo(info('%s failed, %s succeeded.' % (fcount, scount)))
def register_app_version(ctx): eru = ctx.obj['eru'] r = eru.register_app_version(ctx.obj['appname'], ctx.obj['sha1'], ctx.obj['remote'], ctx.obj['appname'], ctx.obj['appconfig']) if r['r']: click.echo(error(r['msg'])) else: click.echo(info('Register successfully'))
def bind_container_network(ctx, container_id, network): if not network: click.echo(error('at least bind 1 network')) return eru = ctx.obj['eru'] try: r = eru.bind_container_network(ctx.obj['appname'], container_id, network) except EruException as e: click.echo(error(e.message)) return if r['r']: click.echo(error(r['msg'])) return rs = ','.join(ip['address'] for ip in r['msg']) click.echo(info('Done.')) click.echo(info('%s bound' % rs))
def list_app_env_content(ctx, env): eru = ctx.obj['eru'] r = eru.list_app_env_content(ctx.obj['appname'], env) if r['r']: click.echo(error(r['msg'])) else: title = ['Key', 'Value'] data = r['data'] content = [(key, data.get(key, '')) for key in sorted(data.keys())] as_form(title, content)
def update_version(ctx, group, pod, version): eru = ctx.obj['eru'] if not version: version = ctx.obj['short_sha1'] r = eru.update_version(group, pod, ctx.obj['appname'], version) if r['r']: click.echo(error(r['msg'])) else: # TODO get tasks id click.echo(info('Update successfully'))
def list_app_env_names(ctx): eru = ctx.obj['eru'] name = ctx.obj['appname'] r = eru.list_app_env_names(name) if r['r']: click.echo(error(r['msg'])) else: title = ['Env', ] content = [[e, ] for e in r['data']] as_form(title, content)
def remove_containers(ctx, group, pod, host, version, ncontainer): eru = ctx.obj['eru'] if not version: version = ctx.obj['short_sha1'] r = eru.remove_containers(group, pod, ctx.obj['appname'], version, host, ncontainer) if r['r']: click.echo(error(r['msg'])) else: # TODO get tasks id click.echo(info('Remove successfully'))
def deploy_public_container(ctx, group, pod, entrypoint, env, ncontainer, version): eru = ctx.obj['eru'] if not version: version = ctx.obj['short_sha1'] r = eru.deploy_public(group, pod, ctx.obj['appname'], ncontainer, version, entrypoint, env) if r['r']: click.echo(error(r['msg'])) else: # TODO get tasks id click.echo(info('Deploy successfully'))
def deploy_public_container(ctx, group, pod, entrypoint, env, ncontainer, version, network): eru = ctx.obj['eru'] network_ids = [] for nname in network: n = eru.get_network_by_name(nname) if 'r' in n and n['r'] == 1: click.echo(error(n['msg'])) ctx.exit(-1) network_ids.append(n['id']) if not version: version = ctx.obj['short_sha1'] r = eru.deploy_public(group, pod, ctx.obj['appname'], ncontainer, version, entrypoint, env, network_ids) if r['r']: click.echo(error(r['msg'])) else: # TODO get tasks id click.echo(info('Deploy successfully'))
def list_app_env_content(ctx, env): eru = ctx.obj['eru'] try: r = eru.list_app_env_content(ctx.obj['appname'], env) except EruException as e: click.echo(error(e.message)) else: title = ['Key', 'Value'] data = r['data'] content = [(key, data.get(key, '')) for key in sorted(data.keys())] as_form(title, content)
def list_app_env_names(ctx): eru = ctx.obj['eru'] name = ctx.obj['appname'] try: r = eru.list_app_env_names(name) except EruException as e: click.echo(error(e.message)) else: title = ['Env', ] content = [[e, ] for e in r['data']] as_form(title, content)
def list_app_containers(ctx): eru = ctx.obj['eru'] name = ctx.obj['appname'] r = eru.list_app_containers(name) if r['r']: click.echo(error(r['msg'])) else: title = ['Name', 'Time', 'Entry', 'Version', 'Alive', 'Host', 'ID'] content = [[c['name'], c['created'], c['entrypoint'], c['version'], 'yes' if c['is_alive'] else 'no', c['host'], c['container_id'][:7]] for c in r['containers']] as_form(title, content)
def list_app_env_names(ctx): eru = ctx.obj['eru'] name = ctx.obj['appname'] r = eru.list_app_env_names(name) if r['r']: click.echo(error(r['msg'])) else: title = [ 'Env', ] content = [[ e, ] for e in r['data']] as_form(title, content)
def register_app_version(ctx, raw): eru = ctx.obj['eru'] try: eru.register_app_version( ctx.obj['appname'], ctx.obj['sha1'], ctx.obj['remote'], ctx.obj['appname'], ctx.obj['appconfig'], raw ) click.echo(info('Register successfully')) except EruException as e: click.echo(error(e.message))
def list_app_containers(ctx): eru = ctx.obj['eru'] name = ctx.obj['appname'] r = eru.list_app_containers(name) if r['r']: click.echo(error(r['msg'])) else: title = [ 'Name', 'Time', 'Entry', 'Version', 'Alive', 'Host', 'Ports', 'ID' ] content = [[ c['name'], c['created'], c['entrypoint'], c['version'], 'yes' if c['is_alive'] else 'no', c['host'], ','.join(str(p) for p in c['ports']), c['container_id'][:7] ] for c in r['containers']] as_form(title, content)
def list_app_versions(ctx): eru = ctx.obj['eru'] name = ctx.obj['appname'] try: r = eru.list_app_versions(name) except EruException as e: click.echo(error(e.message)) else: title = ['Time', 'Version'] content = [ [ humanize.naturaltime(datetime.strptime(c['created'], '%Y-%m-%d %H:%M:%S')), c['sha'][:7], ] for c in r['versions'] ] as_form(title, content)
def build_image(ctx, group, pod, base, version): eru = ctx.obj['eru'] if not version: version = ctx.obj['short_sha1'] r = eru.build_image(group, pod, ctx.obj['appname'], base, version) if r['r']: click.echo(error(r['msg'])) else: for d in eru.build_log(r['task']): if 'stream' in d: click.echo(d['stream'], nl=False) elif 'status' in d: status = d['status'] progress = d.get('progress', '') if progress: click.echo('%s, %s\r' % (status, progress), nl=False) else: click.echo(status)