Skip to content

Instantly share code, notes, and snippets.

@arekinath
Last active December 16, 2015 10:19
Show Gist options
  • Save arekinath/5419243 to your computer and use it in GitHub Desktop.
Save arekinath/5419243 to your computer and use it in GitHub Desktop.
Choose a random hypervisor for sniffle
diff --git a/apps/sniffle/src/sniffle_create_fsm.erl b/apps/sniffle/src/sniffle_create_fsm.erl
index f72ce59..2e94fb9 100644
--- a/apps/sniffle/src/sniffle_create_fsm.erl
+++ b/apps/sniffle/src/sniffle_create_fsm.erl
@@ -99,6 +99,7 @@ create(UUID, Package, Dataset, Config) ->
%%--------------------------------------------------------------------
init([UUID, Package, Dataset, Config]) ->
process_flag(trap_exit, true),
+ random:seed(now()),
Config1 = jsxd:from_list(Config),
%% We're transforming the networks map {nic -> networkid} into
%% an array that is close to what it will look after the VM was
@@ -258,7 +259,11 @@ get_server(_Event, State = #state{
{UUID, Config, Conditions} = eplugin:fold('create:conditions', {UUID, Config, Conditions2}),
CondB = list_to_binary(io_lib:format("~p", [Conditions])),
sniffle_vm:log(UUID, <<"Finding hypervisor ", CondB/binary>>),
- {ok, [{HypervisorID, _} | _]} = sniffle_hypervisor:list(Conditions),
+ {ok, AllCandidates} = sniffle_hypervisor:list(Conditions),
+ [{_, TopWeight} | _] = AllCandidates,
+ Candidates = lists:keytake(TopWeight, 2, AllCandidates),
+ HypIndex = random:uniform(length(Candidates)),
+ {HypervisorID, _} = lists:nth(HypIndex, Candidates),
sniffle_vm:log(UUID, <<"Deploying on hypervisor ", HypervisorID/binary>>),
{ok, H} = sniffle_hypervisor:get(HypervisorID),
{ok, Port} = jsxd:get(<<"port">>, H),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment