cli.py
Go to the documentation of this file.
1 import argparse
2 from argparse import RawTextHelpFormatter
3 import sys
4 import image
5 import logging
6 import yaml
7 import os
8 import rospkg
9 
10 logging.getLogger().setLevel(logging.INFO)
11 
12 def ip_validator(input):
13  ip_and_port = input.split(":")
14  if len(ip_and_port) != 2:
15  msg = "Please give host in the format host:port, e.g. 10.3.4.1:2375 or hostname:2375"
16  raise argparse.ArgumentTypeError(msg)
17  return ip_and_port
18 
19 
20 parser = argparse.ArgumentParser(
21  prog="dockeros",
22  formatter_class=RawTextHelpFormatter,
23  description="Simply running ros nodes in docker containers on remote robots.")
24 parser.add_argument("subcommand", choices=["build", "run", "stop", "push"],
25  help="build: Creates an image that can run roscommand\n\
26 run: Runs an image with your_roscommand (and builds it first)\n\
27 stop: Stops image that runs that command\n\
28 push: Push image to predefined registry\n")
29 
30 g = parser.add_mutually_exclusive_group()
31 g.add_argument("-e", "--env", action='store_true', default=True,
32  help="use the existing docker environment (see https://dockr.ly/2zMPc17 for details)")
33 
34 g.add_argument("-i", "--ip", "--host", nargs=1, type=ip_validator, metavar='HOST:PORT',
35  help="set the host (robot) to deploy image to")
36 
37 parser.add_argument("-f", "--dockerfile", dest="dockerfile", nargs=1, type=open, default=None,
38  help="use a custom Dockerfile")
39 
40 parser.add_argument("-n","--no-build", action='store_true',
41  help="dont (re-)build the image before running")
42 
43 parser.add_argument("roscommand", nargs=argparse.REMAINDER,
44  help="Everything after the subcommand will be interpreted as the ros command to be run in your image")
45 
46 
47 args = parser.parse_args()
48 logging.info("parsing ")
49 
50 if args.dockerfile:
51  dockerfname = os.path.realpath(args.dockerfile[0].name)
52  logging.debug(dockerfname)
53 else:
54  dockerfname = False
55 logging.debug(args)
56 
57 rp = rospkg.RosPack()
58 fname = rp.get_path('dockeros') + '/config/config.yaml'
59 try:
60  config = yaml.load(open(fname))
61 except Exception as e:
62  logging.warn("No config file found under " + fname)
63  config = {}
64 dock_obj = image.DockeROSImage(args.roscommand,
65  config=config,
66  dockerfile=dockerfname)
67 if args.env:
68  dock_obj.make_client()
69 else:
70  dock_obj.make_client(args.ip_and_port[0], args.ip_and_port[1])
71 
72 if args.subcommand == "build":
73  dock_obj.build()
74 
75 elif args.subcommand == "run":
76  if not args.no_build:
77  dock_obj.build()
78  dock_obj.run()
79 
80 elif args.subcommand == "stop":
81  dock_obj.stop()
82 
83 elif args.subcommand == "push":
84  dock_obj.push()
ip_validator
Definition: cli.py:34


dockeros
Author(s):
autogenerated on Mon Dec 9 2019 21:17:31