create_account.py
Go to the documentation of this file.
1 """
2 The create account command creates a new user account on the specified robot.
3 
4 Copyright 2015 Fetch Robotics Inc.
5 Author: Alex Henning
6 """
7 
8 import os
9 import subprocess
10 import sys
11 from copy import deepcopy
12 from getpass import getpass
13 from pipes import quote
14 
15 from ..util import ssh, add_user, add_robot, add_workspace
16 
17 name = "create-account"
18 help_text = "Create an account on a robot"
19 
20 create_user_script = """
21 echo %(fetch_password)s | sudo -S adduser %(user)s --gecos '%(fullname)s,,,' --disabled-password &&
22 echo '%(user)s:%(password)s' | sudo chpasswd &&
23 sudo usermod -G adm,audio,cdrom,sudo,dip,plugdev,lpadmin,sambashare %(user)s
24 """ # noqa
25 
26 setup_user_script = """
27 export FETCH_WORKSPACE=%(remote_workspace)s
28 export ROS_DISTRO=%(ros_distro)s
29 echo source /opt/ros/%(ros_distro)s/setup.bash >> ~/.bashrc.d/40-ros-setup.sh
30 echo source %(remote_workspace)s/devel/setup.bash >> ~/.bashrc.d/40-ros-setup.sh
31 echo '%(password)s' | sudo -S echo granting root priveleges for installation
32 bash ~/initialize.sh
33 """ # noqa
34 
35 
36 def main(args):
37  fullname = args.fullname if args.fullname is not None else args.user
38  print "Creating account %s@%s for %s" % (args.user, args.robot, fullname)
39 
40  # Get fetch password for setup
41  fetch_password = args.fetch_password[-1]
42  if fetch_password is None:
43  fetch_password = getpass(prompt="Fetch password: ")
44 
45  # Prompt for users password
46  password = args.password
47  if password is None:
48  check_password = "Not the same"
49  while password != check_password:
50  if password is not None:
51  print "Passwords don't match, please try again."
52  password = getpass(prompt="Password: ")
53  check_password = getpass(prompt="Password (confirm): ")
54 
55  # Common values to substitute into scripts
56  env = {
57  "fetch_password": fetch_password,
58  "password": password,
59  "user": args.user,
60  "fullname": fullname,
61  "remote_workspace": args.remote_workspace,
62  "ros_distro": os.getenv("ROS_DISTRO"),
63  }
64 
65  # Automatically add robot to list of known_hosts, pinning the
66  # current security certificate.
67  # TODO(security): Force people to verify keys?
68  proc = subprocess.Popen(["sshpass", "-e",
69  "ssh", "-o", "StrictHostKeyChecking=no",
70  "fetch@" + args.robot, "exit"],
71  env={"SSHPASS": fetch_password})
72  proc.wait()
73  if proc.returncode != 0:
74  print "ERROR: Could not add robot to known hosts"
75  sys.exit(-1)
76 
77  # Create the user
78  if ssh("fetch", args.robot, create_user_script % env, fetch_password) != 0:
79  print "ERROR: Creating user failed"
80  sys.exit(-1)
81 
82  # Copy SSH ID
83  # TODO(enhancement): Allow copying of custom ids and of no id
84  dd = deepcopy(os.environ)
85  dd["SSHPASS"] = password
86  proc = subprocess.Popen(["sshpass", "-e",
87  "ssh-copy-id", args.user + "@" + args.robot],
88  env=dd)
89  proc.wait()
90  if proc.returncode != 0:
91  print "WARNING: Copying ID failed, continuing anyways"
92  print "To manually copy an ID, first create one " \
93  "https://help.github.com/articles/generating-ssh-keys/ " \
94  "and then run `ssh-copy-id " + args.user + "@" + args.robot + "`"
95 
96  # Copy over skeleton setup
97  skeleton = args.skeleton
98  if skeleton is None:
99  user_skeleton = os.getenv("HOME") + "/.fetch/robot_skeleton"
100  if os.path.isdir(user_skeleton):
101  skeleton = user_skeleton
102  else:
103  package_dir = subprocess.check_output(["rospack",
104  "find",
105  "fetch_tools"]).strip()
106  skeleton = os.path.join(os.path.dirname(__file__),
107  package_dir + "/resources/robot_skeleton")
108  proc = subprocess.Popen(["scp", "-r", ".", args.user + "@" +
109  args.robot + ":~/"],
110  cwd=skeleton)
111  proc.wait()
112  if proc.returncode != 0:
113  print "ERROR: Could not copy skeleton directory"
114  sys.exit(-1)
115 
116  # Run setup script
117  if ssh(args.user, args.robot, setup_user_script % env) != 0:
118  print "ERRROR: Setting up user failed"
119  sys.exit(-1)
120 
121 
122 def add_arguments(parser):
123  add_user(parser)
124  add_robot(parser)
125  add_workspace(parser)
126  parser.add_argument(
127  "--fetch-password", nargs="?", action="append", default=["robotics"],
128  help="Password for the fetch account (or blank to prompt)"
129  )
130  parser.add_argument("--password", nargs="?", action="store",
131  help="Password for the new account")
132  parser.add_argument("--fullname", action="store", help="Users full name")
133  parser.add_argument("--skeleton", action="store",
134  help="Skeleton directory to setup account")
def add_workspace(parser)
Definition: util.py:64
def ssh(user, host, command, password=None, fname=None)
Definition: util.py:12
def add_robot(parser)
Definition: util.py:59
def add_user(parser)
Definition: util.py:53


fetch_tools
Author(s): Alex Henning
autogenerated on Mon Feb 28 2022 22:19:10