From 1d7138b345910e99edadba154b37c86d4e2c8ed2 Mon Sep 17 00:00:00 2001
From: Ardhi Putra Pratama H <ardhi.putra@cs.ui.ac.id>
Date: Mon, 29 Jan 2018 07:50:53 +0700
Subject: [PATCH] Add extra stuff on cluster management

---
 misc/Vagrantfile            | 76 +++++++++++++++++++++++++++++++++++++
 misc/cluster_adduser-key.sh | 30 +++++++++++++++
 2 files changed, 106 insertions(+)
 create mode 100644 misc/Vagrantfile
 create mode 100755 misc/cluster_adduser-key.sh

diff --git a/misc/Vagrantfile b/misc/Vagrantfile
new file mode 100644
index 0000000..4471077
--- /dev/null
+++ b/misc/Vagrantfile
@@ -0,0 +1,76 @@
+require 'fileutils'
+
+Vagrant.require_version ">= 1.6.0"
+
+# Defaults for config options defined in CONFIG
+$num_instances = 3
+$instance_name_prefix = "cl-ardhi"
+$enable_serial_logging = false
+$share_home = true
+$vm_gui = false
+$vm_memory = 3072
+$vm_cpus = 2
+$forwarded_ports = {}
+
+# Attempt to apply the deprecated environment variable NUM_INSTANCES to
+# $num_instances while allowing config.rb to override it
+if ENV["NUM_INSTANCES"].to_i > 0 && ENV["NUM_INSTANCES"]
+  $num_instances = ENV["NUM_INSTANCES"].to_i
+end
+
+def vm_gui
+  $vb_gui.nil? ? $vm_gui : $vb_gui
+end
+
+def vm_memory
+  $vb_memory.nil? ? $vm_memory : $vb_memory
+end
+
+def vm_cpus
+  $vb_cpus.nil? ? $vm_cpus : $vb_cpus
+end
+
+Vagrant.configure("2") do |config|
+  # always use Vagrants insecure key
+  config.ssh.insert_key = false
+
+  config.vm.box = "ubuntu/xenial64"
+
+  # enable hostmanager
+  config.hostmanager.enabled = true
+
+  # configure the host's /etc/hosts
+  config.hostmanager.manage_host = true
+
+  (1..$num_instances).each do |i|
+    config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
+      config.vm.hostname = vm_name
+
+      if $enable_serial_logging
+        logdir = File.join(File.dirname(__FILE__), "log")
+        FileUtils.mkdir_p(logdir)
+
+        serialFile = File.join(logdir, "%s-serial.txt" % vm_name)
+        FileUtils.touch(serialFile)
+
+        config.vm.provider :virtualbox do |vb, override|
+          vb.customize ["modifyvm", :id, "--uart1", "0x3F8", "4"]
+          vb.customize ["modifyvm", :id, "--uartmode1", serialFile]
+        end
+      end
+
+      $forwarded_ports.each do |guest, host|
+        config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
+      end
+
+      config.vm.provider :virtualbox do |vb|
+        vb.gui = vm_gui
+        vb.memory = vm_memory
+        vb.cpus = vm_cpus
+      end
+
+      ip = "172.17.10.#{i+100}"
+      config.vm.network :private_network, ip: ip
+    end
+  end
+end
diff --git a/misc/cluster_adduser-key.sh b/misc/cluster_adduser-key.sh
new file mode 100755
index 0000000..4f9705b
--- /dev/null
+++ b/misc/cluster_adduser-key.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+username=$1
+password=$2
+p_headnode="PASSWORD HEAD"
+u_headnode="USERNAME HEAD"
+hosts=(cluster01 cluster03 cluster04 cluster05 cluster06)
+
+# end of configuration
+
+pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
+mkdir /var/mirror/user/$username
+
+for hostname in ${hosts[@]}
+do
+	ssh -t $u_headnode@$hostname "echo $p_headnode | sudo -S useradd -m -p $pass -s /bin/bash $username;"
+	sshpass -p "$password" ssh -o StrictHostKeyChecking=no -t $username@$hostname "ssh-keygen -f /home/$username/.ssh/id_rsa -t rsa -N '';"
+done
+
+for mehost in ${hosts[@]}
+do
+	for hostname in ${hosts[@]}
+	do
+		sshpass -p "$password" ssh -t -o "StrictHostKeyChecking no" $username@$mehost "sshpass -p $password ssh-copy-id -o 'StrictHostKeyChecking no' $username@$hostname"
+	done
+done
+
+echo $p_headnode | sudo -S chown -R $username:$username /var/mirror/user/$username
+
+exit 0
-- 
GitLab