00001 """
00002 The create account command creates a new user account on the specified robot.
00003
00004 Copyright 2015 Fetch Robotics Inc.
00005 Author: Alex Henning
00006 """
00007
00008 import os
00009 import subprocess
00010 import sys
00011 from copy import deepcopy
00012 from getpass import getpass
00013 from pipes import quote
00014
00015 from ..util import ssh, add_user, add_robot, add_workspace
00016
00017 name = "create-account"
00018 help_text = "Create an account on a robot"
00019
00020 create_user_script = """
00021 echo %(fetch_password)s | sudo -S adduser %(user)s --gecos '%(fullname)s,,,' --disabled-password &&
00022 echo '%(user)s:%(password)s' | sudo chpasswd &&
00023 sudo usermod -G adm,audio,cdrom,sudo,dip,plugdev,lpadmin,sambashare %(user)s
00024 """
00025
00026 setup_user_script = """
00027 export FETCH_WORKSPACE=%(remote_workspace)s
00028 export ROS_DISTRO=%(ros_distro)s
00029 echo source /opt/ros/%(ros_distro)s/setup.bash >> ~/.bashrc.d/40-ros-setup.sh
00030 echo source %(remote_workspace)s/devel/setup.bash >> ~/.bashrc.d/40-ros-setup.sh
00031 echo '%(password)s' | sudo -S echo granting root priveleges for installation
00032 bash ~/initialize.sh
00033 """
00034
00035
00036 def main(args):
00037 fullname = args.fullname if args.fullname is not None else args.user
00038 print "Creating account %s@%s for %s" % (args.user, args.robot, fullname)
00039
00040
00041 fetch_password = args.fetch_password[-1]
00042 if fetch_password is None:
00043 fetch_password = getpass(prompt="Fetch password: ")
00044
00045
00046 password = args.password
00047 if password is None:
00048 check_password = "Not the same"
00049 while password != check_password:
00050 if password is not None:
00051 print "Passwords don't match, please try again."
00052 password = getpass(prompt="Password: ")
00053 check_password = getpass(prompt="Password (confirm): ")
00054
00055
00056 env = {
00057 "fetch_password": fetch_password,
00058 "password": password,
00059 "user": args.user,
00060 "fullname": fullname,
00061 "remote_workspace": args.remote_workspace,
00062 "ros_distro": os.getenv("ROS_DISTRO"),
00063 }
00064
00065
00066
00067
00068 proc = subprocess.Popen(["sshpass", "-e",
00069 "ssh", "-o", "StrictHostKeyChecking=no",
00070 "fetch@" + args.robot, "exit"],
00071 env={"SSHPASS": fetch_password})
00072 proc.wait()
00073 if proc.returncode != 0:
00074 print "ERROR: Could not add robot to known hosts"
00075 sys.exit(-1)
00076
00077
00078 if ssh("fetch", args.robot, create_user_script % env, fetch_password) != 0:
00079 print "ERROR: Creating user failed"
00080 sys.exit(-1)
00081
00082
00083
00084 dd = deepcopy(os.environ)
00085 dd["SSHPASS"] = password
00086 proc = subprocess.Popen(["sshpass", "-e",
00087 "ssh-copy-id", args.user + "@" + args.robot],
00088 env=dd)
00089 proc.wait()
00090 if proc.returncode != 0:
00091 print "WARNING: Copying ID failed, continuing anyways"
00092 print "To manually copy an ID, first create one " \
00093 "https://help.github.com/articles/generating-ssh-keys/ " \
00094 "and then run `ssh-copy-id " + args.user + "@" + args.robot + "`"
00095
00096
00097 skeleton = args.skeleton
00098 if skeleton is None:
00099 user_skeleton = os.getenv("HOME") + "/.fetch/robot_skeleton"
00100 if os.path.isdir(user_skeleton):
00101 skeleton = user_skeleton
00102 else:
00103 package_dir = subprocess.check_output(["rospack",
00104 "find",
00105 "fetch_tools"]).strip()
00106 skeleton = os.path.join(os.path.dirname(__file__),
00107 package_dir + "/resources/robot_skeleton")
00108 proc = subprocess.Popen(["scp", "-r", ".", args.user + "@" +
00109 args.robot + ":~/"],
00110 cwd=skeleton)
00111 proc.wait()
00112 if proc.returncode != 0:
00113 print "ERROR: Could not copy skeleton directory"
00114 sys.exit(-1)
00115
00116
00117 if ssh(args.user, args.robot, setup_user_script % env) != 0:
00118 print "ERRROR: Setting up user failed"
00119 sys.exit(-1)
00120
00121
00122 def add_arguments(parser):
00123 add_user(parser)
00124 add_robot(parser)
00125 add_workspace(parser)
00126 parser.add_argument(
00127 "--fetch-password", nargs="?", action="append", default=["robotics"],
00128 help="Password for the fetch account (or blank to prompt)"
00129 )
00130 parser.add_argument("--password", nargs="?", action="store",
00131 help="Password for the new account")
00132 parser.add_argument("--fullname", action="store", help="Users full name")
00133 parser.add_argument("--skeleton", action="store",
00134 help="Skeleton directory to setup account")