Updated to run real commands
parent
e5c8ba0b1f
commit
5601fe2c74
|
@ -13,6 +13,8 @@ import argparse
|
||||||
import sys
|
import sys
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
import subprocess
|
||||||
|
import toml
|
||||||
|
|
||||||
def randUserID():
|
def randUserID():
|
||||||
userid = ('tooter_' +
|
userid = ('tooter_' +
|
||||||
|
@ -37,19 +39,21 @@ def main():
|
||||||
"-d", "--debug", action="store_true", help="Enable debugging messages."
|
"-d", "--debug", action="store_true", help="Enable debugging messages."
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-n", "--num", type=int, required=True, help="How manu users to generate."
|
"-n", "--num", type=int, required=True, help="How many users to generate."
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-e", "--email", required=True,
|
"-e", "--email", required=True,
|
||||||
help="Email address to associate with the users.", )
|
help="Email address to associate with the users.")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-o", "--outfile", nargs=1, default="users.toml",
|
"-p", "--path", required=False,
|
||||||
|
default="/home/mastodon/live/bin",
|
||||||
|
help="Path to the tootctl binary. Default: /home/mastodon/live/bin")
|
||||||
|
parser.add_argument(
|
||||||
|
"-o", "--outfile", default="users.toml",
|
||||||
type=argparse.FileType("w", encoding="UTF-8"),
|
type=argparse.FileType("w", encoding="UTF-8"),
|
||||||
help="Output file for the resulting users. Default: users.toml", )
|
help="Output file for the resulting users. Default: users.toml")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
args.debug and print(f"num: {args.num}, email: {args.email}, file: {args.outfile}")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mailbox = args.email.split('@')[0]
|
mailbox = args.email.split('@')[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
@ -61,16 +65,72 @@ def main():
|
||||||
print("ERROR: This doesn't look like an email address: \"{}\"".format(args.email))
|
print("ERROR: This doesn't look like an email address: \"{}\"".format(args.email))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
tootctlprog = f"{args.path}/tootctl"
|
||||||
|
if( not (os.path.isfile(tootctlprog) and os.access(tootctlprog, os.X_OK) ) ):
|
||||||
|
print(f"{tootctlprog} either doesn't exist or isn't executable.")
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
done = 0
|
done = 0
|
||||||
|
usersdict = {}
|
||||||
while( done < args.num ):
|
while( done < args.num ):
|
||||||
userid = randUserID()
|
userid = randUserID()
|
||||||
username = randomName()
|
username = randomName()
|
||||||
emailaddr = "{}+tooter_{}_{}@{}".format(mailbox, userid.split('_')[1],
|
emailaddr = "{}+tooter_{}_{}@{}".format(mailbox, userid.split('_')[1],
|
||||||
userid.split('_')[2], emailDomain )
|
userid.split('_')[2], emailDomain )
|
||||||
|
password = None
|
||||||
|
|
||||||
|
# Inherit current running environment
|
||||||
|
myenv=os.environ.copy()
|
||||||
|
# make sure the rails_env is set
|
||||||
|
myenv["RAILS_ENV"] = "production"
|
||||||
|
# First create the account
|
||||||
|
try:
|
||||||
|
output = subprocess.run([tootctlprog, "accounts", "create", userid,
|
||||||
|
f"--email={emailaddr}", "--confirmed=true" ],
|
||||||
|
encoding='UTF-8', capture_output=True,
|
||||||
|
shell=False, env=myenv )
|
||||||
|
if output.returncode != 0:
|
||||||
|
print("{} exited {}".format(tootctlprog,-(output.returncode)),
|
||||||
|
file=sys.stderr)
|
||||||
|
else:
|
||||||
|
args.debug and print(f"user: {userid} created. rc: {output.returncode}" )
|
||||||
|
except OSError as e:
|
||||||
|
print(f"Calling {tootctlprog} failed:", e, file=sys.stderr)
|
||||||
|
print(output.stdout)
|
||||||
|
print(output.stderr)
|
||||||
|
sys.exit(3)
|
||||||
|
|
||||||
|
try:
|
||||||
|
password=str(output.stdout).split(' ')[2]
|
||||||
|
except IndexError as e:
|
||||||
|
print(f"Creating {userid} failed. Skipping:", e, file=sys.stderr)
|
||||||
|
done += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Now approve it
|
||||||
|
try:
|
||||||
|
output = subprocess.run([tootctlprog, "accounts", "approve", userid],
|
||||||
|
env=myenv, capture_output=True, encoding='UTF-8'),
|
||||||
|
if output[0].returncode != 0:
|
||||||
|
print("{} exited {}".format(tootctlprog,-(output[0].returncode)),
|
||||||
|
file=sys.stderr)
|
||||||
|
else:
|
||||||
|
args.debug and print(f"user: {userid} approved" )
|
||||||
|
except OSError as e:
|
||||||
|
print(f"Calling {tootctlprog} failed:", e, file=sys.stderr)
|
||||||
|
sys.exit(3)
|
||||||
|
|
||||||
|
entry = {}
|
||||||
|
entry["addr"] = emailaddr
|
||||||
|
entry["pass"] = password
|
||||||
|
entry["name"] = username
|
||||||
|
usersdict[userid] = entry
|
||||||
|
|
||||||
args.debug and print(f"user: {userid}, name: \"{username}\", email: \"{emailaddr}\"" )
|
|
||||||
done += 1
|
done += 1
|
||||||
|
|
||||||
|
print(toml.dumps(usersdict),file=args.outfile )
|
||||||
|
args.debug and print(f"Wrote {len(usersdict)} out of {args.num} users to {args.outfile.name}" )
|
||||||
|
|
||||||
# --------------------------------------
|
# --------------------------------------
|
||||||
# Some data for creating users
|
# Some data for creating users
|
||||||
# --------------------------------------
|
# --------------------------------------
|
||||||
|
@ -777,4 +837,4 @@ usernames = [
|
||||||
"Gabriel", "Robert Archer" ]
|
"Gabriel", "Robert Archer" ]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
Loading…
Reference in New Issue