Last active
July 20, 2018 08:50
-
-
Save guoxingx/f6b6c7452834f4750612391eb324298f to your computer and use it in GitHub Desktop.
wrapped ipfs docker cmd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
usage: | |
ipfsdocker start | |
ipfsdocker stop | |
ipfsdocker -h | |
ipfsdocker add <file> | |
""" | |
import sys | |
import os | |
import subprocess | |
CURPATH = os.path.abspath('.') | |
EXPORTDIR = '{}/data/export'.format(CURPATH) | |
IPFSDIR = '{}/data/ipfs'.format(CURPATH) | |
CONTAINER = 'ipfs_host' | |
def start_docker(): | |
cmd = "docker run -d --rm --name {} -v {}:/export -v {}:/data/ipfs -p 4001:4001 -p 127.0.0.1:8080:8080 -p 127.0.0.1:5001:5001 ipfs/go-ipfs:latest".format( | |
CONTAINER, EXPORTDIR, IPFSDIR) | |
p = subprocess.Popen(cmd.split(" "), stdout=subprocess.PIPE) | |
print(p.stdout.read().decode()) | |
def stop_docker(): | |
cmd = 'docker container stop {}'.format(CONTAINER) | |
p = subprocess.Popen(cmd.split(" "), stdout=subprocess.PIPE) | |
print(p.stdout.read().decode()) | |
def run_cmd(args): | |
if 'add' in args: | |
index = args.index('add') | |
index_of_path = index + 1 | |
path = args[index_of_path] | |
while path.startswith('-'): | |
index_of_path += 1 | |
path = args[index_of_path] | |
os.system('cp -r {} {}/'.format(path, EXPORTDIR)) | |
container_path = '/export/{}'.format(path) | |
args[index_of_path] = container_path | |
docker_cmd = ['docker', 'exec', CONTAINER, 'ipfs'] | |
docker_cmd.extend(args) | |
p = subprocess.Popen(docker_cmd, stdout=subprocess.PIPE) | |
print(p.stdout.read().decode()) | |
def run_cli(): | |
args = sys.argv[1:] | |
if 'start' in args: | |
start_docker() | |
elif 'stop' in args: | |
stop_docker() | |
else: | |
run_cmd(args) | |
if __name__ == '__main__': | |
run_cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment