Skip to content

Instantly share code, notes, and snippets.

@micheller
Last active October 26, 2015 13:59
Show Gist options
  • Save micheller/56455639cd6cdfb89d9a to your computer and use it in GitHub Desktop.
Save micheller/56455639cd6cdfb89d9a to your computer and use it in GitHub Desktop.
накостыленный под существующие настройки скрипт
#!/bin/bash -x
SCRIPTRELPATH=`dirname \`realpath $0\``
vmname=""
mountname="" # how it is called, e.g. /mnt/samba/YOURNAMEHERE
sharename="" # windows shared folder name
sharepath=""
vmip=""
mount_parameters="-fstype=cifs,uid=\${UID},domain=WORKGROUP,username=autotest,password=qwerty,iocharset=utf8,rw,file_mode=0644,dir_mode=0755,nounix"
# mount_template="{MOUNTNAME}\t\t-fstype=cifs,uid=${UID},domain=WORKGROUP,username=autotest,password=qwerty,iocharset=utf8,rw,file_mode=0644,dir_mode=0755,nounix ://{VMIP}/{SHARENAME}"
# atexit hooks
OUTPUT="/tmp/$(basename $0).$$.tmp"
>$OUTPUT
trap "rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM EXIT
dialog --title "Mounter" --form "Mount vm PC by taskID" 15 50 0 \
"Task ID: " 1 1 "$vmname" 1 13 30 0 \
"Mounted Name: " 2 1 "$mountname" 2 13 30 0 \
2>$OUTPUT
if [ $? != 0 ]; then exit 1; fi
read vmname mountname <<< $(cat $OUTPUT)
echo -n > $OUTPUT
dialog --radiolist "Select share name:" 13 45 6 \
"AUTOTEST" "F:\\AUTOTEST" on \
"C:" "" off \
"F:" "" off \
"Test" "C:\\Test" off \
"Pain" "C:\\pain" off \
"Other" "Will prompt custom path" off \
2>$OUTPUT
if [ $? != 0 ]; then exit 1; fi
read sharename <<< $(cat $OUTPUT)
echo -n > $OUTPUT
case $sharename in
AUTOTEST)
sharepath="F:\\AUTOTEST"
;;
C:)
sharepath="C:"
sharename="C\$"
;;
F:)
sharepath="F:"
sharename="F\$"
;;
Test)
sharepath="C:\\Test"
;;
Pain)
sharepath="C:\\pain"
;;
Other)
dialog --title "Enter share params" --form "Input proposed share name and path to it" 15 50 0 \
"Share name: " 1 1 "$sharename" 1 13 30 0 \
"Share path: " 2 1 "$sharepath" 2 13 30 0 \
2>$OUTPUT
read -r sharename sharepath <<< $(cat $OUTPUT)
;;
esac
echo $sharename
vmip=$("$SCRIPTRELPATH/PCipfinder.py" "$vmname" -s --pcs pc1 --share --name "$sharename" --folder "$sharepath" 2>/dev/null)
if [ -z "$vmip" ]; then dialog --infobox "No IP address found" 8 40; sleep 2; exit 2; fi
cp /etc/auto.samba /etc/auto.samba.bak
mountstring="$mountname\t\t$mount_parameters ://$vmip/$sharename" #| sed -r -e "s/\{MOUNTNAME\}/$mountname/;s/\{VMIP\}/$vmip/;s/\{SHARENAME\}/$sharename/")
if [ -z $(grep "$mountname" /etc/auto.samba) ]; then
echo -e "$mountstring" >> /etc/auto.samba;
else
sed -i -r -e "s|^$mountname.*\$|$mountstring|" /etc/auto.samba;
fi
# specific code for Emacs Projectile management
# touch /mnt/samba/"$mountname"/.projectile
# service autofs restart
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import logging
import atexit
import argparse
from pyVim import connect
from pyVmomi import vmodl, vim
logging.basicConfig()
log = logging.getLogger("taskmounter")
log.setLevel(logging.INFO)
def _iterable(arg):
if hasattr(arg, "__iter__"):
return arg
else:
return (arg,)
def find_vm(content, vm_name):
children = content.rootFolder.childEntity
for child in children:
if hasattr(child, 'vmFolder'):
datacenter = child
else:
# some other non-datacenter type object
continue
vm_folder = datacenter.vmFolder
vm_list = vm_folder.childEntity
for virtual_machine in vm_list:
if virtual_machine.name == vm_name:
log.info("found vm: {}".format(virtual_machine))
return virtual_machine
def share_vm_folder(content, vm, creds, name, folder):
ps = vim.vm.guest.ProcessManager.ProgramSpec(programPath="C:\\WINDOWS\\system32\\net.exe",
arguments="share {}=\"{}\" /GRANT:{},FULL".format(name, folder, creds.username))
pid = content.guestOperationsManager.processManager.StartProgramInGuest(vm, creds, ps)
if pid < 0:
raise Exception("Couldn't run program on VM {}".format(vm))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("taskId")
parser.add_argument("--pcs", nargs='+', help="Operate on PCs:", default="pc1")
parser.add_argument("--share", action="store_true", help="Share a folder")
parser.add_argument("--name", type=str, help="This will be the share name")
parser.add_argument("--folder", type=str, help="The path to the folder to share")
parser.add_argument("-s", action="store_true", help="Silent, output only useful data")
args = parser.parse_args()
share_name = args.name
share_folder = args.folder
vms_data = [(args.taskId + pc, pc) for pc in _iterable(args.pcs)]
vm = None
esx = None
for vm_name, vm_type in vms_data:
try:
esx = connect.SmartConnect(host='10.250.79.1',
user='root',
pwd='qwe123QWE')
atexit.register(connect.Disconnect, esx)
content = esx.RetrieveContent()
vm = find_vm(content, vm_name)
if vm:
if args.share:
if vm_type == "pc1":
passw = 'qwerty'
elif vm_type == "pc2":
passw = 'pc2pwd'
creds = vim.vm.guest.NamePasswordAuthentication(username="autotest", password=passw)
share_vm_folder(content, vm, creds, share_name, share_folder)
if not args.s:
print vm_type,
print vm.summary.guest.ipAddress
except vmodl.MethodFault as error:
log.fatal("Caught vmodl fault : " + error.msg)
exit(-1)
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment