Difference between revisions of "Script: create-users"

From Dreamwidth Notes
Jump to: navigation, search
(add a link to explain what $LJHOME is)
(Additional notes on "command not found" errors and the use of ~/bin.)
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
 
This will create 100 users named testuser1, testuser2, and so on up to testuser100. Each of them will then be given random edges to each other, and will, if set up to do so, join the community you specified in the script.
 
This will create 100 users named testuser1, testuser2, and so on up to testuser100. Each of them will then be given random edges to each other, and will, if set up to do so, join the community you specified in the script.
  
When running the script, [http://wiki.dreamwidth.net/wiki/index.php/Beginning_dev_checklist#The_location_of_.24LJHOME make sure the environment variable LJHOME is set]!
+
If you're not on a [[Dreamhack]], then when running the script, you will need to [http://wiki.dreamwidth.net/wiki/index.php/Beginning_dev_checklist#The_location_of_.24LJHOME make sure the environment variable LJHOME is set]! (You don't need to worry about this on a Dreamhack because they have LJHOME set all the time anyway.)
  
 
<source lang="perl">
 
<source lang="perl">
Line 123: Line 123:
 
  &lt;len&gt; Yep, seems fine. Just created 100 fake users.
 
  &lt;len&gt; Yep, seems fine. Just created 100 fake users.
  
 +
If you get lots of "command not found" errors while running the script, check to make sure that you're running it as <code>./create-users</code> instead of <code>bash create-users</code> or similar. (This is because the script is not a bash script, but a Perl script, and running it with bash won't work.)
 +
 +
Note also that on a Dreamhack, you can also put the script into your <code>~/bin/</code> directory and simply run it as <code>create-users</code> regardless of which directory you're currently in.
  
 
[[Category: Development]]
 
[[Category: Development]]
 
[[Category: Dev Tools]]
 
[[Category: Dev Tools]]

Latest revision as of 17:21, 5 March 2016

This script, which can be used on either Dreamhacks or manual Dreamwidth installations, will mass-create any number of users that you like, validate them, randomly create watch/trust edges between them, and optionally have them all join an existing community. It's extremely handy for bugs where a large number of users are required.

You will need to edit the first part of the script to make sure the created users have the correct info. Once the script is set up, run it using the following syntax:

./create-users testuser 100

This will create 100 users named testuser1, testuser2, and so on up to testuser100. Each of them will then be given random edges to each other, and will, if set up to do so, join the community you specified in the script.

If you're not on a Dreamhack, then when running the script, you will need to make sure the environment variable LJHOME is set! (You don't need to worry about this on a Dreamhack because they have LJHOME set all the time anyway.)

#!/usr/bin/perl
 
# alter these variables to alter the characteristics of the created accounts
my $email     = 'your-email@example.com';
my $password  = "examplepassword";
my $validated = 1;   # validate new accounts automatically? (no email will be sent in either case)
 
# the next two variables are the percentage chance for each user that it'll grant
# access to or subscribe to any of the users created by this script, including
# itself except where trusting; a user cannot trust themselves. (so a 33% chance
# for $accesschance means that on average, it'll grant access to a third of the
# users created by this script)
my $accesschance    = 10;
my $subscribechance = 20;
 
# if you want the users to all join the same community, put the name of the
# community in the variable below. Note that the community must exist!
my $joincommunity = "";   # if undef or empty, do not join any community
 
# ==============
 
use strict;
use lib "$ENV{LJHOME}/cgi-bin";
require "ljlib.pl";
 
my ( $prefix, $num ) = @ARGV;
 
die "Usage: '$0 <prefix> <number of users to create>'\neg, '$0 circletest 100'; this would create circletest1 .. circletest100.\n" unless defined( $num );
die "Second argument must be a number greater than 0, not '$num'.\n" unless $num > 0;
die "Prefix is too long for this many users; please shorten the prefix.\n" if length( $prefix ) + length( $num ) > 25;
 
my $goodprefix = LJ::canonical_username( $prefix );
die "'$prefix' uses invalid characters; please use another prefix.\n" unless $goodprefix;
 
print "Creating $num users using prefix '$goodprefix'...\n";
 
$| = 1;   # disable buffering
 
for (my $i = 1; $i <= $num; $i++) {
    my $user = $goodprefix . $i;
    LJ::MemCache::delete( 'uidof:' . $user );
    my $u2 = LJ::load_user( $user );
    if ( $u2 ) {
        print "User '$user' already exists, skipping\n";
        next;
    }
    print "Creating '$user'... ";
    my $u = LJ::User->create_personal( user => $user, email => $email, password => $password );
    if ( $u ) {
        print "(userid=" . $u->id . ") ";
        if ( $validated ) {
            print "validating... ";
            LJ::update_user($u, { status => 'A' });
            $u->update_email_alias;
            LJ::Hooks::run_hook( 'email_verified', $u );
        }
        print "done.\n";
    }
    else {
        die "could not create user! Aborting.\n";
    }
}
 
print "All users created, creating edges...\n";
for (my $i = 1; $i <= $num; $i++) {
    my $from_user = $goodprefix . $i;
    my $from_u = LJ::load_user( $from_user );
    die "Could not load from_user '$from_user'" unless $from_u;
    for (my $j = 1; $j <= $num; $j++) {
        my $to_user = $goodprefix . $j;
        my $watch = ( ( int( rand( 100 ) ) + 1 ) <= $subscribechance );
        my $trust = ( ( int( rand( 100 ) ) + 1 ) <= $accesschance ) && ( $from_user ne $to_user );
        next unless ( $watch || $trust );   # skip loading the user unless we're actually going to do anything
        my $to_u = LJ::load_user( $to_user );
        die "Could not load to_user '$to_user'" unless $to_u;
 
        my @actions = ();
        push(@actions, "watch") if $watch;
        push(@actions, "trust") if $trust;
        print "$from_user -> $to_user (" . join(", ", @actions) . ")\n";
        $from_u->add_edge( $to_u, watch => { nonotify => 1 } ) if $watch;
        $from_u->add_edge( $to_u, trust => { nonotify => 1 } ) if $trust;
    }
}
 
if ($joincommunity ne "") {
    my $cu = LJ::load_user($joincommunity);
    die "Could not load community '$joincommunity'. Aborting.\n" unless $cu;
    print "Having all users join '" . $cu->username . "'...\n";
    for (my $i = 1; $i <= $num; $i++) {
        my $user = $goodprefix . $i;
        my $u = LJ::load_user( $user );
        die "Could not load user '$user'" unless $u;
        my $joined = $u->join_community( $cu );
        unless ($joined) {
            die "Error while joining user '$user' to community '" . $cu->username . "'";
        }
    }
}
 
print "All done!\n";

Where should it go in the dreamhack?

<_Simon_> I think you can put that wherever you like
<_Simon_> If you have no preference I'd suggest ~/bin/
<Woggy> When in doubt, blame Brad. ;)
<_Simon_> stick it in ~/bin, chmod it to make it executable, run it and see if anything explodes :-)
<len> Yep, seems fine. Just created 100 fake users.

If you get lots of "command not found" errors while running the script, check to make sure that you're running it as ./create-users instead of bash create-users or similar. (This is because the script is not a bash script, but a Perl script, and running it with bash won't work.)

Note also that on a Dreamhack, you can also put the script into your ~/bin/ directory and simply run it as create-users regardless of which directory you're currently in.