Skip to content

Instantly share code, notes, and snippets.

@kellymclaughlin
Last active August 29, 2015 14:01
Show Gist options
  • Save kellymclaughlin/1d1ee22fe5d14e038ec0 to your computer and use it in GitHub Desktop.
Save kellymclaughlin/1d1ee22fe5d14e038ec0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env escript
%% -*- coding: utf-8 -*-
%%! -pa /usr/lib64/riak-cs/lib/riakc-1.4.1/ebin /usr/lib64/riak-cs/lib/riak_pb-1.4.4.0/ebin /usr/lib64/riak-cs/lib/protobuffs-0.8.1/ebin /usr/lib64/riak-cs/lib/riak_cs-1.4.5/ebin /usr/lib/riak-cs/lib/riakc-1.4.1/ebin /usr/lib/riak-cs/lib/riak_pb-1.4.4.0/ebin /usr/lib/riak-cs/lib/protobuffs-0.8.1/ebin /usr/lib/riak-cs/lib/riak_cs-1.4.5/ebin
%%! -pa /usr/lib64/riak-cs/lib/riakc-1.3.1.1/ebin /usr/lib64/riak-cs/lib/riak_pb-1.3.0/ebin /usr/lib64/riak-cs/lib/protobuffs-0.8.0/ebin /usr/lib/riak-cs/lib/riakc-1.3.1.1/ebin /usr/lib/riak-cs/lib/riak_pb-1.3.0/ebin /usr/lib/riak-cs/lib/protobuffs-0.8.0/ebin /usr/lib/riak-cs/ebin
%%! -pa /Users/kelly/basho/repos/riak_cs/deps/riakc/ebin /Users/kelly/basho/repos/riak_cs/deps/riak_pb/ebin /Users/kelly/basho/repos/riak_cs/deps/protobuffs/ebin /Users/kelly/basho/repos/riak_cs/deps/riakc/ebin /Users/kelly/basho/repos/riak_cs/deps/riak_pb/ebin /Users/kelly/basho/repos/riak_cs/deps/protobuffs/ebin /Users/kelly/basho/repos/riak_cs/deps/mochiweb/ebin /Users/kelly/basho/repos/riak_cs/ebin
-include_lib("riak_cs/include/riak_cs.hrl").
-define(USERS_BUCKET, <<"moss.users">>).
usage() ->
io:format("Usage: ./user_upgrade.escript [<RiakIP>] [<RiakPort>]~n").
main(Args) when length(Args) =/= 2 ->
usage();
main(Args) when length(Args) =:= 2 ->
[RiakHost, Port] = Args,
RiakPort = format_port(Port),
{ok, Pid} = riakc_pb_socket:start(RiakHost, RiakPort),
{ok, UserIds} = riakc_pb_socket:list_keys(Pid, ?USERS_BUCKET),
[begin
UserId = binary_to_list(UserIdBin),
{Record, Object} = get_user(UserId, Pid),
case is_record(Record, moss_user_v1) of
true ->
riak_cs_utils:save_user(update_user_record(Record), Object, Pid),
io:format("User record for ~p updated~n", [UserId]);
false ->
ok
end
end || UserIdBin <- UserIds],
riakc_pb_socket:stop(Pid).
get_user(Id, Pid) ->
{ok, Obj} = riakc_pb_socket:get(Pid, ?USERS_BUCKET, Id),
Record = binary_to_term(riakc_obj:get_value(Obj)),
{Record, Obj}.
update_user_record(UserRecord) ->
#rcs_user_v2{name=UserRecord#moss_user_v1.name,
display_name=UserRecord#moss_user_v1.display_name,
email=UserRecord#moss_user_v1.email,
key_id=UserRecord#moss_user_v1.key_id,
key_secret=UserRecord#moss_user_v1.key_secret,
canonical_id=UserRecord#moss_user_v1.canonical_id,
buckets=UserRecord#moss_user_v1.buckets,
status=enabled}.
format_port(Port) when is_integer(Port) ->
Port;
format_port(Port) when is_list(Port) ->
list_to_integer(Port).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment